Class PostgreSQLGuavaRangeType

java.lang.Object
io.hypersistence.utils.hibernate.type.ImmutableType<com.google.common.collect.Range>
io.hypersistence.utils.hibernate.type.range.guava.PostgreSQLGuavaRangeType
All Implemented Interfaces:
Serializable, org.hibernate.type.Type, org.hibernate.usertype.DynamicParameterizedType, org.hibernate.usertype.ParameterizedType, org.hibernate.usertype.UserType<com.google.common.collect.Range>

public class PostgreSQLGuavaRangeType extends ImmutableType<com.google.common.collect.Range> implements org.hibernate.usertype.DynamicParameterizedType
Maps a Range object type to a PostgreSQL range column type.

Supported range types:

  • int4range
  • int8range
  • numrange
  • tsrange
  • tstzrange
  • daterange
Author:
Edgar Asatryan, Vlad Mihalcea, Jan-Willem Gmelig Meyling
See Also:
  • Field Details

  • Constructor Details

    • PostgreSQLGuavaRangeType

      public PostgreSQLGuavaRangeType()
    • PostgreSQLGuavaRangeType

      public PostgreSQLGuavaRangeType(Class<?> elementType)
  • Method Details

    • getSqlType

      public int getSqlType()
      Specified by:
      getSqlType in interface org.hibernate.usertype.UserType<com.google.common.collect.Range>
    • get

      protected com.google.common.collect.Range get(ResultSet rs, int position, org.hibernate.engine.spi.SharedSessionContractImplementor session, Object owner) throws SQLException
      Description copied from class: ImmutableType
      Get the column value from the JDBC ResultSet.
      Specified by:
      get in class ImmutableType<com.google.common.collect.Range>
      Parameters:
      rs - JDBC ResultSet
      position - database column position
      session - current Hibernate Session
      owner - current Hibernate SessionFactoryImplementor
      Returns:
      column value
      Throws:
      SQLException - in case of failure
    • set

      protected void set(PreparedStatement st, com.google.common.collect.Range range, int index, org.hibernate.engine.spi.SharedSessionContractImplementor session) throws SQLException
      Description copied from class: ImmutableType
      Set the column value on the provided JDBC PreparedStatement.
      Specified by:
      set in class ImmutableType<com.google.common.collect.Range>
      Parameters:
      st - JDBC PreparedStatement
      range - database column value
      index - database column index
      session - current Hibernate Session
      Throws:
      SQLException - in case of failure
    • ofString

      public static <T extends Comparable<?>> com.google.common.collect.Range<T> ofString(String str, Function<String,T> converter, Class<T> clazz)
    • bigDecimalRange

      public static com.google.common.collect.Range<BigDecimal> bigDecimalRange(String range)
      Creates the BigDecimal range from provided string:
      
           Range<BigDecimal> closed = Range.bigDecimalRange("[0.1,1.1]");
           Range<BigDecimal> halfOpen = Range.bigDecimalRange("(0.1,1.1]");
           Range<BigDecimal> open = Range.bigDecimalRange("(0.1,1.1)");
           Range<BigDecimal> leftUnbounded = Range.bigDecimalRange("(,1.1)");
       
      Parameters:
      range - The range string, for example "[5.5,7.8]".
      Returns:
      The range of BigDecimals.
      Throws:
      NumberFormatException - when one of the bounds are invalid.
    • integerRange

      public static com.google.common.collect.Range<Integer> integerRange(String range)
      Creates the Integer range from provided string:
      
           Range<Integer> closed = Range.integerRange("[1,5]");
           Range<Integer> halfOpen = Range.integerRange("(-1,1]");
           Range<Integer> open = Range.integerRange("(1,2)");
           Range<Integer> leftUnbounded = Range.integerRange("(,10)");
           Range<Integer> unbounded = Range.integerRange("(,)");
       
      Parameters:
      range - The range string, for example "[5,7]".
      Returns:
      The range of Integers.
      Throws:
      NumberFormatException - when one of the bounds are invalid.
    • longRange

      public static com.google.common.collect.Range<Long> longRange(String range)
      Creates the Long range from provided string:
      
           Range<Long> closed = Range.longRange("[1,5]");
           Range<Long> halfOpen = Range.longRange("(-1,1]");
           Range<Long> open = Range.longRange("(1,2)");
           Range<Long> leftUnbounded = Range.longRange("(,10)");
           Range<Long> unbounded = Range.longRange("(,)");
       
      Parameters:
      range - The range string, for example "[5,7]".
      Returns:
      The range of Longs.
      Throws:
      NumberFormatException - when one of the bounds are invalid.
    • localDateTimeRange

      public static com.google.common.collect.Range<LocalDateTime> localDateTimeRange(String range)
      Creates the LocalDateTime range from provided string:
      
           Range<LocalDateTime> closed = Range.localDateTimeRange("[2014-04-28 16:00:49,2015-04-28 16:00:49]");
           Range<LocalDateTime> quoted = Range.localDateTimeRange("[\"2014-04-28 16:00:49\",\"2015-04-28 16:00:49\"]");
           Range<LocalDateTime> iso = Range.localDateTimeRange("[\"2014-04-28T16:00:49.2358\",\"2015-04-28T16:00:49\"]");
       

      The valid formats for bounds are:

      • yyyy-MM-dd HH:mm:ss[.SSSSSS]
      • yyyy-MM-dd'T'HH:mm:ss[.SSSSSS]
      Parameters:
      range - The range string, for example "[2014-04-28 16:00:49,2015-04-28 16:00:49]".
      Returns:
      The range of LocalDateTimes.
      Throws:
      DateTimeParseException - when one of the bounds are invalid.
    • localDateRange

      public static com.google.common.collect.Range<LocalDate> localDateRange(String range)
      Creates the LocalDate range from provided string:
      
           Range<LocalDate> closed = Range.localDateRange("[2014-04-28,2015-04-289]");
           Range<LocalDate> quoted = Range.localDateRange("[\"2014-04-28\",\"2015-04-28\"]");
           Range<LocalDate> iso = Range.localDateRange("[\"2014-04-28\",\"2015-04-28\"]");
       

      The valid formats for bounds are:

      • yyyy-MM-dd
      • yyyy-MM-dd
      Parameters:
      range - The range string, for example "[2014-04-28,2015-04-28]".
      Returns:
      The range of LocalDates.
      Throws:
      DateTimeParseException - when one of the bounds are invalid.
    • zonedDateTimeRange

      public static com.google.common.collect.Range<ZonedDateTime> zonedDateTimeRange(String rangeStr)
      Creates the ZonedDateTime range from provided string:
      
           Range<ZonedDateTime> closed = Range.zonedDateTimeRange("[2007-12-03T10:15:30+01:00\",\"2008-12-03T10:15:30+01:00]");
           Range<ZonedDateTime> quoted = Range.zonedDateTimeRange("[\"2007-12-03T10:15:30+01:00\",\"2008-12-03T10:15:30+01:00\"]");
           Range<ZonedDateTime> iso = Range.zonedDateTimeRange("[2011-12-03T10:15:30+01:00[Europe/Paris], 2012-12-03T10:15:30+01:00[Europe/Paris]]");
       

      The valid formats for bounds are:

      • yyyy-MM-dd HH:mm:ss[.SSSSSS]X
      • yyyy-MM-dd'T'HH:mm:ss[.SSSSSS]X
      Parameters:
      rangeStr - The range string, for example "[2011-12-03T10:15:30+01:00,2012-12-03T10:15:30+01:00]".
      Returns:
      The range of ZonedDateTimes.
      Throws:
      DateTimeParseException - when one of the bounds are invalid.
      IllegalArgumentException - when bounds time zones are different.
    • offsetDateTimeRange

      public static com.google.common.collect.Range<OffsetDateTime> offsetDateTimeRange(String rangeStr)
      Creates the OffsetDateTime range from provided string:
      
           Range<OffsetDateTime> closed = Range.offsetDateTimeRange("[2007-12-03T10:15:30+01:00\",\"2008-12-03T10:15:30+01:00]");
           Range<OffsetDateTime> quoted = Range.offsetDateTimeRange("[\"2007-12-03T10:15:30+01:00\",\"2008-12-03T10:15:30+01:00\"]");
           Range<OffsetDateTime> iso = Range.offsetDateTimeRange("[2011-12-03T10:15:30+01:00[Europe/Paris], 2012-12-03T10:15:30+01:00[Europe/Paris]]");
       

      The valid formats for bounds are:

      • yyyy-MM-dd HH:mm:ss[.SSSSSS]X
      • yyyy-MM-dd'T'HH:mm:ss[.SSSSSS]X
      Parameters:
      rangeStr - The range string, for example "[2011-12-03T10:15:30+01:00,2012-12-03T10:15:30+01:00]".
      Returns:
      The range of OffsetDateTimes.
      Throws:
      DateTimeParseException - when one of the bounds are invalid.
      IllegalArgumentException - when bounds time zones are different.
    • asString

      public String asString(com.google.common.collect.Range range)
    • setParameterValues

      public void setParameterValues(Properties parameters)
      Specified by:
      setParameterValues in interface org.hibernate.usertype.ParameterizedType
    • getElementType

      public Class<?> getElementType()