Interface ObjectConverter

All Known Implementing Classes:
ObjectConverterDefault

public interface ObjectConverter
Methods to convert an object from a type to another, when possible. Some methods also allow to compare two elements.
  • Method Details

    • isCanBeConvertedTo

      boolean isCanBeConvertedTo(Class<?> referenceType, Object element)
      Can the element be converted to the specified referenceType?
    • isCanBeConvertedToString

      boolean isCanBeConvertedToString(Object element)
      Can the element be converted to a String?
    • isCanBeConvertedToInteger

      boolean isCanBeConvertedToInteger(Object element)
      Can the element be converted to a Integer?
    • isCanBeConvertedToLong

      boolean isCanBeConvertedToLong(Object element)
      Can the element be converted to a Long?
    • isCanBeConvertedToFloat

      boolean isCanBeConvertedToFloat(Object element)
      Can the element be converted to a Float?
    • isCanBeConvertedToDouble

      boolean isCanBeConvertedToDouble(Object element)
      Can the element be converted to a Double?
    • isCanBeConvertedToBoolean

      boolean isCanBeConvertedToBoolean(Object element)
      Can the element be converted to a Boolean?
    • isCanBeConvertedToBigDecimal

      boolean isCanBeConvertedToBigDecimal(Object element)
      Can the element be converted to a BigDecimal?
    • isCanBeConvertedToByteArray

      boolean isCanBeConvertedToByteArray(Object element)
      Can the element be converted to a byte[]?
    • isCanBeConvertedToDateFromJsonDateFormat

      boolean isCanBeConvertedToDateFromJsonDateFormat(Object element)
      Can the element be converted to a Date? The source element must follow the Json date format.
    • isCanBeConvertedToJsonObject

      boolean isCanBeConvertedToJsonObject(Object element)
      Can the element be converted to a JsonObject?
    • isCanBeConvertedToJsonArray

      boolean isCanBeConvertedToJsonArray(Object element)
      Can the element be converted to a JsonArray?
    • isBase64StringOrNull

      boolean isBase64StringOrNull(Object element)
      Returns true if the element is a valid base 64 String, or is null.
    • convertToJsonObject

      JsonObject convertToJsonObject(Object element) throws CantConvertException
      Converts the element to a JsonObject.
      Throws:
      CantConvertException - if the element can't be converted to the required type.
    • convertToJsonArray

      JsonArray convertToJsonArray(Object element) throws CantConvertException
      Converts the element to a JsonArray.
      Throws:
      CantConvertException - if the element can't be converted to the required type.
    • convertToString

      String convertToString(Object element)
      Converts the element to a JsonObject.
    • convertToInteger

      Integer convertToInteger(Object element) throws CantConvertException
      Converts the element to a Integer.

      The toString() method will be called on the object before trying to convert it to an Integer. If the resulting String contains zeros only decimals, those will be removed and the conversion will work.

      For example : "123.0" will work.

      Throws:
      CantConvertException - if the element can't be converted to the required type.
    • convertToLong

      Long convertToLong(Object element) throws CantConvertException
      Converts the element to a Long.

      The toString() method will be called on the object before trying to convert it to a Long. If the resulting String contains zeros only decimals, those will be removed and the conversion will work.

      For example : "123.0" will work.

      Throws:
      CantConvertException - if the element can't be converted to the required type.
    • convertToFloat

      Float convertToFloat(Object element) throws CantConvertException
      Converts the element to a Float.
      Throws:
      CantConvertException - if the element can't be converted to the required type.
    • convertToDouble

      Double convertToDouble(Object element) throws CantConvertException
      Converts the element to a Double.
      Throws:
      CantConvertException - if the element can't be converted to the required type.
    • convertToBoolean

      Boolean convertToBoolean(Object element) throws CantConvertException
      Converts the element to a JsonArray.

      Il can be converted to a Boolean if its already a Boolean, if it's null or if it's the String "true" or the String "false".

      Throws:
      CantConvertException - if the element can't be converted to the required type.
    • convertToBigDecimal

      BigDecimal convertToBigDecimal(Object element) throws CantConvertException
      Converts the element to a BigDecimal.
      Throws:
      CantConvertException - if the element can't be converted to the required type.
    • convertBase64StringToByteArray

      byte[] convertBase64StringToByteArray(Object element) throws CantConvertException
      Converts the element to a BigDecimal.

      Can be converted to a byte[] if its already a byte[], if it's null or if it's a valid base 64 String.

      Throws:
      CantConvertException - if the element can't be converted to the required type.
    • convertByteArrayToBase64String

      String convertByteArrayToBase64String(byte[] byteArray)
      Converts the element to a Base64 String.
    • convertToDateFromJsonDateFormat

      Date convertToDateFromJsonDateFormat(Object element) throws CantConvertException
      Converts the element to a Date.

      The source element must follow the Json date format (ISO 8601).

      Throws:
      CantConvertException - if the element can't be converted to the required type.
    • convertToInstantFromJsonDateFormat

      Instant convertToInstantFromJsonDateFormat(Object element) throws CantConvertException
      Converts the element to a Instant.

      The source element must follow the Json date format (ISO 8601).

      Throws:
      CantConvertException - if the element can't be converted to the required type.
    • convertTo

      <T> T convertTo(Object element, Class<T> targetType) throws CantConvertException
      Tries to convert the element to the specified targetType.

      By default, only the types native to JsonObject are supported as the target types.

      Throws:
      CantConvertException - if the element can't be converted to the required type.
    • convertToJsonDateFormat

      String convertToJsonDateFormat(Date date)
      Converts the date to a String representaiton compatible with the Json specification.
    • compareTo

      int compareTo(Object elementToCompare, Object compareTo) throws CantCompareException
      Compare two elements.

      If required, try to convert the elementToCompare so it can be compared with compareTo. It is also possible that we have to convert compareTo, for the comparison to be possible. For example, if compareTo is Long(10) and elementToCompare is BigDecimal(12.34), we have to convert both elements to BigDecimal so they can be compared.

      For a comparison to work, the two elements must be (after conversion or not) of the same type and this type must implement Comparable.

      Throws:
      CantCompareException - if the elements can't be compared together.
    • isEquivalent

      boolean isEquivalent(Object elementToCompare, Object compareTo)
      Are the two elements equivalent?

      If required, try to convert the elementToCompare so it can be compared with compareTo. It is also possible that we have to convert compareTo, for the comparison to be possible. For example, if compareTo is Long(10) and elementToCompare is BigDecimal(12.34), we have to convert both elements to BigDecimal so they can be compared.

      For the two elements to be equivalent, they must be (after conversion or not) of the same type and then the Object.equals(Object) method must return true.

    • isAtLeastOneEquivalentElementInCommon

      boolean isAtLeastOneEquivalentElementInCommon(Collection<?> col1, Collection<?> col2)
      Do the two Collections have at least one equivalent element in common?