Skip to content

Commit

Permalink
Release 1.3: adds more autoclosediterator implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
JervenBolleman committed Mar 27, 2024
1 parent f95c5d4 commit 901356a
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 8 deletions.
16 changes: 8 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.jervenbolleman</groupId>
<artifactId>handlegraph4j</artifactId>
<version>1.2</version>
<version>1.3</version>
<packaging>jar</packaging>
<licenses>
<license>
Expand All @@ -29,15 +29,15 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
<version>3.2.5</version>
<configuration>
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.12.1</version>
<configuration>
<release>17</release>
</configuration>
Expand Down Expand Up @@ -69,7 +69,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.6.0</version>
<version>3.6.3</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -82,7 +82,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>3.1.0</version>
<version>3.2.1</version>
<executions>
<execution>
<id>sign-artifacts</id>
Expand All @@ -103,19 +103,19 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.10.0</version>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.10.0</version>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static <T> AutoClosedIterator<T> of(T stat) {
return new AutoClosedIterator<>() {
private final T t = stat;
boolean consumed = false;

@Override
public void close() {
}
Expand All @@ -81,6 +82,71 @@ public T next() {
};
}

/**
* @param <T> type
* @param first first T to iterate over
* @param second second T to iterate over
* @return a new AutoClosedIterator of these two items.
*/
public static <T> AutoClosedIterator<T> of(final T first, final T second) {

return new AutoClosedIterator<>() {
int consumed = 0;

@Override
public void close() {
}

@Override
public boolean hasNext() {
return consumed < 2;
}

@Override
public T next() {
switch (consumed) {
case 0:
consumed++;
return first;
case 1:
consumed++;
return second;
default:
throw new NoSuchElementException();
}
}
};
}

/**
* @param <T> type
* @param t array T to iterate over
* @return a new AutoClosedIterator of the items in the array.
*/
public static <T> AutoClosedIterator<T> of(final T[] t) {

return new AutoClosedIterator<T>() {
int consumed = 0;

@Override
public void close() {
}

@Override
public boolean hasNext() {
return consumed < t.length;
}

@Override
public T next() {
if (consumed >= t.length) {
throw new NoSuchElementException();
}
return t[consumed++];
}
};
}

/**
*
* @param <T> type
Expand All @@ -89,6 +155,11 @@ public T next() {
* @return iterator of first+second in order
*/
public static <T> AutoClosedIterator<T> concat(AutoClosedIterator<T> first, AutoClosedIterator<T> second) {
if (!first.hasNext()) {
return second;
} else if (!second.hasNext()) {
return first;
}
return new AutoClosedIterator<>() {
private boolean useFirst = true;

Expand Down Expand Up @@ -211,6 +282,33 @@ public I next() {
};
}

/**
* Warning this might consume a lot of memory
*
* @param <I> type
* @param s iterable to convert
* @return a wrapped stream iterator.
*/
public static <I> AutoClosedIterator<I> from(Iterable<I> s) {
Iterator<I> i = s.iterator();
return new AutoClosedIterator<>() {
@Override
public void close() {

}

@Override
public boolean hasNext() {
return i.hasNext();
}

@Override
public I next() {
return i.next();
}
};
}

/**
*
* @param <O> output type
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.jervenbolleman.handlegraph4j.iterators;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -40,5 +41,32 @@ public void concatEmptyThenFullIterators() {
assertThrows(NoSuchElementException.class, () -> both.next());
}
}

@Test
public void ofTwoIterators() {
try (AutoClosedIterator<Object> both = AutoClosedIterator.of("First", "Second")) {
assertTrue(both.hasNext());
assertNotNull(both.next());
assertTrue(both.hasNext());
assertNotNull(both.next());
assertFalse(both.hasNext());
assertThrows(NoSuchElementException.class, () -> both.next());
}
}

@Test
public void ofThreeIterators() {
try (AutoClosedIterator<Object> both = AutoClosedIterator.of(new String[]{"First", "Second", "Third"})) {
assertTrue(both.hasNext());
assertNotNull(both.next());
assertTrue(both.hasNext());
var s= both.next();
assertEquals(s, "Second");
assertTrue(both.hasNext());
assertNotNull(both.next());
assertFalse(both.hasNext());
assertThrows(NoSuchElementException.class, () -> both.next());
}
}
}

0 comments on commit 901356a

Please sign in to comment.