-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gh-266: Moved out all Iterable classes into iterable module
- Loading branch information
Showing
9 changed files
with
397 additions
and
253 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
core/src/main/java/uk/gov/gchq/koryphe/iterable/FilteredIterable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2022 Crown Copyright | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.gchq.koryphe.iterable; | ||
|
||
import uk.gov.gchq.koryphe.util.CloseableUtil; | ||
|
||
import java.io.Closeable; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* @param <T> the type of items in the iterator | ||
*/ | ||
public class FilteredIterable<T> implements Closeable, Iterable<T> { | ||
private final Iterable<T> iterable; | ||
private final List<Predicate> predicates; | ||
|
||
public FilteredIterable(final Iterable<T> iterable, final List<Predicate> predicates) { | ||
this.iterable = iterable; | ||
this.predicates = predicates; | ||
} | ||
|
||
@Override | ||
public Iterator<T> iterator() { | ||
return new FilteredIterator<>(iterable.iterator(), predicates); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
CloseableUtil.close(iterable); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
core/src/main/java/uk/gov/gchq/koryphe/iterable/FilteredIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2022 Crown Copyright | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package uk.gov.gchq.koryphe.iterable; | ||
|
||
import uk.gov.gchq.koryphe.impl.predicate.And; | ||
import uk.gov.gchq.koryphe.util.CloseableUtil; | ||
|
||
import java.io.Closeable; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.NoSuchElementException; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* @param <T> the type of items in the iterator | ||
*/ | ||
public class FilteredIterator<T> implements Closeable, Iterator<T> { | ||
private final Iterator<? extends T> iterator; | ||
private final And<T> andPredicate; | ||
|
||
public FilteredIterator(final Iterator<T> iterator, final List<Predicate> predicates) { | ||
this.iterator = iterator; | ||
this.andPredicate = new And<>(predicates); | ||
} | ||
|
||
private T nextElement; | ||
private Boolean hasNext; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (null == hasNext) { | ||
while (iterator.hasNext()) { | ||
final T possibleNext = iterator.next(); | ||
if (andPredicate.test(possibleNext)) { | ||
nextElement = possibleNext; | ||
hasNext = true; | ||
return true; | ||
} | ||
} | ||
hasNext = false; | ||
nextElement = null; | ||
} | ||
|
||
final boolean hasNextResult = Boolean.TRUE.equals(hasNext); | ||
if (!hasNextResult) { | ||
close(); | ||
} | ||
|
||
return hasNextResult; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
if ((null == hasNext) && (!hasNext())) { | ||
throw new NoSuchElementException("Reached the end of the iterator"); | ||
} | ||
|
||
final T elementToReturn = nextElement; | ||
nextElement = null; | ||
hasNext = null; | ||
|
||
return elementToReturn; | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
throw new UnsupportedOperationException("Cannot call remove on a " + getClass().getSimpleName()); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
CloseableUtil.close(iterator); | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
core/src/main/java/uk/gov/gchq/koryphe/iterable/LimitedIterable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright 2022 Crown Copyright | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.gchq.koryphe.iterable; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
|
||
import uk.gov.gchq.koryphe.util.CloseableUtil; | ||
|
||
import java.io.Closeable; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
|
||
import static uk.gov.gchq.koryphe.util.JavaUtils.requireNonNullElse; | ||
|
||
/** | ||
* @param <T> the type of items in the iterator | ||
*/ | ||
public final class LimitedIterable<T> implements Closeable, Iterable<T> { | ||
private final Iterable<T> iterable; | ||
private final int start; | ||
private final Integer end; | ||
private final Boolean truncate; | ||
|
||
public LimitedIterable(final Iterable<T> iterable, final int start, final Integer end, final boolean truncate) { | ||
if (null != end && start > end) { | ||
throw new IllegalArgumentException("The start pointer must be less than the end pointer."); | ||
} | ||
|
||
this.iterable = requireNonNullElse(iterable, Collections.emptyList()); | ||
|
||
this.start = start; | ||
this.end = end; | ||
this.truncate = truncate; | ||
} | ||
|
||
@JsonIgnore | ||
public int getStart() { | ||
return start; | ||
} | ||
|
||
@JsonIgnore | ||
public Integer getEnd() { | ||
return end; | ||
} | ||
|
||
@Override | ||
public void close() { | ||
CloseableUtil.close(iterable); | ||
} | ||
|
||
@Override | ||
public Iterator<T> iterator() { | ||
return new LimitedIterator<>(iterable.iterator(), start, end, truncate); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
core/src/main/java/uk/gov/gchq/koryphe/iterable/LimitedIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright 2022 Crown Copyright | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package uk.gov.gchq.koryphe.iterable; | ||
|
||
import uk.gov.gchq.koryphe.util.CloseableUtil; | ||
|
||
import java.io.Closeable; | ||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
import static uk.gov.gchq.koryphe.util.JavaUtils.requireNonNullElse; | ||
|
||
/** | ||
* @param <T> the type of items in the iterator | ||
*/ | ||
public final class LimitedIterator<T> implements Closeable, Iterator<T> { | ||
private final Iterator<T> iterator; | ||
private final Integer end; | ||
private int index = 0; | ||
private Boolean truncate = true; | ||
|
||
public LimitedIterator(final Iterator<T> iterator, final int start, final Integer end, final boolean truncate) { | ||
if (null != end && start > end) { | ||
throw new IllegalArgumentException("start should be less than end"); | ||
} | ||
|
||
this.iterator = requireNonNullElse(iterator, Collections.emptyIterator()); | ||
this.end = end; | ||
this.truncate = truncate; | ||
|
||
while (index < start && hasNext()) { | ||
next(); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() { | ||
CloseableUtil.close(iterator); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
final boolean withinLimit = (null == end || index < end); | ||
|
||
if (!withinLimit && !truncate && iterator.hasNext()) { | ||
// Throw an exception if we are - not within the limit, we don't want to truncate and there are items remaining. | ||
throw new NoSuchElementException("Limit of " + end + " exceeded."); | ||
} | ||
|
||
final boolean hasNext = withinLimit && iterator.hasNext(); | ||
if (!hasNext) { | ||
close(); | ||
} | ||
|
||
return hasNext; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
|
||
index++; | ||
return iterator.next(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
core/src/main/java/uk/gov/gchq/koryphe/iterable/MappedIterable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2022 Crown Copyright | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package uk.gov.gchq.koryphe.iterable; | ||
|
||
import uk.gov.gchq.koryphe.util.CloseableUtil; | ||
|
||
import java.io.Closeable; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* @param <I_ITEM> input type of items in the input iterator | ||
* @param <O_ITEM> output type of items in the output iterator | ||
*/ | ||
public class MappedIterable<I_ITEM, O_ITEM> implements Closeable, Iterable<O_ITEM> { | ||
private final Iterable<I_ITEM> iterable; | ||
private final List<Function> functions; | ||
|
||
public MappedIterable(final Iterable<I_ITEM> iterable, final List<Function> functions) { | ||
this.iterable = iterable; | ||
this.functions = functions; | ||
} | ||
|
||
@Override | ||
public Iterator<O_ITEM> iterator() { | ||
return new MappedIterator<>(iterable.iterator(), functions); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
CloseableUtil.close(iterable); | ||
} | ||
} |
Oops, something went wrong.