001    /*
002    @license.text@
003     */
004    package biz.hammurapi.util;
005    
006    import java.util.Collection;
007    import java.util.Iterator;
008    
009    /**
010     * Converts collection into visitable
011     * @author Pavel Vlasov
012     * @version $Revision: 1.5 $
013     */
014    public class CollectionVisitable implements Visitable {
015        private Collection collection;
016            private boolean visitCollection;
017    
018        public boolean accept(Visitor visitor) {
019            if (visitor==null || collection==null) {
020                    return false;
021            }
022            
023            if (!visitCollection || visitor.visit(collection)) {
024                Iterator it=collection.iterator();
025                while (it.hasNext()) {
026                    VisitableBase.object2visitor(it.next(), visitor);
027                }
028                
029                if (visitCollection && visitor instanceof PoliteVisitor) {
030                    ((PoliteVisitor) visitor).leave(collection);
031                }
032                return true;
033            }
034            
035                    return false;
036        }
037        
038        
039        /**
040         * @param collection
041         * @param visitCollection true - visitor visits the collection and its elements; false - only elements
042         */
043        public CollectionVisitable(Collection collection, boolean visitCollection) {
044            super();
045            this.collection = collection;
046            this.visitCollection=visitCollection;
047        }
048    }