List of usage examples for java.util Iterator Iterator
Iterator
From source file:ArrayMap.java
/** * @see java.util.Map#keySet()/*from w w w . ja v a 2 s . co m*/ */ public Set<K> keySet() { final Object[] iterKeys = theKeys; return new java.util.AbstractSet<K>() { @Override public int size() { return iterKeys.length; } @Override public Iterator<K> iterator() { return new Iterator<K>() { private int index = 0; public boolean hasNext() { return index < iterKeys.length; } public K next() { K ret = (K) iterKeys[index]; index++; return ret; } public void remove() { ArrayMap.this.remove(iterKeys[index - 1]); } }; } }; }
From source file:ai.susi.mind.SusiArgument.java
/** * the iterator returns the thoughts in reverse order, latest thought first *//* w w w.j a v a2 s . c om*/ @Override public Iterator<SusiThought> iterator() { return new Iterator<SusiThought>() { private int p = recall.size(); @Override public boolean hasNext() { return p > 0; } @Override public SusiThought next() { return recall.get(--p); } }; }
From source file:oculus.aperture.common.JSONProperties.java
@Override public Iterable<Boolean> getBooleans(String key) { try {/*w w w. j a v a 2 s .com*/ final JSONArray array = obj.getJSONArray(key); return new Iterable<Boolean>() { @Override public Iterator<Boolean> iterator() { return new Iterator<Boolean>() { private final int n = array.length(); private int i = 0; @Override public boolean hasNext() { return n > i; } @Override public Boolean next() { try { return (n > i) ? array.getBoolean(i++) : null; } catch (JSONException e) { return null; } } @Override public void remove() { throw new UnsupportedOperationException(); } }; } }; } catch (JSONException e) { return EmptyIterable.instance(); } }
From source file:org.paxle.desktop.impl.event.MultipleChangesListener.java
public Iterator<Map.Entry<Object, Object>> iterator(final boolean changedComps) { return new Iterator<Map.Entry<Object, Object>>() { final Iterator<Map.Entry<Object, CompEntry>> it = initialValues.entrySet().iterator(); Map.Entry<Object, Object> cur = null; Map.Entry<Object, Object> next = next0(); private Map.Entry<Object, Object> next0() { while (it.hasNext()) { final Map.Entry<Object, CompEntry> e = it.next(); if (!(changedSet.get(e.getValue().num) ^ changedComps)) return new ChangedCompEntry(e); }/* w w w .ja v a2 s . c o m*/ return null; } public boolean hasNext() { return next != null; } public Map.Entry<Object, Object> next() { cur = next; next = next0(); return cur; } public void remove() { removeComp2Monitor(cur); } }; }
From source file:org.apache.hadoop.hbase.io.hfile.MemcachedBlockCache.java
@Override public Iterator<CachedBlock> iterator() { return new Iterator<CachedBlock>() { @Override//from w w w. ja va2s . c om public boolean hasNext() { return false; } @Override public CachedBlock next() { throw new NoSuchElementException("MemcachedBlockCache can't iterate over blocks."); } @Override public void remove() { } }; }
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 ww w. j av a 2 s.c om @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:org.apache.flink.streaming.api.state.NullableCircularBuffer.java
/** * Returns an iterator over this buffer's elements. * * @return an iterator over this buffer's elements */// w w w . j av a 2s . c o m public Iterator iterator() { return new Iterator() { private int index = start; private int lastReturnedIndex = -1; private boolean isFirst = full; public boolean hasNext() { return isFirst || (index != end); } public Object next() { if (!hasNext()) { throw new NoSuchElementException(); } isFirst = false; lastReturnedIndex = index; index = increment(index); return elements[lastReturnedIndex]; } public void remove() { if (lastReturnedIndex == -1) { throw new IllegalStateException(); } // First element can be removed quickly if (lastReturnedIndex == start) { NullableCircularBuffer.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:org.ovirt.api.metamodel.tests.JsonWriterTest.java
/** * Checks that one million of VMs can be serialized in a reasonable time and without exhausting the memory of the * virtual machine. Note that the test is currently disabled because if it fails it will block other tests, but it * is still useful to run it manually, so please don't remove it. *///from w w w . j a v a2s .c o m @Test @Ignore public void testOneMillion() throws IOException { // Create a iterator that generates the objects: Iterator<V4Vm> infinite = new Iterator<V4Vm>() { private int count; @Override public boolean hasNext() { return count < 1_000_000; } @Override public V4Vm next() { // Note that these are extremely simple objects, they should probably be more complicated for this // test to be realistic: V4Vm object = vm().id(String.valueOf(count)).name("vm" + count) .disks(disk().id("123").alias("disk1")).disks(disk().id("456").alias("disk2")).build(); count++; return object; } }; // Write the objects generated by the iterator to a null stream, so that the only work performed is creating // the objects and converting them to XML: long before = System.currentTimeMillis(); try (JsonWriter writer = new JsonWriter(new NullOutputStream(), false)) { V4JsonVmWriter.writeMany(infinite, writer); } long after = System.currentTimeMillis(); long elapsed = after - before; // Check if it took less than a minute (it should take much more less, approx 2s, but lets be // conservative: assertTrue(elapsed < 60_000_000); }
From source file:org.jenkinsci.plugins.compress_artifacts.ZipStorageTest.java
@Test public void avoidZipExceptionWhileWriting() throws Exception { FileUtils.writeStringToFile(new File(content, "file"), "content"); final Entry<String, String> validArtifact = Collections.singletonMap("file", "file").entrySet().iterator() .next();/*ww w. j a v a 2 s . c o m*/ // Simulate archiving that takes forever serving valid artifact and then block forever on the next. final Map<String, String> artifacts = new HashMap<String, String>() { @Override public Set<Map.Entry<String, String>> entrySet() { return new HashSet<Map.Entry<String, String>>() { @Override public Iterator<Map.Entry<String, String>> iterator() { return new Iterator<Map.Entry<String, String>>() { private boolean block = false; public boolean hasNext() { return true; } public Map.Entry<String, String> next() { if (!block) { block = true; return validArtifact; } synchronized (this) { try { this.wait(); // Block forever throw new AssertionError(); } catch (InterruptedException ex) { // Expected at cleanup time } } return validArtifact; } public void remove() { throw new UnsupportedOperationException(); } }; } }; } }; // start archiving Thread compressor = new Thread("compressing-thread") { @Override public void run() { try { archive(artifacts); } catch (Exception ex) { throw new Error(ex); } } }; compressor.start(); try { Thread.sleep(1000); assertTrue(compressor.isAlive()); assertArrayEquals(new String[0], zs.list()); assertArrayEquals(new VirtualFile[0], zs.list("*")); } finally { compressor.stop(); } }
From source file:org.briljantframework.data.vector.Vectors.java
/** * <p>/* w w w . jav a 2s. c o m*/ * Split {@code vector} into {@code chunks}. Handles the case when {@code vector.size()} is not * evenly dividable by chunks by making some chunks larger. * </p> * * <p> * This implementation is lazy, i.e. chunking is done 'on-the-fly'. To get a list, {@code new * ArrayList<>(Vectors.split(vec, 10))} * </p> * * <p> * Ensures that {@code vector.getType()} is preserved. * </p> * * @param vector the vector * @param chunks the number of chunks * @return a collection of {@code chunk} chunks */ public static Collection<Vector> split(Vector vector, int chunks) { Check.argument(vector.size() >= chunks, "size must be shorter than chunks"); if (vector.size() == chunks) { return Collections.singleton(vector); } int bin = vector.size() / chunks; int remainder = vector.size() % chunks; return new AbstractCollection<Vector>() { @Override public Iterator<Vector> iterator() { return new Iterator<Vector>() { private int current = 0; private int remainders = 0; @Override public boolean hasNext() { return current < vector.size(); } @Override public Vector next() { int binSize = bin; if (remainders < remainder) { remainders++; binSize += 1; } Vector.Builder builder = vector.newBuilder(); for (int i = 0; i < binSize; i++) { builder.add(vector, current++); } return builder.build(); } }; } @Override public int size() { return chunks; } }; }