diff --git a/src/main/java/org/springframework/hateoas/Links.java b/src/main/java/org/springframework/hateoas/Links.java
index 89fdae93..2f717a5b 100644
--- a/src/main/java/org/springframework/hateoas/Links.java
+++ b/src/main/java/org/springframework/hateoas/Links.java
@@ -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 {
private static final Pattern LINK_HEADER_PATTERN = Pattern.compile("(<[^>]*>(;\\w+=\"[^\"]*\")+)");
- static final Links NO_LINKS = new Links(Collections. emptyList());
+ static final Links NO_LINKS = new Links(Collections.emptyList());
private final List links;
@@ -45,7 +47,7 @@ public class Links implements Iterable {
* @param links
*/
public Links(List links) {
- this.links = links == null ? Collections. emptyList() : Collections.unmodifiableList(links);
+ this.links = links == null ? Collections.emptyList() : Collections.unmodifiableList(links);
}
/**
@@ -61,17 +63,13 @@ public class Links implements Iterable {
* 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 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 {
*/
public List getLinks(String rel) {
- List result = new ArrayList();
-
- 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 {
* @return
*/
public boolean hasLink(String rel) {
- return getLink(rel) != null;
+ return getLink(rel).isPresent();
}
/**
@@ -115,7 +107,7 @@ public class Links implements Iterable {
}
Matcher matcher = LINK_HEADER_PATTERN.matcher(source);
- List links = new ArrayList();
+ List links = new ArrayList<>();
while (matcher.find()) {
diff --git a/src/main/java/org/springframework/hateoas/ResourceSupport.java b/src/main/java/org/springframework/hateoas/ResourceSupport.java
index 13007b76..ea5f477d 100755
--- a/src/main/java/org/springframework/hateoas/ResourceSupport.java
+++ b/src/main/java/org/springframework/hateoas/ResourceSupport.java
@@ -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 {
private final List links;
public ResourceSupport() {
- this.links = new ArrayList();
+ this.links = new ArrayList<>();
}
/**
@@ -64,9 +65,7 @@ public class ResourceSupport implements Identifiable {
*/
public void add(Iterable 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 {
*/
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 {
*/
public List getLinks(String rel) {
- List relatedLinks = new ArrayList();
-
- 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());
}
/*
diff --git a/src/main/java/org/springframework/hateoas/TemplateVariable.java b/src/main/java/org/springframework/hateoas/TemplateVariable.java
index 2a4ca6d1..71e30576 100644
--- a/src/main/java/org/springframework/hateoas/TemplateVariable.java
+++ b/src/main/java/org/springframework/hateoas/TemplateVariable.java
@@ -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 + "!"));
}
/*
diff --git a/src/main/java/org/springframework/hateoas/TemplateVariables.java b/src/main/java/org/springframework/hateoas/TemplateVariables.java
index 7daeb4f9..daad0bfe 100644
--- a/src/main/java/org/springframework/hateoas/TemplateVariables.java
+++ b/src/main/java/org/springframework/hateoas/TemplateVariables.java
@@ -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, Seri
*/
public TemplateVariables concat(Collection variables) {
- List result = new ArrayList(this.variables.size() + variables.size());
+ List result = new ArrayList<>(this.variables.size() + variables.size());
result.addAll(this.variables);
- for (TemplateVariable variable : variables) {
- if (!containsEquivalentFor(variable)) {
- result.add(variable);
- }
- }
+ List 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, 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));
}
/*
diff --git a/src/main/java/org/springframework/hateoas/UriTemplate.java b/src/main/java/org/springframework/hateoas/UriTemplate.java
index 5948e8af..05e0edd5 100644
--- a/src/main/java/org/springframework/hateoas/UriTemplate.java
+++ b/src/main/java/org/springframework/hateoas/UriTemplate.java
@@ -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, Serializable {
Matcher matcher = VARIABLE_REGEX.matcher(template);
int baseUriEndIndex = template.length();
- List variables = new ArrayList();
+ List variables = new ArrayList<>();
while (matcher.find()) {
@@ -108,7 +109,7 @@ public class UriTemplate implements Iterable, Serializable {
}
UriComponents components = UriComponentsBuilder.fromUriString(baseUri).build();
- List result = new ArrayList();
+ List result = new ArrayList<>();
for (TemplateVariable variable : variables) {
@@ -171,13 +172,9 @@ public class UriTemplate implements Iterable, Serializable {
*/
public List getVariableNames() {
- List names = new ArrayList();
-
- 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, Serializable {
private TemplateVariables getOptionalVariables() {
- List result = new ArrayList();
-
- 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));
}
/**
diff --git a/src/main/java/org/springframework/hateoas/client/Traverson.java b/src/main/java/org/springframework/hateoas/client/Traverson.java
index 1db7c704..ca902217 100644
--- a/src/main/java/org/springframework/hateoas/client/Traverson.java
+++ b/src/main/java/org/springframework/hateoas/client/Traverson.java
@@ -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> converters = new ArrayList>();
+ List> converters = new ArrayList<>();
converters.add(new StringHttpMessageConverter(Charset.forName("UTF-8")));
List halFlavors = getHalJsonFlavors(mediaTypes);
@@ -148,15 +149,9 @@ public class Traverson {
*/
private static List getHalJsonFlavors(Collection mediaTypes) {
- List result = new ArrayList();
-
- 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 mediaTypes) {
@@ -254,8 +249,8 @@ public class Traverson {
*/
public class TraversalBuilder {
- private List rels = new ArrayList();
- private Map templateParameters = new HashMap();
+ private List rels = new ArrayList<>();
+ private Map templateParameters = new HashMap<>();
private HttpHeaders headers = new HttpHeaders();
private TraversalBuilder() {}
diff --git a/src/main/java/org/springframework/hateoas/core/ControllerEntityLinks.java b/src/main/java/org/springframework/hateoas/core/ControllerEntityLinks.java
index d1966315..552f8676 100644
--- a/src/main/java/org/springframework/hateoas/core/ControllerEntityLinks.java
+++ b/src/main/java/org/springframework/hateoas/core/ControllerEntityLinks.java
@@ -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>>();
+ this.entityToController = new HashMap<>();
- for (Class> controllerType : controllerTypes) {
- registerControllerClass(controllerType);
- }
+ controllerTypes.forEach(this::registerControllerClass);
}
private void registerControllerClass(Class> controllerType) {
diff --git a/src/main/java/org/springframework/hateoas/core/JsonPathLinkDiscoverer.java b/src/main/java/org/springframework/hateoas/core/JsonPathLinkDiscoverer.java
index 9b95062a..224659a3 100644
--- a/src/main/java/org/springframework/hateoas/core/JsonPathLinkDiscoverer.java
+++ b/src/main/java/org/springframework/hateoas/core/JsonPathLinkDiscoverer.java
@@ -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 mediaTypes = new ArrayList(others.length + 1);
+ List 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 links = new ArrayList();
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));
}
}
diff --git a/src/main/java/org/springframework/hateoas/hal/DefaultCurieProvider.java b/src/main/java/org/springframework/hateoas/hal/DefaultCurieProvider.java
index 03248044..0d19a7b2 100644
--- a/src/main/java/org/springframework/hateoas/hal/DefaultCurieProvider.java
+++ b/src/main/java/org/springframework/hateoas/hal/DefaultCurieProvider.java
@@ -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 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));
}
/*
diff --git a/src/main/java/org/springframework/hateoas/hal/Jackson2HalModule.java b/src/main/java/org/springframework/hateoas/hal/Jackson2HalModule.java
index 6b861305..c84f0a6a 100644
--- a/src/main/java/org/springframework/hateoas/hal/Jackson2HalModule.java
+++ b/src/main/java/org/springframework/hateoas/hal/Jackson2HalModule.java
@@ -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(":"));
}
}
diff --git a/src/main/java/org/springframework/hateoas/mvc/ForwardedHeader.java b/src/main/java/org/springframework/hateoas/mvc/ForwardedHeader.java
index dae1ab5d..9c463468 100644
--- a/src/main/java/org/springframework/hateoas/mvc/ForwardedHeader.java
+++ b/src/main/java/org/springframework/hateoas/mvc/ForwardedHeader.java
@@ -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. emptyMap());
+ private static final ForwardedHeader NO_HEADER = new ForwardedHeader(Collections.emptyMap());
private final Map elements;
@@ -51,20 +52,11 @@ class ForwardedHeader {
return NO_HEADER;
}
- Map elements = new HashMap();
+ Map 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);
diff --git a/src/main/java/org/springframework/hateoas/mvc/IdentifiableResourceAssemblerSupport.java b/src/main/java/org/springframework/hateoas/mvc/IdentifiableResourceAssemblerSupport.java
index bae0cd22..def2c360 100644
--- a/src/main/java/org/springframework/hateoas/mvc/IdentifiableResourceAssemblerSupport.java
+++ b/src/main/java/org/springframework/hateoas/mvc/IdentifiableResourceAssemblerSupport.java
@@ -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 result = new ArrayList