1 /* 2 * #%L 3 * VersionRange.java - mongodb-async-driver - Allanbank Consulting, Inc. 4 * %% 5 * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc. 6 * %% 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * #L% 19 */ 20 package com.allanbank.mongodb.client; 21 22 import java.io.Serializable; 23 24 import com.allanbank.mongodb.Version; 25 26 /** 27 * VersionRange provides a simple class to hold a range of versions. 28 * 29 * @api.no This class is <b>NOT</b> part of the drivers API. This class may be 30 * mutated in incompatible ways between any two releases of the driver. 31 * @copyright 2014, Allanbank Consulting, Inc., All Rights Reserved 32 */ 33 public class VersionRange implements Serializable { 34 /** Serialization version for the class. */ 35 private static final long serialVersionUID = 7841643157458023019L; 36 37 /** 38 * Returns a {@link VersionRange} that is only limited by a maximum version. 39 * The minimum version is set to {@link Version#VERSION_0}. 40 * 41 * @param maxVersion 42 * The maximum version in the range. If <code>null</code> then 43 * <code>null</code> is returned. 44 * @return The version range with only a maximum value. 45 */ 46 public static VersionRange maximum(final Version maxVersion) { 47 if (maxVersion != null) { 48 return new VersionRange(Version.VERSION_0, maxVersion); 49 } 50 return null; 51 } 52 53 /** 54 * Returns a {@link VersionRange} that is only limited by a minimum version. 55 * The maximum version is set to {@link Version#UNKNOWN}. 56 * 57 * @param minVersion 58 * The minimum version in the range. If <code>null</code> then 59 * <code>null</code> is returned. 60 * @return The version range with only a minimum value. 61 */ 62 public static VersionRange minimum(final Version minVersion) { 63 if (minVersion != null) { 64 return new VersionRange(minVersion, Version.UNKNOWN); 65 } 66 return null; 67 } 68 69 /** 70 * Returns a {@link VersionRange} based on the minimum and maximum versions 71 * provided. 72 * <p> 73 * If {@code minVersion} and {@code maxVersion} are both <code>null</code> 74 * then <code>null</code> is returned. 75 * </p> 76 * <p> 77 * If only {@code minVersion} is <code>null</code> then the lower bound is 78 * set to {@link Version#VERSION_0}. 79 * </p> 80 * <p> 81 * If only {@code maxVersion} is <code>null</code> then the upper bound is 82 * set to {@link Version#UNKNOWN}. 83 * </p> 84 * 85 * @param minVersion 86 * The minimum version in the range. If <code>null</code> then 87 * <code>null</code> is returned. 88 * @param maxVersion 89 * The maximum version in the range. If <code>null</code> then 90 * <code>null</code> is returned. 91 * @return The version range with only a minimum value. 92 */ 93 public static VersionRange range(final Version minVersion, 94 final Version maxVersion) { 95 if (minVersion != null) { 96 if (maxVersion != null) { 97 return new VersionRange(minVersion, maxVersion); 98 } 99 return new VersionRange(minVersion, Version.UNKNOWN); 100 } 101 else if (maxVersion != null) { 102 return new VersionRange(Version.VERSION_0, maxVersion); 103 } 104 return null; 105 } 106 107 /** The lower, inclusive, bounds of the range. */ 108 private final Version myLowerBounds; 109 110 /** The upper, exclusive, bounds of the range. */ 111 private final Version myUpperBounds; 112 113 /** 114 * Creates a new VersionRange. 115 * 116 * @param lowerBounds 117 * The lower, inclusive, bounds of the range. 118 * @param upperBounds 119 * The upper, exclusive, bounds of the range. 120 */ 121 private VersionRange(final Version lowerBounds, final Version upperBounds) { 122 myLowerBounds = lowerBounds; 123 myUpperBounds = upperBounds; 124 } 125 126 /** 127 * Returns true if the version is within the bounds of this 128 * {@link VersionRange}. 129 * 130 * @param version 131 * The version to check if it is within range. 132 * @return True if the version is within the bounds of this 133 * {@link VersionRange}. 134 */ 135 public boolean contains(final Version version) { 136 return (myLowerBounds.compareTo(version) <= 0) 137 && (myUpperBounds.compareTo(version) > 0); 138 } 139 140 /** 141 * {@inheritDoc} 142 * <p> 143 * Overridden to compare this {@link VersionRange} to the other. 144 * </p> 145 */ 146 @Override 147 public boolean equals(final Object object) { 148 boolean result = false; 149 if (this == object) { 150 result = true; 151 } 152 else if ((object != null) && (getClass() == object.getClass())) { 153 final VersionRange other = (VersionRange) object; 154 155 result = myLowerBounds.equals(other.myLowerBounds) 156 && myUpperBounds.equals(other.myUpperBounds); 157 } 158 return result; 159 } 160 161 /** 162 * Returns the lower, inclusive, bounds of the range. 163 * 164 * @return The lower, inclusive, bounds of the range. 165 */ 166 public Version getLowerBounds() { 167 return myLowerBounds; 168 } 169 170 /** 171 * Returns the upper, exclusive, bounds of the range. 172 * 173 * @return The upper, exclusive, bounds of the range. 174 */ 175 public Version getUpperBounds() { 176 return myUpperBounds; 177 } 178 179 /** 180 * {@inheritDoc} 181 * <p> 182 * Overridden to hash the version range. 183 * </p> 184 */ 185 @Override 186 public int hashCode() { 187 int result = 1; 188 result = (31 * result) + myLowerBounds.hashCode(); 189 result = (31 * result) + myUpperBounds.hashCode(); 190 return result; 191 } 192 193 /** 194 * {@inheritDoc} 195 */ 196 @Override 197 public String toString() { 198 final StringBuilder builder = new StringBuilder(); 199 200 builder.append("["); 201 builder.append(myLowerBounds); 202 builder.append(", "); 203 builder.append(myUpperBounds); 204 builder.append(")"); 205 206 return builder.toString(); 207 } 208 }