Example usage for java.util NoSuchElementException NoSuchElementException

List of usage examples for java.util NoSuchElementException NoSuchElementException

Introduction

In this page you can find the example usage for java.util NoSuchElementException NoSuchElementException.

Prototype

public NoSuchElementException() 

Source Link

Document

Constructs a NoSuchElementException with null as its error message string.

Usage

From source file:androidx.navigation.NavGraph.java

@NonNull
@Override//www.  j a  va2  s  .c om
public Iterator<NavDestination> iterator() {
    return new Iterator<NavDestination>() {
        private int mIndex = -1;
        private boolean mWentToNext = false;

        @Override
        public boolean hasNext() {
            return mIndex + 1 < mNodes.size();
        }

        @Override
        public NavDestination next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            mWentToNext = true;
            return mNodes.valueAt(++mIndex);
        }

        @Override
        public void remove() {
            if (!mWentToNext) {
                throw new IllegalStateException("You must call next() before you can remove an element");
            }
            mNodes.valueAt(mIndex).setParent(null);
            mNodes.removeAt(mIndex);
            mIndex--;
            mWentToNext = false;
        }
    };
}

From source file:edu.cornell.med.icb.io.TSVReader.java

/**
 * Ensures we have a next field./*ww  w  .  ja v a  2 s  .  c o m*/
 */
private void ensureNextField() {
    if (currentTokens == null || currentTokenIndex >= currentTokens.length) {
        throw new NoSuchElementException();
    }
}

From source file:com.textocat.textokit.commons.cpe.JdbcCollectionReader.java

/**
 * {@inheritDoc}//from ww w. java2s .c o m
 */
@Override
public void getNext(CAS cas) throws IOException, CollectionException {
    if (!dbIterator.hasNext()) {
        throw new NoSuchElementException();
    }
    DbTuple tuple = dbIterator.next();
    consumedCount++;
    cas.setDocumentText(tuple.text);
    try {
        DocumentMetadata docMeta = new DocumentMetadata(cas.getJCas());
        docMeta.setSourceUri(tuple.url);
        docMeta.addToIndexes();
    } catch (CASException e) {
        throw new CollectionException(e);
    }
}

From source file:io.druid.data.input.impl.prefetch.PrefetchableTextFilesFirehoseFactory.java

@Override
public Firehose connect(StringInputRowParser firehoseParser, File temporaryDirectory) throws IOException {
    if (!cacheManager.isEnabled() && maxFetchCapacityBytes == 0) {
        return super.connect(firehoseParser, temporaryDirectory);
    }//from www .j  a va2  s. c o m

    if (objects == null) {
        objects = ImmutableList.copyOf(Preconditions.checkNotNull(initObjects(), "objects"));
    }

    Preconditions.checkState(temporaryDirectory.exists(), "temporaryDirectory[%s] does not exist",
            temporaryDirectory);
    Preconditions.checkState(temporaryDirectory.isDirectory(), "temporaryDirectory[%s] is not a directory",
            temporaryDirectory);

    LOG.info("Create a new firehose for [%d] objects", objects.size());

    // fetchExecutor is responsible for background data fetching
    final ExecutorService fetchExecutor = Execs.singleThreaded("firehose_fetch_%d");
    final Fetcher<T> fetcher = new Fetcher<>(cacheManager, objects, fetchExecutor, temporaryDirectory,
            maxFetchCapacityBytes, prefetchTriggerBytes, fetchTimeout, maxFetchRetry, this::openObjectStream);

    return new FileIteratingFirehose(new Iterator<LineIterator>() {
        @Override
        public boolean hasNext() {
            return fetcher.hasNext();
        }

        @Override
        public LineIterator next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }

            final OpenedObject<T> openedObject = fetcher.next();
            final InputStream stream;
            try {
                stream = wrapObjectStream(openedObject.getObject(), openedObject.getObjectStream());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

            return new ResourceCloseableLineIterator(new InputStreamReader(stream, StandardCharsets.UTF_8),
                    openedObject.getResourceCloser());
        }
    }, firehoseParser, () -> {
        fetchExecutor.shutdownNow();
        try {
            Preconditions.checkState(fetchExecutor.awaitTermination(fetchTimeout, TimeUnit.MILLISECONDS));
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new ISE("Failed to shutdown fetch executor during close");
        }
    });
}

From source file:MiniMap.java

/**
 * @see java.util.Map#entrySet()//from   w ww.j a v  a2  s.  c  o m
 */
public Set<Entry<K, V>> entrySet() {
    return new AbstractSet<Entry<K, V>>() {
        @Override
        public Iterator<Entry<K, V>> iterator() {
            return new Iterator<Entry<K, V>>() {
                public boolean hasNext() {
                    return index < size;
                }

                public Entry<K, V> next() {
                    if (!hasNext()) {
                        throw new NoSuchElementException();
                    }

                    keyIndex = nextKey(nextIndex(keyIndex));

                    index++;

                    return new Map.Entry<K, V>() {
                        public K getKey() {
                            return keys[keyIndex];
                        }

                        public V getValue() {
                            return values[keyIndex];
                        }

                        public V setValue(final V value) {
                            final V oldValue = values[keyIndex];

                            values[keyIndex] = value;

                            return oldValue;
                        }
                    };
                }

                public void remove() {
                    keys[keyIndex] = null;
                    values[keyIndex] = null;
                }

                int keyIndex = -1;

                int index = 0;
            };
        }

        @Override
        public int size() {
            return size;
        }
    };
}

From source file:io.druid.data.input.impl.prefetch.Fetcher.java

@Override
public OpenedObject<T> next() {
    if (!hasNext()) {
        throw new NoSuchElementException();
    }//from   w  w  w .  ja  v a  2  s.  c  om

    // If fetch() fails, hasNext() always returns true and next() is always called. The below method checks that
    // fetch() threw an exception and propagates it if exists.
    checkFetchException(false);

    try {
        final OpenedObject<T> openedObject = prefetchEnabled ? openObjectFromLocal() : openObjectFromRemote();
        numRemainingObjects--;
        return openedObject;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Utils.java

public E next() {
    if (next == WAITING && !hasNext()) {
        throw new NoSuchElementException();
    }//from   w  ww.ja  va 2 s .co m
    assert next != WAITING;
    @SuppressWarnings("unchecked")
    // type-checking is done by accept()
    E x = (E) next;
    next = WAITING;
    return x;
}

From source file:demo.util.model.BoundedFifoBuffer.java

/**
 * Returns an iterator over this buffer's elements.
 *
 * @return an iterator over this buffer's elements
 *//*from  w ww  . j av a2  s .co  m*/
@Override
public Iterator<T> iterator() {
    return new Iterator<T>() {

        private int index = start;
        private int lastReturnedIndex = -1;
        private boolean isFirst = full;

        @Override
        public boolean hasNext() {
            return isFirst || (index != end);

        }

        @SuppressWarnings("unchecked")
        @Override
        public T next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            isFirst = false;
            lastReturnedIndex = index;
            index = increment(index);
            return (T) elements[lastReturnedIndex];
        }

        @Override
        public void remove() {
            if (lastReturnedIndex == -1) {
                throw new IllegalStateException();
            }

            // First element can be removed quickly
            if (lastReturnedIndex == start) {
                BoundedFifoBuffer.this.remove();
                lastReturnedIndex = -1;
                return;
            }

            int pos = lastReturnedIndex + 1;
            if (start < lastReturnedIndex && pos < end) {
                // shift in one part
                System.arraycopy(elements, pos, elements, lastReturnedIndex, end - pos);
            } else {
                // Other elements require us to shift the subsequent elements
                while (pos != end) {
                    if (pos >= maxElements) {
                        elements[pos - 1] = elements[0];
                        pos = 0;
                    } else {
                        elements[decrement(pos)] = elements[pos];
                        pos = increment(pos);
                    }
                }
            }

            lastReturnedIndex = -1;
            end = decrement(end);
            elements[end] = null;
            full = false;
            index = decrement(index);
        }

    };
}

From source file:com.predic8.membrane.core.interceptor.authentication.session.LDAPUserDataProvider.java

private String searchUser(String login, HashMap<String, String> userAttrs, DirContext ctx)
        throws NamingException {
    String uid;/*from  w w  w  .  j a  v  a2  s  . c  o  m*/
    SearchControls ctls = new SearchControls();
    ctls.setReturningObjFlag(true);
    ctls.setSearchScope(searchScope);
    String search = searchPattern.replaceAll(Pattern.quote("%LOGIN%"), escapeLDAPSearchFilter(login));
    log.debug("Searching LDAP for " + search);
    NamingEnumeration<SearchResult> answer = ctx.search(base, search, ctls);
    try {
        if (!answer.hasMore())
            throw new NoSuchElementException();
        log.debug("LDAP returned >=1 record.");
        SearchResult result = answer.next();
        uid = result.getName();
        for (Map.Entry<String, String> e : attributeMap.entrySet()) {
            log.debug("found LDAP attribute: " + e.getKey());
            Attribute a = result.getAttributes().get(e.getKey());
            if (a != null)
                userAttrs.put(e.getValue(), a.get().toString());
        }
    } finally {
        answer.close();
    }
    return uid;
}