DEV Community

Discussion on: Baby talk for computers (It's all key: value pairs)

Collapse
 
ehaynes99 profile image
Eric Haynes • Edited

Some babies are jerks, though...

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Objects;

@JsonPropertyOrder({"key", "name", "cats"})
public class ThingDto<T> {
    @NotNull()
    @JsonProperty()
    private T key;

    @NotNull()
    @NotEmpty()
    @JsonProperty()
    private String name;

    @NotNull()
    @Min(0)
    @JsonProperty()
    private int cats;

    public T getKey() {
        return key;
    }

    public void setKey(T key) {
        this.key = key;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getCats() {
        return cats;
    }

    public void setCats(int cats) {
        this.cats = cats;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ThingDto<?> thingDto = (ThingDto<?>) o;
        return cats == thingDto.cats && Objects.equals(key, thingDto.key) && Objects.equals(name, thingDto.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(key, name, cats);
    }
}

ThingDto ivy = new ThingDto<String>();
ivy.setKey("value");
ivy.setName("Ivy");
ivy.setCats(0);
Enter fullscreen mode Exit fullscreen mode