本文整理汇总了Java中java.util.function.UnaryOperator.apply方法的典型用法代码示例。如果您正苦于以下问题:Java UnaryOperator.apply方法的具体用法?Java UnaryOperator.apply怎么用?Java UnaryOperator.apply使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.function.UnaryOperator
的用法示例。
在下文中一共展示了UnaryOperator.apply方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: iterate
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
public static <T> Iterable<T> iterate(T from, T to, UnaryOperator<T> inc) {
return () -> new Iterator<T>() {
T next = from;
boolean finish = false;
@Override
public boolean hasNext() {
return !finish;
}
@Override
public T next() {
if(finish) throw new IndexOutOfBoundsException("Iteration overlaps the value of to: " + to);
if(next.equals(to))
finish = true; // The to bound is inclusive. Finishes on next step.
T curr = next;
next = inc.apply(curr);
return curr;
}
};
}
示例2: replaceAll
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
public void replaceAll(UnaryOperator<E> operator) {
if (operator == null) throw new NullPointerException();
final ReentrantLock lock = l.lock;
lock.lock();
try {
int lo = offset;
int hi = offset + size;
Object[] elements = expectedArray;
if (l.getArray() != elements)
throw new ConcurrentModificationException();
int len = elements.length;
if (lo < 0 || hi > len)
throw new IndexOutOfBoundsException();
Object[] newElements = Arrays.copyOf(elements, len);
for (int i = lo; i < hi; ++i) {
@SuppressWarnings("unchecked") E e = (E) elements[i];
newElements[i] = operator.apply(e);
}
l.setArray(expectedArray = newElements);
} finally {
lock.unlock();
}
}
示例3: replaceAll
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
void replaceAll(UnaryOperator<E> operator, int i, int end) {
// assert Thread.holdsLock(lock);
final Object[] es = getArray().clone();
for (; i < end; i++)
es[i] = operator.apply(elementAt(es, i));
setArray(es);
}
示例4: build
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
@Override
void build(Path inputFile, Path out) throws Throwable {
try {
R8Command.Builder builder = R8Command.builder();
for (UnaryOperator<R8Command.Builder> transformation : builderTransformations) {
builder = transformation.apply(builder);
}
R8Command command = builder.addProgramFiles(inputFile).setOutputPath(out).build();
ToolHelper.runR8(command, this::combinedOptionConsumer);
} catch (ExecutionException e) {
throw e.getCause();
}
}
示例5: replaceAll
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
@Override
public void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
E[] a = this.a;
for (int i = 0; i < a.length; i++) {
a[i] = operator.apply(a[i]);
}
}
示例6: MetaDataUpgrader
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
public MetaDataUpgrader(Collection<UnaryOperator<Map<String, MetaData.Custom>>> customMetaDataUpgraders) {
this.customMetaDataUpgraders = customs -> {
Map<String, MetaData.Custom> upgradedCustoms = new HashMap<>(customs);
for (UnaryOperator<Map<String, MetaData.Custom>> customMetaDataUpgrader : customMetaDataUpgraders) {
upgradedCustoms = customMetaDataUpgrader.apply(upgradedCustoms);
}
return upgradedCustoms;
};
}
示例7: testMixedTryAdvanceForEach
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
private static <T, S extends Spliterator<T>> void testMixedTryAdvanceForEach(
Collection<T> exp,
Supplier<S> supplier,
UnaryOperator<Consumer<T>> boxingAdapter) {
S spliterator = supplier.get();
long sizeIfKnown = spliterator.getExactSizeIfKnown();
boolean isOrdered = spliterator.hasCharacteristics(Spliterator.ORDERED);
// tryAdvance first few elements, then forEach rest
ArrayList<T> dest = new ArrayList<>();
spliterator = supplier.get();
Consumer<T> addToDest = boxingAdapter.apply(dest::add);
for (int i = 0; i < 10 && spliterator.tryAdvance(addToDest); i++) { }
spliterator.forEachRemaining(addToDest);
// Assert that forEach now produces no elements
spliterator.forEachRemaining(boxingAdapter.apply(e -> fail("Spliterator.forEach produced an element after spliterator exhausted: " + e)));
// Assert that tryAdvance now produce no elements
spliterator.tryAdvance(boxingAdapter.apply(e -> fail("Spliterator.tryAdvance produced an element after spliterator exhausted: " + e)));
if (sizeIfKnown >= 0) {
assertEquals(sizeIfKnown, dest.size());
}
assertEquals(dest.size(), exp.size());
if (isOrdered) {
assertEquals(dest, exp);
}
else {
assertContentsUnordered(dest, exp);
}
}
示例8: replaceAll
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public synchronized void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
final int expectedModCount = modCount;
final int size = elementCount;
for (int i=0; modCount == expectedModCount && i < size; i++) {
elementData[i] = operator.apply((E) elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
示例9: replaceAll
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
final int expectedModCount = modCount;
final int size = this.size;
for (int i=0; modCount == expectedModCount && i < size; i++) {
elementData[i] = operator.apply((E) elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
modCount++;
}
示例10: render
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
@Override
public String render(Text model,
UnaryOperator<String> argRes,
RenderService renderer) {
return "<p>" + argRes.apply(model.getValue()) + "</p>";
}
示例11: applySnapshot
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
private void applySnapshot(@Nonnull final ShardDataTreeSnapshot snapshot,
final UnaryOperator<DataTreeModification> wrapper) throws DataValidationFailedException {
final Stopwatch elapsed = Stopwatch.createStarted();
if (anyPendingTransactions()) {
LOG.warn("{}: applying state snapshot with pending transactions", logContext);
}
final Map<Class<? extends ShardDataTreeSnapshotMetadata<?>>, ShardDataTreeSnapshotMetadata<?>> snapshotMeta;
if (snapshot instanceof MetadataShardDataTreeSnapshot) {
snapshotMeta = ((MetadataShardDataTreeSnapshot) snapshot).getMetadata();
} else {
snapshotMeta = ImmutableMap.of();
}
for (ShardDataTreeMetadata<?> m : metadata) {
final ShardDataTreeSnapshotMetadata<?> s = snapshotMeta.get(m.getSupportedType());
if (s != null) {
m.applySnapshot(s);
} else {
m.reset();
}
}
final DataTreeModification mod = wrapper.apply(dataTree.takeSnapshot().newModification());
// delete everything first
mod.delete(YangInstanceIdentifier.EMPTY);
final java.util.Optional<NormalizedNode<?, ?>> maybeNode = snapshot.getRootNode();
if (maybeNode.isPresent()) {
// Add everything from the remote node back
mod.write(YangInstanceIdentifier.EMPTY, maybeNode.get());
}
mod.ready();
final DataTreeModification unwrapped = unwrap(mod);
dataTree.validate(unwrapped);
DataTreeCandidateTip candidate = dataTree.prepare(unwrapped);
dataTree.commit(candidate);
notifyListeners(candidate);
LOG.debug("{}: state snapshot applied in {}", logContext, elapsed);
}
示例12: updateAndGet
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
/**
* Atomically updates the element at index {@code i} 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 i the index
* @param updateFunction a side-effect-free function
* @return the updated value
* @since 1.8
*/
public final E updateAndGet(int i, UnaryOperator<E> updateFunction) {
long offset = checkedByteOffset(i);
E prev, next;
do {
prev = getRaw(offset);
next = updateFunction.apply(prev);
} while (!compareAndSetRaw(offset, prev, next));
return next;
}
示例13: updateAndGet
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
/**
* Atomically updates (with memory effects as specified by {@link
* VarHandle#compareAndSet}) the field of the given object managed
* by this updater 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 obj An object whose field to get and set
* @param updateFunction a side-effect-free function
* @return the updated value
* @since 1.8
*/
public final V updateAndGet(T obj, UnaryOperator<V> updateFunction) {
V prev, next;
do {
prev = get(obj);
next = updateFunction.apply(prev);
} while (!compareAndSet(obj, prev, next));
return next;
}
示例14: ofDateAdjuster
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
/**
* Obtains a {@code TemporalAdjuster} that wraps a date adjuster.
* <p>
* The {@code TemporalAdjuster} is based on the low level {@code Temporal} interface.
* This method allows an adjustment from {@code LocalDate} to {@code LocalDate}
* to be wrapped to match the temporal-based interface.
* This is provided for convenience to make user-written adjusters simpler.
* <p>
* In general, user-written adjusters should be static constants:
* <pre>{@code
* static TemporalAdjuster TWO_DAYS_LATER =
* TemporalAdjusters.ofDateAdjuster(date -> date.plusDays(2));
* }</pre>
*
* @param dateBasedAdjuster the date-based adjuster, not null
* @return the temporal adjuster wrapping on the date adjuster, not null
*/
public static TemporalAdjuster ofDateAdjuster(UnaryOperator<LocalDate> dateBasedAdjuster) {
Objects.requireNonNull(dateBasedAdjuster, "dateBasedAdjuster");
return (temporal) -> {
LocalDate input = LocalDate.from(temporal);
LocalDate output = dateBasedAdjuster.apply(input);
return temporal.with(output);
};
}
示例15: getAndUpdate
import java.util.function.UnaryOperator; //导入方法依赖的package包/类
/**
* Atomically updates the field of the given object managed by this updater
* 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 obj An object whose field to get and set
* @param updateFunction a side-effect-free function
* @return the previous value
* @since 1.8
*/
public final V getAndUpdate(T obj, UnaryOperator<V> updateFunction) {
V prev, next;
do {
prev = get(obj);
next = updateFunction.apply(prev);
} while (!compareAndSet(obj, prev, next));
return prev;
}