Skip to content
This repository has been archived by the owner on Jan 4, 2020. It is now read-only.

Commit

Permalink
added further iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
Torsten Krause committed Jul 11, 2016
1 parent 82a939c commit f7db2c5
Show file tree
Hide file tree
Showing 4 changed files with 315 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2015, 2016 Torsten Krause, Markenwerk GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package net.markenwerk.commons.iterators;

import net.markenwerk.commons.interfaces.Provider;

/**
* An {@link InfiniteIterator} is a {@link ProtectedIterator} that infinitely
* iterates, using a {@link Provider}, tom provide further elements.
*
* @param <Payload>
* The payload type.
* @author Torsten Krause (tk at markenwerk dot net)
* @since TODO
*/
public final class InfiniteIterator<Payload> implements ProtectedIterator<Payload> {

private final Provider<? extends Payload> provider;

/**
* Creates a new {@link InfiniteIterator} that iterates over the given
* {@link Provider}.
*
* @param provider
* The {@link Provider} to be used.
*
* @throws IllegalArgumentException
* If the given {@link Provider} is {@literal null}.
*/
public InfiniteIterator(Provider<? extends Payload> provider) throws IllegalArgumentException {
if (null == provider) {
throw new IllegalArgumentException("provider is null");
}
this.provider = provider;
}

@Override
public boolean hasNext() {
return true;
}

@Override
public Payload next() {
return provider.create();
}

@Override
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Cannot remove from an InfiniteIterator");
}

}
81 changes: 81 additions & 0 deletions src/main/java/net/markenwerk/commons/iterators/PairIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2015, 2016 Torsten Krause, Markenwerk GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package net.markenwerk.commons.iterators;

import java.util.NoSuchElementException;

import net.markenwerk.commons.datastructures.Pair;

/**
* An {@link PairIterator} is a {@link ProtectedIterator} that iterates over a
* given {@link Pair}.
*
* @param <Payload>
* The payload type.
* @author Torsten Krause (tk at markenwerk dot net)
* @since TODO
*/
public final class PairIterator<Payload> implements ProtectedIterator<Payload> {

private final Pair<? extends Payload> pair;

private int index = -1;

/**
* Creates a new {@link PairIterator} that iterates over the given {@link Pair}.
*
* @param pair
* The {@link Pair} to iterate over.
*
* @throws IllegalArgumentException
* If the given {@link Pair} is {@literal null}.
*/
public PairIterator(Pair<? extends Payload> pair) throws IllegalArgumentException {
if (null == pair) {
throw new IllegalArgumentException("pair is null");
}
this.pair = pair;
}

@Override
public boolean hasNext() {
return index < 1;
}

@Override
public Payload next() throws NoSuchElementException {
switch (++index) {
case 0:
return pair.getFirst();
case 1:
return pair.getSecond();
default:
throw new NoSuchElementException("PairIterator has no further element");
}
}

@Override
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Cannot remove from a PairIterator");
}

}
82 changes: 82 additions & 0 deletions src/main/java/net/markenwerk/commons/iterators/TripleIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2015, 2016 Torsten Krause, Markenwerk GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package net.markenwerk.commons.iterators;

import java.util.NoSuchElementException;

import net.markenwerk.commons.datastructures.Triple;

/**
* An {@link TripleIterator} is a {@link ProtectedIterator} that iterates over a
* given {@link Triple}.
*
* @param <Payload>
* The payload type.
* @author Torsten Krause (tk at markenwerk dot net)
* @since TODO
*/
public final class TripleIterator<Payload> implements ProtectedIterator<Payload> {

private final Triple<? extends Payload, ? extends Payload, ? extends Payload> triple;

private int index = -1;

/**
* Creates a new {@link TripleIterator} that iterates over the given {@link Triple}.
*
* @param triple
* The {@link Triple} to iterate over.
*
* @throws IllegalArgumentException
* If the given {@link Triple} is {@literal null}.
*/
public TripleIterator(Triple<? extends Payload, ? extends Payload, ? extends Payload> triple)
throws IllegalArgumentException {
if (null == triple) {
throw new IllegalArgumentException("triple is null");
}
this.triple = triple;
}

@Override
public boolean hasNext() {
return index < 1;
}

@Override
public Payload next() throws NoSuchElementException {
switch (++index) {
case 0:
return triple.getFirst();
case 1:
return triple.getSecond();
default:
throw new NoSuchElementException("TripleIterator has no further element");
}
}

@Override
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Cannot remove from a TripleIterator");
}

}
81 changes: 81 additions & 0 deletions src/main/java/net/markenwerk/commons/iterators/TupleIterator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2015, 2016 Torsten Krause, Markenwerk GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package net.markenwerk.commons.iterators;

import java.util.NoSuchElementException;

import net.markenwerk.commons.datastructures.Tuple;

/**
* An {@link TupleIterator} is a {@link ProtectedIterator} that iterates over a
* given {@link Tuple}.
*
* @param <Payload>
* The payload type.
* @author Torsten Krause (tk at markenwerk dot net)
* @since TODO
*/
public final class TupleIterator<Payload> implements ProtectedIterator<Payload> {

private final Tuple<? extends Payload, ? extends Payload> tuple;

private int index = -1;

/**
* Creates a new {@link TupleIterator} that iterates over the given {@link Tuple}.
*
* @param tuple
* The {@link Tuple} to iterate over.
*
* @throws IllegalArgumentException
* If the given {@link Tuple} is {@literal null}.
*/
public TupleIterator(Tuple<? extends Payload, ? extends Payload> tuple) throws IllegalArgumentException {
if (null == tuple) {
throw new IllegalArgumentException("tuple is null");
}
this.tuple = tuple;
}

@Override
public boolean hasNext() {
return index < 1;
}

@Override
public Payload next() throws NoSuchElementException {
switch (++index) {
case 0:
return tuple.getFirst();
case 1:
return tuple.getSecond();
default:
throw new NoSuchElementException("TupleIterator has no further element");
}
}

@Override
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException("Cannot remove from a TupleIterator");
}

}

0 comments on commit f7db2c5

Please sign in to comment.