1 package net.sf.saxon.value; 2 import net.sf.saxon.expr.StaticProperty; 3 import net.sf.saxon.pattern.AnyNodeTest; 4 import net.sf.saxon.pattern.NoNodeTest; 5 import net.sf.saxon.type.AnyItemType; 6 import net.sf.saxon.type.ItemType; 7 import net.sf.saxon.type.Type; 8 import net.sf.saxon.type.BuiltInAtomicType; 9 10 import java.io.Serializable ; 11 import java.util.Collections ; 12 import java.util.HashMap ; 13 import java.util.Map ; 14 15 21 22 public final class SequenceType implements Serializable { 23 24 private ItemType primaryType; private int cardinality; 27 private static Map pool = Collections.synchronizedMap(new HashMap (50)); 28 29 32 33 public static final SequenceType ANY_SEQUENCE = 34 makeSequenceType(AnyItemType.getInstance(), StaticProperty.ALLOWS_ZERO_OR_MORE); 35 36 39 40 public static final SequenceType SINGLE_ITEM = 41 makeSequenceType(AnyItemType.getInstance(), StaticProperty.EXACTLY_ONE); 42 43 46 47 50 53 54 public static final SequenceType SINGLE_ATOMIC = 55 makeSequenceType(Type.ANY_ATOMIC_TYPE, 56 StaticProperty.EXACTLY_ONE); 57 58 61 62 public static final SequenceType OPTIONAL_ATOMIC = 63 makeSequenceType(Type.ANY_ATOMIC_TYPE, 64 StaticProperty.ALLOWS_ZERO_OR_ONE); 65 68 69 public static final SequenceType ATOMIC_SEQUENCE = 70 makeSequenceType(Type.ANY_ATOMIC_TYPE, 71 StaticProperty.ALLOWS_ZERO_OR_MORE); 72 73 76 77 public static final SequenceType SINGLE_STRING = 78 makeSequenceType(Type.STRING_TYPE, 79 StaticProperty.EXACTLY_ONE); 80 81 84 85 public static final SequenceType SINGLE_INTEGER = 86 makeSequenceType(Type.INTEGER_TYPE, 87 StaticProperty.EXACTLY_ONE); 88 89 92 93 public static final SequenceType OPTIONAL_INTEGER = 94 makeSequenceType(Type.INTEGER_TYPE, 95 StaticProperty.ALLOWS_ZERO_OR_ONE); 96 97 100 101 public static final SequenceType OPTIONAL_NODE = 102 makeSequenceType(AnyNodeTest.getInstance(), 103 StaticProperty.ALLOWS_ZERO_OR_ONE); 104 105 108 109 public static final SequenceType SINGLE_NODE = 110 makeSequenceType(AnyNodeTest.getInstance(), 111 StaticProperty.EXACTLY_ONE); 112 113 114 117 118 public static final SequenceType NODE_SEQUENCE = 119 makeSequenceType(AnyNodeTest.getInstance(), 120 StaticProperty.ALLOWS_ZERO_OR_MORE); 121 122 125 126 public static final SequenceType NUMERIC_SEQUENCE = 127 makeSequenceType(Type.NUMBER_TYPE, StaticProperty.ALLOWS_ZERO_OR_MORE); 128 129 136 private SequenceType(ItemType primaryType, int cardinality) { 137 this.primaryType = primaryType; 138 if (primaryType instanceof NoNodeTest) { 139 this.cardinality = StaticProperty.EMPTY; 140 } else { 141 this.cardinality = cardinality; 142 } 143 } 144 145 152 153 public static SequenceType makeSequenceType(ItemType primaryType, int cardinality) { 154 155 if (!(primaryType instanceof BuiltInAtomicType)) { 156 return new SequenceType(primaryType, cardinality); 157 } 158 159 164 SequenceType[] array = (SequenceType[])pool.get(primaryType); 165 if (array == null) { 166 array = new SequenceType[8]; 167 pool.put(primaryType, array); 168 } 169 int code = StaticProperty.getCardinalityCode(cardinality); 170 if (array[code] == null) { 171 SequenceType s = new SequenceType(primaryType, cardinality); 172 array[code] = s; 173 return s; 174 } else { 175 return array[code]; 176 } 177 } 178 179 184 public ItemType getPrimaryType() { 185 return primaryType; 186 } 187 188 195 public int getCardinality() { 196 return cardinality; 197 } 198 199 200 201 206 public String toString() { 207 String s = primaryType.toString(); 208 if (cardinality == StaticProperty.ALLOWS_ONE_OR_MORE) { 209 s = s + '+'; 210 } else if (cardinality == StaticProperty.ALLOWS_ZERO_OR_MORE) { 211 s = s + '*'; 212 } else if (cardinality == StaticProperty.ALLOWS_ZERO_OR_ONE) { 213 s = s + '?'; 214 } 215 return s; 216 } 217 218 221 public int hashCode() { 222 return primaryType.hashCode() ^ cardinality; 223 } 224 225 228 public boolean equals(Object obj) { 229 if (obj instanceof SequenceType) { 230 return this.primaryType.equals(((SequenceType)obj).primaryType) && 231 this.cardinality == ((SequenceType)obj).cardinality; 232 } 233 return false; 234 } 235 236 237 } 238 239 | Popular Tags |