diff options
author | Eike Stepper | 2012-10-05 11:42:52 +0000 |
---|---|---|
committer | Eike Stepper | 2012-10-05 11:42:52 +0000 |
commit | bd3e3935ef223f341b10072f13427d5e124ff760 (patch) | |
tree | 5c9b70c19df185c6f70e4e8a07add966f2e373e9 /plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection | |
parent | 9699c005e9832e03bf0d150f881a72df71b69880 (diff) | |
download | cdo-bd3e3935ef223f341b10072f13427d5e124ff760.tar.gz cdo-bd3e3935ef223f341b10072f13427d5e124ff760.tar.xz cdo-bd3e3935ef223f341b10072f13427d5e124ff760.zip |
[256624] [UI] Create a Commit History ViewPart drops/I20121005-0745
https://bugs.eclipse.org/bugs/show_bug.cgi?id=256624
Diffstat (limited to 'plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection')
7 files changed, 290 insertions, 7 deletions
diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractFilteredIterator.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractFilteredIterator.java new file mode 100644 index 0000000000..5b99bee3a8 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractFilteredIterator.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.util.collection; + +import java.util.Iterator; + +/** + * An abstract base class for custom iterators that {@link #isValid(Object) filter} the elements of a delegate iterator. + * + * @author Eike Stepper + * @since 3.3 + */ +public abstract class AbstractFilteredIterator<T> extends AbstractIterator<T> +{ + private Iterator<T> delegate; + + public AbstractFilteredIterator(Iterator<T> delegate) + { + this.delegate = delegate; + } + + @Override + protected Object computeNextElement() + { + while (delegate.hasNext()) + { + T element = delegate.next(); + if (isValid(element)) + { + return element; + } + } + + return END_OF_DATA; + } + + protected abstract boolean isValid(T element); +} diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractIterator.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractIterator.java index a503071784..a4bea4ee9e 100644 --- a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractIterator.java +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractIterator.java @@ -14,9 +14,8 @@ import java.util.Iterator; import java.util.NoSuchElementException; /** - * An abstract base class for custom iterators that only requires to implement a single {@link #computeNextElement()} - * method. - * + * An abstract base class for custom iterators that only requires to implement a single {@link #computeNextElement()} method. + * * @author Eike Stepper * @since 3.2 */ @@ -27,7 +26,7 @@ public abstract class AbstractIterator<T> implements Iterator<T> */ protected static final Object END_OF_DATA = new Object(); - private boolean computed; + private boolean nextComputed; private T next; @@ -37,13 +36,13 @@ public abstract class AbstractIterator<T> implements Iterator<T> public final boolean hasNext() { - if (computed) + if (nextComputed) { return true; } Object object = computeNextElement(); - computed = true; + nextComputed = true; if (object == END_OF_DATA) { @@ -63,7 +62,7 @@ public abstract class AbstractIterator<T> implements Iterator<T> throw new NoSuchElementException(); } - computed = false; + nextComputed = false; return next; } diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractListIterator.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractListIterator.java new file mode 100644 index 0000000000..7e031fdc2d --- /dev/null +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/AbstractListIterator.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.util.collection; + +import java.util.NoSuchElementException; + +/** + * An abstract base class for custom list iterators that only requires to implement the {@link #computeNextElement()} and {@link #computePreviousElement()} methods. + * + * @author Eike Stepper + * @since 3.3 + */ +public abstract class AbstractListIterator<T> extends AbstractIterator<T> +{ + private boolean previousComputed; + + private T previous; + + public AbstractListIterator() + { + } + + public final boolean hasPrevious() + { + if (previousComputed) + { + return true; + } + + Object object = computeNextElement(); + previousComputed = true; + + if (object == END_OF_DATA) + { + return false; + } + + @SuppressWarnings("unchecked") + T cast = (T)object; + previous = cast; + return true; + } + + public final T previous() + { + if (!hasPrevious()) + { + throw new NoSuchElementException(); + } + + previousComputed = false; + return previous; + } + + /** + * Returns the previous iteration element, or {@link #END_OF_DATA} if the start of the iteration has been reached. + */ + protected abstract Object computePreviousElement(); +} diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/BidirectionalIterator.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/BidirectionalIterator.java new file mode 100644 index 0000000000..713ab8a58e --- /dev/null +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/BidirectionalIterator.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.util.collection; + +import java.util.Iterator; +import java.util.ListIterator; + +/** + * @author Eike Stepper + * @since 3.3 + */ +public class BidirectionalIterator<T> implements Iterator<T> +{ + private final ListIterator<T> delegate; + + private boolean backward; + + public BidirectionalIterator(ListIterator<T> delegate, boolean backward) + { + this.delegate = delegate; + this.backward = backward; + } + + public BidirectionalIterator(ListIterator<T> delegate) + { + this(delegate, false); + } + + public ListIterator<T> getDelegate() + { + return delegate; + } + + public boolean isBackward() + { + return backward; + } + + public void setBackward(boolean backward) + { + this.backward = backward; + } + + public boolean hasNext() + { + return backward ? delegate.hasPrevious() : delegate.hasNext(); + } + + public T next() + { + return backward ? delegate.previous() : delegate.next(); + } + + public void remove() + { + delegate.remove(); + } +} diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/LimitedIterator.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/LimitedIterator.java new file mode 100644 index 0000000000..44f72628e9 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/LimitedIterator.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.util.collection; + +import java.util.Iterator; + +/** + * @author Eike Stepper + * @since 3.3 + */ +public class LimitedIterator<T> extends AbstractFilteredIterator<T> +{ + private final int limit; + + private int count; + + public LimitedIterator(Iterator<T> delegate, int limit) + { + super(delegate); + this.limit = limit; + } + + public final int getLimit() + { + return limit; + } + + public final int getCount() + { + return count; + } + + @Override + protected boolean isValid(T element) + { + return ++count <= limit; + } +} diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/Predicate.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/Predicate.java new file mode 100644 index 0000000000..bd6d4de9e2 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/Predicate.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.util.collection; + +/** + * @author Eike Stepper + * @since 3.3 + */ +public interface Predicate<T> +{ + public boolean apply(T element); +} diff --git a/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/PredicateIterator.java b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/PredicateIterator.java new file mode 100644 index 0000000000..2a4b1a90d7 --- /dev/null +++ b/plugins/org.eclipse.net4j.util/src/org/eclipse/net4j/util/collection/PredicateIterator.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eike Stepper - initial API and implementation + */ +package org.eclipse.net4j.util.collection; + +import java.util.Iterator; + +/** + * @author Eike Stepper + * @since 3.3 + */ +public class PredicateIterator<T> extends AbstractFilteredIterator<T> +{ + private final Predicate<? super T> predicate; + + public PredicateIterator(Iterator<T> delegate, Predicate<? super T> predicate) + { + super(delegate); + this.predicate = predicate; + } + + public Predicate<? super T> getPredicate() + { + return predicate; + } + + @Override + protected boolean isValid(T element) + { + return predicate.apply(element); + } +} |