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.OWLObjectPropertyAssertionAxiom; import java.net.URI; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Set; /** * @author Yuan-Fang Li * @version $Id$ */ public class ObjectFixedClosableIterator<T extends OWLObjectPropertyAssertionAxiom> extends ImmutableIterator<T> implements CloseableIterator<T>, ResettableIterator<T> { protected Set<T> axiomSet; protected URI obj; protected Iterator<T> iterator; protected T currentAxiom; public ObjectFixedClosableIterator(Set<T> axiomSet, URI obj) { this.axiomSet = axiomSet; this.obj = obj; this.iterator = axiomSet.iterator(); this.currentAxiom = null; getNextMatch(); } private void getNextMatch() { while (iterator.hasNext()) { final T axiom = iterator.next(); final URI currentURI = axiom.getObject().getURI(); if (currentURI.equals(obj)) { currentAxiom = axiom; return; } } currentAxiom = null; } @Override public void reset() { iterator = axiomSet.iterator(); } @Override public void close() { if (axiomSet instanceof UnmodifiableSet) { axiomSet.clear(); } } @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."); } } }