Java Code Examples for com.google.common.math.IntMath#saturatedAdd()
The following examples show how to use
com.google.common.math.IntMath#saturatedAdd() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpHeaderNames.java From armeria with Apache License 2.0 | 6 votes |
private static String malformedHeaderNameMessage(AsciiString name) { final StringBuilder buf = new StringBuilder(IntMath.saturatedAdd(name.length(), 64)); buf.append("malformed header name: "); final int nameLength = name.length(); for (int i = 0; i < nameLength; i++) { final char ch = name.charAt(i); if (PROHIBITED_NAME_CHARS.get(ch)) { buf.append(PROHIBITED_NAME_CHAR_NAMES[ch]); } else { buf.append(ch); } } return buf.toString(); }
Example 2
Source File: HttpHeadersBase.java From armeria with Apache License 2.0 | 6 votes |
private static String malformedHeaderValueMessage(String value) { final StringBuilder buf = new StringBuilder(IntMath.saturatedAdd(value.length(), 64)); buf.append("malformed header value: "); final int valueLength = value.length(); for (int i = 0; i < valueLength; i++) { final char ch = value.charAt(i); if (PROHIBITED_VALUE_CHARS.get(ch)) { buf.append(PROHIBITED_VALUE_CHAR_NAMES[ch]); } else { buf.append(ch); } } return buf.toString(); }
Example 3
Source File: ItemContainer.java From luna with MIT License | 5 votes |
/** * Determines if there is enough space for {@code items} to be added. * * @param items The items. * @return {@code true} if there's enough space for {@code items}. */ public final boolean hasSpaceForAll(Item... items) { int count = 0; for (Item item : items) { count = IntMath.saturatedAdd(count, computeSpaceFor(item)); if (count > computeRemainingSize()) { // Can't fit, no point in checking other items. return false; } } return true; }
Example 4
Source File: ItemContainer.java From luna with MIT License | 5 votes |
/** * Computes the amount of space required to hold {@code items}. * * @param items The items. * @return The amount of space required. */ public final int computeSpaceForAll(Iterable<? extends Item> items) { int count = 0; for (Item item : items) { count = IntMath.saturatedAdd(count, computeSpaceFor(item)); } return count; }
Example 5
Source File: LengthLimitingContentPreviewer.java From armeria with Apache License 2.0 | 5 votes |
@Override public void onData(HttpData data) { requireNonNull(data, "data"); if (data.isEmpty()) { return; } final int length = Math.min(inflatedMaxLength - aggregatedLength, data.length()); bufferList.add(duplicateData(data, length)); aggregatedLength = IntMath.saturatedAdd(aggregatedLength, length); if (aggregatedLength >= inflatedMaxLength || data.isEndOfStream()) { produce(); } }
Example 6
Source File: Lists.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
@Override public int size() { return IntMath.saturatedAdd(rest.length, 1); }
Example 7
Source File: GuavaMathUnitTest.java From tutorials with MIT License | 4 votes |
@Test public void whenSumOverflow_thenReturnMaxInteger() { int result = IntMath.saturatedAdd(Integer.MAX_VALUE, 100); assertThat(result, equalTo(Integer.MAX_VALUE)); }
Example 8
Source File: Lists.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
@Override public int size() { return IntMath.saturatedAdd(rest.length, 2); }
Example 9
Source File: Lists.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
@Override public int size() { return IntMath.saturatedAdd(rest.length, 1); }
Example 10
Source File: Sets.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
/** * Returns an unmodifiable <b>view</b> of the union of two sets. The returned * set contains all elements that are contained in either backing set. * Iterating over the returned set iterates first over all the elements of * {@code set1}, then over each element of {@code set2}, in order, that is not * contained in {@code set1}. * * <p>Results are undefined if {@code set1} and {@code set2} are sets based on * different equivalence relations (as {@link HashSet}, {@link TreeSet}, and * the {@link Map#keySet} of an {@code IdentityHashMap} all are). */ public static <E> SetView<E> union(final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); final Set<? extends E> set2minus1 = difference(set2, set1); return new SetView<E>() { @Override public int size() { return IntMath.saturatedAdd(set1.size(), set2minus1.size()); } @Override public boolean isEmpty() { return set1.isEmpty() && set2.isEmpty(); } @Override public Iterator<E> iterator() { return Iterators.unmodifiableIterator( Iterators.concat(set1.iterator(), set2minus1.iterator())); } @Override public boolean contains(Object object) { return set1.contains(object) || set2.contains(object); } @Override public <S extends Set<E>> S copyInto(S set) { set.addAll(set1); set.addAll(set2); return set; } @Override public ImmutableSet<E> immutableCopy() { return new ImmutableSet.Builder<E>().addAll(set1).addAll(set2).build(); } }; }
Example 11
Source File: Lists.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
@Override public int size() { return IntMath.saturatedAdd(rest.length, 2); }
Example 12
Source File: GuavaIntMathUnitTest.java From tutorials with MIT License | 4 votes |
@Test public void whenSaturatedAddTwoIntegerValues_shouldAddThemAndReturnIntMinIfUnderflow() { int result = IntMath.saturatedAdd(Integer.MIN_VALUE, -1000); assertEquals(Integer.MIN_VALUE, result); }
Example 13
Source File: Sets.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
/** * Returns an unmodifiable <b>view</b> of the union of two sets. The returned * set contains all elements that are contained in either backing set. * Iterating over the returned set iterates first over all the elements of * {@code set1}, then over each element of {@code set2}, in order, that is not * contained in {@code set1}. * * <p>Results are undefined if {@code set1} and {@code set2} are sets based on * different equivalence relations (as {@link HashSet}, {@link TreeSet}, and * the {@link Map#keySet} of an {@code IdentityHashMap} all are). */ public static <E> SetView<E> union( final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); final Set<? extends E> set2minus1 = difference(set2, set1); return new SetView<E>() { @Override public int size() { return IntMath.saturatedAdd(set1.size(), set2minus1.size()); } @Override public boolean isEmpty() { return set1.isEmpty() && set2.isEmpty(); } @Override public Iterator<E> iterator() { return Iterators.unmodifiableIterator(Iterators.concat(set1.iterator(), set2minus1.iterator())); } @Override public boolean contains(Object object) { return set1.contains(object) || set2.contains(object); } @Override public <S extends Set<E>> S copyInto(S set) { set.addAll(set1); set.addAll(set2); return set; } @Override public ImmutableSet<E> immutableCopy() { return new ImmutableSet.Builder<E>().addAll(set1).addAll(set2).build(); } }; }
Example 14
Source File: Lists.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
@Override public int size() { return IntMath.saturatedAdd(rest.length, 2); }
Example 15
Source File: Lists.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
@Override public int size() { return IntMath.saturatedAdd(rest.length, 1); }
Example 16
Source File: Sets.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
/** * Returns an unmodifiable <b>view</b> of the union of two sets. The returned * set contains all elements that are contained in either backing set. * Iterating over the returned set iterates first over all the elements of * {@code set1}, then over each element of {@code set2}, in order, that is not * contained in {@code set1}. * * <p>Results are undefined if {@code set1} and {@code set2} are sets based on * different equivalence relations (as {@link HashSet}, {@link TreeSet}, and * the {@link Map#keySet} of an {@code IdentityHashMap} all are). */ public static <E> SetView<E> union( final Set<? extends E> set1, final Set<? extends E> set2) { checkNotNull(set1, "set1"); checkNotNull(set2, "set2"); final Set<? extends E> set2minus1 = difference(set2, set1); return new SetView<E>() { @Override public int size() { return IntMath.saturatedAdd(set1.size(), set2minus1.size()); } @Override public boolean isEmpty() { return set1.isEmpty() && set2.isEmpty(); } @Override public Iterator<E> iterator() { return Iterators.unmodifiableIterator(Iterators.concat(set1.iterator(), set2minus1.iterator())); } @Override public boolean contains(Object object) { return set1.contains(object) || set2.contains(object); } @Override public <S extends Set<E>> S copyInto(S set) { set.addAll(set1); set.addAll(set2); return set; } @Override public ImmutableSet<E> immutableCopy() { return new ImmutableSet.Builder<E>().addAll(set1).addAll(set2).build(); } }; }
Example 17
Source File: GuavaIntMathUnitTest.java From tutorials with MIT License | 4 votes |
@Test public void whenSaturatedAddTwoIntegerValues_shouldAddThemAndReturnTheResult() { int result = IntMath.saturatedAdd(6, 4); assertEquals(10, result); }
Example 18
Source File: GuavaIntMathUnitTest.java From tutorials with MIT License | 4 votes |
@Test public void whenSaturatedAddTwoIntegerValues_shouldAddThemAndReturnIntMaxIfOverflow() { int result = IntMath.saturatedAdd(Integer.MAX_VALUE, 1000); assertEquals(Integer.MAX_VALUE, result); }
Example 19
Source File: GuavaMathUnitTest.java From tutorials with MIT License | 4 votes |
@Test public void whenSumUnderflow_thenReturnMinInteger() { int result = IntMath.saturatedAdd(Integer.MIN_VALUE, -100); assertThat(result, equalTo(Integer.MIN_VALUE)); }
Example 20
Source File: Lists.java From codebuff with BSD 2-Clause "Simplified" License | 4 votes |
@Override public int size() { return IntMath.saturatedAdd(rest.length, 1); }