001 /* 002 * JBoss DNA (http://www.jboss.org/dna) 003 * See the COPYRIGHT.txt file distributed with this work for information 004 * regarding copyright ownership. Some portions may be licensed 005 * to Red Hat, Inc. under one or more contributor license agreements. 006 * See the AUTHORS.txt file in the distribution for a full listing of 007 * individual contributors. 008 * 009 * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA 010 * is licensed to you under the terms of the GNU Lesser General Public License as 011 * published by the Free Software Foundation; either version 2.1 of 012 * the License, or (at your option) any later version. 013 * 014 * JBoss DNA is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 017 * Lesser General Public License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this software; if not, write to the Free 021 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 022 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 023 */ 024 package org.jboss.dna.graph.property; 025 026 import java.math.BigDecimal; 027 import java.net.URI; 028 import java.util.ArrayList; 029 import java.util.Collections; 030 import java.util.Comparator; 031 import java.util.Iterator; 032 import java.util.List; 033 import java.util.UUID; 034 import net.jcip.annotations.Immutable; 035 import org.jboss.dna.common.util.CheckArg; 036 import org.jboss.dna.graph.GraphI18n; 037 038 /** 039 * @author Randall Hauch 040 * @author John Verhaeg 041 */ 042 @Immutable 043 public enum PropertyType { 044 045 STRING("String", ValueComparators.STRING_COMPARATOR, String.class), 046 BINARY("Binary", ValueComparators.BINARY_COMPARATOR, Binary.class), 047 LONG("Long", ValueComparators.LONG_COMPARATOR, Long.class), 048 DOUBLE("Double", ValueComparators.DOUBLE_COMPARATOR, Double.class), 049 DECIMAL("Decimal", ValueComparators.DECIMAL_COMPARATOR, BigDecimal.class), 050 DATE("Date", ValueComparators.DATE_TIME_COMPARATOR, DateTime.class), 051 BOOLEAN("Boolean", ValueComparators.BOOLEAN_COMPARATOR, Boolean.class), 052 NAME("Name", ValueComparators.NAME_COMPARATOR, Name.class), 053 PATH("Path", ValueComparators.PATH_COMPARATOR, Path.class), 054 UUID("UUID", ValueComparators.UUID_COMPARATOR, UUID.class), 055 REFERENCE("Reference", ValueComparators.REFERENCE_COMPARATOR, Reference.class), 056 URI("URI", ValueComparators.URI_COMPARATOR, URI.class), 057 OBJECT("Object", ValueComparators.OBJECT_COMPARATOR, Object.class); 058 059 private static final List<PropertyType> ALL_PROPERTY_TYPES; 060 static { 061 List<PropertyType> types = new ArrayList<PropertyType>(); 062 for (PropertyType type : PropertyType.values()) { 063 types.add(type); 064 } 065 ALL_PROPERTY_TYPES = Collections.unmodifiableList(types); 066 } 067 068 private final String name; 069 private final Comparator<?> comparator; 070 private final Class<?> valueClass; 071 072 private PropertyType( String name, 073 Comparator<?> comparator, 074 Class<?> valueClass ) { 075 this.name = name; 076 this.comparator = comparator; 077 this.valueClass = valueClass; 078 } 079 080 public Class<?> getValueClass() { 081 return this.valueClass; 082 } 083 084 public String getName() { 085 return this.name; 086 } 087 088 public Comparator<?> getComparator() { 089 return this.comparator; 090 } 091 092 public boolean isTypeFor( Object value ) { 093 return this.valueClass.isInstance(value); 094 } 095 096 public boolean isTypeForEach( Iterable<?> values ) { 097 for (Object value : values) { 098 if (!this.valueClass.isInstance(value)) return false; 099 } 100 return true; 101 } 102 103 public boolean isTypeForEach( Iterator<?> values ) { 104 while (values.hasNext()) { 105 Object value = values.next(); 106 if (!this.valueClass.isInstance(value)) return false; 107 } 108 return true; 109 } 110 111 public static PropertyType discoverType( Object value ) { 112 if (value == null) { 113 throw new IllegalArgumentException(GraphI18n.unableToDiscoverPropertyTypeForNullValue.text()); 114 } 115 for (PropertyType type : PropertyType.values()) { 116 if (type == OBJECT) continue; 117 if (type.isTypeFor(value)) return type; 118 } 119 return OBJECT; 120 } 121 122 /** 123 * Discover the most appropriate {@link PropertyType} whose values can be assigned to variables or parameters of the supplied 124 * type. This method does check whether the supplied {@link Class} is an array, in which case it just evalutes the 125 * {@link Class#getComponentType() component type} of the array. 126 * 127 * @param clazz the class representing the type of a value or parameter; may not be null 128 * @return the PropertyType that best represents the type whose values can be used as a value in the supplied class, or null 129 * if no matching PropertyType could be found 130 */ 131 public static PropertyType discoverType( Class<?> clazz ) { 132 CheckArg.isNotNull(clazz, "clazz"); 133 // Is the supplied class an array (or an array of arrays)? 134 while (clazz.isArray()) { 135 // Then just call extract the component type that of which we have an array ... 136 clazz = clazz.getComponentType(); 137 } 138 // Try each property type, and see if its value type is an exact match ... 139 for (PropertyType type : PropertyType.values()) { 140 if (type.valueClass.equals(clazz)) return type; 141 // If the property type is capable of handling a primitive ... 142 switch (type) { 143 case LONG: 144 if (Long.TYPE.equals(clazz) || Integer.TYPE.equals(clazz) || Short.TYPE.equals(clazz)) return type; 145 if (Integer.class.equals(clazz) || Short.class.equals(clazz)) return type; 146 break; 147 case DOUBLE: 148 if (Double.TYPE.equals(clazz) || Float.TYPE.equals(clazz)) return type; 149 if (Float.class.equals(clazz)) return type; 150 break; 151 case BOOLEAN: 152 if (Boolean.TYPE.equals(clazz)) return type; 153 break; 154 default: 155 break; 156 } 157 } 158 // No value class of the property type matched exactly, so now see if any property type is assignable to 'clazz' ... 159 for (PropertyType type : PropertyType.values()) { 160 if (clazz.isAssignableFrom(type.valueClass)) return type; 161 } 162 // Nothing works, ... 163 return null; 164 } 165 166 /** 167 * Return an iterator over all the property type enumeration literals. 168 * 169 * @return an immutable iterator 170 */ 171 public static Iterator<PropertyType> iterator() { 172 return ALL_PROPERTY_TYPES.iterator(); 173 } 174 }