Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A java modelisation of HAL (Hypertext Application Language)
```xml
<dependency>
<groupId>com.cosium.hal_model</groupId>
<artifactId>hal-model</artifactId>
<artifactId>hal-model2</artifactId>
<version>${hal-model.version}</version>
</dependency>
```
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<url>https://github.com/Cosium/hal-model</url>

<groupId>com.cosium.hal_model</groupId>
<artifactId>hal-model</artifactId>
<version>1.1-SNAPSHOT</version>
<artifactId>hal-model2</artifactId>
<version>2.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
Expand Down

This file was deleted.

22 changes: 0 additions & 22 deletions src/main/java/com/cosium/hal_model/Link.java

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cosium.hal_model;
package com.cosium.hal_model2;

import static java.util.Objects.requireNonNull;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cosium.hal_model;
package com.cosium.hal_model2;

import com.damnhandy.uri.template.UriTemplate;
import java.util.Map;
Expand All @@ -12,12 +12,12 @@ public class HalFormsBody<T> {
private final T representation;
private final Map<String, Link> linkByName;
private final @Nullable String selfUri;
private final Map<String, TemplateRepresentation> templateByKey;
private final Map<String, Template> templateByKey;

public HalFormsBody(
T representation,
@Nullable Map<String, Link> linkByName,
@Nullable Map<String, TemplateRepresentation> templateByKey) {
@Nullable Map<String, Template> templateByKey) {
this.representation = representation;
this.linkByName = Optional.ofNullable(linkByName).map(Map::copyOf).orElseGet(Map::of);
selfUri =
Expand Down Expand Up @@ -54,15 +54,15 @@ public Link requireLink(String name) {
return findLink(name).orElseThrow();
}

public Map<String, TemplateRepresentation> templateByKey() {
public Map<String, Template> templateByKey() {
return templateByKey;
}

public Optional<TemplateRepresentation> findTemplate(String key) {
public Optional<Template> findTemplate(String key) {
return Optional.ofNullable(templateByKey.get(key));
}

public TemplateRepresentation requireTemplate(String key) {
public Template requireTemplate(String key) {
return findTemplate(key).orElseThrow();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cosium.hal_model;
package com.cosium.hal_model2;

import static java.util.Objects.requireNonNull;

Expand Down Expand Up @@ -68,14 +68,14 @@ public HalFormsBody<?> deserialize(
}

JsonNode templates = objectNode.get("_templates");
Map<String, TemplateRepresentation> templateByKey;
Map<String, Template> templateByKey;
if (templates == null) {
templateByKey = Map.of();
} else {
MapType mapType =
deserializationContext
.getTypeFactory()
.constructMapType(Map.class, String.class, TemplateRepresentation.class);
.constructMapType(Map.class, String.class, Template.class);
templateByKey =
deserializationContext.readValue(
createJsonParser(deserializationContext, templates), mapType);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cosium.hal_model;
package com.cosium.hal_model2;

import tools.jackson.core.Version;
import tools.jackson.databind.JacksonModule;
Expand All @@ -24,7 +24,6 @@ public void setupModule(SetupContext context) {
context.addDeserializers(
new SimpleDeserializers()
.addDeserializer(HalFormsBody.class, new HalFormsBodyDeserializer())
.addDeserializer(
InlineElementRepresentation.class, new InlineElementRepresentationDeserializer()));
.addDeserializer(InlineElement.class, new InlineElementDeserializer()));
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/cosium/hal_model2/InlineElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.cosium.hal_model2;

/**
* @author Réda Housni Alaoui
*/
public sealed interface InlineElement permits MapInlineElement, StringInlineElement {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cosium.hal_model;
package com.cosium.hal_model2;

import java.util.Map;
import org.jspecify.annotations.Nullable;
Expand All @@ -11,12 +11,10 @@
/**
* @author Réda Housni Alaoui
*/
class InlineElementRepresentationDeserializer
extends ValueDeserializer<InlineElementRepresentation> {
class InlineElementDeserializer extends ValueDeserializer<InlineElement> {

@Override
public @Nullable InlineElementRepresentation deserialize(
JsonParser p, DeserializationContext ctxt) {
public @Nullable InlineElement deserialize(JsonParser p, DeserializationContext ctxt) {

JsonToken currentToken = p.currentToken();
if (currentToken == JsonToken.START_OBJECT) {
Expand All @@ -26,19 +24,19 @@ class InlineElementRepresentationDeserializer
if (map == null) {
return null;
}
return new MapInlineElementRepresentation(map);
return new MapInlineElement(map);
}
if (currentToken == JsonToken.VALUE_STRING) {
String value = ctxt.readValue(p, String.class);
if (value == null) {
return null;
}
return new StringInlineElementRepresentation(value);
return new StringInlineElement(value);
}

throw MismatchedInputException.from(
p,
InlineElementRepresentation.class,
InlineElement.class,
"%s should have been either %s or %s. But it is not."
.formatted(currentToken, JsonToken.START_OBJECT, JsonToken.VALUE_STRING));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.cosium.hal_model;
package com.cosium.hal_model2;

import static java.util.Objects.requireNonNull;

import com.damnhandy.uri.template.UriTemplate;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.Optional;
Expand All @@ -10,14 +11,14 @@
/**
* @author Réda Housni Alaoui
*/
public class OptionsLinkRepresentation {
public class Link {

private final String href;
private final @Nullable String type;
private final boolean templated;

@JsonCreator
public OptionsLinkRepresentation(
public Link(
@JsonProperty("href") String href,
@JsonProperty("type") @Nullable String type,
@JsonProperty("templated") @Nullable Boolean templated) {
Expand All @@ -26,8 +27,8 @@ public OptionsLinkRepresentation(
this.templated = Optional.ofNullable(templated).orElse(false);
}

public String href() {
return href;
public UriTemplate href() {
return UriTemplate.fromTemplate(href);
}

public Optional<String> type() {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/cosium/hal_model2/MapInlineElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.cosium.hal_model2;

import java.util.Map;
import java.util.Optional;
import org.jspecify.annotations.Nullable;

/**
* @author Réda Housni Alaoui
*/
public record MapInlineElement(Map<String, String> map) implements InlineElement {

public MapInlineElement(@Nullable Map<String, String> map) {
this.map = Optional.ofNullable(map).map(Map::copyOf).orElseGet(Map::of);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cosium.hal_model;
package com.cosium.hal_model2;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -9,20 +9,20 @@
/**
* @author Réda Housni Alaoui
*/
public class OptionsRepresentation {
public class Options {

private final @Nullable List<InlineElementRepresentation> inline;
private final @Nullable OptionsLinkRepresentation link;
private final @Nullable List<InlineElement> inline;
private final @Nullable Link link;
private final @Nullable Long maxItems;
private final long minItems;
private final @Nullable String promptField;
private final List<String> selectedValues;
private final @Nullable String valueField;

@JsonCreator
public OptionsRepresentation(
@JsonProperty("inline") @Nullable List<InlineElementRepresentation> inline,
@JsonProperty("link") @Nullable OptionsLinkRepresentation link,
public Options(
@JsonProperty("inline") @Nullable List<InlineElement> inline,
@JsonProperty("link") @Nullable Link link,
@JsonProperty("maxItems") @Nullable Long maxItems,
@JsonProperty("minItems") @Nullable Long minItems,
@JsonProperty("promptField") @Nullable String promptField,
Expand All @@ -37,11 +37,11 @@ public OptionsRepresentation(
this.valueField = valueField;
}

public Optional<List<InlineElementRepresentation>> inline() {
public Optional<List<InlineElement>> inline() {
return Optional.ofNullable(inline);
}

public Optional<OptionsLinkRepresentation> link() {
public Optional<Link> link() {
return Optional.ofNullable(link);
}

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/cosium/hal_model2/StringInlineElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.cosium.hal_model2;

import static java.util.Objects.requireNonNull;

/**
* @author Réda Housni Alaoui
*/
public record StringInlineElement(String value) implements InlineElement {

public StringInlineElement {
requireNonNull(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cosium.hal_model;
package com.cosium.hal_model2;

import static java.util.Objects.requireNonNull;

Expand All @@ -16,29 +16,29 @@
/**
* @author Réda Housni Alaoui
*/
public class TemplateRepresentation {
public class Template {

private final @Nullable String title;
private final String method;
private final String contentType;
private final Map<String, TemplatePropertyRepresentation> propertyByName;
private final Map<String, TemplateProperty> propertyByName;
private final @Nullable String target;

@JsonCreator
TemplateRepresentation(
Template(
@JsonProperty("title") @Nullable String title,
@JsonProperty("method") String method,
@JsonProperty("contentType") @Nullable String contentType,
@JsonProperty("properties") @Nullable List<TemplatePropertyRepresentation> properties,
@JsonProperty("properties") @Nullable List<TemplateProperty> properties,
@JsonProperty("target") @Nullable String target) {
this.title = title;
this.method = requireNonNull(method, "Attribute 'method' is missing");
this.contentType = Optional.ofNullable(contentType).orElse("application/json");
Map<String, TemplatePropertyRepresentation> mutablePropertyByName =
Map<String, TemplateProperty> mutablePropertyByName =
Optional.ofNullable(properties).orElseGet(List::of).stream()
.collect(
Collectors.toMap(
TemplatePropertyRepresentation::name,
TemplateProperty::name,
Function.identity(),
(e1, e2) -> {
throw new IllegalStateException(
Expand All @@ -61,15 +61,15 @@ public String contentType() {
return contentType;
}

public Map<String, TemplatePropertyRepresentation> propertyByName() {
public Map<String, TemplateProperty> propertyByName() {
return propertyByName;
}

public Optional<TemplatePropertyRepresentation> findProperty(String name) {
public Optional<TemplateProperty> findProperty(String name) {
return Optional.ofNullable(propertyByName.get(name));
}

public TemplatePropertyRepresentation requireProperty(String name) {
public TemplateProperty requireProperty(String name) {
return findProperty(name).orElseThrow();
}

Expand Down
Loading
Loading