mirror of https://github.com/shred/acme4j
Use functional interface for ResourceIterator
parent
c1b677f310
commit
eedc2ae68c
|
@ -125,12 +125,7 @@ public class Registration extends AcmeResource {
|
|||
public Iterator<Authorization> getAuthorizations() throws AcmeException {
|
||||
LOG.debug("getAuthorizations");
|
||||
load();
|
||||
return new ResourceIterator<Authorization>(getSession(), KEY_AUTHORIZATIONS, authorizations) {
|
||||
@Override
|
||||
protected Authorization create(Session session, URI uri) {
|
||||
return Authorization.bind(session, uri);
|
||||
}
|
||||
};
|
||||
return new ResourceIterator<>(getSession(), KEY_AUTHORIZATIONS, authorizations, Authorization::bind);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -147,12 +142,7 @@ public class Registration extends AcmeResource {
|
|||
public Iterator<Certificate> getCertificates() throws AcmeException {
|
||||
LOG.debug("getCertificates");
|
||||
load();
|
||||
return new ResourceIterator<Certificate>(getSession(), KEY_CERTIFICATES, certificates) {
|
||||
@Override
|
||||
protected Certificate create(Session session, URI uri) {
|
||||
return Certificate.bind(session, uri);
|
||||
}
|
||||
};
|
||||
return new ResourceIterator<>(getSession(), KEY_CERTIFICATES, certificates, Certificate::bind);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -20,6 +20,7 @@ import java.util.Deque;
|
|||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
import org.shredzone.acme4j.AcmeResource;
|
||||
import org.shredzone.acme4j.Session;
|
||||
|
@ -34,11 +35,12 @@ import org.shredzone.acme4j.util.JSON;
|
|||
* @param <T>
|
||||
* {@link AcmeResource} type to iterate over
|
||||
*/
|
||||
public abstract class ResourceIterator<T extends AcmeResource> implements Iterator<T> {
|
||||
public class ResourceIterator<T extends AcmeResource> implements Iterator<T> {
|
||||
|
||||
private final Session session;
|
||||
private final String field;
|
||||
private final Deque<URI> uriList = new ArrayDeque<>();
|
||||
private final BiFunction<Session, URI, T> creator;
|
||||
private boolean eol = false;
|
||||
private URI nextUri;
|
||||
|
||||
|
@ -51,11 +53,15 @@ public abstract class ResourceIterator<T extends AcmeResource> implements Iterat
|
|||
* Field name to be used in the JSON response
|
||||
* @param start
|
||||
* URI of the first JSON array, may be {@code null} for an empty iterator
|
||||
* @param creator
|
||||
* Creator for an {@link AcmeResource} that is bound to the given
|
||||
* {@link Session} and {@link URI}.
|
||||
*/
|
||||
public ResourceIterator(Session session, String field, URI start) {
|
||||
public ResourceIterator(Session session, String field, URI start, BiFunction<Session, URI, T> creator) {
|
||||
this.session = Objects.requireNonNull(session, "session");
|
||||
this.field = Objects.requireNonNull(field, "field");
|
||||
this.nextUri = start;
|
||||
this.creator = Objects.requireNonNull(creator, "creator");
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -101,7 +107,7 @@ public abstract class ResourceIterator<T extends AcmeResource> implements Iterat
|
|||
throw new NoSuchElementException("no more " + field);
|
||||
}
|
||||
|
||||
return create(session, next);
|
||||
return creator.apply(session, next);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -112,18 +118,6 @@ public abstract class ResourceIterator<T extends AcmeResource> implements Iterat
|
|||
throw new UnsupportedOperationException("cannot remove " + field);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link AcmeResource} object by binding it to the {@link Session} and
|
||||
* using the given {@link URI}.
|
||||
*
|
||||
* @param session
|
||||
* {@link Session} to bind the object to
|
||||
* @param uri
|
||||
* {@link URI} of the resource
|
||||
* @return Created object
|
||||
*/
|
||||
protected abstract T create(Session session, URI uri);
|
||||
|
||||
/**
|
||||
* Fetches the next batch of URIs. Handles exceptions. Does nothing if there is no
|
||||
* URI of the next batch.
|
||||
|
|
|
@ -31,8 +31,8 @@ import org.shredzone.acme4j.Authorization;
|
|||
import org.shredzone.acme4j.Session;
|
||||
import org.shredzone.acme4j.exception.AcmeException;
|
||||
import org.shredzone.acme4j.provider.TestableConnectionProvider;
|
||||
import org.shredzone.acme4j.util.JSONBuilder;
|
||||
import org.shredzone.acme4j.util.JSON;
|
||||
import org.shredzone.acme4j.util.JSONBuilder;
|
||||
|
||||
/**
|
||||
* Unit test for {@link ResourceIterator}.
|
||||
|
@ -167,12 +167,7 @@ public class ResourceIteratorTest {
|
|||
|
||||
provider.close();
|
||||
|
||||
return new ResourceIterator<Authorization>(session, TYPE, first) {
|
||||
@Override
|
||||
protected Authorization create(Session session, URI uri) {
|
||||
return Authorization.bind(session, uri);
|
||||
}
|
||||
};
|
||||
return new ResourceIterator<>(session, TYPE, first, Authorization::bind);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue