11d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/* 21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2009 The Guava Authors 31d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 41d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License"); 51d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * you may not use this file except in compliance with the License. 61d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * You may obtain a copy of the License at 71d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 81d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * http://www.apache.org/licenses/LICENSE-2.0 91d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Unless required by applicable law or agreed to in writing, software 111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS, 121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * See the License for the specific language governing permissions and 141d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * limitations under the License. 151d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 161d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 171d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpackage com.google.common.collect; 181d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 191d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.Beta; 201d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport com.google.common.annotations.GwtCompatible; 211d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 221d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertimport java.util.NoSuchElementException; 231d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 241d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert/** 251d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * A descriptor for a <i>discrete</i> {@code Comparable} domain such as all 261d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@link Integer}s. A discrete domain is one that supports the three basic 271d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * operations: {@link #next}, {@link #previous} and {@link #distance}, according 281d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * to their specifications. The methods {@link #minValue} and {@link #maxValue} 291d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * should also be overridden for bounded types. 301d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 311d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>A discrete domain always represents the <i>entire</i> set of values of its 321d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * type; it cannot represent partial domains such as "prime integers" or 331d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * "strings of length 5." 341d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 351d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @author Kevin Bourrillion 361d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @since 10.0 371d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @see DiscreteDomains 381d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@GwtCompatible 401d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert@Beta 411d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringertpublic abstract class DiscreteDomain<C extends Comparable> { 421d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** Constructor for use by subclasses. */ 431d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert protected DiscreteDomain() {} 441d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 451d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 461d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the unique least value of type {@code C} that is greater than 471d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code value}, or {@code null} if none exists. Inverse operation to {@link 481d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * #previous}. 491d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 501d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param value any value of type {@code C} 511d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the least value greater than {@code value}, or {@code null} if 521d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code value} is {@code maxValue()} 531d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 541d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public abstract C next(C value); 551d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 561d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 571d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the unique greatest value of type {@code C} that is less than 581d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code value}, or {@code null} if none exists. Inverse operation to {@link 591d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * #next}. 601d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 611d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @param value any value of type {@code C} 621d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the greatest value less than {@code value}, or {@code null} if 631d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code value} is {@code minValue()} 641d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 651d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public abstract C previous(C value); 661d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 671d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 681d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns a signed value indicating how many nested invocations of {@link 691d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * #next} (if positive) or {@link #previous} (if negative) are needed to reach 701d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@code end} starting from {@code start}. For example, if {@code end = 711d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * next(next(next(start)))}, then {@code distance(start, end) == 3} and {@code 721d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * distance(end, start) == -3}. As well, {@code distance(a, a)} is always 731d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * zero. 741d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 751d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>Note that this function is necessarily well-defined for any discrete 761d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * type. 771d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 781d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the distance as described above, or {@link Long#MIN_VALUE} or 791d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * {@link Long#MIN_VALUE} if the distance is too small or too large, 801d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * respectively. 811d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 821d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public abstract long distance(C start, C end); 831d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 841d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 851d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the minimum value of type {@code C}, if it has one. The minimum 861d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * value is the unique value for which {@link Comparable#compareTo(Object)} 871d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * never returns a positive value for any input of type {@code C}. 881d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 891d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>The default implementation throws {@code NoSuchElementException}. 901d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 911d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the minimum value of type {@code C}; never null 921d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @throws NoSuchElementException if the type has no (practical) minimum 931d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * value; for example, {@link java.math.BigInteger} 941d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 951d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public C minValue() { 961d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert throw new NoSuchElementException(); 971d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 981d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert 991d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert /** 1001d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Returns the maximum value of type {@code C}, if it has one. The maximum 1011d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * value is the unique value for which {@link Comparable#compareTo(Object)} 1021d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * never returns a negative value for any input of type {@code C}. 1031d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 1041d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * <p>The default implementation throws {@code NoSuchElementException}. 1051d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * 1061d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @return the maximum value of type {@code C}; never null 1071d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * @throws NoSuchElementException if the type has no (practical) maximum 1081d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * value; for example, {@link java.math.BigInteger} 1091d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert */ 1101d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert public C maxValue() { 1111d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert throw new NoSuchElementException(); 1121d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert } 1131d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert} 114