Package jsonvalues

Class JsPair


  • public final class JsPair
    extends Object
    Immutable pair which represents a JsElem of a Json and its JsPath location: (path, element).
    • Field Detail

      • elem

        public final JsElem elem
        the json element.
      • path

        public final JsPath path
        the location of the element.
    • Method Detail

      • mapIfInt

        public JsPair mapIfInt​(IntUnaryOperator operator)
        Declarative way of implementing if(pair.elem.isInt()) return Pair.of(pair.path, pair.elem.asJsInt().map(operator)) else return pair
        Parameters:
        operator - the function to be applied to map the integer
        Returns:
        the same this instance if the JsElem is not a JsInt or a new pair
      • mapIfStr

        public JsPair mapIfStr​(UnaryOperator<String> fn)
        Declarative way of implementing if(pair.elem.isStr()) return Pair.of(pair.path, pair.elem.asJsStr().map(mapFn)) else return pair
        Parameters:
        fn - the function to be applied to map the string of the JsStr
        Returns:
        the same this instance if the JsElem is not a JsStr or a new pair
      • of

        public static JsPair of​(JsPath path,
                                JsElem elem)
        Returns a json pair from the path and the json element.
        Parameters:
        path - the JsPath object
        elem - the JsElem
        Returns:
        an immutable JsPair
      • of

        public static JsPair of​(JsPath path,
                                int i)
        Returns a json pair from the path and the integer.
        Parameters:
        path - the JsPath
        i - the integer
        Returns:
        an immutable JsPair
      • of

        public static JsPair of​(JsPath path,
                                double d)
        Returns a json pair from the path and the double.
        Parameters:
        path - the JsPath
        d - the double
        Returns:
        an immutable JsPair
      • of

        public static JsPair of​(JsPath path,
                                long l)
        Returns a json pair from the path and the long.
        Parameters:
        path - the JsPath
        l - the long
        Returns:
        an immutable JsPair
      • of

        public static JsPair of​(JsPath path,
                                boolean b)
        Returns a json pair from the path and the boolean.
        Parameters:
        path - the JsPath
        b - the boolean
        Returns:
        an immutable JsPair
      • of

        public static JsPair of​(JsPath path,
                                String s)
        Returns a json pair from the path and the string.
        Parameters:
        path - the JsPath
        s - the string
        Returns:
        an immutable JsPair
      • of

        public static JsPair of​(JsPath path,
                                BigDecimal bd)
        Returns a json pair from the path and the big decimal.
        Parameters:
        path - the JsPath
        bd - the big decimal
        Returns:
        an immutable JsPair
      • of

        public static JsPair of​(JsPath path,
                                BigInteger bi)
        Returns a json pair from the path and the big integer.
        Parameters:
        path - the JsPath
        bi - the big integer
        Returns:
        an immutable JsPair
      • toString

        public String toString()
        Overrides:
        toString in class Object
        Returns:
        string representation of this pair: (path, elem)
      • equals

        public boolean equals​(@Nullable Object that)
        Returns true if that is a pair and both represents the same element at the same location.
        Overrides:
        equals in class Object
        Parameters:
        that - the reference object with which to compare.
        Returns:
        true if this.element.equals(that.element) and this.path.equals(that.path)
      • hashCode

        public int hashCode()
        Returns the hashcode of this pair.
        Overrides:
        hashCode in class Object
        Returns:
        the hashcode of this pair
      • mapElem

        public JsPair mapElem​(UnaryOperator<JsElem> map)
        Returns a new pair with the same path and a new element result of applying the mapping function
        Parameters:
        map - the mapping function which maps the JsElem
        Returns:
        a new JsPair
      • mapPath

        public JsPair mapPath​(UnaryOperator<JsPath> map)
        Returns a new pair with the same element and a new path result of applying the mapping function
        Parameters:
        map - the mapping function which maps the JsPath
        Returns:
        a new JsPair
      • ifJsonElse

        public <T> T ifJsonElse​(BiFunction<JsPath,​Json<?>,​T> ifJson,
                                BiFunction<JsPath,​JsElem,​T> ifNotJson)
        Declarative way of implementing an if(json)return T; else return T; where T is computed by the given functions
        Type Parameters:
        T - type of the result
        Parameters:
        ifJson - function that returns a T and is invoked if the element of this pair is a json
        ifNotJson - function that returns a T and is invoked if the element of this pair is not a json
        Returns:
        object of type T
      • ifJsonElse

        public <T> T ifJsonElse​(BiFunction<JsPath,​JsObj,​T> ifJsOb,
                                BiFunction<JsPath,​JsArray,​T> ifJsArr,
                                BiFunction<JsPath,​JsElem,​T> ifNotJson)
        Declarative way of implementing an if(obj)return T; else if(array) return T; else return T; where T is computed by the given functions
        Type Parameters:
        T - type of the result
        Parameters:
        ifJsOb - function that returns a T and is invoked if the element of this pair is a json object
        ifJsArr - function that returns a T and is invoked if the element of this pair is not a json array
        ifNotJson - function that returns a T and is invoked if the element of this pair is not a json
        Returns:
        object of type T
      • ifElse

        public <R> R ifElse​(Predicate<? super JsPair> predicate,
                            Function<? super JsPair,​R> ifTrue,
                            Function<? super JsPair,​R> ifFalse)
        Declarative way of implementing an if-else. This pair is tested on a given predicate, executing the ifTrue function when true and the ifFalse function otherwise
        Type Parameters:
        R - the type of the returned value
        Parameters:
        predicate - the given predicate
        ifTrue - function to invoked when the predicate is evaluated to true, taking this pair as a parameter
        ifFalse - function to invoked when the predicate is evaluated to false, taking this pair as a parameter
        Returns:
        an object of type R
      • consumeIf

        public void consumeIf​(Predicate<JsPair> predicate,
                              Consumer<JsPair> consumer)
        Consumes this pair if it's evaluated to true o a given predicate
        Parameters:
        predicate - the given predicate
        consumer - the consumer that it's invoked if the predicate is evaluated to true