Java tutorial
/* * Copyright (c) 2009 - 2010. School of Information Technology and Electrical * Engineering, The University of Queensland. This software is being developed * for the "Phenomics Ontoogy Driven Data Management Project (PODD)" project. * PODD is a National e-Research Architecture Taskforce (NeAT) project * co-funded by ANDS and ARCS. * * PODD 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 3 of the License, or * (at your option) any later version. * * PODD 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 PODD. If not, see <http://www.gnu.org/licenses/>. */ package podd.util.iterator; import info.aduna.collections.iterators.CloseableIterator; import org.apache.commons.collections.set.UnmodifiableSet; import org.semanticweb.owl.model.OWLAxiom; import org.semanticweb.owl.model.OWLObject; import java.net.URI; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Set; /** * @author Yuan-Fang Li * @version $Id$ */ public abstract class CloseableOWLAxiomIterator<T extends OWLAxiom> extends ImmutableIterator<T> implements CloseableIterator<T>, ResettableIterator<T> { protected final Set<T> axiomSet; protected final URI property; protected final OWLObject object; protected Iterator<T> iterator; protected T currentAxiom; public CloseableOWLAxiomIterator(Set<T> axiomSet, URI property, OWLObject object) { this.axiomSet = axiomSet; this.property = property; this.object = object; this.iterator = axiomSet.iterator(); currentAxiom = null; getNextMatch(); } private void getNextMatch() { while (iterator.hasNext()) { final T axiom = iterator.next(); final URI currentURI = getPropertyURI(axiom); if ((null == property && null == object) || (null == property && getObject(axiom).equals(object)) || (null == object && currentURI.equals(property)) || (currentURI.equals(property) && getObject(axiom).equals(object))) { currentAxiom = axiom; return; } } currentAxiom = null; } protected abstract URI getPropertyURI(T axiom); protected abstract OWLObject getObject(T axiom); @Override public boolean hasNext() { return (null != currentAxiom); } @Override public T next() { if (hasNext()) { T axiom = currentAxiom; getNextMatch(); return axiom; } else { throw new NoSuchElementException("No more element in the iterator."); } } @Override public void close() { if (axiomSet instanceof UnmodifiableSet) { axiomSet.clear(); } } @Override public void reset() { iterator = axiomSet.iterator(); } }