public void testSaturatedCast() {
for (int value : VALUES) {
assertEquals(value, Ints.saturatedCast((long) value));
}
assertEquals(GREATEST, Ints.saturatedCast(GREATEST + 1L));
assertEquals(LEAST, Ints.saturatedCast(LEAST - 1L));
assertEquals(GREATEST, Ints.saturatedCast(Long.MAX_VALUE));
assertEquals(LEAST, Ints.saturatedCast(Long.MIN_VALUE));
}
public void numeric() {
/**
* Sometimes we need to find the biggest and the smallest value of given array. With min() and max() methods of Ints we can achieve it simply.
*
* Another numeric wrappers (Floats, Doubles, Longs, Shorts) and Chars also provide min and max methods.
*/
int[] testedInt = new int[]{1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000};
int min = Ints.min(testedInt);
int max = Ints.max(testedInt);
assertTrue("Expected min value is 1930 but "+min+" was returned", min == 1930);
assertTrue("Expected max value is 2000 but "+max+" was returned", max == 2000);
/**
* We can also work with saturated casts which returns the nearest number to the long specified in the method. We test with max Long value which is bigger than the Integer one
* (
*/
int intCasted = Ints.saturatedCast(Long.MAX_VALUE);
assertTrue("intCates should be "+Integer.MAX_VALUE+" but is "+intCasted, intCasted == Integer.MAX_VALUE);
/**
* They're also checked casts which allows to cast one value to another one. Some exceptions can be thrown here, for example when casted value is bigger than the
* value that can be accepted by to cast class.
*
* checkedCast(long value) exists also for Shorts wrapper.
*/
try {
Ints.checkedCast(Long.MAX_VALUE);
} catch (Exception e) {
assertTrue("Unexpected exceptions was thrown (IllegalArgumentException expected, "+e.getClass()+ " got)", e.getClass() == IllegalArgumentException.class && e.getMessage().contains("Out of range"));
}
}
public static void main(String[] args) {
long lVal = 3_147_483_648L;
int iVal = Ints.saturatedCast(lVal);
int result = Ints.saturatedCast(lVal);
Assert.assertEquals(Integer.MAX_VALUE, result);
}
public static void main(String[] args) {
// max value of long is too large for int
// thus it'll cast to the nearest int
// which's the max int value
int saturatedCast = Ints.saturatedCast(Long.MAX_VALUE);
System.out.println(saturatedCast);
//2147483647
System.out.println(saturatedCast == Integer.MAX_VALUE); // true
}
public static void main(String[] args) {
// 0 is available in int range [-2147483648, 2147483647]
// so the nearest value to cast
// is itself in int
saturatedCast = Ints.saturatedCast(0L);
System.out.println(saturatedCast);
System.out.println(saturatedCast == 0); // true
}
public ApacheThriftMethodInvoker(
ListeningExecutorService executorService,
ListeningScheduledExecutorService delayService,
TTransportFactory transportFactory,
TProtocolFactory protocolFactory,
Duration connectTimeout,
Duration requestTimeout,
Optional<HostAndPort> socksProxy,
Optional<SSLContext> sslContext)
{
this.executorService = requireNonNull(executorService, "executorService is null");
this.delayService = requireNonNull(delayService, "delayService is null");
this.transportFactory = requireNonNull(transportFactory, "transportFactory is null");
this.protocolFactory = requireNonNull(protocolFactory, "protocolFactory is null");
this.connectTimeoutMillis = Ints.saturatedCast(requireNonNull(connectTimeout, "connectTimeout is null").toMillis());
this.requestTimeoutMillis = Ints.saturatedCast(requireNonNull(requestTimeout, "requestTimeout is null").toMillis());
this.socksProxy = requireNonNull(socksProxy, "socksProxy is null");
this.sslContext = requireNonNull(sslContext, "sslContext is null");
}
public int size() {
// racy single-check idiom
Integer result = size;
if (result == null) {
long total = 0;
for (Range<C> range : ranges) {
total += ContiguousSet.create(range, domain).size();
if (total >= Integer.MAX_VALUE) {
break;
}
}
result = size = Ints.saturatedCast(total);
}
return result.intValue();
}
public Set<EndpointPair<N>> edges() {
return new AbstractSet<EndpointPair<N>>() {
@Override
public UnmodifiableIterator<EndpointPair<N>> iterator() {
return EndpointPairIterator.of(AbstractBaseGraph.this);
}
@Override
public int size() {
return Ints.saturatedCast(edgeCount());
}
@Override
public boolean contains(@Nullable Object obj) {
if (!(obj instanceof EndpointPair)) {
return false;
}
EndpointPair<?> endpointPair = (EndpointPair<?>) obj;
return isDirected() == endpointPair.isOrdered()
&& nodes().contains(endpointPair.nodeU())
&& successors(endpointPair.nodeU()).contains(endpointPair.nodeV());
}
};
}
public int size() {
// racy single-check idiom
Integer result = size;
if (result == null) {
long total = 0;
for (Range<C> range : ranges) {
total += ContiguousSet.create(range, domain).size();
if (total >= Integer.MAX_VALUE) {
break;
}
}
result = size = Ints.saturatedCast(total);
}
return result.intValue();
}
int indexOf(Object target) {
if (contains(target)) {
@SuppressWarnings("unchecked") // if it's contained, it's definitely a C
C c = (C) target;
long total = 0;
for (Range<C> range : ranges) {
if (range.contains(c)) {
return Ints.saturatedCast(total + ContiguousSet.create(range, domain).indexOf(c));
} else {
total += ContiguousSet.create(range, domain).size();
}
}
throw new AssertionError("impossible");
}
return -1;
}