Search in sources :

Example 36 with NoSuchElementException

use of java.util.NoSuchElementException in project camel by apache.

the class TestPublisher method subscribe.

@Override
public void subscribe(Subscriber<? super T> subscriber) {
    subscriber.onSubscribe(new Subscription() {

        private Iterator<T> it = data.iterator();

        private AtomicLong requested = new AtomicLong(0);

        private Object monitor = new Object();

        @Override
        public void request(long l) {
            this.requested.addAndGet(l);
            new Thread() {

                @Override
                public void run() {
                    synchronized (monitor) {
                        boolean wasNonEmpty = it.hasNext();
                        while (requested.longValue() > 0 && it.hasNext()) {
                            T d = it.next();
                            requested.decrementAndGet();
                            if (delay > 0) {
                                try {
                                    Thread.sleep(delay);
                                } catch (InterruptedException ex) {
                                }
                            }
                            subscriber.onNext(d);
                        }
                        if (wasNonEmpty && !it.hasNext()) {
                            // data cannot be added to this test publisher
                            subscriber.onComplete();
                        }
                    }
                }
            }.start();
        }

        @Override
        public void cancel() {
            synchronized (monitor) {
                this.requested.set(0);
                this.it = new Iterator<T>() {

                    @Override
                    public boolean hasNext() {
                        return false;
                    }

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

                    public void run() {
                        subscriber.onComplete();
                    }
                }.start();
            }
        }
    });
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) Iterator(java.util.Iterator) Subscription(org.reactivestreams.Subscription) NoSuchElementException(java.util.NoSuchElementException)

Example 37 with NoSuchElementException

use of java.util.NoSuchElementException in project robovm by robovm.

the class BasicHeaderElementIterator method nextElement.

public HeaderElement nextElement() throws NoSuchElementException {
    if (this.currentElement == null) {
        parseNextElement();
    }
    if (this.currentElement == null) {
        throw new NoSuchElementException("No more header elements available");
    }
    HeaderElement element = this.currentElement;
    this.currentElement = null;
    return element;
}
Also used : HeaderElement(org.apache.http.HeaderElement) NoSuchElementException(java.util.NoSuchElementException)

Example 38 with NoSuchElementException

use of java.util.NoSuchElementException in project okhttp by square.

the class Cache method urls.

/**
   * Returns an iterator over the URLs in this cache. This iterator doesn't throw {@code
   * ConcurrentModificationException}, but if new responses are added while iterating, their URLs
   * will not be returned. If existing responses are evicted during iteration, they will be absent
   * (unless they were already returned).
   *
   * <p>The iterator supports {@linkplain Iterator#remove}. Removing a URL from the iterator evicts
   * the corresponding response from the cache. Use this to evict selected responses.
   */
public Iterator<String> urls() throws IOException {
    return new Iterator<String>() {

        final Iterator<DiskLruCache.Snapshot> delegate = cache.snapshots();

        String nextUrl;

        boolean canRemove;

        @Override
        public boolean hasNext() {
            if (nextUrl != null)
                return true;
            // Prevent delegate.remove() on the wrong item!
            canRemove = false;
            while (delegate.hasNext()) {
                DiskLruCache.Snapshot snapshot = delegate.next();
                try {
                    BufferedSource metadata = Okio.buffer(snapshot.getSource(ENTRY_METADATA));
                    nextUrl = metadata.readUtf8LineStrict();
                    return true;
                } catch (IOException ignored) {
                // We couldn't read the metadata for this snapshot; possibly because the host filesystem
                // has disappeared! Skip it.
                } finally {
                    snapshot.close();
                }
            }
            return false;
        }

        @Override
        public String next() {
            if (!hasNext())
                throw new NoSuchElementException();
            String result = nextUrl;
            nextUrl = null;
            canRemove = true;
            return result;
        }

        @Override
        public void remove() {
            if (!canRemove)
                throw new IllegalStateException("remove() before next()");
            delegate.remove();
        }
    };
}
Also used : Iterator(java.util.Iterator) DiskLruCache(okhttp3.internal.cache.DiskLruCache) ByteString(okio.ByteString) IOException(java.io.IOException) NoSuchElementException(java.util.NoSuchElementException) BufferedSource(okio.BufferedSource)

Example 39 with NoSuchElementException

use of java.util.NoSuchElementException in project databus by linkedin.

the class EventLogReader method read.

public Checkpoint read() {
    Checkpoint checkPoint = new Checkpoint();
    checkPoint.setFlexible();
    if (_enabled) {
        // ArrayList<InternalDatabusEventsListener> eventListeners = new ArrayList<InternalDatabusEventsListener>();
        _eventBuffer.addInternalListener(checkPoint);
        _eventBuffer.addInternalListener(this);
        if (_filesToRead != null) {
            for (File f : _filesToRead) {
                FileChannel readChannel = null;
                try {
                    readChannel = new FileInputStream(f).getChannel();
                } catch (FileNotFoundException e) {
                    throw new RuntimeException(e);
                }
                int numEvents = 0;
                try {
                    numEvents = _eventBuffer.readEvents(readChannel, getEncoding(f));
                } catch (InvalidEventException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                LOG.info("Read " + numEvents + " events");
            }
            _eventBuffer.removeInternalListener(checkPoint);
            _eventBuffer.removeInternalListener(this);
            LOG.info("Checkpoint = " + checkPoint);
            if (_eventSeen) {
                DbusEventIterator iter = _eventBuffer.acquireIterator("EventLogReader:firstEvent");
                try {
                    DbusEvent event = iter.next();
                    String firstEventContent = event.toString();
                    // if we didn't wrap the buffer, and the first event is not an eop marker, delete the first window
                    if (_firstEventContent.equalsIgnoreCase(firstEventContent) && !event.isEndOfPeriodMarker()) {
                        long result = _eventBuffer.deleteFirstWindow();
                        if (result < 0) {
                            throw new RuntimeException("eventBuffer could not delete first window");
                        }
                    }
                } finally {
                    _eventBuffer.releaseIterator(iter);
                }
                if (_lastEopOffset >= 0) {
                    iter = _eventBuffer.new DbusEventIterator(this._lastEopOffset, _eventBuffer.getTail(), "EventLogReader:lastEOP");
                    try {
                        DbusEvent event = iter.next();
                        if (!event.isEndOfPeriodMarker()) {
                            throw new RuntimeException("Tried reading end of period marker, but failed");
                        }
                        if (iter.hasNext()) {
                            _eventBuffer.setTail(iter.getCurrentPosition());
                        }
                    } catch (NoSuchElementException e) {
                        LOG.error("NoSuchElementException at : " + _eventBuffer.getBufferPositionParser().toString(_lastEopOffset));
                        throw e;
                    } finally {
                        _eventBuffer.releaseIterator(iter);
                    }
                }
            }
        }
    }
    return checkPoint;
}
Also used : FileChannel(java.nio.channels.FileChannel) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) File(java.io.File) NoSuchElementException(java.util.NoSuchElementException) DbusEventIterator(com.linkedin.databus.core.DbusEventBuffer.DbusEventIterator)

Example 40 with NoSuchElementException

use of java.util.NoSuchElementException in project zoj by licheng.

the class Main method main.

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    PrintWriter out = new PrintWriter(System.out);
    try {
        for (; ; ) {
            int a = in.nextInt();
            int b = in.nextInt();
            out.println(a + b);
        }
    } catch (NoSuchElementException e) {
    }
    out.close();
}
Also used : Scanner(java.util.Scanner) NoSuchElementException(java.util.NoSuchElementException) PrintWriter(java.io.PrintWriter)

Aggregations

NoSuchElementException (java.util.NoSuchElementException)720 Iterator (java.util.Iterator)122 Test (org.junit.Test)61 Scanner (java.util.Scanner)59 IOException (java.io.IOException)58 ArrayList (java.util.ArrayList)57 StringTokenizer (java.util.StringTokenizer)49 InputMismatchException (java.util.InputMismatchException)46 Locale (java.util.Locale)25 HashMap (java.util.HashMap)22 Map (java.util.Map)22 File (java.io.File)21 List (java.util.List)20 NodeIterator (javax.jcr.NodeIterator)20 HashSet (java.util.HashSet)16 ConcurrentLinkedDeque (java.util.concurrent.ConcurrentLinkedDeque)16 Collection (java.util.Collection)15 LinkedList (java.util.LinkedList)15 Set (java.util.Set)15 Enumeration (java.util.Enumeration)14