com.google.common.collect.ForwardingIterator Java Examples

The following examples show how to use com.google.common.collect.ForwardingIterator. 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 check out the related API usage on the sidebar.
Example #1
Source File: EliminationStack.java    From multiway-pool with 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 File: ProjectUriResourceMap.java    From n4js with 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);
				}
			};
		}
	};
}