src/share/jaxws_classes/com/sun/tools/internal/xjc/model/Multiplicity.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/model/Multiplicity.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,206 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.internal.xjc.model;
    1.30 +
    1.31 +import java.math.BigInteger;
    1.32 +
    1.33 +
    1.34 +
    1.35 +/**
    1.36 + * represents a possible number of occurence.
    1.37 + *
    1.38 + * Usually, denoted by a pair of integers like (1,1) or (5,10).
    1.39 + * A special value "unbounded" is allowed as the upper bound.
    1.40 + *
    1.41 + * <p>
    1.42 + * For example, (0,unbounded) corresponds to the '*' occurence of DTD.
    1.43 + * (0,1) corresponds to the '?' occurence of DTD.
    1.44 + *
    1.45 + * @author
    1.46 + *    <a href="mailto:kohsuke.kawaguchi@sun.com">Kohsuke KAWAGUCHI</a>
    1.47 + */
    1.48 +public final class Multiplicity {
    1.49 +    public final BigInteger min;
    1.50 +    public final BigInteger max;    // null is used to represent "unbounded".
    1.51 +
    1.52 +    public static Multiplicity create(BigInteger min, BigInteger max ) {
    1.53 +        if (BigInteger.ZERO.equals(min) && max==null) return STAR;
    1.54 +        if (BigInteger.ONE.equals(min) && max==null) return PLUS;
    1.55 +        if (max!=null) {
    1.56 +            if(BigInteger.ZERO.equals(min) && BigInteger.ZERO.equals(max))    return ZERO;
    1.57 +            if(BigInteger.ZERO.equals(min) && BigInteger.ONE.equals(max))    return OPTIONAL;
    1.58 +            if(BigInteger.ONE.equals(min) && BigInteger.ONE.equals(max))    return ONE;
    1.59 +        }
    1.60 +        return new Multiplicity(min, max);
    1.61 +    }
    1.62 +
    1.63 +    public static Multiplicity create(int min, Integer max ) {
    1.64 +        return Multiplicity.create(BigInteger.valueOf(min), BigInteger.valueOf(max.intValue()));
    1.65 +    }
    1.66 +
    1.67 +    private Multiplicity(BigInteger min, BigInteger max) {
    1.68 +        this.min = min;
    1.69 +        this.max = max;
    1.70 +    }
    1.71 +
    1.72 +    private Multiplicity(int min, int max) {
    1.73 +        this(BigInteger.valueOf(min), BigInteger.valueOf(max));
    1.74 +    }
    1.75 +
    1.76 +    private Multiplicity(int min, Integer max) {
    1.77 +        this(BigInteger.valueOf(min), (max == null) ? null : BigInteger.valueOf(max));
    1.78 +    }
    1.79 +
    1.80 +    @Override
    1.81 +    public boolean equals(Object o) {
    1.82 +        if (!(o instanceof Multiplicity)) return false;
    1.83 +
    1.84 +        Multiplicity that = (Multiplicity) o;
    1.85 +
    1.86 +        if (!this.min.equals(that.min)) return false;
    1.87 +        if (this.max != null ? !this.max.equals(that.max) : that.max != null) return false;
    1.88 +
    1.89 +        return true;
    1.90 +    }
    1.91 +
    1.92 +    @Override
    1.93 +    public int hashCode() {
    1.94 +        return min.add(max).intValue();
    1.95 +    }
    1.96 +
    1.97 +    /** returns true if the multiplicity is (1,1). */
    1.98 +    public boolean isUnique() {
    1.99 +        if(max==null)    return false;
   1.100 +        return BigInteger.ONE.equals(min) && BigInteger.ONE.equals(max);
   1.101 +    }
   1.102 +
   1.103 +    /** returns true if the multiplicity is (0,1) */
   1.104 +    public boolean isOptional() {
   1.105 +        if(max==null) return false;
   1.106 +        return BigInteger.ZERO.equals(min) && BigInteger.ONE.equals(max);
   1.107 +    }
   1.108 +
   1.109 +    /** returns true if the multiplicity is (0,1) or (1,1). */
   1.110 +    public boolean isAtMostOnce() {
   1.111 +        if(max==null)    return false;
   1.112 +        return max.compareTo(BigInteger.ONE)<=0;
   1.113 +    }
   1.114 +
   1.115 +    /** returns true if the multiplicity is (0,0). */
   1.116 +    public boolean isZero() {
   1.117 +        if(max==null)    return false;
   1.118 +        return BigInteger.ZERO.equals(max);
   1.119 +    }
   1.120 +
   1.121 +    /**
   1.122 +     * Returns true if the multiplicity represented by this object
   1.123 +     * completely includes the multiplicity represented by the
   1.124 +     * other object. For example, we say [1,3] includes [1,2] but
   1.125 +     * [2,4] doesn't include [1,3].
   1.126 +     */
   1.127 +    public boolean includes( Multiplicity rhs ) {
   1.128 +        if (rhs.min.compareTo(min) == -1)   return false;
   1.129 +        if (max==null) return true;
   1.130 +        if (rhs.max==null) return false;
   1.131 +        return rhs.max.compareTo(max) <= 0;
   1.132 +    }
   1.133 +
   1.134 +    /**
   1.135 +     * Returns the string representation of the 'max' property.
   1.136 +     * Either a number or a token "unbounded".
   1.137 +     */
   1.138 +    public String getMaxString() {
   1.139 +        if(max==null)       return "unbounded";
   1.140 +        else                return max.toString();
   1.141 +    }
   1.142 +
   1.143 +    /** gets the string representation.
   1.144 +     * mainly debug purpose.
   1.145 +     */
   1.146 +    @Override
   1.147 +    public String toString() {
   1.148 +        return "("+min+','+getMaxString()+')';
   1.149 +    }
   1.150 +
   1.151 +    /** the constant representing the (0,0) multiplicity. */
   1.152 +    public static final Multiplicity ZERO = new Multiplicity(0,0);
   1.153 +
   1.154 +    /** the constant representing the (1,1) multiplicity. */
   1.155 +    public static final Multiplicity ONE = new Multiplicity(1,1);
   1.156 +
   1.157 +    /** the constant representing the (0,1) multiplicity. */
   1.158 +    public static final Multiplicity OPTIONAL = new Multiplicity(0,1);
   1.159 +
   1.160 +    /** the constant representing the (0,unbounded) multiplicity. */
   1.161 +    public static final Multiplicity STAR = new Multiplicity(0,null);
   1.162 +
   1.163 +    /** the constant representing the (1,unbounded) multiplicity. */
   1.164 +    public static final Multiplicity PLUS = new Multiplicity(1,null);
   1.165 +
   1.166 +// arithmetic methods
   1.167 +    public static Multiplicity choice( Multiplicity lhs, Multiplicity rhs ) {
   1.168 +        return create(
   1.169 +            lhs.min.min(rhs.min),
   1.170 +            (lhs.max==null || rhs.max==null) ? null : lhs.max.max(rhs.max) );
   1.171 +    }
   1.172 +    public static Multiplicity group( Multiplicity lhs, Multiplicity rhs ) {
   1.173 +        return create(
   1.174 +            lhs.min.add(rhs.min),
   1.175 +            (lhs.max==null || rhs.max==null) ? null : lhs.max.add(rhs.max) );
   1.176 +    }
   1.177 +    public static Multiplicity multiply( Multiplicity lhs, Multiplicity rhs ) {
   1.178 +        BigInteger min = lhs.min.multiply(rhs.min);
   1.179 +        BigInteger max;
   1.180 +        if (isZero(lhs.max) || isZero(rhs.max))
   1.181 +            max = BigInteger.ZERO;
   1.182 +        else
   1.183 +        if (lhs.max==null || rhs.max==null)
   1.184 +            max = null;
   1.185 +        else
   1.186 +            max = lhs.max.multiply(rhs.max);
   1.187 +        return create(min,max);
   1.188 +    }
   1.189 +
   1.190 +    private static boolean isZero(BigInteger i) {
   1.191 +        return (i != null && BigInteger.ZERO.equals(i));
   1.192 +    }
   1.193 +
   1.194 +    public static Multiplicity oneOrMore( Multiplicity c ) {
   1.195 +        if(c.max==null)  return c; // (x,*) => (x,*)
   1.196 +        if(BigInteger.ZERO.equals(c.max)) return c; // (0,0) => (0,0)
   1.197 +        else        return create( c.min, null );    // (x,y) => (x,*)
   1.198 +    }
   1.199 +
   1.200 +    public Multiplicity makeOptional() {
   1.201 +        if (BigInteger.ZERO.equals(min)) return this;
   1.202 +        return create(BigInteger.ZERO, max);
   1.203 +    }
   1.204 +
   1.205 +    public Multiplicity makeRepeated() {
   1.206 +        if (max==null || BigInteger.ZERO.equals(max))  return this;   // (0,0)* = (0,0)  and (n,unbounded)* = (n,unbounded)
   1.207 +        return create(min,null);
   1.208 +    }
   1.209 +}

mercurial