    001    /*
    002     * Copyright (C) 2010 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
    017    package com.google.common.collect;
    018
    019    import com.google.common.annotations.Beta;
    020    import com.google.common.annotations.GwtCompatible;
    021
    022    import java.io.Serializable;
    023    import java.math.BigInteger;
    024
    025    /**
    026     * Factories for common {@link DiscreteDomain} instances.
    027     *
    028     * <p>See the Guava User Guide section on <a href=
    029     * "http://code.google.com/p/guava-libraries/wiki/RangesExplained#Discrete_Domains">
    030     * {@code DiscreteDomain}</a>.
    031     *
    032     * @author Gregory Kick
    033     * @since 10.0
    034     */
    035    @GwtCompatible
    036    @Beta
    037    public final class DiscreteDomains {
    038      private DiscreteDomains() {}
    039
    040      /**
    041       * Returns the discrete domain for values of type {@code Integer}.
    042       */
    043      public static DiscreteDomain<Integer> integers() {
    044        return IntegerDomain.INSTANCE;
    045      }
    046
    047      private static final class IntegerDomain extends DiscreteDomain<Integer>
    048          implements Serializable {
    049        private static final IntegerDomain INSTANCE = new IntegerDomain();
    050
    051        @Override public Integer next(Integer value) {
    052          int i = value;
    053          return (i == Integer.MAX_VALUE) ? null : i + 1;
    054        }
    055
    056        @Override public Integer previous(Integer value) {
    057          int i = value;
    058          return (i == Integer.MIN_VALUE) ? null : i - 1;
    059        }
    060
    061        @Override public long distance(Integer start, Integer end) {
    062          return (long) end - start;
    063        }
    064
    065        @Override public Integer minValue() {
    066          return Integer.MIN_VALUE;
    067        }
    068
    069        @Override public Integer maxValue() {
    070          return Integer.MAX_VALUE;
    071        }
    072
    073        private Object readResolve() {
    074          return INSTANCE;
    075        }
    076
    077        private static final long serialVersionUID = 0;
    078      }
    079
    080      /**
    081       * Returns the discrete domain for values of type {@code Long}.
    082       */
    083      public static DiscreteDomain<Long> longs() {
    084        return LongDomain.INSTANCE;
    085      }
    086
    087      private static final class LongDomain extends DiscreteDomain<Long>
    088          implements Serializable {
    089        private static final LongDomain INSTANCE = new LongDomain();
    090
    091        @Override public Long next(Long value) {
    092          long l = value;
    093          return (l == Long.MAX_VALUE) ? null : l + 1;
    094        }
    095
    096        @Override public Long previous(Long value) {
    097          long l = value;
    098          return (l == Long.MIN_VALUE) ? null : l - 1;
    099        }
    100
    101        @Override public long distance(Long start, Long end) {
    102          long result = end - start;
    103          if (end > start && result < 0) { // overflow
    104            return Long.MAX_VALUE;
    105          }
    106          if (end < start && result > 0) { // underflow
    107            return Long.MIN_VALUE;
    108          }
    109          return result;
    110        }
    111
    112        @Override public Long minValue() {
    113          return Long.MIN_VALUE;
    114        }
    115
    116        @Override public Long maxValue() {
    117          return Long.MAX_VALUE;
    118        }
    119
    120        private Object readResolve() {
    121          return INSTANCE;
    122        }
    123
    124        private static final long serialVersionUID = 0;
    125      }
    126
    127      /**
    128       * Returns the discrete domain for values of type {@code BigInteger}.
    129       */
    130      // TODO(kevinb): make sure it's tested, and make it public
    131      static DiscreteDomain<BigInteger> bigIntegers() {
    132        return BigIntegerDomain.INSTANCE;
    133      }
    134
    135      private static final class BigIntegerDomain extends DiscreteDomain<BigInteger>
    136          implements Serializable {
    137        private static final BigIntegerDomain INSTANCE = new BigIntegerDomain();
    138
    139        private static final BigInteger MIN_LONG =
    140            BigInteger.valueOf(Long.MIN_VALUE);
    141        private static final BigInteger MAX_LONG =
    142            BigInteger.valueOf(Long.MAX_VALUE);
    143
    144        @Override public BigInteger next(BigInteger value) {
    145          return value.add(BigInteger.ONE);
    146        }
    147
    148        @Override public BigInteger previous(BigInteger value) {
    149          return value.subtract(BigInteger.ONE);
    150        }
    151
    152        @Override public long distance(BigInteger start, BigInteger end) {
    153          return start.subtract(end).max(MIN_LONG).min(MAX_LONG).longValue();
    154        }
    155
    156        private Object readResolve() {
    157          return INSTANCE;
    158        }
    159
    160        private static final long serialVersionUID = 0;
    161      }
    162    }