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
052 @Override
053 public Integer next(Integer value) {
054 int i = value;
055 return (i == Integer.MAX_VALUE) ? null : i + 1;
056 }
057
058
059 @Override
060 public Integer previous(Integer value) {
061 int i = value;
062 return (i == Integer.MIN_VALUE) ? null : i - 1;
063 }
064
065
066 @Override
067 public long distance(Integer start, Integer end) {
068 return (long) end - start;
069 }
070
071
072 @Override
073 public Integer minValue() {
074 return Integer.MIN_VALUE;
075 }
076
077
078 @Override
079 public Integer maxValue() {
080 return Integer.MAX_VALUE;
081 }
082
083 private Object readResolve() {
084 return INSTANCE;
085 }
086
087 private static final long serialVersionUID = 0;
088 }
089
090 /**
091 * Returns the discrete domain for values of type {@code Long}.
092 */
093 public static DiscreteDomain<Long> longs() {
094 return LongDomain.INSTANCE;
095 }
096
097 private static final class LongDomain extends DiscreteDomain<Long>
098 implements Serializable {
099 private static final LongDomain INSTANCE = new LongDomain();
100
101
102 @Override
103 public Long next(Long value) {
104 long l = value;
105 return (l == Long.MAX_VALUE) ? null : l + 1;
106 }
107
108
109 @Override
110 public Long previous(Long value) {
111 long l = value;
112 return (l == Long.MIN_VALUE) ? null : l - 1;
113 }
114
115
116 @Override
117 public long distance(Long start, Long end) {
118 long result = end - start;
119 if (end > start && result < 0) { // overflow
120 return Long.MAX_VALUE;
121 }
122 if (end < start && result > 0) { // underflow
123 return Long.MIN_VALUE;
124 }
125 return result;
126 }
127
128
129 @Override
130 public Long minValue() {
131 return Long.MIN_VALUE;
132 }
133
134
135 @Override
136 public Long maxValue() {
137 return Long.MAX_VALUE;
138 }
139
140 private Object readResolve() {
141 return INSTANCE;
142 }
143
144 private static final long serialVersionUID = 0;
145 }
146
147 /**
148 * Returns the discrete domain for values of type {@code BigInteger}.
149 */
150 // TODO(kevinb): make sure it's tested, and make it public
151 static DiscreteDomain<BigInteger> bigIntegers() {
152 return BigIntegerDomain.INSTANCE;
153 }
154
155 private static final class BigIntegerDomain extends DiscreteDomain<BigInteger>
156 implements Serializable {
157 private static final BigIntegerDomain INSTANCE = new BigIntegerDomain();
158
159 private static final BigInteger MIN_LONG =
160 BigInteger.valueOf(Long.MIN_VALUE);
161 private static final BigInteger MAX_LONG =
162 BigInteger.valueOf(Long.MAX_VALUE);
163
164
165 @Override
166 public BigInteger next(BigInteger value) {
167 return value.add(BigInteger.ONE);
168 }
169
170
171 @Override
172 public BigInteger previous(BigInteger value) {
173 return value.subtract(BigInteger.ONE);
174 }
175
176
177 @Override
178 public long distance(BigInteger start, BigInteger end) {
179 return start.subtract(end).max(MIN_LONG).min(MAX_LONG).longValue();
180 }
181
182 private Object readResolve() {
183 return INSTANCE;
184 }
185
186 private static final long serialVersionUID = 0;
187 }
188 }