@Override public final LongStream map(LongUnaryOperator mapper) { Objects.requireNonNull(mapper); return new StatelessOp<Long>(this, StreamShape.LONG_VALUE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { @Override Sink<Long> opWrapSink(int flags, Sink<Long> sink) { return new Sink.ChainedLong<Long>(sink) { @Override public void accept(long t) { downstream.accept(mapper.applyAsLong(t)); } }; } }; }
/** * Returns an infinite sequential ordered {@code LongStream} produced by iterative * application of a function {@code f} to an initial element {@code seed}, * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, * {@code f(f(seed))}, etc. * * <p>The first element (position {@code 0}) in the {@code LongStream} will * be the provided {@code seed}. For {@code n > 0}, the element at position * {@code n}, will be the result of applying the function {@code f} to the * element at position {@code n - 1}. * * @param seed the initial element * @param f a function to be applied to to the previous element to produce * a new element * @return a new sequential {@code LongStream} */ public static LongStream iterate(final long seed, final LongUnaryOperator f) { Objects.requireNonNull(f); final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() { long t = seed; @Override public boolean hasNext() { return true; } @Override public long nextLong() { long v = t; t = f.applyAsLong(t); return v; } }; return StreamSupport.longStream(Spliterators.spliteratorUnknownSize( iterator, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false); }
/** * Returns an infinite sequential ordered {@code LongStream} produced by iterative * application of a function {@code f} to an initial element {@code seed}, * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, * {@code f(f(seed))}, etc. * * <p>The first element (position {@code 0}) in the {@code LongStream} will * be the provided {@code seed}. For {@code n > 0}, the element at position * {@code n}, will be the result of applying the function {@code f} to the * element at position {@code n - 1}. * * <p>The action of applying {@code f} for one element * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a> * the action of applying {@code f} for subsequent elements. For any given * element the action may be performed in whatever thread the library * chooses. * * @param seed the initial element * @param f a function to be applied to the previous element to produce * a new element * @return a new sequential {@code LongStream} */ public static LongStream iterate(final long seed, final LongUnaryOperator f) { Objects.requireNonNull(f); Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) { long prev; boolean started; @Override public boolean tryAdvance(LongConsumer action) { Objects.requireNonNull(action); long t; if (started) t = f.applyAsLong(prev); else { t = seed; started = true; } action.accept(prev = t); return true; } }; return StreamSupport.longStream(spliterator, false); }
/** * Returns an infinite sequential ordered {@code LongStream} produced by iterative * application of a function {@code f} to an initial element {@code seed}, * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, * {@code f(f(seed))}, etc. * * <p>The first element (position {@code 0}) in the {@code LongStream} will * be the provided {@code seed}. For {@code n > 0}, the element at position * {@code n}, will be the result of applying the function {@code f} to the * element at position {@code n - 1}. * * @param seed the initial element * @param f a function to be applied to the previous element to produce * a new element * @return a new sequential {@code LongStream} */ public static LongStream iterate(final long seed, final LongUnaryOperator f) { Objects.requireNonNull(f); Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) { long prev; boolean started; @Override public boolean tryAdvance(LongConsumer action) { Objects.requireNonNull(action); long t; if (started) t = f.applyAsLong(prev); else { t = seed; started = true; } action.accept(prev = t); return true; } }; return StreamSupport.longStream(spliterator, false); }
/** * Implements {@link Timers#schedule(Runnable, TimerScheduler)} */ @Override public void schedule(@Pin Consumer<? super Cancel> task, LongUnaryOperator scheduler, Result<? super Cancel> result) { Objects.requireNonNull(task); Objects.requireNonNull(scheduler); // cancel(task); long now = CurrentTime.currentTime(); long nextTime = scheduler.applyAsLong(now); TimerListener listener = new TimerListener(task, scheduler); result.ok(listener); if (now <= nextTime) { listener.queueAt(nextTime); } /* return listener; */ }
@Test public void testBinomialCurvePoints() { LongUnaryOperator idc = IntegerDistributions.forSpec("mapto_binomial(8,0.5)"); long half = Long.MAX_VALUE / 2; long expected = idc.applyAsLong(half); assertThat(expected).isEqualTo(4); expected = idc.applyAsLong(1); assertThat(expected).isEqualTo(0); // threshold test against CDF expected = idc.applyAsLong((long) (0.03515d * (double) Long.MAX_VALUE)); assertThat(expected).isEqualTo(1); expected = idc.applyAsLong((long) (0.03600d * (double) Long.MAX_VALUE)); assertThat(expected).isEqualTo(2); }
@Override public long[] call() throws Exception { long[] output = new long[size]; LongUnaryOperator mapper = IntegerDistributions.forSpec(mapperSpec); // System.out.println("resolved:" + mapper); // System.out.flush(); synchronized (signal) { signal.wait(10000); } for (int i = 0; i < output.length; i++) { output[i] = mapper.applyAsLong(i); // if ((i % 100) == 0) { // System.out.println("wrote t:" + slot + ", iter:" + i + ", val:" + output[i]); // } } return output; }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullCache() { // given final ConcurrentMap<Long, Long> cache = null; final LongUnaryOperator operator = input -> input; final LongFunction<Long> keyFunction = Long::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage("Provide an empty map instead of NULL."); // then new ConcurrentMapBasedLongUnaryOperatorMemoizer<>(cache, keyFunction, operator); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNUSED) public void shouldRequireNonNullOperator() { // given final ConcurrentMap<Long, Long> cache = new ConcurrentHashMap<>(); final LongUnaryOperator operator = null; final LongFunction<Long> keyFunction = Long::valueOf; // when thrown.expect(NullPointerException.class); thrown.expectMessage( "Cannot memoize a NULL LongUnaryOperator - provide an actual LongUnaryOperator to fix this."); // then new ConcurrentMapBasedLongUnaryOperatorMemoizer<>(cache, keyFunction, operator); }
/** * */ @Test public void shouldUseSetCacheKeyAndValue() { // given final ConcurrentMap<Long, Long> cache = new ConcurrentHashMap<>(); final LongUnaryOperator operator = input -> input; final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongUnaryOperatorMemoizer<Long> memoizer = new ConcurrentMapBasedLongUnaryOperatorMemoizer<>( cache, keyFunction, operator); // then memoizer.applyAsLong(123L); Assert.assertFalse("Cache is still empty after memoization", memoizer.viewCacheForTest().isEmpty()); Assert.assertEquals("Memoization key does not match expectations", 123L, memoizer.viewCacheForTest().keySet().iterator().next().longValue()); Assert.assertEquals("Memoization value does not match expectations", 123L, memoizer.viewCacheForTest().values().iterator().next().longValue()); }
/** * */ @Test public void shouldUseCallWrappedOperator() { // given final ConcurrentMap<Long, Long> cache = new ConcurrentHashMap<>(); final LongUnaryOperator operator = Mockito.mock(LongUnaryOperator.class); final LongFunction<Long> keyFunction = Long::valueOf; // when final ConcurrentMapBasedLongUnaryOperatorMemoizer<Long> memoizer = new ConcurrentMapBasedLongUnaryOperatorMemoizer<>( cache, keyFunction, operator); // then memoizer.applyAsLong(123L); Mockito.verify(operator).applyAsLong(123L); }
@Test public void testLongUnaryOp() { LongUnaryOperator longOperand = (l) -> { return l + 1; }; long expected = 33L; assertEquals(expected, longOperand.applyAsLong(32L)); }
/** * Updates the value currently associated with {@code key} with the specified function, * and returns the old value. If there is not currently a value associated with {@code key}, * the function is applied to {@code 0L}. * * @since 21.0 */ @CanIgnoreReturnValue public long getAndUpdate(K key, LongUnaryOperator updaterFunction) { checkNotNull(updaterFunction); AtomicLong holder = new AtomicLong(); map.compute( key, (k, value) -> { long oldValue = (value == null) ? 0L : value.longValue(); holder.set(oldValue); return updaterFunction.applyAsLong(oldValue); }); return holder.get(); }
/** * Constructs a bot thread. * * @param bot the bot used by the reader * @param backOff back off to be used when long polling fails */ public BotThread(Bot bot, LongUnaryOperator backOff) { super(); this.bot = bot; updateReader = new UpdateReader(bot, backOff); updateHandlers = new ArrayList<>(); errorHandlers = new ArrayList<>(); executor = Runnable::run; }
/** * Constructs an UpdateReader. * * @param connection the connection that receive updates * @param backOff back off to be used when long polling fails */ public UpdateReader(TelegramConnection connection, LongUnaryOperator backOff) { this.connection = Objects.requireNonNull(connection); this.backOff = Objects.requireNonNull(backOff); updates = new ConcurrentLinkedQueue<>(); lastUpdateId = -1; allowedUpdates = new String[0]; }
@NonNull @Override public MuVector2l map(@NonNull final LongUnaryOperator operator) { this.x = operator.applyAsLong(this.x); this.y = operator.applyAsLong(this.y); return this; }
@NonNull @Override public MuVector4l map(@NonNull final LongUnaryOperator operator) { this.x = operator.applyAsLong(this.x); this.y = operator.applyAsLong(this.y); this.z = operator.applyAsLong(this.z); this.w = operator.applyAsLong(this.w); return this; }
@NonNull @Override public MuVector3l map(@NonNull final LongUnaryOperator operator) { this.x = operator.applyAsLong(this.x); this.y = operator.applyAsLong(this.y); this.z = operator.applyAsLong(this.z); return this; }
public final long getAndUpdate(LongUnaryOperator updateFunction) { long prev, next; do { prev = value; next = updateFunction.applyAsLong(prev); } while (!compareAndSet(prev, next)); return prev; }
public final long updateAndGet(LongUnaryOperator updateFunction) { long prev, next; do { prev = value; next = updateFunction.applyAsLong(prev); } while (!compareAndSet(prev, next)); return next; }
public final long getAndUpdate(int i, LongUnaryOperator updateFunction) { long offset = checkedByteOffset(i); long prev, next; do { prev = getRaw(offset); next = updateFunction.applyAsLong(prev); } while (!compareAndSetRaw(offset, prev, next)); return prev; }
public final long updateAndGet(int i, LongUnaryOperator updateFunction) { long offset = checkedByteOffset(i); long prev, next; do { prev = getRaw(offset); next = updateFunction.applyAsLong(prev); } while (!compareAndSetRaw(offset, prev, next)); return next; }
private void set(LongUnaryOperator operator, Runnable changed, Runnable notChanged) { synchronized (bits) { long oldState = bits.getAndUpdate(operator); if (bits.get() > 0 && oldState == 0 || bits.get() == 0 && oldState > 0) { if (changed != null) { changed.run(); } } else if (notChanged != null) { notChanged.run(); } } }
TimerListener(Consumer<? super Cancel> task, LongUnaryOperator nextTime) { _task = task; _nextTime = nextTime; // _info = new TaskInfo(_task, CurrentTime.getCurrentTime()); _alarm = new Alarm(this); }
@Override public void handleAlarm(Alarm alarm) { if (isClosed()) { return; } try { _task.accept(this); } catch (Throwable e) { log.log(Level.FINER, e.toString(), e); // _info.lastException(e); throw e; } finally { // _info.lastCompletedTime(CurrentTime.getCurrentTime()); LongUnaryOperator nextTimeOp = _nextTime; boolean isScheduled = false; if (nextTimeOp != null && _alarm != null) { long now = CurrentTime.currentTime(); long nextTime = nextTimeOp.applyAsLong(now); if (now < nextTime) { _alarm.queueAt(nextTime); isScheduled = true; // _info.nextRunTime(nextTime); } } if (! isScheduled) { close(); } } }
private void inc(KEY key, long increment) { Objects.requireNonNull(key); values.get(key).getAndUpdate(new LongUnaryOperator() { @Override public long applyAsLong(long currentValue) { return currentValue + increment + (currentValue == -1 ? 1 : 0); } }); }
public FunctionAssembler andThen(Object functionObject) { if (functionObject instanceof LongUnaryOperator) { return andThen((LongUnaryOperator) functionObject); } if (functionObject instanceof LongFunction) { return andThen((LongFunction) functionObject); } if (functionObject instanceof Function) { return andThen((Function) functionObject); } throw new RuntimeException("Function object was not a type recognized by " + FunctionAssembler.class.getSimpleName() + ", object:" + functionObject); }
/** * Creates a new instance given user-defined sample size and seed methods. */ public SampleOperator(IntUnaryOperator sampleSizeFunction, DataSetType<Type> type, Methods sampleMethod, LongUnaryOperator seedFunction) { super(type, type, true); this.sampleSizeFunction = sampleSizeFunction; this.sampleMethod = sampleMethod; this.seedFunction = seedFunction; }
/** * */ @Test public void shouldMemoizeLongUnaryOperatorWithKeyFunction() { // given final LongUnaryOperator function = a -> 123L; final LongFunction<String> keyFunction = a -> "key"; // when final LongUnaryOperator memoize = CaffeineMemoize.longUnaryOperator(function, keyFunction); // then Assert.assertNotNull("Memoized LongUnaryOperator is NULL", memoize); }