Class CharacterEncoding

All Implemented Interfaces:
Encoding<Character>, NaturalSortAware, Serializable, Comparator<Character>

public class CharacterEncoding extends PrimitiveEncoding<Character>
Character type.
See Also:
  • Constructor Details

    • CharacterEncoding

      public CharacterEncoding(EncodingId encodingId)
  • Method Details

    • withEncodingId

      public CharacterEncoding withEncodingId(EncodingId encodingId)
      Description copied from interface: Encoding
      Build an encoding that has the given EncodingId but is otherwise equivalent to this encoding.

      If this encoding already has encodingId, then this method may (but is not required to) return this same instance.

      Parameters:
      encodingId - new encoding's EncodingId, or null for an anonymized encoding
      Returns:
      a version of this encoding with the given EncodingId
    • read

      public Character read(ByteReader reader)
      Description copied from interface: Encoding
      Read a value from the given input.
      Parameters:
      reader - byte input
      Returns:
      field value (possibly null)
    • write

      public void write(ByteWriter writer, Character value)
      Description copied from interface: Encoding
      Write a value to the given output.
      Parameters:
      writer - byte output
      value - value to write (possibly null)
    • skip

      public void skip(ByteReader reader)
      Description copied from interface: Encoding
      Read and discard a byte[] encoded value from the given input.
      Parameters:
      reader - byte input
    • toString

      public String toString(Character value)
      Description copied from interface: Encoding
      Encode a non-null value as a String for later decoding by fromString().

      Each of the characters in the returned String must be one of the valid XML characters (tab, newline, carriage return, \u0020 - \ud7ff, and \ue000 - \ufffd).

      Specified by:
      toString in interface Encoding<Character>
      Overrides:
      toString in class PrimitiveEncoding<Character>
      Parameters:
      value - actual value, never null
      Returns:
      string encoding of value acceptable to fromString()
      See Also:
    • fromString

      public Character fromString(String string)
      Description copied from interface: Encoding
      Parse a non-null value previously encoded by toString(T).
      Specified by:
      fromString in interface Encoding<Character>
      Overrides:
      fromString in class PrimitiveEncoding<Character>
      Parameters:
      string - non-null value previously encoded as a String by toString(T)
      Returns:
      actual value
    • convert

      public <S> Character convert(Encoding<S> type, S value)
      Description copied from interface: Encoding
      Attempt to convert a value from the given Encoding into a value of this Encoding.

      For a non-null value, the implementation in Encoding first checks whether the value is already a valid value for this encoding; if so, the value is returned. Otherwise, it invokes encoding.toString(value) to convert value into a String, and then attempts to parse that string via this.fromString(); if the parse fails, an IllegalArgumentException is thrown. Note this means that any value will convert successfully to a String, as long as it doesn't contain an invalid escape sequence (see StringEncoding.toString(java.lang.String)).

      If value is null, the implementation in Encoding returns null, unless this encoding does not support null values, in which case an IllegalArgumentException is thrown.

      Permazen's built-in encodings include the following conversions:

      • Non-boolean Primitive types:
        • Convert from other non-boolean primitive types as if by the corresponding Java cast
        • Convert from boolean by converting to zero (if false) or one (if true)
      • Boolean: converts from other primitive types as if by value != 0
      • A char[] array and a String are convertible to each other
      • A char and a String of length one are convertible to each other (other Strings are not)
      • Arrays: converted by converting each array element individually (if possible)
      Specified by:
      convert in interface Encoding<Character>
      Overrides:
      convert in class PrimitiveEncoding<Character>
      Type Parameters:
      S - source encoding
      Parameters:
      type - the Encoding of value
      value - the value to convert
      Returns:
      value converted to this instance's type
    • convertNumber

      protected Character convertNumber(Number value)
      Specified by:
      convertNumber in class PrimitiveEncoding<Character>