Java Iterator singletonIterator(final T nullableValue)

Here you can find the source of singletonIterator(final T nullableValue)

Description

singleton Iterator

License

Apache License

Declaration

public static <T> Iterator<T> singletonIterator(final T nullableValue) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Iterator;

import java.util.NoSuchElementException;

public class Main {
    private static final Iterator<Object> EMPTY_ITERATOR = new Iterator<Object>() {
        @Override/*w  w  w .  j  a v  a  2  s  .  com*/
        public boolean hasNext() {
            return false;
        }

        @Override
        public Object next() {
            throw new NoSuchElementException();
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException("remove");
        }
    };

    public static <T> Iterator<T> singletonIterator(final T nullableValue) {
        if (nullableValue == null) {
            return (Iterator<T>) EMPTY_ITERATOR;
        }
        return new Iterator<T>() {
            boolean done;

            @Override
            public boolean hasNext() {
                return !done;
            }

            @Override
            public T next() {
                if (done) {
                    throw new NoSuchElementException();
                }
                done = true;
                return nullableValue;
            }

            @Override
            public void remove() {
                throw new UnsupportedOperationException("remove");
            }
        };
    }
}

Related

  1. single(Iterator i)
  2. singleElementIterator(T element)
  3. singleOrDefault(Iterator iterator)
  4. singletonIterator(final Object item)
  5. singletonIterator(final T item)
  6. singletonIterator(final T t)
  7. size(Iterator iterator)
  8. splice(LinkedList list, Iterator iterator, LinkedList list2, V v)
  9. toArray(final Iterator iterator)