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 com.google.common.annotations.Beta;
020import com.google.common.annotations.GwtCompatible;
021
022import java.io.Serializable;
023import java.math.BigInteger;
024import java.util.NoSuchElementException;
025
026/**
027 * A descriptor for a <i>discrete</i> {@code Comparable} domain such as all
028 * {@link Integer} instances. A discrete domain is one that supports the three basic
029 * operations: {@link #next}, {@link #previous} and {@link #distance}, according
030 * to their specifications. The methods {@link #minValue} and {@link #maxValue}
031 * should also be overridden for bounded types.
032 *
033 * <p>A discrete domain always represents the <i>entire</i> set of values of its
034 * type; it cannot represent partial domains such as "prime integers" or
035 * "strings of length 5."
036 *
037 * <p>See the Guava User Guide section on <a href=
038 * "http://code.google.com/p/guava-libraries/wiki/RangesExplained#Discrete_Domains">
039 * {@code DiscreteDomain}</a>.
040 *
041 * @author Kevin Bourrillion
042 * @since 10.0
043 */
044@GwtCompatible
045@Beta
046public abstract class DiscreteDomain<C extends Comparable> {
047
048  /**
049   * Returns the discrete domain for values of type {@code Integer}.
050   * 
051   * @since 14.0 (since 10.0 as {@code DiscreteDomains.integers()})
052   */
053  public static DiscreteDomain<Integer> integers() {
054    return IntegerDomain.INSTANCE;
055  }
056
057  private static final class IntegerDomain extends DiscreteDomain<Integer>
058      implements Serializable {
059    private static final IntegerDomain INSTANCE = new IntegerDomain();
060
061    @Override public Integer next(Integer value) {
062      int i = value;
063      return (i == Integer.MAX_VALUE) ? null : i + 1;
064    }
065
066    @Override public Integer previous(Integer value) {
067      int i = value;
068      return (i == Integer.MIN_VALUE) ? null : i - 1;
069    }
070
071    @Override public long distance(Integer start, Integer end) {
072      return (long) end - start;
073    }
074
075    @Override public Integer minValue() {
076      return Integer.MIN_VALUE;
077    }
078
079    @Override public Integer maxValue() {
080      return Integer.MAX_VALUE;
081    }
082
083    private Object readResolve() {
084      return INSTANCE;
085    }
086
087    @Override
088    public String toString() {
089      return "DiscreteDomain.integers()";
090    }
091
092    private static final long serialVersionUID = 0;
093  }
094
095  /**
096   * Returns the discrete domain for values of type {@code Long}.
097   * 
098   * @since 14.0 (since 10.0 as {@code DiscreteDomains.longs()})
099   */
100  public static DiscreteDomain<Long> longs() {
101    return LongDomain.INSTANCE;
102  }
103
104  private static final class LongDomain extends DiscreteDomain<Long>
105      implements Serializable {
106    private static final LongDomain INSTANCE = new LongDomain();
107
108    @Override public Long next(Long value) {
109      long l = value;
110      return (l == Long.MAX_VALUE) ? null : l + 1;
111    }
112
113    @Override public Long previous(Long value) {
114      long l = value;
115      return (l == Long.MIN_VALUE) ? null : l - 1;
116    }
117
118    @Override public long distance(Long start, Long end) {
119      long result = end - start;
120      if (end > start && result < 0) { // overflow
121        return Long.MAX_VALUE;
122      }
123      if (end < start && result > 0) { // underflow
124        return Long.MIN_VALUE;
125      }
126      return result;
127    }
128
129    @Override public Long minValue() {
130      return Long.MIN_VALUE;
131    }
132
133    @Override public Long maxValue() {
134      return Long.MAX_VALUE;
135    }
136
137    private Object readResolve() {
138      return INSTANCE;
139    }
140
141    @Override
142    public String toString() {
143      return "DiscreteDomain.longs()";
144    }
145
146    private static final long serialVersionUID = 0;
147  }
148  
149  /**
150   * Returns the discrete domain for values of type {@code BigInteger}.
151   *
152   * @since 15.0
153   */
154  public static DiscreteDomain<BigInteger> bigIntegers() {
155    return BigIntegerDomain.INSTANCE;
156  }
157
158  private static final class BigIntegerDomain extends DiscreteDomain<BigInteger>
159      implements Serializable {
160    private static final BigIntegerDomain INSTANCE = new BigIntegerDomain();
161
162    private static final BigInteger MIN_LONG =
163        BigInteger.valueOf(Long.MIN_VALUE);
164    private static final BigInteger MAX_LONG =
165        BigInteger.valueOf(Long.MAX_VALUE);
166
167    @Override public BigInteger next(BigInteger value) {
168      return value.add(BigInteger.ONE);
169    }
170
171    @Override public BigInteger previous(BigInteger value) {
172      return value.subtract(BigInteger.ONE);
173    }
174
175    @Override public long distance(BigInteger start, BigInteger end) {
176      return end.subtract(start).max(MIN_LONG).min(MAX_LONG).longValue();
177    }
178
179    private Object readResolve() {
180      return INSTANCE;
181    }
182
183    @Override
184    public String toString() {
185      return "DiscreteDomain.bigIntegers()";
186    }
187
188    private static final long serialVersionUID = 0;
189  }
190  
191  /** Constructor for use by subclasses. */
192  protected DiscreteDomain() {}
193
194  /**
195   * Returns the unique least value of type {@code C} that is greater than
196   * {@code value}, or {@code null} if none exists. Inverse operation to {@link
197   * #previous}.
198   *
199   * @param value any value of type {@code C}
200   * @return the least value greater than {@code value}, or {@code null} if
201   *     {@code value} is {@code maxValue()}
202   */
203  public abstract C next(C value);
204
205  /**
206   * Returns the unique greatest value of type {@code C} that is less than
207   * {@code value}, or {@code null} if none exists. Inverse operation to {@link
208   * #next}.
209   *
210   * @param value any value of type {@code C}
211   * @return the greatest value less than {@code value}, or {@code null} if
212   *     {@code value} is {@code minValue()}
213   */
214  public abstract C previous(C value);
215
216  /**
217   * Returns a signed value indicating how many nested invocations of {@link
218   * #next} (if positive) or {@link #previous} (if negative) are needed to reach
219   * {@code end} starting from {@code start}. For example, if {@code end =
220   * next(next(next(start)))}, then {@code distance(start, end) == 3} and {@code
221   * distance(end, start) == -3}. As well, {@code distance(a, a)} is always
222   * zero.
223   *
224   * <p>Note that this function is necessarily well-defined for any discrete
225   * type.
226   *
227   * @return the distance as described above, or {@link Long#MIN_VALUE} or
228   *     {@link Long#MAX_VALUE} if the distance is too small or too large,
229   *     respectively.
230   */
231  public abstract long distance(C start, C end);
232
233  /**
234   * Returns the minimum value of type {@code C}, if it has one. The minimum
235   * value is the unique value for which {@link Comparable#compareTo(Object)}
236   * never returns a positive value for any input of type {@code C}.
237   *
238   * <p>The default implementation throws {@code NoSuchElementException}.
239   *
240   * @return the minimum value of type {@code C}; never null
241   * @throws NoSuchElementException if the type has no (practical) minimum
242   *     value; for example, {@link java.math.BigInteger}
243   */
244  public C minValue() {
245    throw new NoSuchElementException();
246  }
247
248  /**
249   * Returns the maximum value of type {@code C}, if it has one. The maximum
250   * value is the unique value for which {@link Comparable#compareTo(Object)}
251   * never returns a negative value for any input of type {@code C}.
252   *
253   * <p>The default implementation throws {@code NoSuchElementException}.
254   *
255   * @return the maximum value of type {@code C}; never null
256   * @throws NoSuchElementException if the type has no (practical) maximum
257   *     value; for example, {@link java.math.BigInteger}
258   */
259  public C maxValue() {
260    throw new NoSuchElementException();
261  }
262  
263}