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