Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2009 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.collect;
 
 
A descriptor for a discrete Comparable domain such as all java.lang.Integer instances. A discrete domain is one that supports the three basic operations: next(java.lang.Comparable), previous(java.lang.Comparable) and distance(java.lang.Comparable,java.lang.Comparable), according to their specifications. The methods minValue() and maxValue() should also be overridden for bounded types.

A discrete domain always represents the entire set of values of its type; it cannot represent partial domains such as "prime integers" or "strings of length 5."

See the Guava User Guide section on DiscreteDomain.

Author(s):
Kevin Bourrillion
Since:
10.0
 
 public abstract class DiscreteDomain<C extends Comparable> {

  
Returns the discrete domain for values of type Integer.

Since:
14.0 (since 10.0 as DiscreteDomains.integers())
 
   public static DiscreteDomain<Integerintegers() {
     return .;
   }
 
   private static final class IntegerDomain extends DiscreteDomain<Integer>
       implements Serializable {
     private static final IntegerDomain INSTANCE = new IntegerDomain();
 
     @Override public Integer next(Integer value) {
       int i = value;
       return (i == .) ? null : i + 1;
     }
 
     @Override public Integer previous(Integer value) {
       int i = value;
       return (i == .) ? null : i - 1;
     }
 
     @Override public long distance(Integer startInteger end) {
       return (longend - start;
     }
 
     @Override public Integer minValue() {
       return .;
     }
 
     @Override public Integer maxValue() {
       return .;
     }
 
     private Object readResolve() {
       return ;
     }
 
     @Override
     public String toString() {
       return "DiscreteDomain.integers()";
     }
 
     private static final long serialVersionUID = 0;
   }

  
Returns the discrete domain for values of type Long.

Since:
14.0 (since 10.0 as DiscreteDomains.longs())
 
  public static DiscreteDomain<Longlongs() {
    return .;
  }
  private static final class LongDomain extends DiscreteDomain<Long>
      implements Serializable {
    private static final LongDomain INSTANCE = new LongDomain();
    @Override public Long next(Long value) {
      long l = value;
      return (l == .) ? null : l + 1;
    }
    @Override public Long previous(Long value) {
      long l = value;
      return (l == .) ? null : l - 1;
    }
    @Override public long distance(Long startLong end) {
      long result = end - start;
      if (end > start && result < 0) { // overflow
        return .;
      }
      if (end < start && result > 0) { // underflow
        return .;
      }
      return result;
    }
    @Override public Long minValue() {
      return .;
    }
    @Override public Long maxValue() {
      return .;
    }
    private Object readResolve() {
      return ;
    }
    @Override
    public String toString() {
      return "DiscreteDomain.longs()";
    }
    private static final long serialVersionUID = 0;
  }
  
  
Returns the discrete domain for values of type BigInteger.

Since:
15.0
  public static DiscreteDomain<BigIntegerbigIntegers() {
    return .;
  }
  private static final class BigIntegerDomain extends DiscreteDomain<BigInteger>
      implements Serializable {
    private static final BigIntegerDomain INSTANCE = new BigIntegerDomain();
    private static final BigInteger MIN_LONG =
        BigInteger.valueOf(.);
    private static final BigInteger MAX_LONG =
        BigInteger.valueOf(.);
    @Override public BigInteger next(BigInteger value) {
      return value.add(.);
    }
    @Override public BigInteger previous(BigInteger value) {
      return value.subtract(.);
    }
    @Override public long distance(BigInteger startBigInteger end) {
      return end.subtract(start).max().min().longValue();
    }
    private Object readResolve() {
      return ;
    }
    @Override
    public String toString() {
      return "DiscreteDomain.bigIntegers()";
    }
    private static final long serialVersionUID = 0;
  }
  
  
Constructor for use by subclasses.
  protected DiscreteDomain() {}

  
Returns the unique least value of type C that is greater than value, or null if none exists. Inverse operation to previous(java.lang.Comparable).

Parameters:
value any value of type C
Returns:
the least value greater than value, or null if value is maxValue()
  public abstract C next(C value);

  
Returns the unique greatest value of type C that is less than value, or null if none exists. Inverse operation to next(java.lang.Comparable).

Parameters:
value any value of type C
Returns:
the greatest value less than value, or null if value is minValue()
  public abstract C previous(C value);

  
Returns a signed value indicating how many nested invocations of next(java.lang.Comparable) (if positive) or previous(java.lang.Comparable) (if negative) are needed to reach end starting from start. For example, if end = next(next(next(start))), then distance(start, end) == 3 and distance(end, start) == -3. As well, distance(a, a) is always zero.

Note that this function is necessarily well-defined for any discrete type.

Returns:
the distance as described above, or java.lang.Long.MIN_VALUE or java.lang.Long.MAX_VALUE if the distance is too small or too large, respectively.
  public abstract long distance(C start, C end);

  
Returns the minimum value of type C, if it has one. The minimum value is the unique value for which java.lang.Comparable.compareTo(java.lang.Object) never returns a positive value for any input of type C.

The default implementation throws NoSuchElementException.

Returns:
the minimum value of type C; never null
Throws:
java.util.NoSuchElementException if the type has no (practical) minimum value; for example, java.math.BigInteger
  public C minValue() {
    throw new NoSuchElementException();
  }

  
Returns the maximum value of type C, if it has one. The maximum value is the unique value for which java.lang.Comparable.compareTo(java.lang.Object) never returns a negative value for any input of type C.

The default implementation throws NoSuchElementException.

Returns:
the maximum value of type C; never null
Throws:
java.util.NoSuchElementException if the type has no (practical) maximum value; for example, java.math.BigInteger
  public C maxValue() {
    throw new NoSuchElementException();
  }
  
New to GrepCode? Check out our FAQ X