Revision: 111
Author: christianhujer
Date: 2006-04-26 17:25:30 -0700 (Wed, 26 Apr 2006)
ViewCVS: http://svn.sourceforge.net/japi/?rev=111&view=rev
Log Message:
-----------
Added NotNullIterator and NotNullIterable.
Added Paths:
-----------
trunk/src/app/net/sf/japi/util/NotNullIterator.java
Added: trunk/src/app/net/sf/japi/util/NotNullIterator.java
===================================================================
--- trunk/src/app/net/sf/japi/util/NotNullIterator.java (rev 0)
+++ trunk/src/app/net/sf/japi/util/NotNullIterator.java 2006-04-27 00:25:30 UTC (rev 111)
@@ -0,0 +1,90 @@
+/*
+ * JAPI - (Yet another (hopefully) useful) Java API
+ *
+ * Copyright (C) 2006 Christian Hujer
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ */
+package net.sf.japi.util;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+/** Iterator / Iterable decorator that skips elements which are <code>null</code>.
+ * @author <a href="mailto:ch...@it...">Christian Hujer</a>
+ */
+public class NotNullIterator<T> implements Iterator<T> {
+
+ /** The iterator to use. */
+ private final Iterator<T> iterator;
+
+ /** Prefetched next element. */
+ private T next;
+
+ /** Create a NotNullIterator over another Iterator.
+ * @param iterator
+ */
+ public NotNullIterator(final Iterator<T> iterator) {
+ this.iterator = iterator;
+ prefetchNext();
+ }
+
+ /** Prefetches the next element. */
+ private void prefetchNext() {
+ //noinspection NestedAssignment,ControlFlowStatementWithoutBraces,StatementWithEmptyBody
+ while (iterator.hasNext() && (next = iterator.next()) == null);
+ }
+
+ /** {@inheritDoc} */
+ public boolean hasNext() {
+ return next != null;
+ }
+
+ /** {@inheritDoc} */
+ public T next() {
+ if (next == null) {
+ throw new NoSuchElementException();
+ }
+ try {
+ return next;
+ } finally {
+ prefetchNext();
+ }
+ }
+
+ /** {@inheritDoc} */
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /** An Iterable adapter for NotNullIterator. */
+ public static class NotNullIterable<T> implements Iterable<T> {
+
+ /** The iterable to iterate over. */
+ private final Iterable<T> iterable;
+
+ public NotNullIterable(final Iterable<T> iterable) {
+ this.iterable = iterable;
+ }
+
+ /** {@inheritDoc} */
+ public Iterator<T> iterator() {
+ return new NotNullIterator<T>(iterable.iterator());
+ }
+
+ } // class NotNullIterable
+
+} // class NotNullIterator
Property changes on: trunk/src/app/net/sf/japi/util/NotNullIterator.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|