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