2009-11-03 23:47:53 +01:00
|
|
|
// RotateIterator.java
|
2008-07-20 19:14:51 +02:00
|
|
|
// (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
|
2007-03-08 17:15:40 +01:00
|
|
|
// first published 08.03.2007 on http://www.anomic.de
|
|
|
|
//
|
|
|
|
// This is a part of YaCy, a peer-to-peer based web search engine
|
|
|
|
//
|
2011-03-08 02:51:51 +01:00
|
|
|
// $LastChangedDate$
|
2009-11-03 23:47:53 +01:00
|
|
|
// $LastChangedRevision$
|
|
|
|
// $LastChangedBy$
|
2007-03-08 17:15:40 +01:00
|
|
|
//
|
|
|
|
// LICENSE
|
2012-07-02 15:40:40 +02:00
|
|
|
//
|
2007-03-08 17:15:40 +01:00
|
|
|
// 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
|
|
|
|
|
2012-09-21 16:46:57 +02:00
|
|
|
package net.yacy.kelondro.util;
|
2007-03-08 17:15:40 +01:00
|
|
|
|
2011-12-16 23:59:29 +01:00
|
|
|
import net.yacy.cora.order.CloneableIterator;
|
|
|
|
|
2009-01-30 23:08:08 +01:00
|
|
|
|
2009-01-30 23:44:20 +01:00
|
|
|
public class RotateIterator<E> implements CloneableIterator<E> {
|
2012-07-02 15:40:40 +02:00
|
|
|
|
2012-10-06 03:34:52 +02:00
|
|
|
private CloneableIterator<E> a, clone;
|
2007-03-08 23:07:17 +01:00
|
|
|
Object modifier;
|
2012-10-06 03:34:52 +02:00
|
|
|
private boolean nempty;
|
2008-01-25 12:44:27 +01:00
|
|
|
int terminationCount;
|
2012-07-02 15:40:40 +02:00
|
|
|
|
2009-01-30 23:44:20 +01:00
|
|
|
public RotateIterator(final CloneableIterator<E> a, final Object modifier, final int terminationCount) {
|
2007-03-08 17:15:40 +01:00
|
|
|
// this works currently only for String-type key iterations
|
|
|
|
this.a = a;
|
2007-03-08 23:07:17 +01:00
|
|
|
this.modifier = modifier;
|
2008-01-25 12:44:27 +01:00
|
|
|
this.terminationCount = terminationCount;
|
2008-06-06 18:01:27 +02:00
|
|
|
this.clone = a.clone(modifier);
|
2008-01-24 23:49:00 +01:00
|
|
|
this.nempty = this.clone.hasNext();
|
2007-03-08 17:15:40 +01:00
|
|
|
}
|
2012-07-02 15:40:40 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public RotateIterator<E> clone(final Object modifier) {
|
|
|
|
return new RotateIterator<E>(this.a, modifier, this.terminationCount - 1);
|
2007-03-08 17:15:40 +01:00
|
|
|
}
|
2012-07-02 15:40:40 +02:00
|
|
|
|
|
|
|
@Override
|
2007-03-08 17:15:40 +01:00
|
|
|
public boolean hasNext() {
|
2012-07-02 15:40:40 +02:00
|
|
|
return (this.terminationCount > 0) && (this.nempty);
|
2007-03-08 17:15:40 +01:00
|
|
|
}
|
2012-07-02 15:40:40 +02:00
|
|
|
|
|
|
|
@Override
|
2007-12-27 18:56:59 +01:00
|
|
|
public E next() {
|
2007-04-13 15:40:19 +02:00
|
|
|
// attention: this iterator has no termination - on purpose.
|
|
|
|
// it must be taken care that a calling method has a termination predicate different
|
|
|
|
// from the hasNext() method
|
2012-07-02 15:40:40 +02:00
|
|
|
if (!(this.a.hasNext())) {
|
|
|
|
this.a = this.clone.clone(this.modifier);
|
|
|
|
assert this.a.hasNext();
|
2007-03-08 17:15:40 +01:00
|
|
|
}
|
2012-07-02 15:40:40 +02:00
|
|
|
this.terminationCount--;
|
|
|
|
return this.a.next();
|
2007-03-08 17:15:40 +01:00
|
|
|
}
|
2012-07-02 15:40:40 +02:00
|
|
|
|
|
|
|
@Override
|
2007-03-08 17:15:40 +01:00
|
|
|
public void remove() {
|
2012-07-02 15:40:40 +02:00
|
|
|
this.a.remove();
|
2007-03-08 17:15:40 +01:00
|
|
|
}
|
2012-07-02 15:40:40 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
this.a.close();
|
|
|
|
this.clone.close();
|
|
|
|
}
|
|
|
|
|
2007-03-08 17:15:40 +01:00
|
|
|
}
|