Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2011, 2012). All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.osgi.framework;
 
Version range. A version range is an interval describing a set of versions.

A range has a left (lower) endpoint and a right (upper) endpoint. Each endpoint can be open (excluded from the set) or closed (included in the set).

VersionRange objects are immutable.

Version:
$Id: d0c21e6a5015a7fa0b33179a29122ea7d137145a $
Since:
1.7
Immutable:
 
 
 public class VersionRange {
The left endpoint is open and is excluded from the range.

The value of LEFT_OPEN is '('.

 
 	public static final char	LEFT_OPEN'(';
The left endpoint is closed and is included in the range.

The value of LEFT_CLOSED is '['.

 
 	public static final char	LEFT_CLOSED'[';
The right endpoint is open and is excluded from the range.

The value of RIGHT_OPEN is ')'.

 
 	public static final char	RIGHT_OPEN')';
The right endpoint is closed and is included in the range.

The value of RIGHT_CLOSED is ']'.

 
 	public static final char	RIGHT_CLOSED']';
 
 	private final boolean		leftClosed;
 	private final Version		left;
 	private final Version		right;
 	private final boolean		rightClosed;
 	private final boolean		empty;
 
 	private transient String	versionRangeString /* default to null */;
 	private transient int		hash /* default to 0 */;
 
 	private static final String	LEFT_OPEN_DELIMITER"(";
 	private static final String	LEFT_CLOSED_DELIMITER"[";
 	private static final String	RIGHT_OPEN_DELIMITER")";
 	private static final String	RIGHT_CLOSED_DELIMITER"]";
 	private static final String	ENDPOINT_DELIMITER",";

Creates a version range from the specified versions.

Parameters:
leftType Must be either LEFT_CLOSED or LEFT_OPEN .
leftEndpoint Left endpoint of range. Must not be null.
rightEndpoint Right endpoint of range. May be null to indicate the right endpoint is Infinity.
rightType Must be either RIGHT_CLOSED or RIGHT_OPEN.
Throws:
java.lang.IllegalArgumentException If the arguments are invalid.
 
 	public VersionRange(char leftTypeVersion leftEndpointVersion rightEndpointchar rightType) {
 		if ((leftType != ) && (leftType != )) {
 			throw new IllegalArgumentException("invalid leftType \"" + leftType + "\"");
 		}
 		if ((rightType != ) && (rightType != )) {
 			throw new IllegalArgumentException("invalid rightType \"" + rightType + "\"");
 		}
		if (leftEndpoint == null) {
			throw new IllegalArgumentException("null leftEndpoint argument");
		}
		 = leftType == ;
		 = rightType == ;
		 = leftEndpoint;
		 = rightEndpoint;
	}

Creates a version range from the specified string.

Version range string grammar:

 range ::= interval | atleast
 interval ::= ( '[' | '(' ) left ',' right ( ']' | ')' )
 left ::= version
 right ::= version
 atleast ::= version
 

Parameters:
range String representation of the version range. The versions in the range must contain no whitespace. Other whitespace in the range string is ignored.
Throws:
java.lang.IllegalArgumentException If range is improperly formatted.
	public VersionRange(String range) {
		boolean closedLeft;
		boolean closedRight;
		Version endpointLeft;
		Version endpointRight;
		try {
			StringTokenizer st = new StringTokenizer(rangetrue);
			String token = st.nextToken().trim(); // whitespace or left delim
			if (token.length() == 0) { // leading whitespace
				token = st.nextToken(); // left delim
			}
			closedLeft = .equals(token);
			if (!closedLeft && !.equals(token)) {
				// first token is not a delimiter, so it must be "atleast"
				if (st.hasMoreTokens()) { // there must be no more tokens
					throw new IllegalArgumentException("invalid range \"" + range + "\": invalid format");
				}
				 = true;
				 = false;
				 = parseVersion(tokenrange);
				 = null;
				 = false;
				return;
			}
			endpointLeft = parseVersion(versionrange);
			token = st.nextToken(); // consume comma
			version = st.nextToken();
			token = st.nextToken(); // right delim
			closedRight = .equals(token);
			if (!closedRight && !.equals(token)) {
				throw new IllegalArgumentException("invalid range \"" + range + "\": invalid format");
			}
			endpointRight = parseVersion(versionrange);
			if (st.hasMoreTokens()) { // any more tokens have to be whitespace
				token = st.nextToken("").trim();
				if (token.length() != 0) { // trailing whitespace
					throw new IllegalArgumentException("invalid range \"" + range + "\": invalid format");
				}
			}
catch (NoSuchElementException e) {
			IllegalArgumentException iae = new IllegalArgumentException("invalid range \"" + range + "\": invalid format");
			iae.initCause(e);
			throw iae;
		}
		 = closedLeft;
		 = closedRight;
		 = endpointLeft;
		 = endpointRight;
	}

Parse version component into a Version.

Parameters:
version version component string
range Complete range string for exception message, if any
Returns:
Version
	private static Version parseVersion(String versionString range) {
		try {
			return Version.parseVersion(version);
catch (IllegalArgumentException e) {
			IllegalArgumentException iae = new IllegalArgumentException("invalid range \"" + range + "\": " + e.getMessage());
			iae.initCause(e);
			throw iae;
		}
	}

Returns the left endpoint of this version range.

Returns:
The left endpoint.
	public Version getLeft() {
		return ;
	}

Returns the right endpoint of this version range.

Returns:
The right endpoint. May be null which indicates the right endpoint is Infinity.
	public Version getRight() {
		return ;
	}

Returns the type of the left endpoint of this version range.

Returns:
LEFT_CLOSED if the left endpoint is closed or LEFT_OPEN if the left endpoint is open.
	public char getLeftType() {
	}

Returns the type of the right endpoint of this version range.

Returns:
RIGHT_CLOSED if the right endpoint is closed or RIGHT_OPEN if the right endpoint is open.
	public char getRightType() {
	}

Returns whether this version range includes the specified version.

Parameters:
version The version to test for inclusion in this version range.
Returns:
true if the specified version is included in this version range; false otherwise.
	public boolean includes(Version version) {
		if () {
			return false;
		}
		if (.compareTo(version) >= ( ? 1 : 0)) {
			return false;
		}
		if ( == null) {
			return true;
		}
		return .compareTo(version) >= ( ? 0 : 1);
	}

Returns the intersection of this version range with the specified version ranges.

Parameters:
ranges The version ranges to intersect with this version range.
Returns:
A version range representing the intersection of this version range and the specified version ranges. If no version ranges are specified, then this version range is returned.
	public VersionRange intersection(VersionRange... ranges) {
		if ((ranges == null) || (ranges.length == 0)) {
			return this;
		}
		// prime with data from this version range
		boolean closedLeft = ;
		boolean closedRight = ;
		Version endpointLeft = ;
		Version endpointRight = ;
		for (VersionRange range : ranges) {
			int comparison = endpointLeft.compareTo(range.left);
			if (comparison == 0) {
				closedLeft = closedLeft && range.leftClosed;
else {
				if (comparison < 0) { // move endpointLeft to the right
					endpointLeft = range.left;
					closedLeft = range.leftClosed;
				}
			}
			if (range.right != null) {
				if (endpointRight == null) {
					endpointRight = range.right;
					closedRight = range.rightClosed;
else {
					comparison = endpointRight.compareTo(range.right);
					if (comparison == 0) {
						closedRight = closedRight && range.rightClosed;
else {
						if (comparison > 0) { // move endpointRight to the left
							endpointRight = range.right;
							closedRight = range.rightClosed;
						}
					}
				}
			}
		}
		return new VersionRange(closedLeft ?  : endpointLeftendpointRightclosedRight ?  : );
	}

Returns whether this version range is empty. A version range is empty if the set of versions defined by the interval is empty.

Returns:
true if this version range is empty; false otherwise.
	public boolean isEmpty() {
		return ;
	}

Internal isEmpty behavior.

Returns:
true if this version range is empty; false otherwise.
	private boolean isEmpty0() {
		if ( == null) { // infinity
			return false;
		}
		int comparison = .compareTo();
		if (comparison == 0) { // endpoints equal
			return ! || !;
		}
		return comparison > 0; // true if left > right
	}

Returns whether this version range contains only a single version.

Returns:
true if this version range contains only a single version; false otherwise.
	public boolean isExact() {
		if ( || ( == null)) {
			return false;
		}
		if () {
			if () {
				// [l,r]: exact if l == r
				return .equals();
else {
				// [l,r): exact if l++ >= r
				Version adjacent1 = new Version(.getMajor(), .getMinor(), .getMicro(), .getQualifier() + "-");
				return adjacent1.compareTo() >= 0;
			}
else {
			if () {
				// (l,r] is equivalent to [l++,r]: exact if l++ == r
				Version adjacent1 = new Version(.getMajor(), .getMinor(), .getMicro(), .getQualifier() + "-");
				return adjacent1.equals();
else {
				// (l,r) is equivalent to [l++,r): exact if (l++)++ >=r
				Version adjacent2 = new Version(.getMajor(), .getMinor(), .getMicro(), .getQualifier() + "--");
				return adjacent2.compareTo() >= 0;
			}
		}
	}

Returns the string representation of this version range.

The format of the version range string will be a version string if the right end point is Infinity (null) or an interval string.

Returns:
The string representation of this version range.
	public String toString() {
		if ( != null) {
		}
		String leftVersion = .toString();
		if ( == null) {
			StringBuffer result = new StringBuffer(leftVersion.length() + 1);
			return  = result.toString();
		}
		String rightVerion = .toString();
		StringBuffer result = new StringBuffer(leftVersion.length() + rightVerion.length() + 5);
		return  = result.toString();
	}

Returns a hash code value for the object.

Returns:
An integer which is a hash code value for this object.
	public int hashCode() {
		if ( != 0) {
			return ;
		}
		if () {
			return  = 31;
		}
		int h = 31 + ( ? 7 : 5);
		h = 31 * h + .hashCode();
		if ( != null) {
			h = 31 * h + .hashCode();
			h = 31 * h + ( ? 7 : 5);
		}
		return  = h;
	}

Compares this VersionRange object to another object.

A version range is considered to be equal to another version range if both the endpoints and their types are equal or if both version ranges are empty.

Parameters:
object The VersionRange object to be compared.
Returns:
true if object is a VersionRange and is equal to this object; false otherwise.
	public boolean equals(Object object) {
		if (object == this) { // quicktest
			return true;
		}
		if (!(object instanceof VersionRange)) {
			return false;
		}
		VersionRange other = (VersionRangeobject;
		if ( && other.empty) {
			return true;
		}
		if ( == null) {
			return ( == other.leftClosed) && (other.right == null) && .equals(other.left);
		}
		return ( == other.leftClosed) && ( == other.rightClosed) && .equals(other.left) && .equals(other.right);
	}

Returns the filter string for this version range using the specified attribute name.

Parameters:
attributeName The attribute name to use in the returned filter string.
Returns:
A filter string for this version range using the specified attribute name.
Throws:
java.lang.IllegalArgumentException If the specified attribute name is not a valid attribute name.
See also:
"Core Specification, Filters, for a description of the filter string syntax."
	public String toFilterString(String attributeName) {
		if (attributeName.length() == 0) {
			throw new IllegalArgumentException("invalid attributeName \"" + attributeName + "\"");
		}
		for (char ch : attributeName.toCharArray()) {
			if ((ch == '=') || (ch == '>') || (ch == '<') || (ch == '~') || (ch == '(') || (ch == ')')) {
				throw new IllegalArgumentException("invalid attributeName \"" + attributeName + "\"");
			}
		}
		StringBuffer result = new StringBuffer(128);
		if ( != null) {
			result.append("(&");
		}
		if () {
			result.append('(');
			result.append(attributeName);
			result.append(">=");
			result.append(')');
else {
			result.append("(!(");
			result.append(attributeName);
			result.append("<=");
			result.append("))");
		}
		if ( != null) {
			if () {
				result.append('(');
				result.append(attributeName);
				result.append("<=");
				result.append(')');
else {
				result.append("(!(");
				result.append(attributeName);
				result.append(">=");
				result.append("))");
			}
			result.append(')');
		}
		return result.toString();
	}
New to GrepCode? Check out our FAQ X