1/*
2 * Copyright (C) 2010 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.collect;
18
19import com.google.common.annotations.Beta;
20import com.google.common.annotations.GwtCompatible;
21
22import java.io.Serializable;
23import java.math.BigInteger;
24
25/**
26 * Factories for common {@link DiscreteDomain} instances.
27 *
28 * @author Gregory Kick
29 * @since 10.0
30 */
31@GwtCompatible
32@Beta
33public final class DiscreteDomains {
34  private DiscreteDomains() {}
35
36  /**
37   * Returns the discrete domain for values of type {@code Integer}.
38   */
39  public static DiscreteDomain<Integer> integers() {
40    return IntegerDomain.INSTANCE;
41  }
42
43  private static final class IntegerDomain extends DiscreteDomain<Integer>
44      implements Serializable {
45    private static final IntegerDomain INSTANCE = new IntegerDomain();
46
47    @Override public Integer next(Integer value) {
48      int i = value;
49      return (i == Integer.MAX_VALUE) ? null : i + 1;
50    }
51
52    @Override public Integer previous(Integer value) {
53      int i = value;
54      return (i == Integer.MIN_VALUE) ? null : i - 1;
55    }
56
57    @Override public long distance(Integer start, Integer end) {
58      return (long) end - start;
59    }
60
61    @Override public Integer minValue() {
62      return Integer.MIN_VALUE;
63    }
64
65    @Override public Integer maxValue() {
66      return Integer.MAX_VALUE;
67    }
68
69    private Object readResolve() {
70      return INSTANCE;
71    }
72
73    private static final long serialVersionUID = 0;
74  }
75
76  /**
77   * Returns the discrete domain for values of type {@code Long}.
78   */
79  public static DiscreteDomain<Long> longs() {
80    return LongDomain.INSTANCE;
81  }
82
83  private static final class LongDomain extends DiscreteDomain<Long>
84      implements Serializable {
85    private static final LongDomain INSTANCE = new LongDomain();
86
87    @Override public Long next(Long value) {
88      long l = value;
89      return (l == Long.MAX_VALUE) ? null : l + 1;
90    }
91
92    @Override public Long previous(Long value) {
93      long l = value;
94      return (l == Long.MIN_VALUE) ? null : l - 1;
95    }
96
97    @Override public long distance(Long start, Long end) {
98      long result = end - start;
99      if (end > start && result < 0) { // overflow
100        return Long.MAX_VALUE;
101      }
102      if (end < start && result > 0) { // underflow
103        return Long.MIN_VALUE;
104      }
105      return result;
106    }
107
108    @Override public Long minValue() {
109      return Long.MIN_VALUE;
110    }
111
112    @Override public Long maxValue() {
113      return Long.MAX_VALUE;
114    }
115
116    private Object readResolve() {
117      return INSTANCE;
118    }
119
120    private static final long serialVersionUID = 0;
121  }
122
123  /**
124   * Returns the discrete domain for values of type {@code BigInteger}.
125   */
126  // TODO(kevinb): make sure it's tested, and make it public
127  static DiscreteDomain<BigInteger> bigIntegers() {
128    return BigIntegerDomain.INSTANCE;
129  }
130
131  private static final class BigIntegerDomain extends DiscreteDomain<BigInteger>
132      implements Serializable {
133    private static final BigIntegerDomain INSTANCE = new BigIntegerDomain();
134
135    private static final BigInteger MIN_LONG =
136        BigInteger.valueOf(Long.MIN_VALUE);
137    private static final BigInteger MAX_LONG =
138        BigInteger.valueOf(Long.MAX_VALUE);
139
140    @Override public BigInteger next(BigInteger value) {
141      return value.add(BigInteger.ONE);
142    }
143
144    @Override public BigInteger previous(BigInteger value) {
145      return value.subtract(BigInteger.ONE);
146    }
147
148    @Override public long distance(BigInteger start, BigInteger end) {
149      return start.subtract(end).max(MIN_LONG).min(MAX_LONG).longValue();
150    }
151
152    private Object readResolve() {
153      return INSTANCE;
154    }
155
156    private static final long serialVersionUID = 0;
157  }
158}
159