Java Code Examples for com.google.common.primitives.Ints.saturatedCast()

The following are Jave code examples for showing how to use saturatedCast() of the com.google.common.primitives.Ints class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: drift   File: ApacheThriftMethodInvoker.java   View Source Code Vote up 6 votes
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");
}
 
Example 2
Project: googles-monorepo-demo   File: ImmutableRangeSet.java   View Source Code Vote up 6 votes
@Override
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();
}
 
Example 3
Project: googles-monorepo-demo   File: AbstractBaseGraph.java   View Source Code Vote up 6 votes
/**
 * An implementation of {@link BaseGraph#edges()} defined in terms of {@link #nodes()} and {@link
 * #successors(Object)}.
 */
@Override
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());
    }
  };
}
 
Example 4
Project: guava-mock   File: ImmutableRangeSet.java   View Source Code Vote up 6 votes
@Override
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();
}
 
Example 5
Project: guava-mock   File: ImmutableRangeSet.java   View Source Code Vote up 6 votes
@Override
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;
}
 
Example 6
Project: googles-monorepo-demo   File: MapMakerInternalMap.java   View Source Code Vote up 5 votes
@Override
public int size() {
  Segment<K, V, E, S>[] segments = this.segments;
  long sum = 0;
  for (int i = 0; i < segments.length; ++i) {
    sum += segments[i].count;
  }
  return Ints.saturatedCast(sum);
}
 
Example 7
Project: googles-monorepo-demo   File: Iterators.java   View Source Code Vote up 5 votes
/**
 * Returns the number of elements remaining in {@code iterator}. The iterator
 * will be left exhausted: its {@code hasNext()} method will return
 * {@code false}.
 */
public static int size(Iterator<?> iterator) {
  long count = 0L;
  while (iterator.hasNext()) {
    iterator.next();
    count++;
  }
  return Ints.saturatedCast(count);
}
 
Example 8
Project: guava-mock   File: MapMakerInternalMap.java   View Source Code Vote up 5 votes
@Override
public int size() {
  Segment<K, V, E, S>[] segments = this.segments;
  long sum = 0;
  for (int i = 0; i < segments.length; ++i) {
    sum += segments[i].count;
  }
  return Ints.saturatedCast(sum);
}
 
Example 9
Project: guava-mock   File: ConcurrentHashMultiset.java   View Source Code Vote up 5 votes
/**
 * {@inheritDoc}
 *
 * <p>If the data in the multiset is modified by any other threads during this method,
 * it is undefined which (if any) of these modifications will be reflected in the result.
 */
@Override
public int size() {
  long sum = 0L;
  for (AtomicInteger value : countMap.values()) {
    sum += value.get();
  }
  return Ints.saturatedCast(sum);
}
 
Example 10
Project: guava-mock   File: AbstractBaseGraph.java   View Source Code Vote up 5 votes
/**
 * An implementation of {@link BaseGraph#edges()} defined in terms of {@link #nodes()} and {@link
 * #successors(Object)}.
 */
@Override
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());
    }

    // Mostly safe: We check contains(u) before calling successors(u), so we perform unsafe
    // operations only in weird cases like checking for an EndpointPair<ArrayList> in a
    // Graph<LinkedList>.
    @SuppressWarnings("unchecked")
    @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((N) endpointPair.nodeU()).contains(endpointPair.nodeV());
    }
  };
}
 
Example 11
Project: guava-mock   File: ConcurrentHashMultisetBenchmark.java   View Source Code Vote up 5 votes
/**
 * {@inheritDoc}
 *
 * <p>If the data in the multiset is modified by any other threads during this
 * method, it is undefined which (if any) of these modifications will be
 * reflected in the result.
 */
@Override public int size() {
  long sum = 0L;
  for (Integer value : countMap.values()) {
    sum += value;
  }
  return Ints.saturatedCast(sum);
}
 
Example 12
Project: googles-monorepo-demo   File: TreeMultiset.java   View Source Code Vote up 4 votes
@Override
int distinctElements() {
  return Ints.saturatedCast(aggregateForEntries(Aggregate.DISTINCT));
}
 
Example 13
Project: sstable-adaptor   File: MemoryInputStream.java   View Source Code Vote up 4 votes
public MemoryInputStream(Memory mem)
{
    this(mem, Ints.saturatedCast(mem.size));
}
 
Example 14
Project: sstable-adaptor   File: MemoryInputStream.java   View Source Code Vote up 4 votes
@Override
public int available()
{
    return Ints.saturatedCast(buffer.remaining() + memRemaining());
}
 
Example 15
Project: guava-mock   File: AbstractMapBasedMultiset.java   View Source Code Vote up 4 votes
@Override
public int size() {
  return Ints.saturatedCast(size);
}
 
Example 16
Project: googles-monorepo-demo   File: AbstractMapBasedMultiset.java   View Source Code Vote up 4 votes
@Override
public int size() {
  return Ints.saturatedCast(size);
}
 
Example 17
Project: guava-mock   File: IntMath.java   View Source Code Vote up 2 votes
/**
 * Returns the sum of {@code a} and {@code b} unless it would overflow or underflow in which case
 * {@code Integer.MAX_VALUE} or {@code Integer.MIN_VALUE} is returned, respectively.
 *
 * @since 20.0
 */
@Beta
public static int saturatedAdd(int a, int b) {
  return Ints.saturatedCast((long) a + b);
}
 
Example 18
Project: guava-mock   File: IntMath.java   View Source Code Vote up 2 votes
/**
 * Returns the product of {@code a} and {@code b} unless it would overflow or underflow in which
 * case {@code Integer.MAX_VALUE} or {@code Integer.MIN_VALUE} is returned, respectively.
 *
 * @since 20.0
 */
@Beta
public static int saturatedMultiply(int a, int b) {
  return Ints.saturatedCast((long) a * b);
}
 
Example 19
Project: googles-monorepo-demo   File: IntMath.java   View Source Code Vote up 2 votes
/**
 * Returns the sum of {@code a} and {@code b} unless it would overflow or underflow in which case
 * {@code Integer.MAX_VALUE} or {@code Integer.MIN_VALUE} is returned, respectively.
 *
 * @since 20.0
 */
@Beta
public static int saturatedAdd(int a, int b) {
  return Ints.saturatedCast((long) a + b);
}
 
Example 20
Project: googles-monorepo-demo   File: IntMath.java   View Source Code Vote up 2 votes
/**
 * Returns the product of {@code a} and {@code b} unless it would overflow or underflow in which
 * case {@code Integer.MAX_VALUE} or {@code Integer.MIN_VALUE} is returned, respectively.
 *
 * @since 20.0
 */
@Beta
public static int saturatedMultiply(int a, int b) {
  return Ints.saturatedCast((long) a * b);
}