本文整理汇总了Java中java9.util.Objects.requireNonNull方法的典型用法代码示例。如果您正苦于以下问题:Java Objects.requireNonNull方法的具体用法?Java Objects.requireNonNull怎么用?Java Objects.requireNonNull使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java9.util.Objects
的用法示例。
在下文中一共展示了Objects.requireNonNull方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mapToDouble
import java9.util.Objects; //导入方法依赖的package包/类
@Override
public final DoubleStream mapToDouble(IntToDoubleFunction mapper) {
Objects.requireNonNull(mapper);
return new DoublePipeline.StatelessOp<Integer>(this, StreamShape.INT_VALUE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) {
@Override
Sink<Integer> opWrapSink(int flags, Sink<Double> sink) {
return new Sink.ChainedInt<Double>(sink) {
@Override
public void accept(int t) {
downstream.accept(mapper.applyAsDouble(t));
}
};
}
};
}
示例2: uniHandleStage
import java9.util.Objects; //导入方法依赖的package包/类
private <V> CompletableFuture<V> uniHandleStage(
Executor e, BiFunction<? super T, Throwable, ? extends V> f) {
Objects.requireNonNull(f);
CompletableFuture<V> d = newIncompleteFuture();
Object r;
if ((r = result) == null)
unipush(new UniHandle<T,V>(e, d, this, f));
else if (e == null)
d.uniHandle(r, f, null);
else {
try {
e.execute(new UniHandle<T,V>(null, d, this, f));
} catch (Throwable ex) {
d.result = encodeThrowable(ex);
}
}
return d;
}
示例3: forEachRemaining
import java9.util.Objects; //导入方法依赖的package包/类
@Override
public void forEachRemaining(T_CONS action) {
Objects.requireNonNull(action);
if (sliceOrigin >= fence)
return;
if (index >= fence)
return;
if (index >= sliceOrigin && (index + s.estimateSize()) <= sliceFence) {
// The spliterator is contained within the slice
s.forEachRemaining(action);
index = fence;
} else {
// The spliterator intersects with the slice
while (sliceOrigin > index) {
s.tryAdvance(emptyConsumer());
index++;
}
// Traverse elements up to the fence
for (;index < fence; index++) {
s.tryAdvance(action);
}
}
}
示例4: forEachRemaining
import java9.util.Objects; //导入方法依赖的package包/类
@Override
public void forEachRemaining(IntConsumer action) {
int[] a; int i, hi; // hoist accesses and checks from loop
Objects.requireNonNull(action);
if ((a = array).length >= (hi = fence) &&
(i = index) >= 0 && i < (index = hi)) {
do { action.accept(a[i]); } while (++i < hi);
}
}
示例5: opWrapSink
import java9.util.Objects; //导入方法依赖的package包/类
@Override
public Sink<T> opWrapSink(int flags, Sink<T> sink) {
Objects.requireNonNull(sink);
// If the input is already naturally sorted and this operation
// also naturally sorted then this is a no-op
if (StreamOpFlag.SORTED.isKnown(flags) && isNaturalSort)
return sink;
else if (StreamOpFlag.SIZED.isKnown(flags))
return new SizedRefSortingSink<>(sink, comparator);
else
return new RefSortingSink<>(sink, comparator);
}
示例6: forEachRemaining
import java9.util.Objects; //导入方法依赖的package包/类
@Override
public void forEachRemaining(LongConsumer action) {
Objects.requireNonNull(action);
if (count == -2) {
action.accept(first);
count = -1;
}
}
示例7: makeInt
import java9.util.Objects; //导入方法依赖的package包/类
/**
* Constructs a {@code TerminalOp} that implements a functional reduce on
* {@code int} values.
*
* @param identity the identity for the combining function
* @param operator the combining function
* @return a {@code TerminalOp} implementing the reduction
*/
public static TerminalOp<Integer, Integer>
makeInt(int identity, IntBinaryOperator operator) {
Objects.requireNonNull(operator);
class ReducingSink
implements AccumulatingSink<Integer, Integer, ReducingSink>, Sink.OfInt {
private int state;
@Override
public void begin(long size) {
state = identity;
}
@Override
public void accept(int t) {
state = operator.applyAsInt(state, t);
}
@Override
public Integer get() {
return state;
}
@Override
public void combine(ReducingSink other) {
accept(other.state);
}
}
return new ReduceOp<Integer, Integer, ReducingSink>(StreamShape.INT_VALUE) {
@Override
public ReducingSink makeSink() {
return new ReducingSink();
}
};
}
示例8: screenExecutor
import java9.util.Objects; //导入方法依赖的package包/类
/**
* Null-checks user executor argument, and translates uses of
* commonPool to ASYNC_POOL in case parallelism disabled.
*/
static Executor screenExecutor(Executor e) {
if (!USE_COMMON_POOL && e == ForkJoinPool.commonPool())
return ASYNC_POOL;
return Objects.requireNonNull(e);
}
示例9: flatMapToInt
import java9.util.Objects; //导入方法依赖的package包/类
@Override
public final IntStream flatMapToInt(Function<? super P_OUT, ? extends IntStream> mapper) {
Objects.requireNonNull(mapper);
return new IntPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<P_OUT> opWrapSink(int flags, Sink<Integer> sink) {
return new Sink.ChainedReference<P_OUT, Integer>(sink) {
// true if cancellationRequested() has been called
boolean cancellationRequested;
// cache the consumer to avoid creation on every accepted element
IntConsumer downstreamAsInt = downstream::accept;
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(P_OUT u) {
IntStream result = null;
try {
result = mapper.apply(u);
if (result != null) {
if (!cancellationRequested) {
result.sequential().forEach(downstreamAsInt);
}
else {
Spliterator.OfInt s = result.sequential().spliterator();
do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsInt));
}
}
} finally {
if (result != null) {
result.close();
}
}
}
@Override
public boolean cancellationRequested() {
cancellationRequested = true;
return downstream.cancellationRequested();
}
};
}
};
}
示例10: flatMapToLong
import java9.util.Objects; //导入方法依赖的package包/类
@Override
public final LongStream flatMapToLong(Function<? super P_OUT, ? extends LongStream> mapper) {
Objects.requireNonNull(mapper);
return new LongPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE,
StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
@Override
Sink<P_OUT> opWrapSink(int flags, Sink<Long> sink) {
return new Sink.ChainedReference<P_OUT, Long>(sink) {
// true if cancellationRequested() has been called
boolean cancellationRequested;
// cache the consumer to avoid creation on every accepted element
LongConsumer downstreamAsLong = downstream::accept;
@Override
public void begin(long size) {
downstream.begin(-1);
}
@Override
public void accept(P_OUT u) {
LongStream result = null;
try {
result = mapper.apply(u);
if (result != null) {
if (!cancellationRequested) {
result.sequential().forEach(downstreamAsLong);
}
else {
Spliterator.OfLong s = result.sequential().spliterator();
do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstreamAsLong));
}
}
} finally {
if (result != null) {
result.close();
}
}
}
@Override
public boolean cancellationRequested() {
cancellationRequested = true;
return downstream.cancellationRequested();
}
};
}
};
}
示例11: iterate
import java9.util.Objects; //导入方法依赖的package包/类
/**
* Returns an infinite sequential ordered {@link Stream} 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 Stream} 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 <S> the type of the operand and seed, a subtype of T
* @param <T> the type of stream elements
* @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 Stream}
*/
public static <T, S extends T> Stream<T> iterate(S seed, UnaryOperator<S> f) {
Objects.requireNonNull(f);
Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<T>(Long.MAX_VALUE,
Spliterator.ORDERED | Spliterator.IMMUTABLE) {
S prev;
boolean started;
@Override
public boolean tryAdvance(Consumer<? super T> action) {
Objects.requireNonNull(action);
S s;
if (started) {
s = f.apply(prev);
} else {
s = seed;
started = true;
}
action.accept(prev = s);
return true;
}
};
return StreamSupport.stream(spliterator, false);
}
示例12: replaceAll
import java9.util.Objects; //导入方法依赖的package包/类
/**
* Replaces each entry's value with the result of invoking the given
* function on that entry until all entries have been processed or the
* function throws an exception. Exceptions thrown by the function are
* relayed to the caller.
*
* <p><b>Implementation Requirements:</b><br>
* <p>The default implementation is equivalent to, for the {@code map}:
* <pre> {@code
* for ((Map.Entry<K, V> entry : map.entrySet())
* do {
* K k = entry.getKey();
* V v = entry.getValue();
* } while(!replace(k, v, function.apply(k, v)));
* }</pre>
*
* The default implementation may retry these steps when multiple
* threads attempt updates including potentially calling the function
* repeatedly for a given key.
*
* <p>This implementation assumes that the ConcurrentMap cannot contain null
* values and {@code get()} returning null unambiguously means the key is
* absent. Implementations which support null values <strong>must</strong>
* override the default implementation.
*
* @param <K> the type of keys maintained by the passed map
* @param <V> the type of mapped values in the passed map
* @param map the {@code ConcurrentMap} on which to execute the {@code replaceAll}
* operation.
* @param function the function to apply to each entry
* @throws UnsupportedOperationException if the {@code set} operation
* is not supported by the map's entry set iterator.
* @throws ClassCastException if the class of a replacement value
* prevents it from being stored in the map
* @throws NullPointerException if the specified function is null, or the
* specified replacement value is null, and the map does not permit null
* values
* @throws ClassCastException if a replacement value is of an inappropriate
* type for the map (optional)
* @throws NullPointerException if function or a replacement value is null,
* and the map does not permit null keys or values (optional)
* @throws IllegalArgumentException if some property of a replacement value
* prevents it from being stored in the map (optional)
* @throws ConcurrentModificationException if an entry is found to be
* removed during iteration
* @since 1.8
*/
public static <K, V> void replaceAll(ConcurrentMap<K, V> map, BiFunction<? super K, ? super V, ? extends V> function) {
Objects.requireNonNull(map);
Objects.requireNonNull(function);
forEach(map, (k,v) -> {
while (!map.replace(k, v, function.apply(k, v))) {
// v changed or k is gone
if ( (v = map.get(k)) == null) {
// k is no longer in the map.
break;
}
}
});
}
示例13: orTimeout
import java9.util.Objects; //导入方法依赖的package包/类
/**
* Exceptionally completes this CompletableFuture with
* a {@link TimeoutException} if not otherwise completed
* before the given timeout.
*
* @param timeout how long to wait before completing exceptionally
* with a TimeoutException, in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the
* {@code timeout} parameter
* @return this CompletableFuture
* @since 9
*/
public CompletableFuture<T> orTimeout(long timeout, TimeUnit unit) {
Objects.requireNonNull(unit);
if (result == null)
whenComplete(new Canceller(Delayer.delay(new Timeout(this),
timeout, unit)));
return this;
}
示例14: comparing
import java9.util.Objects; //导入方法依赖的package包/类
/**
* Accepts a function that extracts a {@link java.lang.Comparable
* Comparable} sort key from a type {@code T}, and returns a {@code
* Comparator<T>} that compares by that sort key.
*
* <p>The returned comparator is serializable if the specified function
* is also serializable.
*
* <p><b>API Note:</b><br>
* For example, to obtain a {@code Comparator} that compares {@code
* Person} objects by their last name,
*
* <pre>{@code
* Comparator<Person> byLastName = Comparators.comparing(Person::getLastName);
* }</pre>
*
* @param <T> the type of element to be compared
* @param <U> the type of the {@code Comparable} sort key
* @param keyExtractor the function used to extract the {@link
* Comparable} sort key
* @return a comparator that compares by an extracted key
* @throws NullPointerException if the argument is null
* @since 1.8
*/
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
Function<? super T, ? extends U> keyExtractor)
{
Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable)
(c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
}
示例15: SubmissionPublisher
import java9.util.Objects; //导入方法依赖的package包/类
/**
* Creates a new SubmissionPublisher using the given Executor for
* async delivery to subscribers, with the given maximum buffer size
* for each subscriber, and, if non-null, the given handler invoked
* when any Subscriber throws an exception in method {@link
* Flow.Subscriber#onNext(Object) onNext}.
*
* @param executor the executor to use for async delivery,
* supporting creation of at least one independent thread
* @param maxBufferCapacity the maximum capacity for each
* subscriber's buffer (the enforced capacity may be rounded up to
* the nearest power of two and/or bounded by the largest value
* supported by this implementation; method {@link #getMaxBufferCapacity}
* returns the actual value)
* @param handler if non-null, procedure to invoke upon exception
* thrown in method {@code onNext}
* @throws NullPointerException if executor is null
* @throws IllegalArgumentException if maxBufferCapacity not
* positive
*/
public SubmissionPublisher(Executor executor, int maxBufferCapacity,
BiConsumer<? super Subscriber<? super T>, ? super Throwable> handler) {
Objects.requireNonNull(executor);
if (maxBufferCapacity <= 0)
throw new IllegalArgumentException("capacity must be positive");
this.executor = executor;
this.onNextHandler = handler;
this.maxBufferCapacity = roundCapacity(maxBufferCapacity);
}