Skip to content

Commit

Permalink
gh-266: Moved out all Iterable classes into iterable module
Browse files Browse the repository at this point in the history
  • Loading branch information
t92549 committed May 6, 2022
1 parent 2e4bcf0 commit 8d47243
Show file tree
Hide file tree
Showing 9 changed files with 397 additions and 253 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package uk.gov.gchq.koryphe.commonutil.iterable;
package uk.gov.gchq.koryphe.iterable;

import uk.gov.gchq.koryphe.util.CloseableUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.gov.gchq.koryphe.commonutil.iterable;
package uk.gov.gchq.koryphe.iterable;

import uk.gov.gchq.koryphe.util.CloseableUtil;

Expand Down
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);
}
}
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);
}
}
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);
}
}
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();
}
}
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);
}
}
Loading

0 comments on commit 8d47243

Please sign in to comment.