From 898c552f770592ad25ee0ab0350bcf509c2a466a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Richard=20K=C3=B6rber?= Date: Sat, 11 Feb 2017 11:57:06 +0100 Subject: [PATCH] Add stream method to JSON.Array --- .../org/shredzone/acme4j/Authorization.java | 47 ++++++++++--------- .../java/org/shredzone/acme4j/Metadata.java | 10 ++-- .../org/shredzone/acme4j/Registration.java | 6 +-- .../acme4j/connector/ResourceIterator.java | 5 +- .../java/org/shredzone/acme4j/util/JSON.java | 25 ++++++++++ .../org/shredzone/acme4j/util/JSONTest.java | 22 +++++++++ 6 files changed, 81 insertions(+), 34 deletions(-) diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java index 146990b7..74e95002 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Authorization.java @@ -13,6 +13,7 @@ */ package org.shredzone.acme4j; +import static java.util.stream.Collectors.toList; import static org.shredzone.acme4j.util.AcmeUtils.parseTimestamp; import java.net.HttpURLConnection; @@ -121,8 +122,7 @@ public class Authorization extends AcmeResource { */ @SuppressWarnings("unchecked") public T findChallenge(String type) { - Collection result = findCombination(type); - return !result.isEmpty() ? (T) result.iterator().next() : null; + return (T) findCombination(type).stream().findFirst().orElse(null); } /** @@ -253,15 +253,12 @@ public class Authorization extends AcmeResource { * @return List of {@link Challenge} */ private List fetchChallenges(JSON json) { - JSON.Array jsonChallenges = json.get("challenges").asArray(); - List cr = new ArrayList<>(); - for (JSON.Value c : jsonChallenges) { - Challenge ch = getSession().createChallenge(c.asObject()); - if (ch != null) { - cr.add(ch); - } - } - return cr; + Session session = getSession(); + + return Collections.unmodifiableList(json.get("challenges").asArray().stream() + .map(JSON.Value::asObject) + .map(session::createChallenge) + .collect(toList())); } /** @@ -279,16 +276,24 @@ public class Authorization extends AcmeResource { return Arrays.asList(challenges); } - List> cmb = new ArrayList<>(jsonCombinations.size()); - for (JSON.Value v : jsonCombinations) { - JSON.Array c = v.asArray(); - List clist = new ArrayList<>(c.size()); - for (JSON.Value n : c) { - clist.add(challenges.get(n.asInt())); - } - cmb.add(clist); - } - return cmb; + return Collections.unmodifiableList(jsonCombinations.stream() + .map(JSON.Value::asArray) + .map(this::findChallenges) + .collect(toList())); + } + + /** + * Converts an array of challenge indexes to a list of matching {@link Challenge}. + * + * @param combination + * {@link Array} of the challenge indexes + * @return List of matching {@link Challenge} + */ + private List findChallenges(JSON.Array combination) { + return combination.stream() + .mapToInt(JSON.Value::asInt) + .mapToObj(challenges::get) + .collect(toList()); } } diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Metadata.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Metadata.java index 0dd83e95..6448fb86 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Metadata.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Metadata.java @@ -13,11 +13,11 @@ */ package org.shredzone.acme4j; +import static java.util.stream.Collectors.toList; + import java.net.URI; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.List; import org.shredzone.acme4j.util.JSON; import org.shredzone.acme4j.util.JSON.Array; @@ -66,11 +66,7 @@ public class Metadata { return Collections.emptyList(); } - List result = new ArrayList<>(array.size()); - for (Value v : array) { - result.add(v.asString()); - } - return result; + return array.stream().map(Value::asString).collect(toList()); } /** diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/Registration.java b/acme4j-client/src/main/java/org/shredzone/acme4j/Registration.java index 60e18de9..e2547827 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/Registration.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/Registration.java @@ -352,9 +352,9 @@ public class Registration extends AcmeResource { if (json.contains(KEY_CONTACT)) { contacts.clear(); - for (JSON.Value v : json.get(KEY_CONTACT).asArray()) { - contacts.add(v.asURI()); - } + json.get(KEY_CONTACT).asArray().stream() + .map(JSON.Value::asURI) + .forEach(contacts::add); } this.authorizations = json.get(KEY_AUTHORIZATIONS).asURI(); diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/connector/ResourceIterator.java b/acme4j-client/src/main/java/org/shredzone/acme4j/connector/ResourceIterator.java index e5d25606..0ef74d32 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/connector/ResourceIterator.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/connector/ResourceIterator.java @@ -161,9 +161,8 @@ public class ResourceIterator implements Iterator { if (array == null) { return; } - for (JSON.Value v : array) { - uriList.add(v.asURI()); - } + + array.stream().map(JSON.Value::asURI).forEach(uriList::add); } } diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/util/JSON.java b/acme4j-client/src/main/java/org/shredzone/acme4j/util/JSON.java index 50cc2128..fcdb7f98 100644 --- a/acme4j-client/src/main/java/org/shredzone/acme4j/util/JSON.java +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/util/JSON.java @@ -34,7 +34,10 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; +import java.util.Objects; import java.util.Set; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; import org.jose4j.json.JsonUtil; import org.jose4j.lang.JoseException; @@ -214,6 +217,15 @@ public final class JSON implements Serializable { return new Value(path + '[' + index + ']', data.get(index)); } + /** + * Returns a stream of values. + * + * @return {@link Stream} of all {@link Value} of this array + */ + public Stream stream() { + return StreamSupport.stream(spliterator(), false); + } + /** * Creates a new {@link Iterator} that iterates over the array {@link Value}. */ @@ -364,6 +376,19 @@ public final class JSON implements Serializable { throw new AcmeProtocolException(path + ": bad date " + val, ex); } } + + @Override + public boolean equals(Object obj) { + if (obj == null || !(obj instanceof Value)) { + return false; + } + return Objects.equals(val, ((Value) obj).val); + } + + @Override + public int hashCode() { + return val != null ? val.hashCode() : 0; + } } /** diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/util/JSONTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/util/JSONTest.java index 697ea0b7..c828ae6c 100644 --- a/acme4j-client/src/test/java/org/shredzone/acme4j/util/JSONTest.java +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/util/JSONTest.java @@ -29,8 +29,11 @@ import java.net.URL; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import java.util.NoSuchElementException; +import java.util.stream.Collectors; import org.junit.Test; import org.shredzone.acme4j.exception.AcmeProtocolException; @@ -141,6 +144,25 @@ public class JSONTest { } } + /** + * Test the array stream. + */ + @Test + public void testArrayStream() { + JSON json = TestUtils.getJsonAsObject("json"); + JSON.Array array = json.get("array").asArray(); + + List streamValues = array.stream().collect(Collectors.toList()); + + List iteratorValues = new ArrayList<>(); + Iterator it = array.iterator(); + while (it.hasNext()) { + iteratorValues.add(it.next()); + } + + assertThat(streamValues, contains(iteratorValues.toArray())); + } + /** * Test all getters on existing values. */