Class RepeatedVector

All Implemented Interfaces:
Serializable, Iterable<Number>, Collection<Number>, List<Number>, RandomAccess, SequencedCollection<Number>

final class RepeatedVector extends Vector implements Serializable
A vector whose values are the repetitions of the values given in a base vector. This vector can be created as a result of Vector.compress(double). The intent is to handle the cases found in netCDF files where localization grids (e.g. the "longitude" variable storing longitude values of all points in a grid) contains a lot of repetitions.

cycleLength is usually the length of the base vector, but not necessarily. If occurrences = 1 and cycleLength = 4 for example, then this class handles repetitions like below:

If occurrences > 1, then this class handles repetitions in a different way (in this example, cycleLength is still 4):
Since:
1.0
Version:
1.1
  • Field Details

    • serialVersionUID

      private static final long serialVersionUID
      For cross-version compatibility.
      See Also:
    • base

      private final Vector base
      The vector on which this vector is derived from.
    • occurrences

      private final int occurrences
      Number of times that each base element appears in a row before to move to the next base element. See class javadoc for more information.
    • cycleLength

      private final int cycleLength
      Length of the sequence of values to repeat, after conversion to base vector indices. Usually equals to the length of the base vector but can also be smaller. Shall not be greater than base.size(). See class javadoc for more information.
    • size

      private final int size
      The size of this vector. This is often cycleLength × occurrences, but not necessarily.
  • Constructor Details

    • RepeatedVector

      RepeatedVector(Vector base, int occurrences, int cycleLength, int size)
      Creates a new vector of repeated data.
      Parameters:
      base - the vector on which this vector is derived from.
      occurrences - number of time that each element is repeated.
      cycleLength - length of the sequence of values to repeat.
      size - this vector size, usually base.size() * repetition.
    • RepeatedVector

      RepeatedVector(Vector base, int[] repetitions, double tolerance)
      Creates a vector of repeated data from the result of a call to Vector.repetitions(int...).
      Parameters:
      base - the vector on which this vector is derived from.
      repetitions - results of Vector.repetitions(int...). Must be non-empty.
      tolerance - tolerance factor for compression of the base vector.
  • Method Details

    • toBase

      private int toBase(int index)
      Converts the given index from this vector domain to an index in the base vector.
    • getElementType

      public final Class<? extends Number> getElementType()
      Returns the type of values, which is inherited from the base vector.
      Specified by:
      getElementType in class Vector
      Returns:
      the type of elements in this vector.
      See Also:
    • isEmptyOrNaN

      public final boolean isEmptyOrNaN()
      Forwards to the base vector.
      Overrides:
      isEmptyOrNaN in class Vector
      Returns:
      whether this vector is empty or contains only NaN values.
    • isSinglePrecision

      public final boolean isSinglePrecision()
      Description copied from class: Vector
      Returns true if values in this vector can be casted to single-precision floating point numbers (float) without precision lost. In case of doubt, this method conservatively returns false.
      Overrides:
      isSinglePrecision in class Vector
      Returns:
      whether values in this vector can be casted to float primitive type.
      See Also:
    • isInteger

      public final boolean isInteger()
      Description copied from class: Vector
      Returns true if this vector contains only integer values. This method may iterate over all values for performing this verification.
      Overrides:
      isInteger in class Vector
      Returns:
      true if this vector contains only integer values.
    • isUnsigned

      public final boolean isUnsigned()
      Description copied from class: Vector
      Returns true if integer values shall be interpreted as unsigned values. This method may return true for data stored in byte[], short[], int[] or long[] arrays, but never for data stored in float[] and double[] arrays. The default implementation returns false.

      Unless otherwise noticed in Javadoc, users do not need to care about this information since Vector methods will perform automatically the operations needed for unsigned integers.

      Overrides:
      isUnsigned in class Vector
      Returns:
      true if the integer values shall be interpreted as unsigned values.
    • size

      public final int size()
      Description copied from class: Vector
      Returns the number of elements in this vector.
      Specified by:
      size in interface Collection<Number>
      Specified by:
      size in interface List<Number>
      Specified by:
      size in class Vector
      Returns:
      the number of elements in this vector.
    • isNaN

      public final boolean isNaN(int i)
      Description copied from class: Vector
      Returns true if the value at the given index is null or NaN.
      Specified by:
      isNaN in class Vector
      Parameters:
      i - the index in the [0 … size-1] range.
      Returns:
      true if the value at the given index is NaN.
    • doubleValue

      public final double doubleValue(int i)
      Description copied from class: Vector
      Returns the value at the given index as a double. This is the safest method since all primitive types supported by Vector are convertible to the double type.
      Specified by:
      doubleValue in class Vector
      Parameters:
      i - the index in the [0 … size-1] range.
      Returns:
      the value at the given index.
      See Also:
    • floatValue

      public final float floatValue(int i)
      Description copied from class: Vector
      Returns the value at the given index as a float. This method may result in a lost of precision if this vector stores or computes its values with the double type.

      The default implementation delegates to Vector.doubleValue(int) and cast the result to float.

      Overrides:
      floatValue in class Vector
      Parameters:
      i - the index in the [0 … size-1] range.
      Returns:
      the value at the given index.
      See Also:
    • longValue

      public final long longValue(int i)
      Description copied from class: Vector
      Returns the value at the given index as a long. If this vector uses floating point values, the value is rounded to the nearest integer.

      The default implementation delegates to Vector.doubleValue(int) and verifies if the result can be rounded to a long with an error not greater than 0.5. Subclasses that store or compute their values with an integer type should override this method.

      Overrides:
      longValue in class Vector
      Parameters:
      i - the index in the [0 … size-1] range.
      Returns:
      the value at the given index.
    • intValue

      public final int intValue(int i)
      Description copied from class: Vector
      Returns the value at the given index as an int. If this vector uses floating point values, the value is rounded to the nearest integer.

      The default implementation delegates to Vector.longValue(int) and verifies if the result fits in the int type. Subclasses that store or compute their values with the int, short or byte type should override this method.

      Overrides:
      intValue in class Vector
      Parameters:
      i - the index in the [0 … size-1] range.
      Returns:
      the value at the given index.
    • shortValue

      public final short shortValue(int i)
      Description copied from class: Vector
      Returns the value at the given index as a short. If this vector uses floating point values, the value is rounded to the nearest integer.

      The default implementation delegates to Vector.longValue(int) and verifies if the result fits in the short type. Subclasses that store or compute their values with the short or byte type should override this method.

      Overrides:
      shortValue in class Vector
      Parameters:
      i - the index in the [0 … size-1] range.
      Returns:
      the value at the given index.
    • byteValue

      public final byte byteValue(int i)
      Description copied from class: Vector
      Returns the value at the given index as a byte. If this vector uses floating point values, the value is rounded to the nearest integer.

      The default implementation delegates to Vector.longValue(int) and verifies if the result fits in the byte type. Subclasses that store or compute their values with the byte type should override this method.

      Overrides:
      byteValue in class Vector
      Parameters:
      i - the index in the [0 … size-1] range.
      Returns:
      the value at the given index.
    • stringValue

      public final String stringValue(int i)
      Description copied from class: Vector
      Returns a string representation of the value at the given index. Invoking this method is generally equivalent to invoking String.valueOf(get(index)) except if the values are unsigned integers.
      Specified by:
      stringValue in class Vector
      Parameters:
      i - the index in the [0 … size-1] range.
      Returns:
      a string representation of the value at the given index (may be null).
      See Also:
    • get

      public final Number get(int i)
      Description copied from class: Vector
      Returns the number at the given index, or null if none. The object returned by this method is usually an instance of the class returned by Vector.getElementType(), but may also be an instance of a wider type if this is necessary for representing the values.
      Example: if Vector.getElementType() returns Byte.class but Vector.isUnsigned() returns true, then this method will rather return instances of Short because that type is the smallest Java primitive type capable to hold byte values in the [0 … 255] range. But the elements are still stored internally as byte, and the vector cannot accept values outside the [0 … 255] range even if they are valid Short values.
      The class of returned objects should be stable. For example, this method should not use different types for different range of values. This stability is recommended but not guaranteed because Vector can also wrap arbitrary Number[] arrays.
      Specified by:
      get in interface List<Number>
      Specified by:
      get in class Vector
      Parameters:
      i - the index in the [0 … size-1] range.
      Returns:
      the value at the given index (may be null).
    • range

      public final NumberRange<?> range()
      The range of values in this vector is the range of values in the base vector if we use all its data.
      Overrides:
      range in class Vector
      Returns:
      minimal and maximal values found in this vector.
    • range

      final NumberRange<?> range(IntSupplier indices, int count)
      Overridden for efficiency in case base itself overrides that method. Overriding that method is optional; the default implementation would have worked.
      Overrides:
      range in class Vector
      Parameters:
      indices - supplier of indices of the values to examine for computing the range, or null for the 0, 1, 2, … n-1 sequence.
      count - number of indices to get from the supplier.
      Returns:
      the range of all values at the given indices.
    • set

      public final Number set(int index, Number value)
      Do not allow setting values.
      Specified by:
      set in interface List<Number>
      Specified by:
      set in class Vector
      Parameters:
      index - the index in the [0 … size-1] range.
      value - the value to set at the given index.
      Returns:
      the value previously stored at the given index.
    • repetitions

      public int[] repetitions(int... candidates)
      Returns the parameters used by this RepeatedVector instance on the assumption that they are the result of a previous invocation to Vector.repetitions(int...).
      Overrides:
      repetitions in class Vector
      Parameters:
      candidates - probable values, or null or an empty array if unknown. If non-empty, those values will be used for narrowing the search, which may improve performances. There is no guarantee that the values returned by this method will be among the given candidates.
      Returns:
      the number of times that entities (numbers, or group of numbers) appears consecutively with identical values. If no such repetition is found, an empty array.
      See Also:
    • repeat

      public Vector repeat(boolean eachValue, int count)
      Returns a vector whose value is the content of this vector repeated count times.
      Overrides:
      repeat in class Vector
      Parameters:
      eachValue - whether to apply the repetition on each value (true) or on the whole vector (false).
      count - number of repetitions as a positive number (including zero).
      Returns:
      this vector repeated count time.
      See Also:
    • increment

      public final Number increment(double tolerance)
      Returns null since the repetition of a sequence of numbers implies that there is no regular increment. An exception to this rule would be if the base vector contains a constant value or if the repetition is exactly 1, but we should not have created a RepeatedVector in such cases.
      Overrides:
      increment in class Vector
      Parameters:
      tolerance - the tolerance threshold for verifying if the increment is constant.
      Returns:
      the increment as a signed value, or null if the increment is not constant.
    • compress

      public Vector compress(double tolerance)
      Returns this since this vector is considered already compressed. Actually it may be possible to compress more if the base vector has been modified after RepeatedVector construction. But it should not happen since this vector is read-only and Vector.compress(double) recommends to not keep reference to the original vector.
      Overrides:
      compress in class Vector
      Parameters:
      tolerance - maximal difference allowed between original and compressed vectors (can be zero).
      Returns:
      a more compact vector with the same data than this vector, or this.
    • backingVector

      final Vector backingVector()
      Informs Vector.pick(int...) that this vector is backed by another vector.
      Overrides:
      backingVector in class Vector
    • toBacking

      final int[] toBacking(int[] indices)
      Converts an array of indexes used by this vector to the indexes used by the backing vector. This method must also check index validity.
      Overrides:
      toBacking in class Vector
      Parameters:
      indices - the indexes given by the user.
      Returns:
      the indexes to use. Must be a new array in order to protect it from user changes.
    • createSubSampling

      Vector createSubSampling(int first, int step, int length)
      Implementation of Vector.subSampling(int,int,int). Arguments validity has been verified by the caller.
      Overrides:
      createSubSampling in class Vector
      Parameters:
      first - index of the first value to be included in the returned view.
      step - the index increment in this vector between two consecutive values in the returned vector. Can be positive, zero or negative.
      length - the length of the vector to be returned. Cannot be greater than the length of this vector, except if the step is zero.