com.github.t9t.jooq.json.Jsonb Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jooq-postgresql-json Show documentation
Show all versions of jooq-postgresql-json Show documentation
jOOQ support for PostgreSQL json & jsonb
package com.github.t9t.jooq.json;
import java.util.Objects;
import static java.util.Objects.requireNonNull;
/**
* Represents a {@code jsonb} value. When using {@link JsonBinding}, {@code null} values from the database are turned
* as a {@code null}, not a {@code Json} instance with a {@code null} value.
*
* The {@link #toString()} method returns only the value without any additions, so can be used safely in place of
* {@link #getValue()}.
*
* To create an instance of a non-{@code null} value, use {@link #of(String)}. When you have a nullable value, use
* {@link #ofNullable(String)} which returns {@code null} in case the input is {@code null}.
*/
public final class Jsonb {
private final String value;
private Jsonb(String value) {
this.value = requireNonNull(value, "value");
}
/**
* Create an instance containing the non-{@code null} string. No checks are done to validate if the string is valid
* JSON.
*
* @param value non-{@code null} string
* @return Instance representing the string value
* @see #ofNullable(String)
*/
public static Jsonb of(String value) {
return new Jsonb(value);
}
/**
* Create an instance containing string if it's not {@code null}. When the {@code value} is {@code null},
* {@code null} is returned. No checks are done to validate if the string is valid JSON.
*
* @param value nullable string
* @return Instance representing the string value or {@code null}
* @see #of(String)
*/
public static Jsonb ofNullable(String value) {
return value == null ? null : of(value);
}
public static Jsonb copy(Jsonb in) {
return in == null ? null : of(in.value);
}
/**
* Retrieve the non-{@code null} JSON value
*
* @return JSON value, non-{@code null}
*/
public String getValue() {
return value;
}
@Override
public String toString() {
return value;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
return Objects.equals(value, ((Jsonb) o).value);
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}