Java源码示例:com.fernandocejas.arrow.checks.Preconditions
示例1
/**
* Returns the elements of {@code unfiltered} that satisfy a predicate.
*/
public static <T> UnmodifiableIterator<T> filter(
final Iterator<T> unfiltered, final Predicate<? super T> predicate) {
Preconditions.checkNotNull(unfiltered);
Preconditions.checkNotNull(predicate);
return new AbstractIterator<T>() {
@Override
protected T computeNext() {
while (unfiltered.hasNext()) {
T element = unfiltered.next();
if (predicate.apply(element)) {
return element;
}
}
return endOfData();
}
};
}
示例2
/**
* Removes every element that satisfies the provided predicate from the
* iterator. The iterator will be left exhausted: its {@code hasNext()}
* method will return {@code false}.
*
* @param removeFrom the iterator to (potentially) remove elements from
* @param predicate a predicate that determines whether an element should
* be removed
* @return {@code true} if any elements were removed from the iterator
* @since 2.0
*/
public static <T> boolean removeIf(
Iterator<T> removeFrom, Predicate<? super T> predicate) {
Preconditions.checkNotNull(predicate);
boolean modified = false;
while (removeFrom.hasNext()) {
if (predicate.apply(removeFrom.next())) {
removeFrom.remove();
modified = true;
}
}
return modified;
}
示例3
/**
* Adds all elements in {@code iterator} to {@code collection}. The iterator
* will be left exhausted: its {@code hasNext()} method will return
* {@code false}.
*
* @return {@code true} if {@code collection} was modified as a result of this
* operation
*/
public static <T> boolean addAll(Collection<T> addTo, Iterator<? extends T> iterator) {
Preconditions.checkNotNull(addTo);
Preconditions.checkNotNull(iterator);
boolean wasModified = false;
while (iterator.hasNext()) {
wasModified |= addTo.add(iterator.next());
}
return wasModified;
}
示例4
private static <T> UnmodifiableIterator<List<T>> partitionImpl(
final Iterator<T> iterator, final int size, final boolean pad) {
Preconditions.checkNotNull(iterator);
Preconditions.checkArgument(size > 0);
return new UnmodifiableIterator<List<T>>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public List<T> next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Object[] array = new Object[size];
int count = 0;
for (; count < size && iterator.hasNext(); count++) {
array[count] = iterator.next();
}
for (int i = count; i < size; i++) {
array[i] = null; // for GWT
}
@SuppressWarnings("unchecked") // we only put Ts in it
List<T> list = Collections.unmodifiableList(
(List<T>) Arrays.asList(array));
return pad || count == size ? list : list.subList(0, count);
}
};
}
示例5
/**
* Returns {@code true} if every element returned by {@code iterator}
* satisfies the given predicate. If {@code iterator} is empty, {@code true}
* is returned.
*/
public static <T> boolean all(Iterator<T> iterator, Predicate<? super T> predicate) {
Preconditions.checkNotNull(predicate);
while (iterator.hasNext()) {
T element = iterator.next();
if (!predicate.apply(element)) {
return false;
}
}
return true;
}
示例6
/**
* Calls {@code next()} on {@code iterator}, either {@code numberToAdvance} times
* or until {@code hasNext()} returns {@code false}, whichever comes first.
*
* @return the number of elements the iterator was advanced
* @since 13.0 (since 3.0 as {@code Iterators.skip})
*/
public static int advance(Iterator<?> iterator, int numberToAdvance) {
Preconditions.checkNotNull(iterator);
Preconditions.checkArgument(numberToAdvance >= 0, "numberToAdvance must be nonnegative");
int i;
for (i = 0; i < numberToAdvance && iterator.hasNext(); i++) {
iterator.next();
}
return i;
}
示例7
/**
* Clears the iterator using its remove method.
*/
static void clear(Iterator<?> iterator) {
Preconditions.checkNotNull(iterator);
while (iterator.hasNext()) {
iterator.next();
iterator.remove();
}
}
示例8
/**
* Executes the current use case.
*
* @param observer {@link DisposableObserver} which will be listening to the observable build
* by {@link #buildUseCaseObservable(Params)} ()} method.
* @param params Parameters (Optional) used to build/execute this use case.
*/
public void execute(DisposableObserver<T> observer, Params params) {
Preconditions.checkNotNull(observer);
final Observable<T> observable = this.buildUseCaseObservable(params)
.subscribeOn(Schedulers.from(threadExecutor))
.observeOn(postExecutionThread.getScheduler());
addDisposable(observable.subscribeWith(observer));
}
示例9
public void execute(Params params, DisposableSubscriber<T> subscriber) {
Preconditions.checkNotNull(subscriber);
final Flowable<T> flowable = this.buildUseCaseObservable(params);
addDisposable(flowable.subscribeWith(subscriber));
}
示例10
public <P> void execute(Flowable<P> flowable, DisposableSubscriber<P> subscriber) {
Preconditions.checkNotNull(flowable);
Preconditions.checkNotNull(subscriber);
addDisposable(flowable.subscribeWith(subscriber));
}
示例11
private void addDisposable(Disposable disposable) {
Preconditions.checkNotNull(disposable);
Preconditions.checkNotNull(mDisposables);
mDisposables.add(disposable);
}
示例12
/**
* Get current user id from fragments arguments.
*/
private int currentUserId() {
final Bundle arguments = getArguments();
Preconditions.checkNotNull(arguments, "Fragment arguments cannot be null");
return arguments.getInt(PARAM_USER_ID);
}
示例13
/**
* Dispose from current {@link CompositeDisposable}.
*/
private void addDisposable(Disposable disposable) {
Preconditions.checkNotNull(disposable);
Preconditions.checkNotNull(disposables);
disposables.add(disposable);
}
示例14
@Override Observable<User> buildUseCaseObservable(Params params) {
Preconditions.checkNotNull(params);
return this.userRepository.user(params.userId);
}
示例15
/**
* Returns the index in {@code iterator} of the first element that satisfies
* the provided {@code predicate}, or {@code -1} if the Iterator has no such
* elements.
*
* <p>More formally, returns the lowest index {@code i} such that
* {@code predicate.apply(Iterators.get(iterator, i))} returns {@code true},
* or {@code -1} if there is no such index.
*
* <p>If -1 is returned, the iterator will be left exhausted: its
* {@code hasNext()} method will return {@code false}. Otherwise,
* the iterator will be set to the element which satisfies the
* {@code predicate}.
*
* @since 2.0
*/
public static <T> int indexOf(Iterator<T> iterator, Predicate<? super T> predicate) {
Preconditions.checkNotNull(predicate, "predicate");
for (int i = 0; iterator.hasNext(); i++) {
T current = iterator.next();
if (predicate.apply(current)) {
return i;
}
}
return -1;
}
示例16
/**
* Divides an iterable into unmodifiable sublists of the given size (the final
* iterable may be smaller). For example, partitioning an iterable containing
* {@code [a, b, c, d, e]} with a partition size of 3 yields {@code
* [[a, b, c], [d, e]]} -- an outer iterable containing two inner lists of
* three and two elements, all in the original order.
*
* <p>Iterators returned by the returned iterable do not support the {@link
* Iterator#remove()} method. The returned lists implement {@link
* RandomAccess}, whether or not the input list does.
*
* <p><b>Note:</b> if {@code iterable} is a {@link List}, use {@link
* Lists#partition(List, int)} instead.
*
* @param iterable the iterable to return a partitioned view of
* @param size the desired size of each partition (the last may be smaller)
* @return an iterable of unmodifiable lists containing the elements of {@code
* iterable} divided into partitions
* @throws IllegalArgumentException if {@code size} is nonpositive
*/
public static <T> Iterable<List<T>> partition(
final Iterable<T> iterable, final int size) {
checkNotNull(iterable);
Preconditions.checkArgument(size > 0);
return new Iterable<List<T>>() {
@Override
public Iterator<List<T>> iterator() {
return Iterators.partition(iterable.iterator(), size);
}
};
}
示例17
/**
* Divides an iterable into unmodifiable sublists of the given size, padding
* the final iterable with null values if necessary. For example, partitioning
* an iterable containing {@code [a, b, c, d, e]} with a partition size of 3
* yields {@code [[a, b, c], [d, e, null]]} -- an outer iterable containing
* two inner lists of three elements each, all in the original order.
*
* <p>Iterators returned by the returned iterable do not support the {@link
* Iterator#remove()} method.
*
* @param iterable the iterable to return a partitioned view of
* @param size the desired size of each partition
* @return an iterable of unmodifiable lists containing the elements of {@code
* iterable} divided into partitions (the final iterable may have
* trailing null elements)
* @throws IllegalArgumentException if {@code size} is nonpositive
*/
public static <T> Iterable<List<T>> paddedPartition(
final Iterable<T> iterable, final int size) {
checkNotNull(iterable);
Preconditions.checkArgument(size > 0);
return new Iterable<List<T>>() {
@Override
public Iterator<List<T>> iterator() {
return Iterators.paddedPartition(iterable.iterator(), size);
}
};
}