Java Code Examples for java.util.function.LongUnaryOperator

The following examples show how to use java.util.function.LongUnaryOperator. These examples are extracted from open source projects. 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 Project: openjdk-jdk9   Author: AdoptOpenJDK   File: LongPipeline.java    License: GNU General Public License v2.0 6 votes vote down vote up
@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));
                }
            };
        }
    };
}
 
Example #2
Source Project: dragonwell8_jdk   Author: alibaba   File: LongPipeline.java    License: GNU General Public License v2.0 6 votes vote down vote up
@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));
                }
            };
        }
    };
}
 
Example #3
Source Project: dragonwell8_jdk   Author: alibaba   File: LongStream.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    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);
}
 
Example #4
Source Project: Bytecoder   Author: mirkosertic   File: LongPipeline.java    License: Apache License 2.0 6 votes vote down vote up
@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));
                }
            };
        }
    };
}
 
Example #5
Source Project: TencentKona-8   Author: Tencent   File: LongPipeline.java    License: GNU General Public License v2.0 6 votes vote down vote up
@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));
                }
            };
        }
    };
}
 
Example #6
Source Project: jdk8u60   Author: chenghanpeng   File: LongPipeline.java    License: GNU General Public License v2.0 6 votes vote down vote up
@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));
                }
            };
        }
    };
}
 
Example #7
Source Project: jdk8u60   Author: chenghanpeng   File: LongStream.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #8
Source Project: JDKSourceCode1.8   Author: wupeixuan   File: LongStream.java    License: MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example #9
Source Project: desugar_jdk_libs   Author: google   File: LongStream.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #10
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: LongStream.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
    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);
}
 
Example #11
Source Project: Bytecoder   Author: mirkosertic   File: LongStream.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #12
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: LongPipeline.java    License: GNU General Public License v2.0 6 votes vote down vote up
@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));
                }
            };
        }
    };
}
 
Example #13
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: LongStream.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #14
Source Project: besu   Author: hyperledger   File: RLPDecodingHelpers.java    License: Apache License 2.0 5 votes vote down vote up
/** Read from the provided offset a size of the provided length, assuming this is enough bytes. */
private static int extractSizeFromLongItem(
    final LongUnaryOperator getter, final long offset, final int sizeLength) {
  if (sizeLength > 4) {
    throw new RLPException(
        "RLP item at offset "
            + offset
            + " with size value consuming "
            + sizeLength
            + " bytes exceeds max supported size of "
            + Integer.MAX_VALUE);
  }

  long res = 0;
  int shift = 0;
  for (int i = 0; i < sizeLength; i++) {
    res |= (getter.applyAsLong(offset + (sizeLength - 1) - i) & 0xFF) << shift;
    shift += 8;
  }
  try {
    return Math.toIntExact(res);
  } catch (final ArithmeticException e) {
    throw new RLPException(
        "RLP item at offset "
            + offset
            + " with size value consuming "
            + sizeLength
            + " bytes exceeds max supported size of "
            + Integer.MAX_VALUE,
        e);
  }
}
 
Example #15
Source Project: besu   Author: hyperledger   File: RLPDecodingHelpers.java    License: Apache License 2.0 5 votes vote down vote up
static RLPElementMetadata rlpElementMetadata(
    final LongUnaryOperator byteGetter, final long size, final long elementStart) {
  final int prefix = Math.toIntExact(byteGetter.applyAsLong(elementStart)) & 0xFF;
  final Kind kind = Kind.of(prefix);
  long payloadStart = 0;
  int payloadSize = 0;

  switch (kind) {
    case BYTE_ELEMENT:
      payloadStart = elementStart;
      payloadSize = 1;
      break;
    case SHORT_ELEMENT:
      payloadStart = elementStart + 1;
      payloadSize = prefix - 0x80;
      break;
    case LONG_ELEMENT:
      final int sizeLengthElt = prefix - 0xb7;
      payloadStart = elementStart + 1 + sizeLengthElt;
      payloadSize = readLongSize(byteGetter, size, elementStart, sizeLengthElt);
      break;
    case SHORT_LIST:
      payloadStart = elementStart + 1;
      payloadSize = prefix - 0xc0;
      break;
    case LONG_LIST:
      final int sizeLengthList = prefix - 0xf7;
      payloadStart = elementStart + 1 + sizeLengthList;
      payloadSize = readLongSize(byteGetter, size, elementStart, sizeLengthList);
      break;
  }

  return new RLPElementMetadata(kind, elementStart, payloadStart, payloadSize);
}
 
Example #16
Source Project: besu   Author: hyperledger   File: RLPDecodingHelpers.java    License: Apache License 2.0 5 votes vote down vote up
/** The size of the item payload for a "long" item, given the length in bytes of the said size. */
private static int readLongSize(
    final LongUnaryOperator byteGetter,
    final long sizeOfRlpEncodedByteString,
    final long item,
    final int sizeLength) {
  // We will read sizeLength bytes from item + 1. There must be enough bytes for this or the input
  // is corrupted.
  if (sizeOfRlpEncodedByteString - (item + 1) < sizeLength) {
    throw new CorruptedRLPInputException(
        String.format(
            "Invalid RLP item: value of size %d has not enough bytes to read the %d "
                + "bytes payload size",
            sizeOfRlpEncodedByteString, sizeLength));
  }

  // That size (which is at least 1 byte by construction) shouldn't have leading zeros.
  if (byteGetter.applyAsLong(item + 1) == 0) {
    throw new MalformedRLPInputException("Malformed RLP item: size of payload has leading zeros");
  }

  final int res = RLPDecodingHelpers.extractSizeFromLongItem(byteGetter, item + 1, sizeLength);

  // We should not have had the size written separately if it was less than 56 bytes long.
  if (res < 56) {
    throw new MalformedRLPInputException(
        String.format("Malformed RLP item: written as a long item, but size %d < 56 bytes", res));
  }

  return res;
}
 
Example #17
Source Project: presto   Author: prestosql   File: PartitionTransforms.java    License: Apache License 2.0 5 votes vote down vote up
private static Block extractDate(Block block, LongUnaryOperator function)
{
    BlockBuilder builder = INTEGER.createFixedSizeBlockBuilder(block.getPositionCount());
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (block.isNull(position)) {
            builder.appendNull();
            continue;
        }
        long value = DATE.getLong(block, position);
        value = function.applyAsLong(value);
        INTEGER.writeLong(builder, value);
    }
    return builder.build();
}
 
Example #18
Source Project: presto   Author: prestosql   File: PartitionTransforms.java    License: Apache License 2.0 5 votes vote down vote up
private static Block extractTimestampWithTimeZone(Block block, LongUnaryOperator function)
{
    BlockBuilder builder = INTEGER.createFixedSizeBlockBuilder(block.getPositionCount());
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (block.isNull(position)) {
            builder.appendNull();
            continue;
        }
        long value = unpackMillisUtc(TIMESTAMP_WITH_TIME_ZONE.getLong(block, position));
        value = function.applyAsLong(value);
        INTEGER.writeLong(builder, value);
    }
    return builder.build();
}
 
Example #19
Source Project: rheem   Author: rheem-ecosystem   File: SampleOperator.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #20
Source Project: oxygen   Author: justlive1   File: WheelTimer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * cron任务
 *
 * @param command 任务
 * @param cron 表达式
 * @return ScheduledFuture
 */
public ScheduledFuture<Void> scheduleOnCron(Runnable command, String cron) {
  start();
  LongUnaryOperator operator = new CronExpression(cron).operator();
  long deadline = operator.applyAsLong(0);
  if (deadline == Long.MIN_VALUE) {
    throw Exceptions.fail("cron doesn't have any match in the future");
  }
  PeriodTask task = new PeriodTask(deadline, command, operator, this);
  addTaskInLock(task);
  return task;
}
 
Example #21
Source Project: incubator-ratis   Author: apache   File: TestRaftLogIndex.java    License: Apache License 2.0 5 votes vote down vote up
static void assertUpdate(RaftLogIndex index, BiFunction<RaftLogIndex, LongUnaryOperator, Boolean> update,
    long oldValue, LongUnaryOperator op, boolean expectUpdate) {
  Assert.assertEquals(oldValue, index.get());
  final boolean updated = update.apply(index, op);
  Assert.assertEquals(expectUpdate, updated);
  Assert.assertEquals(expectUpdate? op.applyAsLong(oldValue): oldValue, index.get());
}
 
Example #22
Source Project: rheem   Author: rheem-ecosystem   File: FlinkSampleOperator.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new instance.
 */
public FlinkSampleOperator(IntUnaryOperator sampleSizeFunction, DataSetType<Type> type, LongUnaryOperator seedFunction) {
    super(sampleSizeFunction, type, Methods.RANDOM, seedFunction);
}
 
Example #23
Source Project: rheem   Author: rheem-ecosystem   File: SparkRandomPartitionSampleOperator.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new instance.
 */
public SparkRandomPartitionSampleOperator(IntUnaryOperator sampleSizeFunction, DataSetType<Type> type, LongUnaryOperator seedFunction) {
    super(sampleSizeFunction, type, Methods.RANDOM, seedFunction);
}
 
Example #24
Source Project: rheem   Author: rheem-ecosystem   File: JavaReservoirSampleOperator.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new instance.
 */
public JavaReservoirSampleOperator(IntUnaryOperator sampleSizeFunction, DataSetType<Type> type, LongUnaryOperator seed) {
    super(sampleSizeFunction, type, Methods.RESERVOIR, seed);
}
 
Example #25
Source Project: oxygen   Author: justlive1   File: CronExpression.java    License: Apache License 2.0 4 votes vote down vote up
public LongUnaryOperator operator() {
  return new CronOperator(this);
}
 
Example #26
Source Project: oxygen   Author: justlive1   File: PeriodTask.java    License: Apache License 2.0 4 votes vote down vote up
PeriodTask(long deadline, Runnable runnable, LongUnaryOperator operator, WheelTimer timer) {
  super(deadline, runnable);
  this.operator = operator;
  this.timer = timer;
}
 
Example #27
Source Project: catnip   Author: mewna   File: RoleData.java    License: BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Nonnull
public RoleData updatePermissions(@Nonnull final LongUnaryOperator updater) {
    return permissions(updater.applyAsLong(permissions == null ? 0 : permissions));
}
 
Example #28
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: DefaultMethodStreams.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public LongStream map(LongUnaryOperator mapper) {
    return s.map(mapper);
}
 
Example #29
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: AtomicLong.java    License: GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the current value with the results of
 * applying the given function, returning the previous value. The
 * function should be side-effect-free, since it may be re-applied
 * when attempted updates fail due to contention among threads.
 *
 * @param updateFunction a side-effect-free function
 * @return the previous value
 * @since 1.8
 */
public final long getAndUpdate(LongUnaryOperator updateFunction) {
    long prev, next;
    do {
        prev = get();
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSet(prev, next));
    return prev;
}
 
Example #30
Source Project: jdk8u-jdk   Author: lambdalab-mirror   File: AtomicLong.java    License: GNU General Public License v2.0 3 votes vote down vote up
/**
 * Atomically updates the current value with the results of
 * applying the given function, returning the updated value. The
 * function should be side-effect-free, since it may be re-applied
 * when attempted updates fail due to contention among threads.
 *
 * @param updateFunction a side-effect-free function
 * @return the updated value
 * @since 1.8
 */
public final long updateAndGet(LongUnaryOperator updateFunction) {
    long prev, next;
    do {
        prev = get();
        next = updateFunction.applyAsLong(prev);
    } while (!compareAndSet(prev, next));
    return next;
}