Java Code Examples for com.google.common.collect.ForwardingIterator

The following examples show how to use com.google.common.collect.ForwardingIterator. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: multiway-pool   Source File: EliminationStack.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<E> iterator() {
  final class ReadOnlyIterator extends AbstractIterator<E> {
    Node<E> current = top.get();

    @Override
    protected E computeNext() {
      for (;;) {
        if (current == null) {
          return endOfData();
        }
        E e = current.get();
        current = current.next;
        if (e != null) {
          return e;
        }
      }
    }
  };
  return new ForwardingIterator<E>() {
    final ReadOnlyIterator delegate = new ReadOnlyIterator();

    @Override
    public void remove() {
      checkState(delegate.current != null);
      delegate.current.lazySet(null);
    }

    @Override
    protected Iterator<E> delegate() {
      return delegate;
    }
  };
}
 
Example 2
Source Project: n4js   Source File: ProjectUriResourceMap.java    License: Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Collection<Resource> values() {
	if (localContents.isEmpty()) {
		return Collections.emptyList();
	}
	// Nasty ... we have to support values().iterator().remove()
	Collection<Resource> base = super.values();
	Collection<Resource> delegate = Collections2.filter(base,
			resource -> localContents.contains(resource.getURI()));
	return new ForwardingCollection<>() {

		@Override
		protected Collection<Resource> delegate() {
			return delegate;
		}

		@Override
		public Iterator<Resource> iterator() {
			Iterator<URI> delegateIterator = localContents.iterator();
			Iterator<Resource> resourceIterator = Iterators.transform(delegateIterator, globalMap::get);
			return new ForwardingIterator<>() {

				private URI prev = null;

				@Override
				protected Iterator<Resource> delegate() {
					return resourceIterator;
				}

				public Resource next() {
					Resource result = super.next();
					prev = result.getURI();
					return result;
				}

				public void remove() {
					super.remove();
					globalMap.remove(prev);
				}
			};
		}
	};
}