Package jsonvalues

Class JsArray

  • All Implemented Interfaces:
    java.lang.Iterable<JsValue>, Json<JsArray>, JsValue

    public final class JsArray
    extends java.lang.Object
    implements Json<JsArray>, java.lang.Iterable<JsValue>
    Represents a json array, which is an ordered list of elements.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  JsArray.TYPE
      Type of arrays: SET, MULTISET or LIST.
    • Constructor Summary

      Constructors 
      Constructor Description
      JsArray()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      JsArray append​(JsValue e, JsValue... others)
      Adds one or more elements, starting from the first, to the back of this array.
      JsArray appendAll​(JsArray array)
      Adds all the elements of the given array, starting from the head, to the back of this array.
      boolean containsValue​(JsValue el)
      Returns true if this json contains the given element in the first level.
      JsArray delete​(int index)  
      JsArray delete​(JsPath path)
      Removes the element in this json located at the given path, if it exists, returning the same this instance otherwise
      static JsArray empty()  
      boolean equals​(java.lang.Object that)  
      boolean equals​(JsArray array, JsArray.TYPE ARRAY_AS)
      Returns true if this array is equal to the given as a parameter.
      JsArray filterAllKeys​(java.util.function.BiPredicate<? super JsPath,​? super JsValue> filter)
      Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.
      JsArray filterAllKeys​(java.util.function.Predicate<? super java.lang.String> filter)
      Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.
      JsArray filterAllObjs​(java.util.function.BiPredicate<? super JsPath,​? super JsObj> filter)
      Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.
      JsArray filterAllObjs​(java.util.function.Predicate<? super JsObj> filter)
      Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.
      JsArray filterAllValues​(java.util.function.BiPredicate<? super JsPath,​? super JsPrimitive> filter)
      Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.
      JsArray filterAllValues​(java.util.function.Predicate<? super JsPrimitive> filter)
      Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.
      JsArray filterKeys​(java.util.function.BiPredicate<? super java.lang.String,​? super JsValue> filter)  
      JsArray filterKeys​(java.util.function.Predicate<? super java.lang.String> filter)
      Filters the keys in the first level of this json, removing those that don't ifPredicateElse the predicate.
      JsArray filterObjs​(java.util.function.BiPredicate<? super java.lang.Integer,​? super JsObj> filter)  
      JsArray filterObjs​(java.util.function.Predicate<? super JsObj> filter)
      Filters the pair of jsons in the first level of this json, removing those that don't ifPredicateElse the predicate.
      JsArray filterValues​(java.util.function.BiPredicate<? super java.lang.Integer,​? super JsPrimitive> filter)  
      JsArray filterValues​(java.util.function.Predicate<? super JsPrimitive> filter)
      Filters the pairs of elements in the first level of this json, removing those that don't ifPredicateElse the predicate.
      JsValue get​(int i)
      returns the element located at the specified index or JsNothing if it doesn't exist.
      JsValue get​(JsPath path)
      Returns the element located at the given path or JsNothing if it doesn't exist.
      JsArray getArray​(int index)
      Returns the array located at the given index or null if it doesn't exist or it's not a json array.
      JsArray getArray​(int index, java.util.function.Supplier<JsArray> orElse)
      Returns the array located at the given index or the default value provided if it doesn't exist or it's not a json array.
      java.math.BigDecimal getBigDec​(int index)
      Returns the number located at the given index as a big decimal or null if it doesn't exist or it's not a decimal number.
      java.math.BigDecimal getBigDec​(int index, java.util.function.Supplier<java.math.BigDecimal> orElse)
      Returns the number located at the given index as a big decimal or the default value provided if it doesn't exist or it's not a decimal number.
      java.math.BigInteger getBigInt​(int index)
      Returns the number located at the given index as a big integer or null if it doesn't exist or it's not an integral number.
      java.math.BigInteger getBigInt​(int index, java.util.function.Supplier<java.math.BigInteger> orElse)
      Returns the number located at the given index as a big integer or the default value provided if it doesn't exist or it's not an integral number.
      byte[] getBinary​(int index)
      Returns the array of bytes located at the given index or null if it doesn't exist or it's not an array of bytes.
      byte[] getBinary​(int index, java.util.function.Supplier<byte[]> orElse)
      Returns the array of bytes located at the given index or the default value provided if it doesn't exist or it's not an array of bytes.
      java.lang.Boolean getBool​(int index)
      Returns the boolean located at the given index or null if it doesn't exist.
      java.lang.Boolean getBool​(int index, java.util.function.Supplier<java.lang.Boolean> orElse)
      Returns the boolean located at the given index or the default value provided if it doesn't exist.
      java.lang.Double getDouble​(int index)
      Returns the number located at the given index as a double or null if it doesn't exist or it's not a decimal number.
      java.lang.Double getDouble​(int index, java.util.function.Supplier<java.lang.Double> orElse)
      Returns the number located at the given index as a double or the default value provided if it doesn't exist or it's not a decimal number.
      java.time.Instant getInstant​(int index)
      Returns the instant located at the given index or null if it doesn't exist or it's not an instant.
      java.time.Instant getInstant​(int index, java.util.function.Supplier<java.time.Instant> orElse)
      Returns the instant located at the given index or the default value provided if it doesn't exist or it's not an instant.
      java.lang.Integer getInt​(int index)
      Returns the integral number located at the given index as an integer or null if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an integer.
      java.lang.Integer getInt​(int index, java.util.function.Supplier<java.lang.Integer> orElse)
      Returns the integral number located at the given index as an integer or the default value provided if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an integer.
      java.lang.Long getLong​(int index)
      Returns the long number located at the given index as an long or null if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an long.
      java.lang.Long getLong​(int index, java.util.function.Supplier<java.lang.Long> orElse)
      Returns the long number located at the given index as an long or the default value provided if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an long.
      JsObj getObj​(int index)
      Returns the object located at the given index or null if it doesn't exist or it's not a json object.
      JsObj getObj​(int index, java.util.function.Supplier<JsObj> orElse)
      Returns the object located at the given index or the default value provided if it doesn't exist or it's not a json object.
      java.lang.String getStr​(int index)
      Returns the string located at the given index or null if it doesn't exist.
      java.lang.String getStr​(int index, java.util.function.Supplier<java.lang.String> orElse)
      Returns the string located at the given index or the default value provided if it doesn't exist.
      int hashCode()
      equals method is inherited, so it's implemented.
      JsValue head()
      Returns the first element of this array.
      int id()  
      JsArray init()
      Returns all the elements of this array except the last one.
      JsArray intersection​(JsArray that, JsArray.TYPE ARRAY_AS)
      this.intersection(that, SET) returns an array with the elements that exist in both this and that.
      JsArray intersectionAll​(JsArray that)
      this.intersectionAll(that) behaves as this.intersection(that, LIST), but for those elements that are containers of the same type and are located at the same position, the result is their intersection.
      boolean isArray()
      Returns true if this JsValue is a JsArray
      boolean isEmpty()
      return true if there's no element in this json
      java.util.Iterator<JsValue> iterator()  
      JsValue last()
      Returns the last element of this array.
      JsArray mapAllKeys​(java.util.function.BiFunction<? super JsPath,​? super JsValue,​java.lang.String> fn)
      Maps all the keys of this json.
      JsArray mapAllKeys​(java.util.function.Function<? super java.lang.String,​java.lang.String> fn)
      Maps all the keys of this json.
      JsArray mapAllObjs​(java.util.function.BiFunction<? super JsPath,​? super JsObj,​JsValue> fn)
      Maps all the jsons of this json.
      JsArray mapAllObjs​(java.util.function.Function<? super JsObj,​JsValue> fn)
      Maps all the jsons of this json.
      JsArray mapAllValues​(java.util.function.BiFunction<? super JsPath,​? super JsPrimitive,​? extends JsValue> fn)
      Maps all the values of this json.
      JsArray mapAllValues​(java.util.function.Function<? super JsPrimitive,​? extends JsValue> fn)
      Maps all the values of this json.
      JsArray mapKeys​(java.util.function.BiFunction<? super java.lang.String,​? super JsValue,​java.lang.String> fn)  
      JsArray mapKeys​(java.util.function.Function<? super java.lang.String,​java.lang.String> fn)
      Maps the keys in the first level of this json.
      JsArray mapObjs​(java.util.function.BiFunction<? super java.lang.Integer,​? super JsObj,​JsValue> fn)  
      JsArray mapObjs​(java.util.function.Function<? super JsObj,​JsValue> fn)
      Maps the jsons in the first level of this json.
      JsArray mapValues​(java.util.function.BiFunction<? super java.lang.Integer,​? super JsPrimitive,​? extends JsValue> fn)  
      JsArray mapValues​(java.util.function.Function<? super JsPrimitive,​? extends JsValue> fn)
      Maps the values in the first level of this json.
      static JsArray of​(boolean bool, boolean... others)
      Returns an immutable array from one or more booleans.
      static JsArray of​(double number, double... others)
      Returns an immutable array from one or more doubles.
      static JsArray of​(int number, int... others)
      Returns an immutable array from one or more integers.
      static JsArray of​(long number, long... others)
      Returns an immutable array from one or more longs.
      static JsArray of​(java.lang.String str, java.lang.String... others)
      Returns an immutable array from one or more strings.
      static JsArray of​(java.math.BigInteger number, java.math.BigInteger... others)
      Returns an immutable array from one or more big integers.
      static JsArray of​(JsPair pair, JsPair... others)
      Returns an immutable array from one or more pairs.
      static JsArray of​(JsValue e)  
      static JsArray of​(JsValue e, JsValue e1)
      Returns an immutable two-element array.
      static JsArray of​(JsValue e, JsValue e1, JsValue e2)
      Returns an immutable three-element array.
      static JsArray of​(JsValue e, JsValue e1, JsValue e2, JsValue e3)
      Returns an immutable four-element array.
      static JsArray of​(JsValue e, JsValue e1, JsValue e2, JsValue e3, JsValue e4)
      Returns an immutable five-element array.
      static JsArray of​(JsValue e, JsValue e1, JsValue e2, JsValue e3, JsValue e4, JsValue... rest)
      Returns an immutable array.
      static JsArray ofIterable​(java.lang.Iterable<? extends JsValue> iterable)
      returns an immutable json array from an iterable of json elements
      static JsArray parse​(java.lang.String str)
      Tries to parse the string into an immutable json array.
      static JsArray parseYaml​(java.lang.String str)
      Tries to parse the YAML string into an immutable json array.
      JsArray prepend​(JsValue e, JsValue... others)
      Adds one or more elements, starting from the last, to the front of this array.
      JsArray prependAll​(JsArray array)
      Adds all the elements of the array, starting from the last, to the front of this array.
      <R> java.util.Optional<R> reduce​(java.util.function.BinaryOperator<R> op, java.util.function.BiFunction<? super java.lang.Integer,​? super JsPrimitive,​R> map, java.util.function.BiPredicate<? super java.lang.Integer,​? super JsPrimitive> predicate)  
      <R> java.util.Optional<R> reduce​(java.util.function.BinaryOperator<R> op, java.util.function.Function<? super JsPrimitive,​R> map, java.util.function.Predicate<? super JsPrimitive> predicate)
      Performs a reduction on the values that satisfy the predicate in the first level of this json.
      <R> java.util.Optional<R> reduceAll​(java.util.function.BinaryOperator<R> op, java.util.function.BiFunction<? super JsPath,​? super JsPrimitive,​R> map, java.util.function.BiPredicate<? super JsPath,​? super JsPrimitive> predicate)
      Performs a reduction on the values of this json that satisfy the predicate.
      <R> java.util.Optional<R> reduceAll​(java.util.function.BinaryOperator<R> op, java.util.function.Function<? super JsPrimitive,​R> map, java.util.function.Predicate<? super JsPrimitive> predicate)
      Performs a reduction on the values of this json that satisfy the predicate.
      JsArray set​(int index, JsValue element)  
      JsArray set​(int index, JsValue value, JsValue padElement)  
      JsArray set​(JsPath path, JsValue element)
      Inserts the element at the path in this json, replacing any existing element and filling with JsNull empty indexes in arrays when necessary.
      JsArray set​(JsPath path, JsValue value, JsValue padElement)
      Inserts the element at the path in this json, replacing any existing element and filling with padElement empty indexes in arrays when necessary.
      int size()
      Returns the number of elements in the first level of this json
      java.util.stream.Stream<JsPair> stream()
      Returns a stream over the pairs of elements in the first level of this json object.
      java.util.stream.Stream<JsPair> streamAll()
      Returns a stream over all the pairs of elements in this json object.
      java.util.stream.Stream<JsValue> streamValues()  
      JsArray tail()
      Returns a json array consisting of all elements of this array except the first one.
      java.lang.String toString()
      // Single-check idiom Item 83 from effective java
      JsArray union​(JsArray that, JsArray.TYPE ARRAY_AS)
      this.union(that, SET) returns this plus those elements from that that don't exist in this.
      JsArray unionAll​(JsArray that)
      returns this plus those elements from that which position is >= this.size(), and, at the positions where a container of the same type exists in both this and that, the result is their union.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.lang.Iterable

        forEach, spliterator
    • Constructor Detail

      • JsArray

        public JsArray()
    • Method Detail

      • empty

        public static JsArray empty()
      • of

        public static JsArray of​(JsPair pair,
                                 JsPair... others)
        Returns an immutable array from one or more pairs.
        Parameters:
        pair - a pair
        others - more optional pairs
        Returns:
        an immutable JsArray
        Throws:
        UserError - if an elem of a pair is mutable
      • of

        public static JsArray of​(JsValue e,
                                 JsValue e1,
                                 JsValue e2,
                                 JsValue e3,
                                 JsValue e4,
                                 JsValue... rest)
        Returns an immutable array.
        Parameters:
        e - a JsValue
        e1 - a JsValue
        e2 - a JsValue
        e3 - a JsValue
        e4 - a JsValue
        rest - more optional JsValue
        Returns:
        an immutable JsArray
        Throws:
        UserError - if an elem is a mutable Json
      • of

        public static JsArray of​(JsValue e,
                                 JsValue e1,
                                 JsValue e2,
                                 JsValue e3,
                                 JsValue e4)
        Returns an immutable five-element array.
        Parameters:
        e - a JsValue
        e1 - a JsValue
        e2 - a JsValue
        e3 - a JsValue
        e4 - a JsValue
        Returns:
        an immutable five-element JsArray
        Throws:
        UserError - if an elem is a mutable Json
      • append

        public final JsArray append​(JsValue e,
                                    JsValue... others)
        Adds one or more elements, starting from the first, to the back of this array.
        Parameters:
        e - the JsValue to be added to the back.
        others - more optional JsValue to be added to the back
        Returns:
        a new JsArray
      • of

        public static JsArray of​(JsValue e,
                                 JsValue e1,
                                 JsValue e2,
                                 JsValue e3)
        Returns an immutable four-element array.
        Parameters:
        e - a JsValue
        e1 - a JsValue
        e2 - a JsValue
        e3 - a JsValue
        Returns:
        an immutable four-element JsArray
        Throws:
        UserError - if an elem is a mutable Json
      • of

        public static JsArray of​(JsValue e,
                                 JsValue e1,
                                 JsValue e2)
        Returns an immutable three-element array.
        Parameters:
        e - a JsValue
        e1 - a JsValue
        e2 - a JsValue
        Returns:
        an immutable three-element JsArray
        Throws:
        UserError - if an elem is a mutable Json
      • of

        public static JsArray of​(JsValue e,
                                 JsValue e1)
        Returns an immutable two-element array.
        Parameters:
        e - a JsValue
        e1 - a JsValue
        Returns:
        an immutable two-element JsArray
        Throws:
        UserError - if an elem is a mutable Json
      • of

        public static JsArray of​(java.lang.String str,
                                 java.lang.String... others)
        Returns an immutable array from one or more strings.
        Parameters:
        str - a string
        others - more optional strings
        Returns:
        an immutable JsArray
      • of

        public static JsArray of​(int number,
                                 int... others)
        Returns an immutable array from one or more integers.
        Parameters:
        number - an integer
        others - more optional integers
        Returns:
        an immutable JsArray
      • of

        public static JsArray of​(boolean bool,
                                 boolean... others)
        Returns an immutable array from one or more booleans.
        Parameters:
        bool - an boolean
        others - more optional booleans
        Returns:
        an immutable JsArray
      • of

        public static JsArray of​(long number,
                                 long... others)
        Returns an immutable array from one or more longs.
        Parameters:
        number - a long
        others - more optional longs
        Returns:
        an immutable JsArray
      • of

        public static JsArray of​(java.math.BigInteger number,
                                 java.math.BigInteger... others)
        Returns an immutable array from one or more big integers.
        Parameters:
        number - a big integer
        others - more optional big integers
        Returns:
        an immutable JsArray
      • of

        public static JsArray of​(double number,
                                 double... others)
        Returns an immutable array from one or more doubles.
        Parameters:
        number - a double
        others - more optional doubles
        Returns:
        an immutable JsArray
      • ofIterable

        public static JsArray ofIterable​(java.lang.Iterable<? extends JsValue> iterable)
        returns an immutable json array from an iterable of json elements
        Parameters:
        iterable - the iterable of json elements
        Returns:
        an immutable json array
      • parseYaml

        public static JsArray parseYaml​(java.lang.String str)
        Tries to parse the YAML string into an immutable json array.
        Parameters:
        str - the YAML to be parsed
        Returns:
        a JsArray
        Throws:
        MalformedJson - if the string doesnt represent a json array
      • parse

        public static JsArray parse​(java.lang.String str)
        Tries to parse the string into an immutable json array.
        Parameters:
        str - the string to be parsed
        Returns:
        a JsArray
        Throws:
        MalformedJson - if the string doesnt represent a json array
      • appendAll

        public final JsArray appendAll​(JsArray array)
        Adds all the elements of the given array, starting from the head, to the back of this array.
        Parameters:
        array - the JsArray of elements to be added to the back
        Returns:
        a new JsArray
      • equals

        public boolean equals​(JsArray array,
                              JsArray.TYPE ARRAY_AS)
        Returns true if this array is equal to the given as a parameter. In the case of ARRAY_AS=LIST, this method is equivalent to JsArray.equals(Object).
        Parameters:
        array - the given array
        ARRAY_AS - option to define if arrays are considered SETS, LISTS OR MULTISET
        Returns:
        true if both arrays are equals according to ARRAY_AS parameter
      • getInt

        public java.lang.Integer getInt​(int index)
        Returns the integral number located at the given index as an integer or null if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an integer.
        Parameters:
        index - the index
        Returns:
        the integral number located at the given index or null
      • getInt

        public java.lang.Integer getInt​(int index,
                                        java.util.function.Supplier<java.lang.Integer> orElse)
        Returns the integral number located at the given index as an integer or the default value provided if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an integer.
        Parameters:
        index - the index
        orElse - the default value
        Returns:
        the integral number located at the given index or null
      • getLong

        public java.lang.Long getLong​(int index)
        Returns the long number located at the given index as an long or null if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an long.
        Parameters:
        index - the index
        Returns:
        the long number located at the given index or null
      • getLong

        public java.lang.Long getLong​(int index,
                                      java.util.function.Supplier<java.lang.Long> orElse)
        Returns the long number located at the given index as an long or the default value provided if it doesn't exist or it's not an integral number or it's an integral number but doesn't fit in an long.
        Parameters:
        index - the index
        orElse - the default value
        Returns:
        the long number located at the given index or the default value provided
      • getStr

        public java.lang.String getStr​(int index)
        Returns the string located at the given index or null if it doesn't exist.
        Parameters:
        index - the index
        Returns:
        the string located at the given index or null
      • getStr

        public java.lang.String getStr​(int index,
                                       java.util.function.Supplier<java.lang.String> orElse)
        Returns the string located at the given index or the default value provided if it doesn't exist.
        Parameters:
        index - the index
        orElse - the default value
        Returns:
        the string located at the given index or the default value
      • getInstant

        public java.time.Instant getInstant​(int index)
        Returns the instant located at the given index or null if it doesn't exist or it's not an instant. If the element is an instant formatted as a string, it's returned as an instant as well.
        Parameters:
        index - the given index
        Returns:
        an instant
      • getInstant

        public java.time.Instant getInstant​(int index,
                                            java.util.function.Supplier<java.time.Instant> orElse)
        Returns the instant located at the given index or the default value provided if it doesn't exist or it's not an instant. If the element is an instant formatted as a string, it's returned as an instant as well.
        Parameters:
        index - the given index
        orElse - the default value
        Returns:
        an instant
      • getBinary

        public byte[] getBinary​(int index)
        Returns the array of bytes located at the given index or null if it doesn't exist or it's not an array of bytes. If the element is a string in base64, it's returned as an array of bytes as well.
        Parameters:
        index - the given index
        Returns:
        an array of bytes
      • getBinary

        public byte[] getBinary​(int index,
                                java.util.function.Supplier<byte[]> orElse)
        Returns the array of bytes located at the given index or the default value provided if it doesn't exist or it's not an array of bytes. If the element is a string in base64, it's returned as an array of bytes as well.
        Parameters:
        index - the given index
        orElse - the default value
        Returns:
        an array of bytes
      • getBool

        public java.lang.Boolean getBool​(int index)
        Returns the boolean located at the given index or null if it doesn't exist.
        Parameters:
        index - the index
        Returns:
        the boolean located at the given index or null
      • getBool

        public java.lang.Boolean getBool​(int index,
                                         java.util.function.Supplier<java.lang.Boolean> orElse)
        Returns the boolean located at the given index or the default value provided if it doesn't exist.
        Parameters:
        index - the index
        orElse - the default value
        Returns:
        the boolean located at the given index or the default value provided
      • getDouble

        public java.lang.Double getDouble​(int index)
        Returns the number located at the given index as a double or null if it doesn't exist or it's not a decimal number. If the number is a BigDecimal, the conversion is identical to the specified in BigDecimal.doubleValue() and in some cases it can lose information about the precision of the BigDecimal
        Parameters:
        index - the index
        Returns:
        the double number located at the given index or null
      • getDouble

        public java.lang.Double getDouble​(int index,
                                          java.util.function.Supplier<java.lang.Double> orElse)
        Returns the number located at the given index as a double or the default value provided if it doesn't exist or it's not a decimal number. If the number is a BigDecimal, the conversion is identical to the specified in BigDecimal.doubleValue() and in some cases it can lose information about the precision of the BigDecimal
        Parameters:
        index - the index
        orElse - the default value
        Returns:
        the double number located at the given index or null
      • getBigDec

        public java.math.BigDecimal getBigDec​(int index)
        Returns the number located at the given index as a big decimal or null if it doesn't exist or it's not a decimal number.
        Parameters:
        index - the index
        Returns:
        the decimal number located at the given index or null
      • getBigDec

        public java.math.BigDecimal getBigDec​(int index,
                                              java.util.function.Supplier<java.math.BigDecimal> orElse)
        Returns the number located at the given index as a big decimal or the default value provided if it doesn't exist or it's not a decimal number.
        Parameters:
        index - the index
        orElse - the default value
        Returns:
        the decimal number located at the given index or the default value provided
      • getBigInt

        public java.math.BigInteger getBigInt​(int index)
        Returns the number located at the given index as a big integer or null if it doesn't exist or it's not an integral number.
        Parameters:
        index - the index
        Returns:
        the integral number located at the given index or null
      • getBigInt

        public java.math.BigInteger getBigInt​(int index,
                                              java.util.function.Supplier<java.math.BigInteger> orElse)
        Returns the number located at the given index as a big integer or the default value provided if it doesn't exist or it's not an integral number.
        Parameters:
        index - the index
        orElse - the default value provided
        Returns:
        the integral number located at the given index or null
      • getObj

        public JsObj getObj​(int index)
        Returns the object located at the given index or null if it doesn't exist or it's not a json object.
        Parameters:
        index - the index
        Returns:
        the object located at the given index or null
      • getObj

        public JsObj getObj​(int index,
                            java.util.function.Supplier<JsObj> orElse)
        Returns the object located at the given index or the default value provided if it doesn't exist or it's not a json object.
        Parameters:
        index - the index
        orElse - the default value
        Returns:
        the object located at the given index or the default value provided
      • getArray

        public JsArray getArray​(int index)
        Returns the array located at the given index or null if it doesn't exist or it's not a json array.
        Parameters:
        index - the index
        Returns:
        the array located at the given index or null
      • getArray

        public JsArray getArray​(int index,
                                java.util.function.Supplier<JsArray> orElse)
        Returns the array located at the given index or the default value provided if it doesn't exist or it's not a json array.
        Parameters:
        index - the index
        orElse - the default value
        Returns:
        the array located at the given index or the default value provided
      • containsValue

        public final boolean containsValue​(JsValue el)
        Description copied from interface: Json
        Returns true if this json contains the given element in the first level.
        Specified by:
        containsValue in interface Json<JsArray>
        Parameters:
        el - the give element JsValue whose presence in this JsArray is to be tested
        Returns:
        true if this JsArray contains the JsValue
      • get

        public JsValue get​(JsPath path)
        Description copied from interface: Json
        Returns the element located at the given path or JsNothing if it doesn't exist.
        Specified by:
        get in interface Json<JsArray>
        Parameters:
        path - the JsPath object of the element that will be returned
        Returns:
        the JsValue located at the given JsPath or JsNothing if it doesn't exist
      • filterValues

        public final JsArray filterValues​(java.util.function.BiPredicate<? super java.lang.Integer,​? super JsPrimitive> filter)
      • filterAllValues

        public final JsArray filterAllValues​(java.util.function.BiPredicate<? super JsPath,​? super JsPrimitive> filter)
        Description copied from interface: Json
        Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.
        Specified by:
        filterAllValues in interface Json<JsArray>
        Parameters:
        filter - the predicate which takes as the input every JsPair of this json
        Returns:
        same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
      • filterAllValues

        public JsArray filterAllValues​(java.util.function.Predicate<? super JsPrimitive> filter)
        Description copied from interface: Json
        Filters all the pairs of elements of this json, removing those that don't ifPredicateElse the predicate.
        Specified by:
        filterAllValues in interface Json<JsArray>
        Parameters:
        filter - the predicate which takes as the input every JsPair of this json
        Returns:
        same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
      • filterKeys

        public final JsArray filterKeys​(java.util.function.BiPredicate<? super java.lang.String,​? super JsValue> filter)
      • filterKeys

        public JsArray filterKeys​(java.util.function.Predicate<? super java.lang.String> filter)
        Description copied from interface: Json
        Filters the keys in the first level of this json, removing those that don't ifPredicateElse the predicate.
        Specified by:
        filterKeys in interface Json<JsArray>
        Parameters:
        filter - the predicate which takes as the input every JsPair in the first level of this json
        Returns:
        same this instance if all the keys satisfy the predicate or a new filtered json of the same type T
        See Also:
        how to filter the keys of the whole json and not only the first level
      • filterAllKeys

        public final JsArray filterAllKeys​(java.util.function.BiPredicate<? super JsPath,​? super JsValue> filter)
        Description copied from interface: Json
        Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.
        Specified by:
        filterAllKeys in interface Json<JsArray>
        Parameters:
        filter - the predicate which takes as the input every JsPair of this json
        Returns:
        same this instance if all the keys satisfy the predicate or a new filtered json of the same type T
      • filterAllKeys

        public JsArray filterAllKeys​(java.util.function.Predicate<? super java.lang.String> filter)
        Description copied from interface: Json
        Filters all the keys of this json, removing those that don't ifPredicateElse the predicate.
        Specified by:
        filterAllKeys in interface Json<JsArray>
        Parameters:
        filter - the predicate which takes as the input every JsPair of this json
        Returns:
        same this instance if all the keys satisfy the predicate or a new filtered json of the same type T
      • filterObjs

        public final JsArray filterObjs​(java.util.function.BiPredicate<? super java.lang.Integer,​? super JsObj> filter)
      • filterObjs

        public JsArray filterObjs​(java.util.function.Predicate<? super JsObj> filter)
        Description copied from interface: Json
        Filters the pair of jsons in the first level of this json, removing those that don't ifPredicateElse the predicate.
        Specified by:
        filterObjs in interface Json<JsArray>
        Parameters:
        filter - the predicate which takes as the input every JsPair in the first level of this json
        Returns:
        same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
      • filterAllObjs

        public final JsArray filterAllObjs​(java.util.function.BiPredicate<? super JsPath,​? super JsObj> filter)
        Description copied from interface: Json
        Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.
        Specified by:
        filterAllObjs in interface Json<JsArray>
        Parameters:
        filter - the predicate which takes as the input every JsPair of this json
        Returns:
        same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
      • filterAllObjs

        public JsArray filterAllObjs​(java.util.function.Predicate<? super JsObj> filter)
        Description copied from interface: Json
        Filters all the pair of jsons of this json, removing those that don't ifPredicateElse the predicate.
        Specified by:
        filterAllObjs in interface Json<JsArray>
        Parameters:
        filter - the predicate which takes as the input every JsPair of this json
        Returns:
        same this instance if all the pairs satisfy the predicate or a new filtered json of the same type T
      • isEmpty

        public final boolean isEmpty()
        Description copied from interface: Json
        return true if there's no element in this json
        Specified by:
        isEmpty in interface Json<JsArray>
        Returns:
        true if empty, false otherwise
      • mapValues

        public final JsArray mapValues​(java.util.function.BiFunction<? super java.lang.Integer,​? super JsPrimitive,​? extends JsValue> fn)
      • mapValues

        public JsArray mapValues​(java.util.function.Function<? super JsPrimitive,​? extends JsValue> fn)
        Description copied from interface: Json
        Maps the values in the first level of this json.
        Specified by:
        mapValues in interface Json<JsArray>
        Parameters:
        fn - the mapping function
        Returns:
        a new mapped json of the same type T
      • mapAllValues

        public JsArray mapAllValues​(java.util.function.BiFunction<? super JsPath,​? super JsPrimitive,​? extends JsValue> fn)
        Description copied from interface: Json
        Maps all the values of this json.
        Specified by:
        mapAllValues in interface Json<JsArray>
        Parameters:
        fn - the mapping function
        Returns:
        a new mapped json of the same type T
      • mapAllValues

        public JsArray mapAllValues​(java.util.function.Function<? super JsPrimitive,​? extends JsValue> fn)
        Description copied from interface: Json
        Maps all the values of this json.
        Specified by:
        mapAllValues in interface Json<JsArray>
        Parameters:
        fn - the mapping function
        Returns:
        a new mapped json of the same type T
      • mapKeys

        public final JsArray mapKeys​(java.util.function.BiFunction<? super java.lang.String,​? super JsValue,​java.lang.String> fn)
      • mapKeys

        public JsArray mapKeys​(java.util.function.Function<? super java.lang.String,​java.lang.String> fn)
        Description copied from interface: Json
        Maps the keys in the first level of this json.
        Specified by:
        mapKeys in interface Json<JsArray>
        Parameters:
        fn - the mapping function
        Returns:
        a new mapped json of the same type T
      • mapAllKeys

        public final JsArray mapAllKeys​(java.util.function.BiFunction<? super JsPath,​? super JsValue,​java.lang.String> fn)
        Description copied from interface: Json
        Maps all the keys of this json.
        Specified by:
        mapAllKeys in interface Json<JsArray>
        Parameters:
        fn - the mapping function
        Returns:
        a new mapped json of the same type T
      • mapAllKeys

        public JsArray mapAllKeys​(java.util.function.Function<? super java.lang.String,​java.lang.String> fn)
        Description copied from interface: Json
        Maps all the keys of this json.
        Specified by:
        mapAllKeys in interface Json<JsArray>
        Parameters:
        fn - the mapping function
        Returns:
        a new mapped json of the same type T
      • mapObjs

        public final JsArray mapObjs​(java.util.function.BiFunction<? super java.lang.Integer,​? super JsObj,​JsValue> fn)
      • mapObjs

        public JsArray mapObjs​(java.util.function.Function<? super JsObj,​JsValue> fn)
        Description copied from interface: Json
        Maps the jsons in the first level of this json.
        Specified by:
        mapObjs in interface Json<JsArray>
        Parameters:
        fn - the mapping function
        Returns:
        a new mapped json of the same type T
      • mapAllObjs

        public final JsArray mapAllObjs​(java.util.function.BiFunction<? super JsPath,​? super JsObj,​JsValue> fn)
        Description copied from interface: Json
        Maps all the jsons of this json.
        Specified by:
        mapAllObjs in interface Json<JsArray>
        Parameters:
        fn - the mapping function
        Returns:
        a new mapped json of the same type T
      • mapAllObjs

        public JsArray mapAllObjs​(java.util.function.Function<? super JsObj,​JsValue> fn)
        Description copied from interface: Json
        Maps all the jsons of this json.
        Specified by:
        mapAllObjs in interface Json<JsArray>
        Parameters:
        fn - the mapping function
        Returns:
        a new mapped json of the same type T
      • set

        public JsArray set​(JsPath path,
                           JsValue element)
        Description copied from interface: Json
        Inserts the element at the path in this json, replacing any existing element and filling with JsNull empty indexes in arrays when necessary.

        The same instance is returned when the head of the path is a key and this is an array or the head of the path is an index and this is an object or the element is JsNothing

        Specified by:
        set in interface Json<JsArray>
        Parameters:
        path - the JsPath object where the element will be inserted at
        element - the JsValue that will be inserted
        Returns:
        the same instance or a new json of the same type T
      • set

        public final JsArray set​(JsPath path,
                                 JsValue value,
                                 JsValue padElement)
        Description copied from interface: Json
        Inserts the element at the path in this json, replacing any existing element and filling with padElement empty indexes in arrays when necessary.

        The same instance is returned when the head of the path is a key and this is an array or the head of the path is an index and this is an object or the element is JsNothing

        Specified by:
        set in interface Json<JsArray>
        Parameters:
        path - the JsPath object where the element will be inserted at
        value - the JsValue that will be inserted
        padElement - the JsValue that will be inserted in arrays when padding is necessary
        Returns:
        the same instance or a new json of the same type T
      • reduce

        public final <R> java.util.Optional<R> reduce​(java.util.function.BinaryOperator<R> op,
                                                      java.util.function.BiFunction<? super java.lang.Integer,​? super JsPrimitive,​R> map,
                                                      java.util.function.BiPredicate<? super java.lang.Integer,​? super JsPrimitive> predicate)
      • reduce

        public <R> java.util.Optional<R> reduce​(java.util.function.BinaryOperator<R> op,
                                                java.util.function.Function<? super JsPrimitive,​R> map,
                                                java.util.function.Predicate<? super JsPrimitive> predicate)
        Description copied from interface: Json
        Performs a reduction on the values that satisfy the predicate in the first level of this json. The reduction is performed mapping each value with the mapping function and then applying the operator
        Specified by:
        reduce in interface Json<JsArray>
        Type Parameters:
        R - the type of the operands of the operator
        Parameters:
        op - the operator upon two objects of type R
        map - the mapping function which produces an object of type R from a JsValue
        predicate - the predicate that determines what JsValue will be mapped and reduced
        Returns:
        an Optional describing the of of the reduction
        See Also:
        to apply the reduction in all the Json and not only in the first level
      • reduceAll

        public final <R> java.util.Optional<R> reduceAll​(java.util.function.BinaryOperator<R> op,
                                                         java.util.function.BiFunction<? super JsPath,​? super JsPrimitive,​R> map,
                                                         java.util.function.BiPredicate<? super JsPath,​? super JsPrimitive> predicate)
        Description copied from interface: Json
        Performs a reduction on the values of this json that satisfy the predicate. The reduction is performed mapping each value with the mapping function and then applying the operator
        Specified by:
        reduceAll in interface Json<JsArray>
        Type Parameters:
        R - the type of the operands of the operator
        Parameters:
        op - the operator upon two objects of type R
        map - the mapping function which produces an object of type R from a JsValue
        predicate - the predicate that determines what JsValue will be mapped and reduced
        Returns:
        an Optional describing the result of the reduction
      • reduceAll

        public <R> java.util.Optional<R> reduceAll​(java.util.function.BinaryOperator<R> op,
                                                   java.util.function.Function<? super JsPrimitive,​R> map,
                                                   java.util.function.Predicate<? super JsPrimitive> predicate)
        Description copied from interface: Json
        Performs a reduction on the values of this json that satisfy the predicate. The reduction is performed mapping each value with the mapping function and then applying the operator
        Specified by:
        reduceAll in interface Json<JsArray>
        Type Parameters:
        R - the type of the operands of the operator
        Parameters:
        op - the operator upon two objects of type R
        map - the mapping function which produces an object of type R from a JsValue
        predicate - the predicate that determines what JsValue will be mapped and reduced
        Returns:
        an Optional describing the result of the reduction
      • delete

        public final JsArray delete​(JsPath path)
        Description copied from interface: Json
        Removes the element in this json located at the given path, if it exists, returning the same this instance otherwise
        Specified by:
        delete in interface Json<JsArray>
        Parameters:
        path - the given JsPath object
        Returns:
        a json of the same type T
      • size

        public final int size()
        Description copied from interface: Json
        Returns the number of elements in the first level of this json
        Specified by:
        size in interface Json<JsArray>
        Returns:
        the number of elements in the first level of this json
      • streamAll

        public java.util.stream.Stream<JsPair> streamAll()
        Description copied from interface: Json
        Returns a stream over all the pairs of elements in this json object.
        Specified by:
        streamAll in interface Json<JsArray>
        Returns:
        a Stream over all the JsPairs in this json
      • stream

        public java.util.stream.Stream<JsPair> stream()
        Description copied from interface: Json
        Returns a stream over the pairs of elements in the first level of this json object.
        Specified by:
        stream in interface Json<JsArray>
        Returns:
        a Stream over all the JsPairs in the first level of this json
      • get

        public JsValue get​(int i)
        returns the element located at the specified index or JsNothing if it doesn't exist. It never throws an exception, it's a total function.
        Parameters:
        i - the index
        Returns:
        a JsValue
      • hashCode

        public final int hashCode()
        equals method is inherited, so it's implemented. The purpose of this method is to cache the hashcode once calculated. the object is immutable and it won't change Single-check idiom Item 83 from Effective Java
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public final boolean equals​(java.lang.Object that)
        Overrides:
        equals in class java.lang.Object
      • toString

        public final java.lang.String toString()
        // Single-check idiom Item 83 from effective java
        Overrides:
        toString in class java.lang.Object
      • head

        public final JsValue head()
        Returns the first element of this array.
        Returns:
        the first JsValue of this JsArray
        Throws:
        UserError - if this JsArray is empty
      • tail

        public final JsArray tail()
        Returns a json array consisting of all elements of this array except the first one.
        Returns:
        a JsArray consisting of all the elements of this JsArray except the head
        Throws:
        UserError - if this JsArray is empty.
      • init

        public final JsArray init()
        Returns all the elements of this array except the last one.
        Returns:
        JsArray with all the JsValue except the last one
        Throws:
        UserError - if this JsArray is empty
      • intersection

        public final JsArray intersection​(JsArray that,
                                          JsArray.TYPE ARRAY_AS)
        this.intersection(that, SET) returns an array with the elements that exist in both this and that. this.intersection(that, MULTISET) returns an array with the elements that exist in both this and that, being duplicates allowed. this.intersection(that, LIST) returns an array with the elements that exist in both this and that and are located at the same position.
        Parameters:
        that - the other array
        ARRAY_AS - option to define if arrays are considered SETS, LISTS OR MULTISET
        Returns:
        a new JsArray of the same type as the inputs (mutable or immutable)
      • intersectionAll

        public JsArray intersectionAll​(JsArray that)
        this.intersectionAll(that) behaves as this.intersection(that, LIST), but for those elements that are containers of the same type and are located at the same position, the result is their intersection. So this operation is kind of a recursive intersection
        Parameters:
        that - the other array
        Returns:
        a JsArray of the same type as the inputs (mutable or immutable)
      • id

        public int id()
        Specified by:
        id in interface JsValue
      • isArray

        public boolean isArray()
        Description copied from interface: JsValue
        Returns true if this JsValue is a JsArray
        Specified by:
        isArray in interface JsValue
        Returns:
        true if this JsValue is a JsArray
      • iterator

        public final java.util.Iterator<JsValue> iterator()
        Specified by:
        iterator in interface java.lang.Iterable<JsValue>
      • last

        public final JsValue last()
        Returns the last element of this array.
        Returns:
        the last JsValue of this JsArray
        Throws:
        UserError - if this JsArray is empty
      • prepend

        public final JsArray prepend​(JsValue e,
                                     JsValue... others)
        Adds one or more elements, starting from the last, to the front of this array.
        Parameters:
        e - the JsValue to be added to the front.
        others - more optional JsValue to be added to the front
        Returns:
        a new JsArray
      • prependAll

        public final JsArray prependAll​(JsArray array)
        Adds all the elements of the array, starting from the last, to the front of this array.
        Parameters:
        array - the JsArray of elements to be added to the front
        Returns:
        a new JsArray
      • delete

        public final JsArray delete​(int index)
      • union

        public final JsArray union​(JsArray that,
                                   JsArray.TYPE ARRAY_AS)
        this.union(that, SET) returns this plus those elements from that that don't exist in this. this.union(that, MULTISET) returns this plus those elements from that appended to the back. this.union(that, LIST) returns this plus those elements from that which position is >= this.size().
        Parameters:
        that - the other array
        ARRAY_AS - option to define if arrays are considered SETS, LISTS OR MULTISET
        Returns:
        a new json array of the same type as the inputs (mutable or immutable)
      • unionAll

        public final JsArray unionAll​(JsArray that)
        returns this plus those elements from that which position is >= this.size(), and, at the positions where a container of the same type exists in both this and that, the result is their union. This operations doesn't make any sense if arrays are not considered lists, because there is no notion of order.
        Parameters:
        that - the other array
        Returns:
        a new JsArray of the same type as the inputs (mutable or immutable)