KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > value > SequenceType


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 JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14
15 /**
16  * SequenceType: a sequence type consists of a primary type, which indicates the type of item,
17  * and a cardinality, which indicates the number of occurrences permitted. Where the primary type
18  * is element or attribute, there may also be a content type, indicating the required type
19  * annotation on the element or attribute content.
20  */

21
22 public final class SequenceType implements Serializable JavaDoc {
23
24     private ItemType primaryType; // the primary type of the item, e.g. "element", "comment", or "integer"
25
private int cardinality; // the required cardinality
26

27     private static Map JavaDoc pool = Collections.synchronizedMap(new HashMap JavaDoc(50));
28
29     /**
30      * A type that allows any sequence of items
31      */

32
33     public static final SequenceType ANY_SEQUENCE =
34             makeSequenceType(AnyItemType.getInstance(), StaticProperty.ALLOWS_ZERO_OR_MORE);
35
36     /**
37      * A type that allows exactly one item, of any kind
38      */

39
40     public static final SequenceType SINGLE_ITEM =
41             makeSequenceType(AnyItemType.getInstance(), StaticProperty.EXACTLY_ONE);
42
43      /**
44      * A type that allows zero or one items, of any kind
45      */

46
47 // public static final SequenceType OPTIONAL_ITEM =
48
// new SequenceType(Type.ITEM, Type.ITEM, StaticProperty.CARDINALITY_ALLOWS_ZERO_OR_ONE);
49

50     /**
51     * A type that allows exactly one atomic value
52     */

53
54    public static final SequenceType SINGLE_ATOMIC =
55             makeSequenceType(Type.ANY_ATOMIC_TYPE,
56                              StaticProperty.EXACTLY_ONE);
57
58     /**
59     * A type that allows zero or one atomic values
60     */

61
62    public static final SequenceType OPTIONAL_ATOMIC =
63             makeSequenceType(Type.ANY_ATOMIC_TYPE,
64                              StaticProperty.ALLOWS_ZERO_OR_ONE);
65     /**
66     * A type that allows zero or more atomic values
67     */

68
69    public static final SequenceType ATOMIC_SEQUENCE =
70             makeSequenceType(Type.ANY_ATOMIC_TYPE,
71                              StaticProperty.ALLOWS_ZERO_OR_MORE);
72
73     /**
74      * A type that allows a single string
75      */

76
77     public static final SequenceType SINGLE_STRING =
78             makeSequenceType(Type.STRING_TYPE,
79                              StaticProperty.EXACTLY_ONE);
80
81     /**
82      * A type that allows a single integer
83      */

84
85     public static final SequenceType SINGLE_INTEGER =
86             makeSequenceType(Type.INTEGER_TYPE,
87                              StaticProperty.EXACTLY_ONE);
88
89     /**
90      * A type that allows a single integer
91      */

92
93     public static final SequenceType OPTIONAL_INTEGER =
94             makeSequenceType(Type.INTEGER_TYPE,
95                              StaticProperty.ALLOWS_ZERO_OR_ONE);
96
97     /**
98      * A type that allows zero or one nodes
99      */

100
101     public static final SequenceType OPTIONAL_NODE =
102             makeSequenceType(AnyNodeTest.getInstance(),
103                              StaticProperty.ALLOWS_ZERO_OR_ONE);
104
105     /**
106      * A type that allows a single node
107      */

108
109     public static final SequenceType SINGLE_NODE =
110             makeSequenceType(AnyNodeTest.getInstance(),
111                              StaticProperty.EXACTLY_ONE);
112
113
114     /**
115      * A type that allows a sequence of zero or more nodes
116      */

117
118     public static final SequenceType NODE_SEQUENCE =
119             makeSequenceType(AnyNodeTest.getInstance(),
120                              StaticProperty.ALLOWS_ZERO_OR_MORE);
121
122     /**
123      * A type that allows a sequence of zero or more numeric values
124      */

125
126     public static final SequenceType NUMERIC_SEQUENCE =
127             makeSequenceType(Type.NUMBER_TYPE, StaticProperty.ALLOWS_ZERO_OR_MORE);
128
129     /**
130      * Construct an instance of SequenceType. This is a private constructor: all external
131      * clients use the factory method makeSequenceType(), to allow object pooling.
132      *
133      * @param primaryType The item type
134      * @param cardinality The required cardinality
135      */

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     /**
146      * Construct an instance of SequenceType. This is a factory method: it maintains a
147      * pool of SequenceType objects to reduce the amount of object creation.
148      *
149      * @param primaryType The item type
150      * @param cardinality The required cardinality
151      */

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         // For each ItemType, there is an array of 8 SequenceTypes, one for each possible
160
// cardinality (including impossible cardinalities, such as "0 or many"). The pool
161
// is a static HashMap that obtains this array, given an ItemType. The array contains null
162
// entries for cardinalities that have not been requested.
163

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     /**
180      * Get the "primary" part of this required type. E.g. for type element(*, xs:date) the "primary type" is element()
181      *
182      * @return The item type code of the primary type
183      */

184     public ItemType getPrimaryType() {
185         return primaryType;
186     }
187
188     /**
189      * Get the cardinality component of this SequenceType. This is one of the constants Cardinality.EXACTLY_ONE,
190      * Cardinality.ONE_OR_MORE, etc
191      *
192      * @return the required cardinality
193      * @see net.sf.saxon.value.Cardinality
194      */

195     public int getCardinality() {
196         return cardinality;
197     }
198
199
200
201     /**
202      * Return a string representation of this SequenceType
203      * @return the string representation as an instance of the XPath
204      * SequenceType construct
205      */

206     public String JavaDoc toString() {
207         String JavaDoc 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     /**
219      * Returns a hash code value for the object.
220      */

221     public int hashCode() {
222         return primaryType.hashCode() ^ cardinality;
223     }
224
225     /**
226      * Indicates whether some other object is "equal to" this one.
227      */

228     public boolean equals(Object JavaDoc 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 //
240
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
241
// you may not use this file except in compliance with the License. You may obtain a copy of the
242
// License at http://www.mozilla.org/MPL/
243
//
244
// Software distributed under the License is distributed on an "AS IS" basis,
245
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
246
// See the License for the specific language governing rights and limitations under the License.
247
//
248
// The Original Code is: all this file.
249
//
250
// The Initial Developer of the Original Code is Michael H. Kay
251
//
252
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
253
//
254
// Contributor(s): none.
255
//
256
Popular Tags