KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.value;
2
3 import net.sf.saxon.expr.StaticProperty;
4
5 /**
6 * This class contains static methods to manipulate the cardinality
7 * property of a type.
8 * Cardinality of expressions is denoted by one of the values ONE_OR_MORE, ZERO_OR_MORE,
9 * ZERO_OR_ONE, EXACTLY_ONE, or EMPTY. These are combinations of the three bit-significant
10 * values ALLOWS_ZERO, ALLOWS_ONE, and ALLOWS_MANY.
11 */

12
13 public final class Cardinality {
14
15     /**
16     * Private constructor: no instances allowed
17     */

18
19     private Cardinality() {}
20
21     /**
22      * Determine whether multiple occurrences are allowed
23      */

24
25     public static final boolean allowsMany(int cardinality) {
26         return (cardinality & StaticProperty.ALLOWS_MANY) != 0;
27     }
28
29     /**
30      * Determine whether empty sequence is allowed
31      */

32
33     public static final boolean allowsZero(int cardinality) {
34         return (cardinality & StaticProperty.ALLOWS_ZERO) != 0;
35     }
36
37     /**
38     * Form the union of two cardinalities. The cardinality of the expression "if (c) then e1 else e2"
39     * is the union of the cardinalities of e1 and e2.
40     * @param c1 a cardinality
41     * @param c2 another cardinality
42     * @return the cardinality that allows both c1 and c2
43     */

44
45     public static final int union(int c1, int c2) {
46         int r = c1 | c2;
47         // eliminate disallowed options
48
if (r == (StaticProperty.ALLOWS_MANY |
49                     StaticProperty.ALLOWS_ZERO ))
50             r = StaticProperty.ALLOWS_ZERO_OR_MORE;
51         return r;
52     }
53
54     /**
55      * Form the sum of two cardinalities
56      */

57
58     public static final int sum(int c1, int c2) {
59         if (allowsMany(c1) || allowsMany(c2)) {
60             return c1 | c2;
61         }
62         if (!allowsZero(c1) && !allowsZero(c2)) {
63             return StaticProperty.ALLOWS_ONE_OR_MORE;
64         } else {
65             return StaticProperty.ALLOWS_ZERO_OR_MORE;
66         }
67     }
68
69     /**
70     * Test if one cardinality subsumes another. Cardinality c1 subsumes c2 if every option permitted
71     * by c2 is also permitted by c1.
72     * @param c1 a cardinality
73     * @param c2 another cardinality
74     * @return true if if every option permitted
75     * by c2 is also permitted by c1.
76     */

77
78     public static final boolean subsumes(int c1, int c2) {
79         return (c1|c2)==c1;
80     }
81
82     /**
83      * Add two cardinalities
84      */

85
86     public static final int add(int c1, int c2) {
87         if (c1==StaticProperty.EMPTY) {
88             return c2;
89         }
90         if (c2==StaticProperty.EMPTY) {
91             return c1;
92         }
93         boolean allowsZero = Cardinality.allowsZero(c1) && Cardinality.allowsZero(c2);
94         return StaticProperty.ALLOWS_ONE_OR_MORE | (allowsZero ? StaticProperty.ALLOWS_ZERO : 0);
95     }
96
97     /**
98      * Multiply two cardinalities
99      */

100
101     public static final int multiply(int c1, int c2) {
102         if (c1==StaticProperty.EMPTY || c2==StaticProperty.EMPTY) {
103             return StaticProperty.EMPTY;
104         }
105         if (c2==StaticProperty.EXACTLY_ONE) {
106             return c1;
107         }
108         if (c1==StaticProperty.EXACTLY_ONE) {
109             return c2;
110         }
111         if (c1==StaticProperty.ALLOWS_ZERO_OR_ONE && c2==StaticProperty.ALLOWS_ZERO_OR_ONE) {
112             return StaticProperty.ALLOWS_ZERO_OR_ONE;
113         }
114         if (c1==StaticProperty.ALLOWS_ONE_OR_MORE && c2==StaticProperty.ALLOWS_ONE_OR_MORE) {
115             return StaticProperty.ALLOWS_ONE_OR_MORE;
116         }
117         return StaticProperty.ALLOWS_ZERO_OR_MORE;
118     }
119
120     /**
121     * Display the cardinality
122     */

123
124     public static String JavaDoc toString(int cardinality) {
125         switch (cardinality) {
126             case StaticProperty.ALLOWS_ZERO_OR_ONE:
127                 return "zero or one";
128             case StaticProperty.EXACTLY_ONE:
129                 return "exactly one";
130             case StaticProperty.ALLOWS_ZERO_OR_MORE:
131                 return "zero or more";
132             case StaticProperty.ALLOWS_ONE_OR_MORE:
133                 return "one or more";
134             case StaticProperty.EMPTY:
135                 return "exactly zero";
136             case StaticProperty.ALLOWS_MANY:
137                 return "more than one";
138             default:
139                 return "code " + cardinality;
140         }
141     }
142
143     /**
144      * Get the occurence indicator representing the cardinality
145      */

146
147     public static String JavaDoc getOccurrenceIndicator(int cardinality) {
148         switch (cardinality) {
149             case StaticProperty.ALLOWS_ZERO_OR_ONE:
150                 return "?";
151             case StaticProperty.EXACTLY_ONE:
152                 return "";
153             case StaticProperty.ALLOWS_ZERO_OR_MORE:
154                 return "*";
155             case StaticProperty.ALLOWS_ONE_OR_MORE:
156                 return "+";
157             default: throw new AssertionError JavaDoc("unknown cardinality value");
158         }
159     }
160 }
161
162 //
163
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
164
// you may not use this file except in compliance with the License. You may obtain a copy of the
165
// License at http://www.mozilla.org/MPL/
166
//
167
// Software distributed under the License is distributed on an "AS IS" basis,
168
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
169
// See the License for the specific language governing rights and limitations under the License.
170
//
171
// The Original Code is: all this file.
172
//
173
// The Initial Developer of the Original Code is Michael H. Kay
174
//
175
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
176
//
177
// Contributor(s): none.
178
//
179
Popular Tags