    001package com.hammurapi.common;
    002
    003import java.util.ArrayList;
    004import java.util.Iterator;
    005import java.util.List;
    006
    007import com.hammurapi.extract.Predicate;
    008
    009/**
    010 * Context delegating to other contexts "parts".
    011 * @author Pavel Vlasov
    012 *
    013 */
    014public class CompositeContext implements MutableContext {
    015
    016        private Context[] parts;
    017
    018        public CompositeContext() {
    019                //
    020        }
    021
    022        public CompositeContext(Context...parts) {
    023                this.parts = parts;
    024        }
    025
    026        public void setParts(Context...parts) {
    027                this.parts = parts;
    028        }
    029
    030        public <T> Iterator<T> lookupAll(Class<T> serviceClass, Predicate<T, Context>... selectors) {
    031                List<T> all = new ArrayList<T>();
    032                for (Context part: parts) {
    033                        if (part!=null) {
    034                                Iterator<T> cit = part.lookupAll(serviceClass, selectors);
    035                                while (cit.hasNext()) {
    036                                        all.add(cit.next());
    037                                }
    038                        }
    039                }
    040                return all.iterator();
    041        }
    042
    043        public <T> T lookup(Class<T> serviceClass, Predicate<T, Context>... selectors) {
    044                for (Context part: parts) {
    045                        if (part!=null) {
    046                                T ret = part.lookup(serviceClass, selectors);
    047                                if (ret!=null) {
    048                                        return ret;
    049                                }
    050                        }
    051                }
    052                return null;
    053        }
    054
    055        public Object lookup(String name) {
    056                for (Context part: parts) {
    057                        if (part!=null) {
    058                                Object ret = part.lookup(name);
    059                                if (ret!=null) {
    060                                        return ret;
    061                                }
    062                        }
    063                }
    064                return null;
    065        }
    066
    067        public void bind(String name, Object obj) {
    068                for (Context part: parts) {
    069                        if (part instanceof MutableContext) {
    070                                ((MutableContext) part).bind(name, obj);
    071                                return;
    072                        }
    073                }
    074                throw new IllegalStateException("Composite context doesn't contain mutable parts.");
    075        }
    076
    077        public <T> void register(Class<? super T> type, T service) {
    078                for (Context part: parts) {
    079                        if (part instanceof MutableContext) {
    080                                ((MutableContext) part).register(type, service);
    081                                return;
    082                        }
    083                }
    084                throw new IllegalStateException("Composite context doesn't contain mutable parts.");
    085        }
    086
    087        public Object unbind(String name) {
    088                for (Context part: parts) {
    089                        if (part instanceof MutableContext) {
    090                                return ((MutableContext) part).unbind(name);
    091                        }
    092                }
    093                throw new IllegalStateException("Composite context doesn't contain mutable parts.");
    094        }
    095
    096        public <T> void unregister(Class<? super T> type, T service) {
    097                for (Context part: parts) {
    098                        if (part instanceof MutableContext) {
    099                                ((MutableContext) part).unregister(type, service);
    100                                return;
    101                        }
    102                }
    103                throw new IllegalStateException("Composite context doesn't contain mutable parts.");
    104        }
    105}
    106