| 1 | package com.hammurapi.common; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.Iterator; |
| 5 | import java.util.List; |
| 6 | |
| 7 | /** |
| 8 | * Context delegating to other contexts "parts". |
| 9 | * @author Pavel Vlasov |
| 10 | * |
| 11 | */ |
| 12 | public class CompositeContext implements MutableContext { |
| 13 | |
| 14 | private Context[] parts; |
| 15 | |
| 16 | public CompositeContext() { |
| 17 | // |
| 18 | } |
| 19 | |
| 20 | public CompositeContext(Context...parts) { |
| 21 | this.parts = parts; |
| 22 | } |
| 23 | |
| 24 | public void setParts(Context...parts) { |
| 25 | this.parts = parts; |
| 26 | } |
| 27 | |
| 28 | public <T> Iterator<T> lookupAll(Class<T> serviceClass) { |
| 29 | List<T> all = new ArrayList<T>(); |
| 30 | for (Context part: parts) { |
| 31 | if (part!=null) { |
| 32 | Iterator<T> cit = part.lookupAll(serviceClass); |
| 33 | while (cit.hasNext()) { |
| 34 | all.add(cit.next()); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | return all.iterator(); |
| 39 | } |
| 40 | |
| 41 | public <T> T lookup(Class<T> serviceClass) { |
| 42 | for (Context part: parts) { |
| 43 | if (part!=null) { |
| 44 | T ret = part.lookup(serviceClass); |
| 45 | if (ret!=null) { |
| 46 | return ret; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | public Object lookup(String name) { |
| 54 | for (Context part: parts) { |
| 55 | if (part!=null) { |
| 56 | Object ret = part.lookup(name); |
| 57 | if (ret!=null) { |
| 58 | return ret; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | public void bind(String name, Object obj) { |
| 66 | for (Context part: parts) { |
| 67 | if (part instanceof MutableContext) { |
| 68 | ((MutableContext) part).bind(name, obj); |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | throw new IllegalStateException("Composite context doesn't contain mutable parts."); |
| 73 | } |
| 74 | |
| 75 | public <T> void register(Class<? super T> type, T service) { |
| 76 | for (Context part: parts) { |
| 77 | if (part instanceof MutableContext) { |
| 78 | ((MutableContext) part).register(type, service); |
| 79 | return; |
| 80 | } |
| 81 | } |
| 82 | throw new IllegalStateException("Composite context doesn't contain mutable parts."); |
| 83 | } |
| 84 | |
| 85 | public Object unbind(String name) { |
| 86 | for (Context part: parts) { |
| 87 | if (part instanceof MutableContext) { |
| 88 | return ((MutableContext) part).unbind(name); |
| 89 | } |
| 90 | } |
| 91 | throw new IllegalStateException("Composite context doesn't contain mutable parts."); |
| 92 | } |
| 93 | |
| 94 | public <T> void unregister(Class<? super T> type, T service) { |
| 95 | for (Context part: parts) { |
| 96 | if (part instanceof MutableContext) { |
| 97 | ((MutableContext) part).unregister(type, service); |
| 98 | return; |
| 99 | } |
| 100 | } |
| 101 | throw new IllegalStateException("Composite context doesn't contain mutable parts."); |
| 102 | } |
| 103 | } |
| 104 | |