001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */ 
017    package org.apache.commons.betwixt.strategy.impl;
018    
019    import java.util.ArrayList;
020    import java.util.Collection;
021    
022    import org.apache.commons.betwixt.strategy.CollectiveTypeStrategy;
023    
024    /**
025     * Strategy that allows specific classes to be marked as 
026     * collective ({@link #overrideCollective(Class)})
027     * or not collective ({@link #overrideNotCollective(Class)}).
028     * @since 0.8
029     */
030    public class OverrideCollectiveTypeStategy extends CollectiveTypeStrategy {
031    
032        private final CollectiveTypeStrategy delegate;
033        
034        private final Collection collectiveClasses;
035        private final Collection notCollectiveClasses;
036        
037        /**
038         * Constructs a strategy which delegates to CollectiveTypeStrategy#DEFAULT
039         *
040         */
041        public OverrideCollectiveTypeStategy() {
042            this(CollectiveTypeStrategy.DEFAULT);
043        }
044        
045        /**
046         * Constructs a strategy which delegates all those that it does not override.
047         * @param delegate
048         */
049        public OverrideCollectiveTypeStategy(CollectiveTypeStrategy delegate) {
050            super();
051            this.delegate = delegate;
052            collectiveClasses = new ArrayList();
053            notCollectiveClasses = new ArrayList();
054        }
055    
056        /**
057         * Marks the given type to be treated as collective.
058         * @param type <code>Class</code>, not null
059         */
060        public void overrideCollective(Class type) {
061            collectiveClasses.add(type);
062        }
063    
064        /**
065         * Marks the given type to be treated as not collective
066         * @param type
067         */
068        public void overrideNotCollective(Class type) {
069            notCollectiveClasses.add(type);
070        }
071        
072        /**
073         * @see CollectiveTypeStrategy#isCollective(Class)
074         */
075        public boolean isCollective(Class type) {
076            boolean result = delegate.isCollective(type);
077            if (collectiveClasses.contains(type)) {
078                result = true;
079            } else if (notCollectiveClasses.contains(type)) {
080                result = false;
081            }
082            return result;
083        }
084    
085    }