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;
018    
019    import java.util.Collection;
020    import java.util.Enumeration;
021    import java.util.Iterator;
022    import java.util.Map;
023    
024    /**
025     * Specifies which types should be regarded as collective
026     * @since 0.8
027     */
028    public abstract class CollectiveTypeStrategy {
029    
030        /**
031         * Default collective type strategy
032         */
033        public static final CollectiveTypeStrategy DEFAULT = new Default();
034        
035        /** 
036         * Is this a loop type class?
037         * @since 0.7
038         * @param type is this <code>Class</code> a loop type?
039         * @return true if the type is a loop type, or if type is null 
040         */
041        public abstract boolean isCollective(Class type);
042        
043        /**
044         * Default collective type strategy
045         */
046        public static class Default extends CollectiveTypeStrategy {
047    
048            /**
049             * Basic implementation returns true for all the standard java
050             * collective types and their subclasses.
051             */
052            public boolean isCollective(Class type) {
053                // consider: should this be factored into a pluggable strategy?
054                // check for NPEs
055                if (type == null) {
056                    return false;
057                }
058                return type.isArray() 
059                    || Map.class.isAssignableFrom( type ) 
060                    || Collection.class.isAssignableFrom( type ) 
061                    || Enumeration.class.isAssignableFrom( type ) 
062                    || Iterator.class.isAssignableFrom( type )
063                    || Map.Entry.class.isAssignableFrom( type ) ;
064    
065            }
066    
067        }
068    }