001/* 002 * Copyright (C) 2009 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import static com.google.common.base.Preconditions.checkArgument; 020import static com.google.common.collect.CollectPreconditions.checkNonnegative; 021 022import com.google.common.annotations.GwtCompatible; 023import com.google.common.primitives.Ints; 024import com.google.errorprone.annotations.CanIgnoreReturnValue; 025import java.io.Serializable; 026import java.math.BigInteger; 027import java.util.NoSuchElementException; 028import org.jspecify.annotations.Nullable; 029 030/** 031 * A descriptor for a <i>discrete</i> {@code Comparable} domain such as all {@link Integer} 032 * instances. A discrete domain is one that supports the three basic operations: {@link #next}, 033 * {@link #previous} and {@link #distance}, according to their specifications. The methods {@link 034 * #minValue} and {@link #maxValue} should also be overridden for bounded types. 035 * 036 * <p>A discrete domain always represents the <i>entire</i> set of values of its type; it cannot 037 * represent partial domains such as "prime integers" or "strings of length 5." 038 * 039 * <p>See the Guava User Guide section on <a href= 040 * "https://github.com/google/guava/wiki/RangesExplained#discrete-domains">{@code 041 * DiscreteDomain}</a>. 042 * 043 * @author Kevin Bourrillion 044 * @since 10.0 045 */ 046@SuppressWarnings("rawtypes") // https://github.com/google/guava/issues/989 047@GwtCompatible 048public abstract class DiscreteDomain<C extends Comparable> { 049 050 /** 051 * Returns the discrete domain for values of type {@code Integer}. 052 * 053 * <p>This method always returns the same object. That object is serializable; deserializing it 054 * results in the same object too. 055 * 056 * @since 14.0 (since 10.0 as {@code DiscreteDomains.integers()}) 057 */ 058 public static DiscreteDomain<Integer> integers() { 059 return IntegerDomain.INSTANCE; 060 } 061 062 private static final class IntegerDomain extends DiscreteDomain<Integer> implements Serializable { 063 private static final IntegerDomain INSTANCE = new IntegerDomain(); 064 065 IntegerDomain() { 066 super(true); 067 } 068 069 @Override 070 public @Nullable Integer next(Integer value) { 071 int i = value; 072 return (i == Integer.MAX_VALUE) ? null : i + 1; 073 } 074 075 @Override 076 public @Nullable Integer previous(Integer value) { 077 int i = value; 078 return (i == Integer.MIN_VALUE) ? null : i - 1; 079 } 080 081 @Override 082 Integer offset(Integer origin, long distance) { 083 checkNonnegative(distance, "distance"); 084 return Ints.checkedCast(origin.longValue() + distance); 085 } 086 087 @Override 088 public long distance(Integer start, Integer end) { 089 return (long) end - start; 090 } 091 092 @Override 093 public Integer minValue() { 094 return Integer.MIN_VALUE; 095 } 096 097 @Override 098 public Integer maxValue() { 099 return Integer.MAX_VALUE; 100 } 101 102 private Object readResolve() { 103 return INSTANCE; 104 } 105 106 @Override 107 public String toString() { 108 return "DiscreteDomain.integers()"; 109 } 110 111 private static final long serialVersionUID = 0; 112 } 113 114 /** 115 * Returns the discrete domain for values of type {@code Long}. 116 * 117 * <p>This method always returns the same object. That object is serializable; deserializing it 118 * results in the same object too. 119 * 120 * @since 14.0 (since 10.0 as {@code DiscreteDomains.longs()}) 121 */ 122 public static DiscreteDomain<Long> longs() { 123 return LongDomain.INSTANCE; 124 } 125 126 private static final class LongDomain extends DiscreteDomain<Long> implements Serializable { 127 private static final LongDomain INSTANCE = new LongDomain(); 128 129 LongDomain() { 130 super(true); 131 } 132 133 @Override 134 public @Nullable Long next(Long value) { 135 long l = value; 136 return (l == Long.MAX_VALUE) ? null : l + 1; 137 } 138 139 @Override 140 public @Nullable Long previous(Long value) { 141 long l = value; 142 return (l == Long.MIN_VALUE) ? null : l - 1; 143 } 144 145 @Override 146 Long offset(Long origin, long distance) { 147 checkNonnegative(distance, "distance"); 148 long result = origin + distance; 149 if (result < 0) { 150 checkArgument(origin < 0, "overflow"); 151 } 152 return result; 153 } 154 155 @Override 156 public long distance(Long start, Long end) { 157 long result = end - start; 158 if (end > start && result < 0) { // overflow 159 return Long.MAX_VALUE; 160 } 161 if (end < start && result > 0) { // underflow 162 return Long.MIN_VALUE; 163 } 164 return result; 165 } 166 167 @Override 168 public Long minValue() { 169 return Long.MIN_VALUE; 170 } 171 172 @Override 173 public Long maxValue() { 174 return Long.MAX_VALUE; 175 } 176 177 private Object readResolve() { 178 return INSTANCE; 179 } 180 181 @Override 182 public String toString() { 183 return "DiscreteDomain.longs()"; 184 } 185 186 private static final long serialVersionUID = 0; 187 } 188 189 /** 190 * Returns the discrete domain for values of type {@code BigInteger}. 191 * 192 * <p>This method always returns the same object. That object is serializable; deserializing it 193 * results in the same object too. 194 * 195 * @since 15.0 196 */ 197 public static DiscreteDomain<BigInteger> bigIntegers() { 198 return BigIntegerDomain.INSTANCE; 199 } 200 201 private static final class BigIntegerDomain extends DiscreteDomain<BigInteger> 202 implements Serializable { 203 private static final BigIntegerDomain INSTANCE = new BigIntegerDomain(); 204 205 BigIntegerDomain() { 206 super(true); 207 } 208 209 private static final BigInteger MIN_LONG = BigInteger.valueOf(Long.MIN_VALUE); 210 private static final BigInteger MAX_LONG = BigInteger.valueOf(Long.MAX_VALUE); 211 212 @Override 213 public BigInteger next(BigInteger value) { 214 return value.add(BigInteger.ONE); 215 } 216 217 @Override 218 public BigInteger previous(BigInteger value) { 219 return value.subtract(BigInteger.ONE); 220 } 221 222 @Override 223 BigInteger offset(BigInteger origin, long distance) { 224 checkNonnegative(distance, "distance"); 225 return origin.add(BigInteger.valueOf(distance)); 226 } 227 228 @Override 229 public long distance(BigInteger start, BigInteger end) { 230 return end.subtract(start).max(MIN_LONG).min(MAX_LONG).longValue(); 231 } 232 233 private Object readResolve() { 234 return INSTANCE; 235 } 236 237 @Override 238 public String toString() { 239 return "DiscreteDomain.bigIntegers()"; 240 } 241 242 private static final long serialVersionUID = 0; 243 } 244 245 final boolean supportsFastOffset; 246 247 /** Constructor for use by subclasses. */ 248 protected DiscreteDomain() { 249 this(false); 250 } 251 252 /** Private constructor for built-in DiscreteDomains supporting fast offset. */ 253 private DiscreteDomain(boolean supportsFastOffset) { 254 this.supportsFastOffset = supportsFastOffset; 255 } 256 257 /** 258 * Returns, conceptually, "origin + distance", or equivalently, the result of calling {@link 259 * #next} on {@code origin} {@code distance} times. 260 */ 261 C offset(C origin, long distance) { 262 C current = origin; 263 checkNonnegative(distance, "distance"); 264 for (long i = 0; i < distance; i++) { 265 current = next(current); 266 if (current == null) { 267 throw new IllegalArgumentException( 268 "overflowed computing offset(" + origin + ", " + distance + ")"); 269 } 270 } 271 return current; 272 } 273 274 /** 275 * Returns the unique least value of type {@code C} that is greater than {@code value}, or {@code 276 * null} if none exists. Inverse operation to {@link #previous}. 277 * 278 * @param value any value of type {@code C} 279 * @return the least value greater than {@code value}, or {@code null} if {@code value} is {@code 280 * maxValue()} 281 */ 282 public abstract @Nullable C next(C value); 283 284 /** 285 * Returns the unique greatest value of type {@code C} that is less than {@code value}, or {@code 286 * null} if none exists. Inverse operation to {@link #next}. 287 * 288 * @param value any value of type {@code C} 289 * @return the greatest value less than {@code value}, or {@code null} if {@code value} is {@code 290 * minValue()} 291 */ 292 public abstract @Nullable C previous(C value); 293 294 /** 295 * Returns a signed value indicating how many nested invocations of {@link #next} (if positive) or 296 * {@link #previous} (if negative) are needed to reach {@code end} starting from {@code start}. 297 * For example, if {@code end = next(next(next(start)))}, then {@code distance(start, end) == 3} 298 * and {@code distance(end, start) == -3}. As well, {@code distance(a, a)} is always zero. 299 * 300 * <p>Note that this function is necessarily well-defined for any discrete type. 301 * 302 * @return the distance as described above, or {@link Long#MIN_VALUE} or {@link Long#MAX_VALUE} if 303 * the distance is too small or too large, respectively. 304 */ 305 public abstract long distance(C start, C end); 306 307 /** 308 * Returns the minimum value of type {@code C}, if it has one. The minimum value is the unique 309 * value for which {@link Comparable#compareTo(Object)} never returns a positive value for any 310 * input of type {@code C}. 311 * 312 * <p>The default implementation throws {@code NoSuchElementException}. 313 * 314 * @return the minimum value of type {@code C}; never null 315 * @throws NoSuchElementException if the type has no (practical) minimum value; for example, 316 * {@link java.math.BigInteger} 317 */ 318 @CanIgnoreReturnValue 319 public C minValue() { 320 throw new NoSuchElementException(); 321 } 322 323 /** 324 * Returns the maximum value of type {@code C}, if it has one. The maximum value is the unique 325 * value for which {@link Comparable#compareTo(Object)} never returns a negative value for any 326 * input of type {@code C}. 327 * 328 * <p>The default implementation throws {@code NoSuchElementException}. 329 * 330 * @return the maximum value of type {@code C}; never null 331 * @throws NoSuchElementException if the type has no (practical) maximum value; for example, 332 * {@link java.math.BigInteger} 333 */ 334 @CanIgnoreReturnValue 335 public C maxValue() { 336 throw new NoSuchElementException(); 337 } 338}