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

jsonvalues.JsStr Maven / Gradle / Ivy

package jsonvalues;

import java.time.Instant;
import java.time.format.DateTimeParseException;
import java.util.Base64;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

import static java.util.Objects.requireNonNull;

/**
 Represents an immutable json string.
 */
public final class JsStr implements JsValue, Comparable {

    public static final int ID = 2;
    /**
     prism between the sum type JsValue and JsStr
     */
    public static final Prism prism =
            new Prism<>(s -> s.isStr() ? Optional.of(s.toJsStr().value) : Optional.empty(),
                        JsStr::of
            );

    public static final Prism base64Prism =
            new Prism<>(s -> {
                try {
                    return Optional.of(Base64.getDecoder()
                                             .decode(s));
                } catch (IllegalArgumentException e) {
                    return Optional.empty();
                }
            },
                       bytes -> Base64.getEncoder().encodeToString(bytes)
            );

    public static final Prism instantPrism =
            new Prism<>(s -> {
                try {
                    return Optional.of(Instant.parse(s));
                } catch (DateTimeParseException e) {
                    return Optional.empty();
                }
            },
                        Instant::toString
            );

    /**
     The string value.
     */
    public final String value;

    private JsStr(String value) {
        this.value = value;
    }

    @Override
    public int id() {
        return ID;
    }

    @Override
    public boolean isStr() {
        return true;
    }

    /**
     Compares two {@code JsStr} objects lexicographically.

     @see String#compareTo(String)
     */
    @Override
    public int compareTo(final JsStr o) {
        return value.compareTo(requireNonNull(o).value);
    }

    /**
     Tests this JsStr on a predicate.

     @param predicate the predicate
     @return true if this string satisfies the predicate
     */
    public boolean test(Predicate predicate) {
        return requireNonNull(predicate).test(value);
    }


    /**
     Returns the hashcode of this json string.

     @return hashcode of this JsStr
     */
    @Override
    public int hashCode() {
        return value.hashCode();
    }

    /**
     Indicates whether some other object is "equal to" this json string.

     @param that the reference object with which to compare.
     @return true if that is a JsStr with the same value as this JsStr
     */
    @Override
    public boolean equals(final Object that) {
        if (this == that) return true;
        if (that == null ) return false;
        if(that instanceof JsStr) {
            final JsStr thatStr = (JsStr) that;
            return Objects.equals(value,
                                  thatStr.value
                                 );
        }
        else if(that instanceof JsInstant){
            return JsStr.instantPrism.reverseGet.apply(((JsInstant) that).value).equals(value);
        }
        else if(that instanceof JsBinary){
            return JsStr.base64Prism.reverseGet.apply(((JsBinary) that).value).equals(value);
        }
        return false;
    }

    /**
     Returns the string representation of this json string which is its value quoted.

     @return the value quoted.
     */
    @Override
    public String toString() {
        return "\"" + value + "\"";
    }

    /**
     Maps this JsStr into another one.

     @param fn the mapping function
     @return a new JsStr
     */
    public JsStr map(final UnaryOperator fn) {
        return JsStr.of(requireNonNull(fn).apply(value));
    }

    /**
     Static factory method to create a JsStr from a string.

     @param str the string
     @return a new JsStr
     */
    public static JsStr of(String str) {
        return new JsStr(requireNonNull(str));
    }

    @Override
    public boolean isBinary() {
        return JsStr.base64Prism.getOptional.apply(value).isPresent();
    }

    @Override
    public boolean isInstant() {
        return JsStr.instantPrism.getOptional.apply(value).isPresent();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy