#653 - Use Java 8 Stream API.
This commit is contained in:
committed by
Oliver Gierke
parent
7a5b7b1c9f
commit
4c7227e274
@@ -20,8 +20,10 @@ import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -35,7 +37,7 @@ public class Links implements Iterable<Link> {
|
||||
|
||||
private static final Pattern LINK_HEADER_PATTERN = Pattern.compile("(<[^>]*>(;\\w+=\"[^\"]*\")+)");
|
||||
|
||||
static final Links NO_LINKS = new Links(Collections.<Link> emptyList());
|
||||
static final Links NO_LINKS = new Links(Collections.emptyList());
|
||||
|
||||
private final List<Link> links;
|
||||
|
||||
@@ -45,7 +47,7 @@ public class Links implements Iterable<Link> {
|
||||
* @param links
|
||||
*/
|
||||
public Links(List<Link> links) {
|
||||
this.links = links == null ? Collections.<Link> emptyList() : Collections.unmodifiableList(links);
|
||||
this.links = links == null ? Collections.emptyList() : Collections.unmodifiableList(links);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,17 +63,13 @@ public class Links implements Iterable<Link> {
|
||||
* Returns the {@link Link} with the given rel.
|
||||
*
|
||||
* @param rel the relation type to lookup a link for.
|
||||
* @return the {@link Link} with the given rel or {@literal null} if none found.
|
||||
* @return the link with the given rel or {@literal Optional#empty()} if none found.
|
||||
*/
|
||||
public Link getLink(String rel) {
|
||||
public Optional<Link> getLink(String rel) {
|
||||
|
||||
for (Link link : links) {
|
||||
if (link.getRel().equals(rel)) {
|
||||
return link;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return links.stream()
|
||||
.filter(link -> link.getRel().equals(rel))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,15 +79,9 @@ public class Links implements Iterable<Link> {
|
||||
*/
|
||||
public List<Link> getLinks(String rel) {
|
||||
|
||||
List<Link> result = new ArrayList<Link>();
|
||||
|
||||
for (Link link : links) {
|
||||
if (link.getRel().endsWith(rel)) {
|
||||
result.add(link);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return links.stream()
|
||||
.filter(link -> link.getRel().endsWith(rel))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -99,7 +91,7 @@ public class Links implements Iterable<Link> {
|
||||
* @return
|
||||
*/
|
||||
public boolean hasLink(String rel) {
|
||||
return getLink(rel) != null;
|
||||
return getLink(rel).isPresent();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -115,7 +107,7 @@ public class Links implements Iterable<Link> {
|
||||
}
|
||||
|
||||
Matcher matcher = LINK_HEADER_PATTERN.matcher(source);
|
||||
List<Link> links = new ArrayList<Link>();
|
||||
List<Link> links = new ArrayList<>();
|
||||
|
||||
while (matcher.find()) {
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -18,6 +18,7 @@ package org.springframework.hateoas;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
|
||||
@@ -36,7 +37,7 @@ public class ResourceSupport implements Identifiable<Link> {
|
||||
private final List<Link> links;
|
||||
|
||||
public ResourceSupport() {
|
||||
this.links = new ArrayList<Link>();
|
||||
this.links = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,9 +65,7 @@ public class ResourceSupport implements Identifiable<Link> {
|
||||
*/
|
||||
public void add(Iterable<Link> links) {
|
||||
Assert.notNull(links, "Given links must not be null!");
|
||||
for (Link candidate : links) {
|
||||
add(candidate);
|
||||
}
|
||||
links.forEach(this::add);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,13 +123,9 @@ public class ResourceSupport implements Identifiable<Link> {
|
||||
*/
|
||||
public Link getLink(String rel) {
|
||||
|
||||
for (Link link : links) {
|
||||
if (link.getRel().equals(rel)) {
|
||||
return link;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
return getLinks(rel).stream()
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,15 +135,9 @@ public class ResourceSupport implements Identifiable<Link> {
|
||||
*/
|
||||
public List<Link> getLinks(String rel) {
|
||||
|
||||
List<Link> relatedLinks = new ArrayList<Link>();
|
||||
|
||||
for (Link link : links) {
|
||||
if (link.getRel().equals(rel)) {
|
||||
relatedLinks.add(link);
|
||||
}
|
||||
}
|
||||
|
||||
return relatedLinks;
|
||||
return links.stream()
|
||||
.filter(link -> link.getRel().equals(rel))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -184,13 +184,10 @@ public final class TemplateVariable implements Serializable {
|
||||
*/
|
||||
public static TemplateVariable.VariableType from(String key) {
|
||||
|
||||
for (TemplateVariable.VariableType type : values()) {
|
||||
if (type.key.equals(key)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
throw new IllegalArgumentException("Unsupported variable type " + key + "!");
|
||||
return Arrays.stream(values())
|
||||
.filter(type -> type.key.equals(key))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new IllegalArgumentException("Unsupported variable type " + key + "!"));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -26,6 +26,7 @@ import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.hateoas.TemplateVariable.VariableType;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -81,14 +82,14 @@ public final class TemplateVariables implements Iterable<TemplateVariable>, Seri
|
||||
*/
|
||||
public TemplateVariables concat(Collection<TemplateVariable> variables) {
|
||||
|
||||
List<TemplateVariable> result = new ArrayList<TemplateVariable>(this.variables.size() + variables.size());
|
||||
List<TemplateVariable> result = new ArrayList<>(this.variables.size() + variables.size());
|
||||
result.addAll(this.variables);
|
||||
|
||||
for (TemplateVariable variable : variables) {
|
||||
if (!containsEquivalentFor(variable)) {
|
||||
result.add(variable);
|
||||
}
|
||||
}
|
||||
List<TemplateVariable> filtered = variables.stream()
|
||||
.filter(variable -> !containsEquivalentFor(variable))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
result.addAll(filtered);
|
||||
|
||||
return new TemplateVariables(result);
|
||||
}
|
||||
@@ -114,13 +115,8 @@ public final class TemplateVariables implements Iterable<TemplateVariable>, Seri
|
||||
|
||||
private boolean containsEquivalentFor(TemplateVariable candidate) {
|
||||
|
||||
for (TemplateVariable variable : this.variables) {
|
||||
if (variable.isEquivalent(candidate)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return this.variables.stream()
|
||||
.anyMatch(variable -> variable.isEquivalent(candidate));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2015 the original author or authors.
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,6 +24,7 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.hateoas.TemplateVariable.VariableType;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -57,7 +58,7 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
|
||||
|
||||
Matcher matcher = VARIABLE_REGEX.matcher(template);
|
||||
int baseUriEndIndex = template.length();
|
||||
List<TemplateVariable> variables = new ArrayList<TemplateVariable>();
|
||||
List<TemplateVariable> variables = new ArrayList<>();
|
||||
|
||||
while (matcher.find()) {
|
||||
|
||||
@@ -108,7 +109,7 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
|
||||
}
|
||||
|
||||
UriComponents components = UriComponentsBuilder.fromUriString(baseUri).build();
|
||||
List<TemplateVariable> result = new ArrayList<TemplateVariable>();
|
||||
List<TemplateVariable> result = new ArrayList<>();
|
||||
|
||||
for (TemplateVariable variable : variables) {
|
||||
|
||||
@@ -171,13 +172,9 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
|
||||
*/
|
||||
public List<String> getVariableNames() {
|
||||
|
||||
List<String> names = new ArrayList<String>();
|
||||
|
||||
for (TemplateVariable variable : variables) {
|
||||
names.add(variable.getName());
|
||||
}
|
||||
|
||||
return names;
|
||||
return variables.asList().stream()
|
||||
.map(TemplateVariable::getName)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -255,15 +252,9 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
|
||||
|
||||
private TemplateVariables getOptionalVariables() {
|
||||
|
||||
List<TemplateVariable> result = new ArrayList<TemplateVariable>();
|
||||
|
||||
for (TemplateVariable variable : this) {
|
||||
if (!variable.isRequired()) {
|
||||
result.add(variable);
|
||||
}
|
||||
}
|
||||
|
||||
return new TemplateVariables(result);
|
||||
return variables.asList().stream()
|
||||
.filter(variable -> !variable.isRequired())
|
||||
.collect(Collectors.collectingAndThen(Collectors.toList(), TemplateVariables::new));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2013-2015 the original author or authors.
|
||||
* Copyright 2013-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -26,6 +26,7 @@ import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.hateoas.Link;
|
||||
@@ -128,7 +129,7 @@ public class Traverson {
|
||||
|
||||
Assert.notNull(mediaTypes, "Media types must not be null!");
|
||||
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<HttpMessageConverter<?>>();
|
||||
List<HttpMessageConverter<?>> converters = new ArrayList<>();
|
||||
converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
|
||||
|
||||
List<MediaType> halFlavors = getHalJsonFlavors(mediaTypes);
|
||||
@@ -148,15 +149,9 @@ public class Traverson {
|
||||
*/
|
||||
private static List<MediaType> getHalJsonFlavors(Collection<MediaType> mediaTypes) {
|
||||
|
||||
List<MediaType> result = new ArrayList<MediaType>();
|
||||
|
||||
for (MediaType mediaType : mediaTypes) {
|
||||
if (MediaTypes.HAL_JSON.isCompatibleWith(mediaType)) {
|
||||
result.add(mediaType);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return mediaTypes.stream()
|
||||
.filter(MediaTypes.HAL_JSON::isCompatibleWith)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static final RestOperations createDefaultTemplate(List<MediaType> mediaTypes) {
|
||||
@@ -254,8 +249,8 @@ public class Traverson {
|
||||
*/
|
||||
public class TraversalBuilder {
|
||||
|
||||
private List<Hop> rels = new ArrayList<Hop>();
|
||||
private Map<String, Object> templateParameters = new HashMap<String, Object>();
|
||||
private List<Hop> rels = new ArrayList<>();
|
||||
private Map<String, Object> templateParameters = new HashMap<>();
|
||||
private HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
private TraversalBuilder() {}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -69,11 +69,9 @@ public class ControllerEntityLinks extends AbstractEntityLinks {
|
||||
Assert.notNull(linkBuilderFactory, "LinkBuilderFactory must not be null!");
|
||||
|
||||
this.linkBuilderFactory = linkBuilderFactory;
|
||||
this.entityToController = new HashMap<Class<?>, Class<?>>();
|
||||
this.entityToController = new HashMap<>();
|
||||
|
||||
for (Class<?> controllerType : controllerTypes) {
|
||||
registerControllerClass(controllerType);
|
||||
}
|
||||
controllerTypes.forEach(this::registerControllerClass);
|
||||
}
|
||||
|
||||
private void registerControllerClass(Class<?> controllerType) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -25,6 +25,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.LinkDiscoverer;
|
||||
@@ -88,7 +89,7 @@ public class JsonPathLinkDiscoverer implements LinkDiscoverer {
|
||||
|
||||
this.pathTemplate = pathTemplate;
|
||||
|
||||
List<MediaType> mediaTypes = new ArrayList<MediaType>(others.length + 1);
|
||||
List<MediaType> mediaTypes = new ArrayList<>(others.length + 1);
|
||||
mediaTypes.add(mediaType);
|
||||
mediaTypes.addAll(Arrays.asList(others));
|
||||
|
||||
@@ -168,17 +169,14 @@ public class JsonPathLinkDiscoverer implements LinkDiscoverer {
|
||||
|
||||
if (parseResult instanceof JSONArray) {
|
||||
|
||||
List<Link> links = new ArrayList<Link>();
|
||||
JSONArray array = (JSONArray) parseResult;
|
||||
|
||||
for (Object element : array) {
|
||||
links.add(new Link(element.toString(), rel));
|
||||
}
|
||||
|
||||
return Collections.unmodifiableList(links);
|
||||
return array.stream()
|
||||
.map(element -> new Link(element.toString(), rel))
|
||||
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableList));
|
||||
}
|
||||
|
||||
return Collections.unmodifiableList(Arrays.asList(new Link(parseResult.toString(), rel)));
|
||||
return Collections.unmodifiableList(Collections.singletonList(new Link(parseResult.toString(), rel)));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -188,12 +186,7 @@ public class JsonPathLinkDiscoverer implements LinkDiscoverer {
|
||||
@Override
|
||||
public boolean supports(MediaType delimiter) {
|
||||
|
||||
for (MediaType mediaType : this.mediaTypes) {
|
||||
if (mediaType.isCompatibleWith(delimiter)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return this.mediaTypes.stream()
|
||||
.anyMatch(mediaType -> mediaType.isCompatibleWith(delimiter));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,6 @@ import lombok.Getter;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@@ -99,11 +98,9 @@ public class DefaultCurieProvider implements CurieProvider {
|
||||
@Override
|
||||
public Collection<? extends Object> getCurieInformation(Links links) {
|
||||
|
||||
List<Curie> result = curies.entrySet().stream() //
|
||||
return curies.entrySet().stream() //
|
||||
.map(it -> new Curie(it.getKey(), getCurieHref(it.getKey(), it.getValue()))) //
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return Collections.unmodifiableCollection(result);
|
||||
.collect(Collectors.collectingAndThen(Collectors.toList(), Collections::unmodifiableCollection));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -914,13 +914,8 @@ public class Jackson2HalModule extends SimpleModule {
|
||||
*/
|
||||
public boolean hasCuriedEmbed(Iterable<?> source) {
|
||||
|
||||
for (String rel : map(source).keySet()) {
|
||||
if (rel.contains(":")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
return map(source).keySet().stream()
|
||||
.anyMatch(rel -> rel.contains(":"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014 the original author or authors.
|
||||
* Copyright 2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,9 +15,10 @@
|
||||
*/
|
||||
package org.springframework.hateoas.mvc;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -31,7 +32,7 @@ import org.springframework.util.StringUtils;
|
||||
class ForwardedHeader {
|
||||
|
||||
public static String NAME = "Forwarded";
|
||||
private static final ForwardedHeader NO_HEADER = new ForwardedHeader(Collections.<String, String> emptyMap());
|
||||
private static final ForwardedHeader NO_HEADER = new ForwardedHeader(Collections.emptyMap());
|
||||
|
||||
private final Map<String, String> elements;
|
||||
|
||||
@@ -51,20 +52,11 @@ class ForwardedHeader {
|
||||
return NO_HEADER;
|
||||
}
|
||||
|
||||
Map<String, String> elements = new HashMap<String, String>();
|
||||
Map<String, String> elements = Arrays.stream(source.split(";"))
|
||||
.map(part -> part.split("="))
|
||||
.filter(keyValue -> keyValue.length == 2)
|
||||
.collect(Collectors.toMap((it) -> it[0].trim(), (it) -> it[1].trim()));
|
||||
|
||||
for (String part : source.split(";")) {
|
||||
|
||||
String[] keyValue = part.split("=");
|
||||
|
||||
if (keyValue.length != 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
elements.put(keyValue[0].trim(), keyValue[1].trim());
|
||||
}
|
||||
|
||||
Assert.notNull(elements, "Forwarded elements must not be null!");
|
||||
Assert.isTrue(!elements.isEmpty(), "At least one forwarded element needs to be present!");
|
||||
|
||||
return new ForwardedHeader(elements);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2013 the original author or authors.
|
||||
* Copyright 2012-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,9 +17,7 @@ package org.springframework.hateoas.mvc;
|
||||
|
||||
import static org.springframework.hateoas.mvc.ControllerLinkBuilder.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
import org.springframework.hateoas.ResourceAssembler;
|
||||
@@ -82,12 +80,8 @@ public abstract class IdentifiableResourceAssemblerSupport<T extends Identifiabl
|
||||
*/
|
||||
private Object[] unwrapIdentifyables(Object[] values) {
|
||||
|
||||
List<Object> result = new ArrayList<Object>(values.length);
|
||||
|
||||
for (Object element : Arrays.asList(values)) {
|
||||
result.add(element instanceof Identifiable ? ((Identifiable<?>) element).getId() : element);
|
||||
}
|
||||
|
||||
return result.toArray();
|
||||
return Arrays.stream(values)
|
||||
.map(element -> element instanceof Identifiable ? ((Identifiable<?>) element).getId() : element)
|
||||
.toArray();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user