Search in sources :

Example 11 with Consumer

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();
}
Also used : LongConsumer(java8.util.function.LongConsumer) IntConsumer(java8.util.function.IntConsumer) Consumer(java8.util.function.Consumer) DoubleConsumer(java8.util.function.DoubleConsumer) Iterator(java.util.Iterator) NoSuchElementException(java.util.NoSuchElementException)

Example 12 with Consumer

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;
}
Also used : Consumer(java9.util.function.Consumer) ChatEvent(com.azure.android.communication.chat.models.ChatEvent) ChatEventType(com.azure.android.communication.chat.models.ChatEventType)

Aggregations

Consumer (java8.util.function.Consumer)11 List (java.util.List)10 StreamSupport (java8.util.stream.StreamSupport)10 Test (org.testng.annotations.Test)10 Arrays (java.util.Arrays)9 Spliterator (java8.util.Spliterator)9 Spliterators (java8.util.Spliterators)9 Iterator (java.util.Iterator)7 LinkedList (java.util.LinkedList)7 NoSuchElementException (java.util.NoSuchElementException)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7 ArrayDeque (java.util.ArrayDeque)6 ArrayList (java.util.ArrayList)6 Collection (java.util.Collection)6 Collections (java.util.Collections)6 ConcurrentModificationException (java.util.ConcurrentModificationException)6 Deque (java.util.Deque)6 HashSet (java.util.HashSet)6 PriorityQueue (java.util.PriorityQueue)6 Queue (java.util.Queue)6