public static void setAll(int[] array, IntUnaryOperator generator) { for (int i = 0; i < array.length; ++i) { array[i] = generator.applyAsInt(i); } }
public final int updateAndGet(IntUnaryOperator updateFunction) { int expectedVersion; int newValue; do { expectedVersion = version; newValue = updateFunction.applyAsInt(value); } while (expectedVersion != version); ++version; value = newValue; return newValue; }
public final int getAndUpdate(IntUnaryOperator updateFunction) { int expectedVersion; int result; int newValue; do { expectedVersion = version; result = value; newValue = updateFunction.applyAsInt(value); } while (expectedVersion != version); ++version; value = newValue; return result; }
import java.util.function.*; import static java.lang.System.out; // Tested with JDK 1.8.0-ea-b75 public class CurryingAndPartialFunctionApplication { public static void main(String[] args) { IntBinaryOperator simpleAdd = (a, b) -> a + b; IntFunction<IntUnaryOperator> curriedAdd = a -> b -> a + b; // Demonstrating simple add: out.println(simpleAdd.applyAsInt(4, 5)); // Demonstrating curried add: out.println(curriedAdd.apply(4).applyAsInt(5)); // Curried version lets you perform partial application: IntUnaryOperator adder5 = curriedAdd.apply(5); out.println(adder5.applyAsInt(4)); out.println(adder5.applyAsInt(6)); } }
@Override public ToIntNullable<T> mapIfPresent(IntUnaryOperator mapper) { return t -> isNull.test(t) ? null : mapper.applyAsInt(original.applyAsInt(t)); }
@Override public int applyAsInt(T object) { return this.mapper.applyAsInt(this.inner.applyAsInt(object)); }
@Override public int applyAsInt(T object) throws NullPointerException { return mapper.applyAsInt(delegate.applyAsInt(object)); }
@Override public boolean next(IntPredicate consumer) { while (true) { int valueToReport = value; value = f.applyAsInt(value); if (!consumer.test(valueToReport)) { return true; } } } }
static <T> int binarySearch(IndexedSeq<T> seq, IntUnaryOperator comparison) { int low = 0; int high = seq.size() - 1; while (low <= high) { final int mid = (low + high) >>> 1; final int cmp = comparison.applyAsInt(mid); if (cmp < 0) { low = mid + 1; } else if (cmp > 0) { high = mid - 1; } else { return mid; } } return -(low + 1); } }
@Override protected IntPredicate wrap(IntPredicate consumer) { return t -> consumer.test(mapper.applyAsInt(t)); }
@Override public Integer apply(T object) { return delegate.isNull(object) ? null : mapper.applyAsInt(delegate.applyAsInt(object)); }
@Override public ToInt<T> orElse(Integer value) { return object -> delegate.isNull(object) ? value : mapper.applyAsInt(delegate.applyAsInt(object)); }
/** * Add an invocation to this tracker. * * @param producer the invocation producer, which may be called more than once * @param <T> the invocation type * @return the produced invocation */ public <T extends Invocation> T addInvocation(IntFunction<T> producer) { final ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current(); final IntUnaryOperator intMasker = this.intMasker; final IntIndexMap<Invocation> invocations = this.invocations; int id; T invocation; for (;;) { id = intMasker.applyAsInt(threadLocalRandom.nextInt()); if (invocations.containsKey(id)) { continue; } invocation = producer.apply(id); if (invocations.putIfAbsent(invocation) != null) { continue; } return invocation; } }
debugStream.close(); System.exit(cleanup.applyAsInt(exceptions));
/** * Atomically update the current value using the given {@link IntUnaryOperator update function}. * * @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects), * because it will be applied several times if update attempts fail due to concurrent calls. Must not be * {@literal null}. * @return the previous value. * @since 2.2 */ public int getAndUpdate(IntUnaryOperator updateFunction) { Assert.notNull(updateFunction, "Update function must not be null!"); int previousValue, newValue; do { previousValue = get(); newValue = updateFunction.applyAsInt(previousValue); } while (!compareAndSet(previousValue, newValue)); return previousValue; }
/** * Atomically update the current value using the given {@link IntUnaryOperator update function}. * * @param updateFunction the function which calculates the value to set. Should be a pure function (no side effects), * because it will be applied several times if update attempts fail due to concurrent calls. Must not be * {@literal null}. * @return the updated value. * @since 2.2 */ public int updateAndGet(IntUnaryOperator updateFunction) { Assert.notNull(updateFunction, "Update function must not be null!"); int previousValue, newValue; do { previousValue = get(); newValue = updateFunction.applyAsInt(previousValue); } while (!compareAndSet(previousValue, newValue)); return newValue; }
/** * Try get a value for a key and if not present then apply mapping function. * * @param key to search on. * @param mappingFunction to provide a value if the get returns null. * @return the value if found otherwise the missing value. */ public int computeIfAbsent(final int key, final IntUnaryOperator mappingFunction) { int value = get(key); if (value == initialValue) { value = mappingFunction.applyAsInt(key); if (value != initialValue) { put(key, value); } } return value; }
/** * Primitive specialised version of {@link #computeIfAbsent(Object, Function)} * * @param key to search on. * @param mappingFunction to provide a value if the get returns null. * @return the value if found otherwise the missing value. */ public int computeIfAbsent(final int key, final IntUnaryOperator mappingFunction) { int value = get(key); if (value == missingValue) { value = mappingFunction.applyAsInt(key); if (value != missingValue) { put(key, value); } } return value; }
@Override public ToInt<T> orElseGet(ToInt<T> getter) { return object -> delegate.isNull(object) ? getter.applyAsInt(object) : mapper.applyAsInt(delegate.applyAsInt(object)); }
public BasicBlockMapper(IntUnaryOperator mapFunction) { this((BasicBlock block) -> block.getProgram().basicBlockAt(mapFunction.applyAsInt(block.getIndex()))); }