use of java8.util.function.Consumer in project streamsupport by stefan-zobel.
the class Spliterators method iterator.
// Iterators from Spliterators
/**
* Creates an {@code Iterator} from a {@code Spliterator}.
*
* <p>Traversal of elements should be accomplished through the iterator.
* The behaviour of traversal is undefined if the spliterator is operated
* after the iterator is returned.
*
* @param <T> Type of elements
* @param spliterator The spliterator
* @return An iterator
* @throws NullPointerException if the given spliterator is {@code null}
*/
public static <T> Iterator<T> iterator(Spliterator<? extends T> spliterator) {
Objects.requireNonNull(spliterator);
class Adapter implements Iterator<T>, Consumer<T> {
boolean valueReady = false;
T nextElement;
@Override
public void accept(T t) {
valueReady = true;
nextElement = t;
}
@Override
public boolean hasNext() {
if (!valueReady)
spliterator.tryAdvance(this);
return valueReady;
}
@Override
public T next() {
if (!valueReady && !hasNext())
throw new NoSuchElementException();
else {
valueReady = false;
T t = nextElement;
nextElement = null;
return t;
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove");
}
}
return new Adapter();
}
use of java8.util.function.Consumer in project azure-sdk-for-android by Azure.
the class PushNotificationClient method handlePushNotification.
/**
* Handle incoming push notification.
* Invoke corresponding chat event handle if registered.
* @param pushNotification Incoming push notification payload from the FCM SDK.
*
* @return True if there's handler(s) for incoming push notification; otherwise, false.
*/
public boolean handlePushNotification(ChatPushNotification pushNotification) {
this.logger.info(" Receive handle push notification request.");
ChatEventType chatEventType = this.parsePushNotificationEventType(pushNotification);
this.logger.info(" " + chatEventType + " received.");
if (this.pushNotificationListeners.containsKey(chatEventType)) {
ChatEvent event = this.parsePushNotificationEvent(chatEventType, pushNotification);
Set<Consumer<ChatEvent>> callbacks = this.pushNotificationListeners.get(chatEventType);
for (Consumer<ChatEvent> callback : callbacks) {
this.logger.info(" invoke callback " + callback + " for " + chatEventType);
callback.accept(event);
}
return true;
}
return false;
}
Aggregations