author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.tools.internal.xjc.model; |
|
27 |
||
28 |
import java.math.BigInteger; |
|
29 |
||
30 |
||
31 |
||
32 |
/** |
|
33 |
* represents a possible number of occurence. |
|
34 |
* |
|
35 |
* Usually, denoted by a pair of integers like (1,1) or (5,10). |
|
36 |
* A special value "unbounded" is allowed as the upper bound. |
|
37 |
* |
|
38 |
* <p> |
|
39 |
* For example, (0,unbounded) corresponds to the '*' occurence of DTD. |
|
40 |
* (0,1) corresponds to the '?' occurence of DTD. |
|
41 |
* |
|
42 |
* @author |
|
43 |
* <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a> |
|
44 |
*/ |
|
45 |
public final class Multiplicity { |
|
46 |
public final BigInteger min; |
|
47 |
public final BigInteger max; // null is used to represent "unbounded". |
|
48 |
||
49 |
public static Multiplicity create(BigInteger min, BigInteger max ) { |
|
50 |
if (BigInteger.ZERO.equals(min) && max==null) return STAR; |
|
51 |
if (BigInteger.ONE.equals(min) && max==null) return PLUS; |
|
52 |
if (max!=null) { |
|
53 |
if(BigInteger.ZERO.equals(min) && BigInteger.ZERO.equals(max)) return ZERO; |
|
54 |
if(BigInteger.ZERO.equals(min) && BigInteger.ONE.equals(max)) return OPTIONAL; |
|
55 |
if(BigInteger.ONE.equals(min) && BigInteger.ONE.equals(max)) return ONE; |
|
56 |
} |
|
57 |
return new Multiplicity(min, max); |
|
58 |
} |
|
59 |
||
60 |
public static Multiplicity create(int min, Integer max ) { |
|
61 |
return Multiplicity.create(BigInteger.valueOf(min), BigInteger.valueOf(max.intValue())); |
|
62 |
} |
|
63 |
||
64 |
private Multiplicity(BigInteger min, BigInteger max) { |
|
65 |
this.min = min; |
|
66 |
this.max = max; |
|
67 |
} |
|
68 |
||
69 |
private Multiplicity(int min, int max) { |
|
70 |
this(BigInteger.valueOf(min), BigInteger.valueOf(max)); |
|
71 |
} |
|
72 |
||
73 |
private Multiplicity(int min, Integer max) { |
|
74 |
this(BigInteger.valueOf(min), (max == null) ? null : BigInteger.valueOf(max)); |
|
75 |
} |
|
76 |
||
77 |
@Override |
|
78 |
public boolean equals(Object o) { |
|
79 |
if (!(o instanceof Multiplicity)) return false; |
|
80 |
||
81 |
Multiplicity that = (Multiplicity) o; |
|
82 |
||
83 |
if (!this.min.equals(that.min)) return false; |
|
84 |
if (this.max != null ? !this.max.equals(that.max) : that.max != null) return false; |
|
85 |
||
86 |
return true; |
|
87 |
} |
|
88 |
||
89 |
@Override |
|
90 |
public int hashCode() { |
|
91 |
return min.add(max).intValue(); |
|
92 |
} |
|
93 |
||
94 |
/** returns true if the multiplicity is (1,1). */ |
|
95 |
public boolean isUnique() { |
|
96 |
if(max==null) return false; |
|
97 |
return BigInteger.ONE.equals(min) && BigInteger.ONE.equals(max); |
|
98 |
} |
|
99 |
||
100 |
/** returns true if the multiplicity is (0,1) */ |
|
101 |
public boolean isOptional() { |
|
102 |
if(max==null) return false; |
|
103 |
return BigInteger.ZERO.equals(min) && BigInteger.ONE.equals(max); |
|
104 |
} |
|
105 |
||
106 |
/** returns true if the multiplicity is (0,1) or (1,1). */ |
|
107 |
public boolean isAtMostOnce() { |
|
108 |
if(max==null) return false; |
|
109 |
return max.compareTo(BigInteger.ONE)<=0; |
|
110 |
} |
|
111 |
||
112 |
/** returns true if the multiplicity is (0,0). */ |
|
113 |
public boolean isZero() { |
|
114 |
if(max==null) return false; |
|
115 |
return BigInteger.ZERO.equals(max); |
|
116 |
} |
|
117 |
||
118 |
/** |
|
119 |
* Returns true if the multiplicity represented by this object |
|
120 |
* completely includes the multiplicity represented by the |
|
121 |
* other object. For example, we say [1,3] includes [1,2] but |
|
122 |
* [2,4] doesn't include [1,3]. |
|
123 |
*/ |
|
124 |
public boolean includes( Multiplicity rhs ) { |
|
125 |
if (rhs.min.compareTo(min) == -1) return false; |
|
126 |
if (max==null) return true; |
|
127 |
if (rhs.max==null) return false; |
|
128 |
return rhs.max.compareTo(max) <= 0; |
|
129 |
} |
|
130 |
||
131 |
/** |
|
132 |
* Returns the string representation of the 'max' property. |
|
133 |
* Either a number or a token "unbounded". |
|
134 |
*/ |
|
135 |
public String getMaxString() { |
|
136 |
if(max==null) return "unbounded"; |
|
137 |
else return max.toString(); |
|
138 |
} |
|
139 |
||
140 |
/** gets the string representation. |
|
141 |
* mainly debug purpose. |
|
142 |
*/ |
|
143 |
@Override |
|
144 |
public String toString() { |
|
145 |
return "("+min+','+getMaxString()+')'; |
|
146 |
} |
|
147 |
||
148 |
/** the constant representing the (0,0) multiplicity. */ |
|
149 |
public static final Multiplicity ZERO = new Multiplicity(0,0); |
|
150 |
||
151 |
/** the constant representing the (1,1) multiplicity. */ |
|
152 |
public static final Multiplicity ONE = new Multiplicity(1,1); |
|
153 |
||
154 |
/** the constant representing the (0,1) multiplicity. */ |
|
155 |
public static final Multiplicity OPTIONAL = new Multiplicity(0,1); |
|
156 |
||
157 |
/** the constant representing the (0,unbounded) multiplicity. */ |
|
158 |
public static final Multiplicity STAR = new Multiplicity(0,null); |
|
159 |
||
160 |
/** the constant representing the (1,unbounded) multiplicity. */ |
|
161 |
public static final Multiplicity PLUS = new Multiplicity(1,null); |
|
162 |
||
163 |
// arithmetic methods |
|
164 |
public static Multiplicity choice( Multiplicity lhs, Multiplicity rhs ) { |
|
165 |
return create( |
|
166 |
lhs.min.min(rhs.min), |
|
167 |
(lhs.max==null || rhs.max==null) ? null : lhs.max.max(rhs.max) ); |
|
168 |
} |
|
169 |
public static Multiplicity group( Multiplicity lhs, Multiplicity rhs ) { |
|
170 |
return create( |
|
171 |
lhs.min.add(rhs.min), |
|
172 |
(lhs.max==null || rhs.max==null) ? null : lhs.max.add(rhs.max) ); |
|
173 |
} |
|
174 |
public static Multiplicity multiply( Multiplicity lhs, Multiplicity rhs ) { |
|
175 |
BigInteger min = lhs.min.multiply(rhs.min); |
|
176 |
BigInteger max; |
|
177 |
if (isZero(lhs.max) || isZero(rhs.max)) |
|
178 |
max = BigInteger.ZERO; |
|
179 |
else |
|
180 |
if (lhs.max==null || rhs.max==null) |
|
181 |
max = null; |
|
182 |
else |
|
183 |
max = lhs.max.multiply(rhs.max); |
|
184 |
return create(min,max); |
|
185 |
} |
|
186 |
||
187 |
private static boolean isZero(BigInteger i) { |
|
188 |
return (i != null && BigInteger.ZERO.equals(i)); |
|
189 |
} |
|
190 |
||
191 |
public static Multiplicity oneOrMore( Multiplicity c ) { |
|
192 |
if(c.max==null) return c; // (x,*) => (x,*) |
|
193 |
if(BigInteger.ZERO.equals(c.max)) return c; // (0,0) => (0,0) |
|
194 |
else return create( c.min, null ); // (x,y) => (x,*) |
|
195 |
} |
|
196 |
||
197 |
public Multiplicity makeOptional() { |
|
198 |
if (BigInteger.ZERO.equals(min)) return this; |
|
199 |
return create(BigInteger.ZERO, max); |
|
200 |
} |
|
201 |
||
202 |
public Multiplicity makeRepeated() { |
|
203 |
if (max==null || BigInteger.ZERO.equals(max)) return this; // (0,0)* = (0,0) and (n,unbounded)* = (n,unbounded) |
|
204 |
return create(min,null); |
|
205 |
} |
|
206 |
} |