Add stream method to JSON.Array

pull/30/head
Richard Körber 2017-02-11 11:57:06 +01:00
parent eedc2ae68c
commit 898c552f77
6 changed files with 81 additions and 34 deletions

View File

@ -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 extends Challenge> T findChallenge(String type) {
Collection<Challenge> 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<Challenge> fetchChallenges(JSON json) {
JSON.Array jsonChallenges = json.get("challenges").asArray();
List<Challenge> 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<List<Challenge>> cmb = new ArrayList<>(jsonCombinations.size());
for (JSON.Value v : jsonCombinations) {
JSON.Array c = v.asArray();
List<Challenge> clist = new ArrayList<>(c.size());
for (JSON.Value n : c) {
clist.add(challenges.get(n.asInt()));
return Collections.unmodifiableList(jsonCombinations.stream()
.map(JSON.Value::asArray)
.map(this::findChallenges)
.collect(toList()));
}
cmb.add(clist);
}
return cmb;
/**
* 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<Challenge> findChallenges(JSON.Array combination) {
return combination.stream()
.mapToInt(JSON.Value::asInt)
.mapToObj(challenges::get)
.collect(toList());
}
}

View File

@ -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<String> result = new ArrayList<>(array.size());
for (Value v : array) {
result.add(v.asString());
}
return result;
return array.stream().map(Value::asString).collect(toList());
}
/**

View File

@ -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();

View File

@ -161,9 +161,8 @@ public class ResourceIterator<T extends AcmeResource> implements Iterator<T> {
if (array == null) {
return;
}
for (JSON.Value v : array) {
uriList.add(v.asURI());
}
array.stream().map(JSON.Value::asURI).forEach(uriList::add);
}
}

View File

@ -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<Value> 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;
}
}
/**

View File

@ -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<JSON.Value> streamValues = array.stream().collect(Collectors.toList());
List<JSON.Value> iteratorValues = new ArrayList<>();
Iterator<JSON.Value> it = array.iterator();
while (it.hasNext()) {
iteratorValues.add(it.next());
}
assertThat(streamValues, contains(iteratorValues.toArray()));
}
/**
* Test all getters on existing values.
*/