All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openapitools.jackson.nullable.JsonNullable Maven / Gradle / Ivy

Go to download

JsonNullable wrapper class and Jackson module to support fields with meaningful null values.

There is a newer version: 0.2.6
Show newest version
package org.openapitools.jackson.nullable;

import java.io.Serializable;
import java.util.NoSuchElementException;
import java.util.function.Consumer;

public class JsonNullable implements Serializable {

    private static final long serialVersionUID = 1L;

    private static final JsonNullable UNDEFINED = new JsonNullable(null, false);

    private T value;

    private boolean isPresent;

    private JsonNullable(T value, boolean isPresent) {
        this.value = value;
        this.isPresent = isPresent;
    }

    /**
     * Create a JsonNullable representing an undefined value (not present).
     *
     * @param 
     * @return
     */
    public static  JsonNullable undefined() {
        @SuppressWarnings("unchecked")
        JsonNullable t = (JsonNullable) UNDEFINED;
        return t;
    }

    /**
     * Create a JsonNullable from the submitted value.
     *
     * @param value the value
     * @param    the type of the value
     * @return the JsonNullable with the submitted value present.
     */
    public static  JsonNullable of(T value) {
        return new JsonNullable(value, true);
    }

    /**
     * Obtain the value of this JsonNullable.
     *
     * @return the value, if present
     * @throws NoSuchElementException if no value is present
     */
    public T get() {
        if (!isPresent) {
            throw new NoSuchElementException("Value is undefined");
        }
        return value;
    }

    /**
     * Obtain the value of this JsonNullable.
     *
     * @param other the value to be returned if no value is present
     * @return the value of this JsonNullable if present, the submitted value otherwise
     */
    public T orElse(T other) {
        return this.isPresent ? this.value : other;
    }

    public boolean isPresent() {
        return isPresent;
    }

    /**
     * If a value is present, performs the given action with the value,
     * otherwise does nothing.
     *
     * @param action the action to be performed, if a value is present
     */
    public void ifPresent(
            Consumer action) {

        if (this.isPresent) {
            action.accept(value);
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }

        if (!(obj instanceof JsonNullable)) {
            return false;
        }

        JsonNullable other = (JsonNullable) obj;
        return equals(value, other.value) &&
                equals(isPresent, other.isPresent);
    }

    private static boolean equals(Object a, Object b) {
        return (a == b) || (a != null && a.equals(b));
    }

    @Override
    public int hashCode() {
        int result = 31 + (value == null ? 0 : value.hashCode());
        Boolean bool1 = Boolean.TRUE;
        bool1.hashCode();
        result = 31 * result + (isPresent ? 1231 : 1237);
        return result;
    }

    @Override
    public String toString() {
        return this.isPresent ? String.format("JsonNullable[%s]", value) : "JsonNullable.undefined";
    }
}