aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.internal.xjc.model; aoqi@0: aoqi@0: import java.math.BigInteger; aoqi@0: aoqi@0: aoqi@0: aoqi@0: /** aoqi@0: * represents a possible number of occurence. aoqi@0: * aoqi@0: * Usually, denoted by a pair of integers like (1,1) or (5,10). aoqi@0: * A special value "unbounded" is allowed as the upper bound. aoqi@0: * aoqi@0: *

aoqi@0: * For example, (0,unbounded) corresponds to the '*' occurence of DTD. aoqi@0: * (0,1) corresponds to the '?' occurence of DTD. aoqi@0: * aoqi@0: * @author aoqi@0: * Kohsuke KAWAGUCHI aoqi@0: */ aoqi@0: public final class Multiplicity { aoqi@0: public final BigInteger min; aoqi@0: public final BigInteger max; // null is used to represent "unbounded". aoqi@0: aoqi@0: public static Multiplicity create(BigInteger min, BigInteger max ) { aoqi@0: if (BigInteger.ZERO.equals(min) && max==null) return STAR; aoqi@0: if (BigInteger.ONE.equals(min) && max==null) return PLUS; aoqi@0: if (max!=null) { aoqi@0: if(BigInteger.ZERO.equals(min) && BigInteger.ZERO.equals(max)) return ZERO; aoqi@0: if(BigInteger.ZERO.equals(min) && BigInteger.ONE.equals(max)) return OPTIONAL; aoqi@0: if(BigInteger.ONE.equals(min) && BigInteger.ONE.equals(max)) return ONE; aoqi@0: } aoqi@0: return new Multiplicity(min, max); aoqi@0: } aoqi@0: aoqi@0: public static Multiplicity create(int min, Integer max ) { aoqi@0: return Multiplicity.create(BigInteger.valueOf(min), BigInteger.valueOf(max.intValue())); aoqi@0: } aoqi@0: aoqi@0: private Multiplicity(BigInteger min, BigInteger max) { aoqi@0: this.min = min; aoqi@0: this.max = max; aoqi@0: } aoqi@0: aoqi@0: private Multiplicity(int min, int max) { aoqi@0: this(BigInteger.valueOf(min), BigInteger.valueOf(max)); aoqi@0: } aoqi@0: aoqi@0: private Multiplicity(int min, Integer max) { aoqi@0: this(BigInteger.valueOf(min), (max == null) ? null : BigInteger.valueOf(max)); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public boolean equals(Object o) { aoqi@0: if (!(o instanceof Multiplicity)) return false; aoqi@0: aoqi@0: Multiplicity that = (Multiplicity) o; aoqi@0: aoqi@0: if (!this.min.equals(that.min)) return false; aoqi@0: if (this.max != null ? !this.max.equals(that.max) : that.max != null) return false; aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: public int hashCode() { aoqi@0: return min.add(max).intValue(); aoqi@0: } aoqi@0: aoqi@0: /** returns true if the multiplicity is (1,1). */ aoqi@0: public boolean isUnique() { aoqi@0: if(max==null) return false; aoqi@0: return BigInteger.ONE.equals(min) && BigInteger.ONE.equals(max); aoqi@0: } aoqi@0: aoqi@0: /** returns true if the multiplicity is (0,1) */ aoqi@0: public boolean isOptional() { aoqi@0: if(max==null) return false; aoqi@0: return BigInteger.ZERO.equals(min) && BigInteger.ONE.equals(max); aoqi@0: } aoqi@0: aoqi@0: /** returns true if the multiplicity is (0,1) or (1,1). */ aoqi@0: public boolean isAtMostOnce() { aoqi@0: if(max==null) return false; aoqi@0: return max.compareTo(BigInteger.ONE)<=0; aoqi@0: } aoqi@0: aoqi@0: /** returns true if the multiplicity is (0,0). */ aoqi@0: public boolean isZero() { aoqi@0: if(max==null) return false; aoqi@0: return BigInteger.ZERO.equals(max); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns true if the multiplicity represented by this object aoqi@0: * completely includes the multiplicity represented by the aoqi@0: * other object. For example, we say [1,3] includes [1,2] but aoqi@0: * [2,4] doesn't include [1,3]. aoqi@0: */ aoqi@0: public boolean includes( Multiplicity rhs ) { aoqi@0: if (rhs.min.compareTo(min) == -1) return false; aoqi@0: if (max==null) return true; aoqi@0: if (rhs.max==null) return false; aoqi@0: return rhs.max.compareTo(max) <= 0; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Returns the string representation of the 'max' property. aoqi@0: * Either a number or a token "unbounded". aoqi@0: */ aoqi@0: public String getMaxString() { aoqi@0: if(max==null) return "unbounded"; aoqi@0: else return max.toString(); aoqi@0: } aoqi@0: aoqi@0: /** gets the string representation. aoqi@0: * mainly debug purpose. aoqi@0: */ aoqi@0: @Override aoqi@0: public String toString() { aoqi@0: return "("+min+','+getMaxString()+')'; aoqi@0: } aoqi@0: aoqi@0: /** the constant representing the (0,0) multiplicity. */ aoqi@0: public static final Multiplicity ZERO = new Multiplicity(0,0); aoqi@0: aoqi@0: /** the constant representing the (1,1) multiplicity. */ aoqi@0: public static final Multiplicity ONE = new Multiplicity(1,1); aoqi@0: aoqi@0: /** the constant representing the (0,1) multiplicity. */ aoqi@0: public static final Multiplicity OPTIONAL = new Multiplicity(0,1); aoqi@0: aoqi@0: /** the constant representing the (0,unbounded) multiplicity. */ aoqi@0: public static final Multiplicity STAR = new Multiplicity(0,null); aoqi@0: aoqi@0: /** the constant representing the (1,unbounded) multiplicity. */ aoqi@0: public static final Multiplicity PLUS = new Multiplicity(1,null); aoqi@0: aoqi@0: // arithmetic methods aoqi@0: public static Multiplicity choice( Multiplicity lhs, Multiplicity rhs ) { aoqi@0: return create( aoqi@0: lhs.min.min(rhs.min), aoqi@0: (lhs.max==null || rhs.max==null) ? null : lhs.max.max(rhs.max) ); aoqi@0: } aoqi@0: public static Multiplicity group( Multiplicity lhs, Multiplicity rhs ) { aoqi@0: return create( aoqi@0: lhs.min.add(rhs.min), aoqi@0: (lhs.max==null || rhs.max==null) ? null : lhs.max.add(rhs.max) ); aoqi@0: } aoqi@0: public static Multiplicity multiply( Multiplicity lhs, Multiplicity rhs ) { aoqi@0: BigInteger min = lhs.min.multiply(rhs.min); aoqi@0: BigInteger max; aoqi@0: if (isZero(lhs.max) || isZero(rhs.max)) aoqi@0: max = BigInteger.ZERO; aoqi@0: else aoqi@0: if (lhs.max==null || rhs.max==null) aoqi@0: max = null; aoqi@0: else aoqi@0: max = lhs.max.multiply(rhs.max); aoqi@0: return create(min,max); aoqi@0: } aoqi@0: aoqi@0: private static boolean isZero(BigInteger i) { aoqi@0: return (i != null && BigInteger.ZERO.equals(i)); aoqi@0: } aoqi@0: aoqi@0: public static Multiplicity oneOrMore( Multiplicity c ) { aoqi@0: if(c.max==null) return c; // (x,*) => (x,*) aoqi@0: if(BigInteger.ZERO.equals(c.max)) return c; // (0,0) => (0,0) aoqi@0: else return create( c.min, null ); // (x,y) => (x,*) aoqi@0: } aoqi@0: aoqi@0: public Multiplicity makeOptional() { aoqi@0: if (BigInteger.ZERO.equals(min)) return this; aoqi@0: return create(BigInteger.ZERO, max); aoqi@0: } aoqi@0: aoqi@0: public Multiplicity makeRepeated() { aoqi@0: if (max==null || BigInteger.ZERO.equals(max)) return this; // (0,0)* = (0,0) and (n,unbounded)* = (n,unbounded) aoqi@0: return create(min,null); aoqi@0: } aoqi@0: }