#653 - Polishing.
Expose nullability in Identifiable, ResourceSupport and Links via Optional.
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
package org.springframework.hateoas;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Interface to mark objects that are identifiable by an ID of any type.
|
||||
@@ -27,7 +28,7 @@ public interface Identifiable<ID extends Serializable> {
|
||||
/**
|
||||
* Returns the id identifying the object.
|
||||
*
|
||||
* @return the identifier or {@literal null} if not available.
|
||||
* @return the identifier or {@link Optional#empty()} if not available.
|
||||
*/
|
||||
ID getId();
|
||||
Optional<ID> getId();
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.hateoas;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
@@ -105,7 +106,7 @@ public class PagedResources<T> extends Resources<T> {
|
||||
* @return
|
||||
*/
|
||||
@JsonIgnore
|
||||
public Link getNextLink() {
|
||||
public Optional<Link> getNextLink() {
|
||||
return getLink(Link.REL_NEXT);
|
||||
}
|
||||
|
||||
@@ -115,7 +116,7 @@ public class PagedResources<T> extends Resources<T> {
|
||||
* @return
|
||||
*/
|
||||
@JsonIgnore
|
||||
public Link getPreviousLink() {
|
||||
public Optional<Link> getPreviousLink() {
|
||||
return getLink(Link.REL_PREVIOUS);
|
||||
}
|
||||
|
||||
@@ -251,8 +252,8 @@ public class PagedResources<T> extends Resources<T> {
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("Metadata { number: %d, total pages: %d, total elements: %d, size: %d }", number,
|
||||
totalPages, totalElements, size);
|
||||
return String.format("Metadata { number: %d, total pages: %d, total elements: %d, size: %d }", number, totalPages,
|
||||
totalElements, size);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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,7 +18,8 @@ package org.springframework.hateoas;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
|
||||
@@ -37,14 +38,14 @@ public class ResourceSupport implements Identifiable<Link> {
|
||||
private final List<Link> links;
|
||||
|
||||
public ResourceSupport() {
|
||||
this.links = new ArrayList<>();
|
||||
this.links = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Link} with a rel of {@link Link#REL_SELF}.
|
||||
*/
|
||||
@JsonIgnore
|
||||
public Link getId() {
|
||||
public Optional<Link> getId() {
|
||||
return getLink(Link.REL_SELF);
|
||||
}
|
||||
|
||||
@@ -65,7 +66,7 @@ public class ResourceSupport implements Identifiable<Link> {
|
||||
*/
|
||||
public void add(Iterable<Link> links) {
|
||||
Assert.notNull(links, "Given links must not be null!");
|
||||
links.forEach(this::add);
|
||||
links.forEach(this::add);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,7 +95,7 @@ public class ResourceSupport implements Identifiable<Link> {
|
||||
* @return
|
||||
*/
|
||||
public boolean hasLink(String rel) {
|
||||
return getLink(rel) != null;
|
||||
return getLink(rel).isPresent();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,13 +120,23 @@ public class ResourceSupport implements Identifiable<Link> {
|
||||
* Returns the link with the given rel.
|
||||
*
|
||||
* @param rel
|
||||
* @return the link with the given rel or {@literal null} if none found.
|
||||
* @return the link with the given rel or {@link Optional#empty()} if none found.
|
||||
*/
|
||||
public Link getLink(String rel) {
|
||||
public Optional<Link> getLink(String rel) {
|
||||
return getLinks(rel).stream().findFirst();
|
||||
}
|
||||
|
||||
return getLinks(rel).stream()
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
/**
|
||||
* Returns the link with the given rel.
|
||||
*
|
||||
* @param rel
|
||||
* @return the link with the given rel.
|
||||
* @throws IllegalArgumentException in case no link with the given rel can be found.
|
||||
*/
|
||||
public Link getRequiredLink(String rel) {
|
||||
|
||||
return getLink(rel) //
|
||||
.orElseThrow(() -> new IllegalArgumentException(String.format("No link with rel %s found!", rel)));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -135,9 +146,8 @@ public class ResourceSupport implements Identifiable<Link> {
|
||||
*/
|
||||
public List<Link> getLinks(String rel) {
|
||||
|
||||
return links.stream()
|
||||
.filter(link -> link.getRel().equals(rel))
|
||||
.collect(Collectors.toList());
|
||||
return links.stream() //
|
||||
.filter(link -> link.getRel().equals(rel)).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -19,6 +19,7 @@ import static org.springframework.hateoas.core.EncodingUtils.*;
|
||||
import static org.springframework.web.util.UriComponentsBuilder.*;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
import org.springframework.hateoas.Link;
|
||||
@@ -68,6 +69,8 @@ public abstract class LinkBuilderSupport<T extends LinkBuilder> implements LinkB
|
||||
*/
|
||||
public T slash(Object object) {
|
||||
|
||||
object = Optional.class.isInstance(object) ? ((Optional<?>) object).orElse(null) : object;
|
||||
|
||||
if (object == null) {
|
||||
return getThis();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user