com.sun.tools.xjc.model.Multiplicity Maven / Gradle / Ivy
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package com.sun.tools.xjc.model;
/**
* represents a possible number of occurence.
*
* Usually, denoted by a pair of integers like (1,1) or (5,10).
* A special value "unbounded" is allowed as the upper bound.
*
*
* For example, (0,unbounded) corresponds to the '*' occurence of DTD.
* (0,1) corresponds to the '?' occurence of DTD.
*
* @author
* Kohsuke KAWAGUCHI
*/
public final class Multiplicity {
public final int min;
public final Integer max; // null is used to represent "unbounded".
public static Multiplicity create( int min, Integer max ) {
if(min==0 && max==null) return STAR;
if(min==1 && max==null) return PLUS;
if(max!=null) {
if(min==0 && max==0) return ZERO;
if(min==0 && max==1) return OPTIONAL;
if(min==1 && max==1) return ONE;
}
return new Multiplicity(min,max);
}
private Multiplicity( int min, Integer max ) {
this.min = min; this.max = max;
}
public boolean equals(Object o) {
if (!(o instanceof Multiplicity)) return false;
Multiplicity that = (Multiplicity) o;
if (this.min != that.min) return false;
if (this.max != null ? !this.max.equals(that.max) : that.max != null) return false;
return true;
}
public int hashCode() {
int result;
result = min;
result = 29 * result + (max != null ? max.hashCode() : 0);
return result;
}
/** returns true if the multiplicity is (1,1). */
public boolean isUnique() {
if(max==null) return false;
return min==1 && max==1;
}
/** returns true if the multiplicity is (0,1) */
public boolean isOptional() {
if(max==null) return false;
return min==0 && max==1;
}
/** returns true if the multiplicity is (0,1) or (1,1). */
public boolean isAtMostOnce() {
if(max==null) return false;
return max<=1;
}
/** returns true if the multiplicity is (0,0). */
public boolean isZero() {
if(max==null) return false;
return max==0;
}
/**
* Returns true if the multiplicity represented by this object
* completely includes the multiplicity represented by the
* other object. For example, we say [1,3] includes [1,2] but
* [2,4] doesn't include [1,3].
*/
public boolean includes( Multiplicity rhs ) {
if( rhs.min (x,*)
if(c.max==0 ) return c; // (0,0) => (0,0)
else return create( c.min, null ); // (x,y) => (x,*)
}
public Multiplicity makeOptional() {
if(min==0) return this;
return create(0,max);
}
public Multiplicity makeRepeated() {
if(max==null || max==0) return this; // (0,0)* = (0,0) and (n,unbounded)* = (n,unbounded)
return create(min,null);
}
}