![JAR search and dependency download from the Maven repository](/logo.png)
javadoc.src-html.com.google.common.collect.DiscreteDomains.html Maven / Gradle / Ivy
The newest version!
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 * @author Gregory Kick
029 * @since 10.0
030 */
031 @GwtCompatible
032 @Beta
033 public final class DiscreteDomains {
034 private DiscreteDomains() {}
035
036 /**
037 * Returns the discrete domain for values of type {@code Integer}.
038 */
039 public static DiscreteDomain<Integer> integers() {
040 return IntegerDomain.INSTANCE;
041 }
042
043 private static final class IntegerDomain extends DiscreteDomain<Integer>
044 implements Serializable {
045 private static final IntegerDomain INSTANCE = new IntegerDomain();
046
047 @Override public Integer next(Integer value) {
048 int i = value;
049 return (i == Integer.MAX_VALUE) ? null : i + 1;
050 }
051
052 @Override public Integer previous(Integer value) {
053 int i = value;
054 return (i == Integer.MIN_VALUE) ? null : i - 1;
055 }
056
057 @Override public long distance(Integer start, Integer end) {
058 return (long) end - start;
059 }
060
061 @Override public Integer minValue() {
062 return Integer.MIN_VALUE;
063 }
064
065 @Override public Integer maxValue() {
066 return Integer.MAX_VALUE;
067 }
068
069 private Object readResolve() {
070 return INSTANCE;
071 }
072
073 private static final long serialVersionUID = 0;
074 }
075
076 /**
077 * Returns the discrete domain for values of type {@code Long}.
078 */
079 public static DiscreteDomain<Long> longs() {
080 return LongDomain.INSTANCE;
081 }
082
083 private static final class LongDomain extends DiscreteDomain<Long>
084 implements Serializable {
085 private static final LongDomain INSTANCE = new LongDomain();
086
087 @Override public Long next(Long value) {
088 long l = value;
089 return (l == Long.MAX_VALUE) ? null : l + 1;
090 }
091
092 @Override public Long previous(Long value) {
093 long l = value;
094 return (l == Long.MIN_VALUE) ? null : l - 1;
095 }
096
097 @Override public long distance(Long start, Long end) {
098 long result = end - start;
099 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 }
© 2015 - 2025 Weber Informatics LLC | Privacy Policy