1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.betwixt.strategy; 18 19 import java.util.Collection; 20 import java.util.Enumeration; 21 import java.util.Iterator; 22 import java.util.Map; 23 24 /** 25 * Specifies which types should be regarded as collective 26 * @since 0.8 27 */ 28 public abstract class CollectiveTypeStrategy { 29 30 /** 31 * Default collective type strategy 32 */ 33 public static final CollectiveTypeStrategy DEFAULT = new Default(); 34 35 /** 36 * Is this a loop type class? 37 * @since 0.7 38 * @param type is this <code>Class</code> a loop type? 39 * @return true if the type is a loop type, or if type is null 40 */ 41 public abstract boolean isCollective(Class type); 42 43 /** 44 * Default collective type strategy 45 */ 46 public static class Default extends CollectiveTypeStrategy { 47 48 /** 49 * Basic implementation returns true for all the standard java 50 * collective types and their subclasses. 51 */ 52 public boolean isCollective(Class type) { 53 // consider: should this be factored into a pluggable strategy? 54 // check for NPEs 55 if (type == null) { 56 return false; 57 } 58 return type.isArray() 59 || Map.class.isAssignableFrom( type ) 60 || Collection.class.isAssignableFrom( type ) 61 || Enumeration.class.isAssignableFrom( type ) 62 || Iterator.class.isAssignableFrom( type ) 63 || Map.Entry.class.isAssignableFrom( type ) ; 64 65 } 66 67 } 68 }