#842 - Remove Identifiable.
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
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.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public interface Identifiable<ID extends Serializable> {
|
||||
|
||||
/**
|
||||
* Returns the id identifying the object.
|
||||
*
|
||||
* @return the identifier or {@link Optional#empty()} if not available.
|
||||
*/
|
||||
Optional<ID> getId();
|
||||
}
|
||||
@@ -23,7 +23,6 @@ import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
/**
|
||||
@@ -33,7 +32,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
* @author Johhny Lim
|
||||
* @author Greg Turnquist
|
||||
*/
|
||||
public class RepresentationModel<T extends RepresentationModel<? extends T>> implements Identifiable<Link> {
|
||||
public class RepresentationModel<T extends RepresentationModel<? extends T>> {
|
||||
|
||||
private final List<Link> links;
|
||||
|
||||
@@ -57,14 +56,6 @@ public class RepresentationModel<T extends RepresentationModel<? extends T>> imp
|
||||
this.links.addAll(initialLinks);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Link} with a rel of {@link IanaLinkRelations#SELF}.
|
||||
*/
|
||||
@JsonIgnore
|
||||
public Optional<Link> getId() {
|
||||
return getLink(IanaLinkRelations.SELF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given link to the resource.
|
||||
*
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.hateoas.server;
|
||||
import java.util.function.Function;
|
||||
|
||||
import org.springframework.hateoas.IanaLinkRelations;
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.server.TypedEntityLinks.ExtendedTypedEntityLinks;
|
||||
import org.springframework.plugin.core.Plugin;
|
||||
@@ -59,7 +58,7 @@ public interface EntityLinks extends Plugin<Class<?>> {
|
||||
* Implementations will know about the URI structure being used to expose item-resource URIs.
|
||||
*
|
||||
* @param type the entity type to point to, must not be {@literal null}.
|
||||
* @param id the id of the object of the handed type, {@link Identifiable}s will be unwrapped.
|
||||
* @param id the id of the object of the handed type, must not be {@literal null}.
|
||||
* @return the {@link LinkBuilder} pointing to the item resource identified by the given type and id. Will never be
|
||||
* {@literal null}.
|
||||
* @throws IllegalArgumentException in case the given type is unknown the entity links infrastructure.
|
||||
@@ -84,16 +83,6 @@ public interface EntityLinks extends Plugin<Class<?>> {
|
||||
return linkForItemResource(entity.getClass(), identifierExtractor.apply(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link LinkBuilder} able to create links to the controller managing the given entity.
|
||||
*
|
||||
* @see #linkForItemResource(Class, Object)
|
||||
* @param entity the entity type to point to, must not be {@literal null}.
|
||||
* @return the {@link LinkBuilder} pointing the given entity. Will never be {@literal null}.
|
||||
* @throws IllegalArgumentException in case the type of the given entity is unknown the entity links infrastructure.
|
||||
*/
|
||||
LinkBuilder linkForItemResource(Identifiable<?> entity);
|
||||
|
||||
/**
|
||||
* Creates a {@link Link} pointing to the collection resource of the given type. The relation type of the link will be
|
||||
* determined by the implementation class and should be defaulted to {@link IanaLinkRelations#SELF}.
|
||||
@@ -131,16 +120,6 @@ public interface EntityLinks extends Plugin<Class<?>> {
|
||||
return linkToItemResource(entity.getClass(), identifierExtractor.apply(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@link Link} pointing to item resource backing the given entity. The relation type of the link will be
|
||||
* determined by the implementation class and should be defaulted to {@link IanaLinkRelations#SELF}.
|
||||
*
|
||||
* @param entity the entity type to point to, must not be {@literal null}.
|
||||
* @return the {@link Link} pointing to the resource exposed for the given entity. Will never be {@literal null}.
|
||||
* @throws IllegalArgumentException in case the type of the given entity is unknown the entity links infrastructure.
|
||||
*/
|
||||
Link linkToItemResource(Identifiable<?> entity);
|
||||
|
||||
/**
|
||||
* Creates a {@link TypedEntityLinks} instance using the given identifier extractor function.
|
||||
*
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.hateoas.server;
|
||||
import java.net.URI;
|
||||
|
||||
import org.springframework.hateoas.IanaLinkRelations;
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.LinkRelation;
|
||||
|
||||
@@ -30,23 +29,13 @@ import org.springframework.hateoas.LinkRelation;
|
||||
public interface LinkBuilder {
|
||||
|
||||
/**
|
||||
* Adds the given object's {@link String} representation as sub-resource to the current URI. Will unwrap
|
||||
* {@link Identifiable}s to their id value (see {@link Identifiable#getId()}).
|
||||
* Adds the given object's {@link String} representation as sub-resource to the current URI.
|
||||
*
|
||||
* @param object
|
||||
* @return
|
||||
*/
|
||||
LinkBuilder slash(Object object);
|
||||
|
||||
/**
|
||||
* Adds the given {@link Identifiable}'s id as sub-resource. Will simply return the {@link LinkBuilder} as is if the
|
||||
* given entity is {@literal null}.
|
||||
*
|
||||
* @param identifiable
|
||||
* @return
|
||||
*/
|
||||
LinkBuilder slash(Identifiable<?> identifiable);
|
||||
|
||||
/**
|
||||
* Creates a URI of the link built by the current builder instance.
|
||||
*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2012 the original author or authors.
|
||||
* Copyright 2012-2019 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,31 +15,18 @@
|
||||
*/
|
||||
package org.springframework.hateoas.server.core;
|
||||
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.server.EntityLinks;
|
||||
import org.springframework.hateoas.server.LinkBuilder;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Implementation base class to delegate the higher level methods of {@link EntityLinks} by delegating to the more fine
|
||||
* grained ones to reduce the implementation effort for actual implementation classes.
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class AbstractEntityLinks implements EntityLinks {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.EntityLinks#getLinkToSingleResource(org.springframework.hateoas.Identifiable)
|
||||
*/
|
||||
@Override
|
||||
public Link linkToItemResource(Identifiable<?> entity) {
|
||||
Assert.notNull(entity, "Entity must not be null!");
|
||||
return linkToItemResource(entity.getClass(), entity.getId());
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.EntityLinks#linkForSingleResource(java.lang.Class, java.lang.Object)
|
||||
*/
|
||||
@@ -47,14 +34,4 @@ public abstract class AbstractEntityLinks implements EntityLinks {
|
||||
public LinkBuilder linkForItemResource(Class<?> type, Object id) {
|
||||
return linkFor(type).slash(id);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.EntityLinks#linkForSingleResource(org.springframework.hateoas.Identifiable)
|
||||
*/
|
||||
@Override
|
||||
public LinkBuilder linkForItemResource(Identifiable<?> entity) {
|
||||
Assert.notNull(entity, "Entity must not be null!");
|
||||
return linkForItemResource(entity.getClass(), entity.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ import java.util.function.Function;
|
||||
|
||||
import org.springframework.hateoas.Affordance;
|
||||
import org.springframework.hateoas.IanaLinkRelations;
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.LinkRelation;
|
||||
import org.springframework.hateoas.server.LinkBuilder;
|
||||
@@ -93,10 +92,6 @@ public abstract class LinkBuilderSupport<T extends LinkBuilder> implements LinkB
|
||||
return getThis();
|
||||
}
|
||||
|
||||
if (object instanceof Identifiable) {
|
||||
return slash((Identifiable<?>) object);
|
||||
}
|
||||
|
||||
String path = object.toString();
|
||||
|
||||
if (path.endsWith("#")) {
|
||||
@@ -130,19 +125,6 @@ public abstract class LinkBuilderSupport<T extends LinkBuilder> implements LinkB
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.LinkBuilder#slash(org.springframework.hateoas.Identifiable)
|
||||
*/
|
||||
public T slash(Identifiable<?> identifyable) {
|
||||
|
||||
if (identifyable == null) {
|
||||
return getThis();
|
||||
}
|
||||
|
||||
return slash(identifyable.getId());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.LinkBuilder#toUri()
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.hateoas.server.mvc;
|
||||
|
||||
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.springframework.hateoas.Identifiable;
|
||||
import org.springframework.hateoas.RepresentationModel;
|
||||
import org.springframework.hateoas.server.RepresentationModelAssembler;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Base class to implement {@link RepresentationModelAssembler}s. Will automate {@link RepresentationModel} instance
|
||||
* creation and make sure a self-link is always added.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public abstract class IdentifiableRepresentationModelAssemblerSupport<T extends Identifiable<?>, D extends RepresentationModel<D>>
|
||||
extends RepresentationModelAssemblerSupport<T, D> {
|
||||
|
||||
private final Class<?> controllerClass;
|
||||
|
||||
/**
|
||||
* Creates a new {@link RepresentationModelAssemblerSupport} using the given controller class and resource type.
|
||||
*
|
||||
* @param controllerClass must not be {@literal null}.
|
||||
* @param resourceType must not be {@literal null}.
|
||||
*/
|
||||
public IdentifiableRepresentationModelAssemblerSupport(Class<?> controllerClass, Class<D> resourceType) {
|
||||
|
||||
super(controllerClass, resourceType);
|
||||
this.controllerClass = controllerClass;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new resource and adds a self link to it consisting using the {@link Identifiable}'s id.
|
||||
*
|
||||
* @param entity must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
protected D createModel(T entity) {
|
||||
return createModel(entity, new Object[0]);
|
||||
}
|
||||
|
||||
protected D createModel(T entity, Object... parameters) {
|
||||
return createModelWithId(entity.getId(), entity, parameters);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected D createModelWithId(Object id, T entity, Object... parameters) {
|
||||
|
||||
Assert.notNull(entity, "Entity must not be null!");
|
||||
Assert.notNull(id, "Id must not be null!");
|
||||
|
||||
D instance = instantiateModel(entity);
|
||||
instance.add(linkTo(controllerClass, unwrapIdentifyables(parameters)).slash(id).withSelfRel());
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the ids of the given values in case they're {@link Identifiable}s. Returns all other objects as they are.
|
||||
*
|
||||
* @param values must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
private Object[] unwrapIdentifyables(Object[] values) {
|
||||
|
||||
return Arrays.stream(values) //
|
||||
.map(element -> element instanceof Identifiable ? ((Identifiable<?>) element).getId().get() : element) //
|
||||
.toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user