Last active
November 20, 2021 14:41
-
-
Save esotrope/269a47a1315a6dd505cf8fb306489476 to your computer and use it in GitHub Desktop.
autovalue vs lombok vs immutables syntax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
curl -X POST -H "Content-Type: application/json" -d '{"children":[{"id":"0"},{"id":"1"}],"childrenById":{"2":{"id":"2"}}}' http://localhost:8080/api/immutables | |
*/ | |
@RestController | |
@RequestMapping("/api/lombok") | |
public class LombokController { | |
@RequestMapping(method = RequestMethod.GET) | |
public Parent get() { | |
return Parent.builder() | |
.children( | |
ImmutableList.of(Parent.Child.builder().id("a").build()) | |
) | |
.childrenById( | |
ImmutableMap.of("a", Parent.Child.builder().id("a").build()) | |
) | |
.build(); | |
} | |
@RequestMapping(method = RequestMethod.POST) | |
public Parent post(@RequestBody Parent parent) { | |
return parent; | |
} | |
@Data | |
@Builder | |
@AllArgsConstructor | |
public static class Parent { | |
public List<Child> children; | |
public Map<String, Child> childrenById; | |
@Data | |
@Builder | |
@AllArgsConstructor | |
public static class Child { | |
String id; | |
} | |
} | |
} | |
@RestController | |
@RequestMapping("/api/immutables") | |
public class ImmutablesController { | |
@RequestMapping(method = RequestMethod.GET) | |
public Parent get(){ | |
return ImmutableParent.builder() | |
.addChildren( | |
ImmutableChild.builder().id("0").build(), | |
ImmutableChild.builder().id("1").build() | |
) | |
.putChildrenById("2", ImmutableChild.builder().id("2").build()) | |
.build(); | |
} | |
@RequestMapping(method = RequestMethod.POST) | |
public Parent post(@RequestBody ImmutableParent parent){ | |
return parent; | |
} | |
@Value.Immutable | |
@JsonDeserialize(builder = ImmutableParent.Builder.class) | |
public interface Parent { | |
List<Child> children(); | |
Map<String, Child> childrenById(); | |
@Value.Immutable | |
@JsonDeserialize(builder = ImmutableChild.Builder.class) | |
interface Child { | |
String id(); | |
} | |
} | |
} | |
@RestController | |
@RequestMapping("/api/autovalue") | |
public class AutoValueController { | |
@RequestMapping(method = RequestMethod.GET) | |
public Parent get() { | |
return Parent.builder() | |
.children( | |
ImmutableList.of(Parent.Child.builder().id("a").build()) | |
) | |
.childrenById( | |
ImmutableMap.of("a", Parent.Child.builder().id("a").build()) | |
) | |
.build(); | |
} | |
@RequestMapping(method = RequestMethod.POST) | |
public Parent post(@RequestBody Parent parent) { | |
return parent; | |
} | |
@AutoValue | |
@JsonDeserialize(builder = AutoValue_AutoValueController_Parent.Builder.class) | |
static abstract class Parent { | |
@JsonProperty | |
abstract List<Child> children(); | |
@JsonProperty | |
abstract Map<String, Child> childrenById(); | |
static Parent.Builder builder() { | |
return new AutoValue_AutoValueController_Parent.Builder(); | |
} | |
@AutoValue.Builder | |
abstract static class Builder { | |
@JsonCreator | |
private static Parent.Builder create() { return AutoValue_AutoValueController_Parent.builder(); } | |
@JsonProperty abstract Builder children(List<Child> value); | |
@JsonProperty abstract Builder childrenById(Map<String, Child> value); | |
abstract Parent build(); | |
} | |
@AutoValue | |
@JsonDeserialize(builder = AutoValue_AutoValueController_Parent_Child.Builder.class) | |
static abstract class Child { | |
static Child.Builder builder() { | |
return new AutoValue_AutoValueController_Parent_Child.Builder(); | |
} | |
@JsonProperty | |
abstract String id(); | |
@AutoValue.Builder | |
abstract static class Builder { | |
@JsonCreator | |
private static Builder create() { return AutoValue_AutoValueController_Parent_Child.builder(); } | |
@JsonProperty abstract Child.Builder id(String value); | |
abstract Child build(); | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment