Class PostgreSQLGuavaRangeType

  • All Implemented Interfaces:
    java.io.Serializable, org.hibernate.type.Type, org.hibernate.usertype.DynamicParameterizedType, org.hibernate.usertype.ParameterizedType, org.hibernate.usertype.UserType

    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
    See Also:
    Serialized Form
    • Nested Class Summary

      • Nested classes/interfaces inherited from interface org.hibernate.usertype.DynamicParameterizedType

        org.hibernate.usertype.DynamicParameterizedType.ParameterType
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.Class<?> elementType  
      private static com.google.common.collect.Range<java.math.BigDecimal> EMPTY_BIGDECIMAL_RANGE
      An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      private static com.google.common.collect.Range<java.time.LocalDate> EMPTY_DATE_RANGE
      An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      private static com.google.common.collect.Range<java.lang.Integer> EMPTY_INT_RANGE
      An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      private static com.google.common.collect.Range<java.time.LocalDateTime> EMPTY_LOCALDATETIME_RANGE
      An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      private static com.google.common.collect.Range<java.lang.Long> EMPTY_LONG_RANGE
      An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      private static com.google.common.collect.Range<java.time.OffsetDateTime> EMPTY_OFFSETDATETIME_RANGE
      An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      private static com.google.common.collect.Range<java.time.ZonedDateTime> EMPTY_ZONEDDATETIME_RANGE
      An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      static PostgreSQLGuavaRangeType INSTANCE  
      private static java.time.format.DateTimeFormatter LOCAL_DATE_TIME  
      private static java.time.format.DateTimeFormatter OFFSET_DATE_TIME  
      private java.lang.reflect.Type type  
      • Fields inherited from interface org.hibernate.usertype.DynamicParameterizedType

        ACCESS_TYPE, ENTITY, IS_DYNAMIC, IS_PRIMARY_KEY, PARAMETER_TYPE, PROPERTY, RETURNED_CLASS, XPROPERTY
    • Field Detail

      • EMPTY_INT_RANGE

        private static final com.google.common.collect.Range<java.lang.Integer> EMPTY_INT_RANGE
        An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      • EMPTY_LONG_RANGE

        private static final com.google.common.collect.Range<java.lang.Long> EMPTY_LONG_RANGE
        An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      • EMPTY_BIGDECIMAL_RANGE

        private static final com.google.common.collect.Range<java.math.BigDecimal> EMPTY_BIGDECIMAL_RANGE
        An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      • EMPTY_LOCALDATETIME_RANGE

        private static final com.google.common.collect.Range<java.time.LocalDateTime> EMPTY_LOCALDATETIME_RANGE
        An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      • EMPTY_OFFSETDATETIME_RANGE

        private static final com.google.common.collect.Range<java.time.OffsetDateTime> EMPTY_OFFSETDATETIME_RANGE
        An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      • EMPTY_ZONEDDATETIME_RANGE

        private static final com.google.common.collect.Range<java.time.ZonedDateTime> EMPTY_ZONEDDATETIME_RANGE
        An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      • EMPTY_DATE_RANGE

        private static final com.google.common.collect.Range<java.time.LocalDate> EMPTY_DATE_RANGE
        An empty int range that satisfies Range.isEmpty() to map PostgreSQL's empty to.
      • LOCAL_DATE_TIME

        private static final java.time.format.DateTimeFormatter LOCAL_DATE_TIME
      • OFFSET_DATE_TIME

        private static final java.time.format.DateTimeFormatter OFFSET_DATE_TIME
      • type

        private java.lang.reflect.Type type
      • elementType

        private java.lang.Class<?> elementType
    • Constructor Detail

      • PostgreSQLGuavaRangeType

        public PostgreSQLGuavaRangeType()
      • PostgreSQLGuavaRangeType

        public PostgreSQLGuavaRangeType​(java.lang.Class<?> elementType)
    • Method Detail

      • sqlTypes

        public int[] sqlTypes()
        Specified by:
        sqlTypes in interface org.hibernate.usertype.UserType
      • get

        protected com.google.common.collect.Range get​(java.sql.ResultSet rs,
                                                      java.lang.String[] names,
                                                      org.hibernate.engine.spi.SharedSessionContractImplementor session,
                                                      java.lang.Object owner)
                                               throws java.sql.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
        names - database column name
        session - current Hibernate Session
        owner - current Hibernate SessionFactoryImplementor
        Returns:
        column value
        Throws:
        java.sql.SQLException - in case of failure
      • set

        protected void set​(java.sql.PreparedStatement st,
                           com.google.common.collect.Range range,
                           int index,
                           org.hibernate.engine.spi.SharedSessionContractImplementor session)
                    throws java.sql.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:
        java.sql.SQLException - in case of failure
      • determineRangeType

        private java.lang.String determineRangeType​(com.google.common.collect.Range<?> range)
      • ofString

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

        public static com.google.common.collect.Range<java.math.BigDecimal> bigDecimalRange​(java.lang.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:
        java.lang.NumberFormatException - when one of the bounds are invalid.
      • integerRange

        public static com.google.common.collect.Range<java.lang.Integer> integerRange​(java.lang.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:
        java.lang.NumberFormatException - when one of the bounds are invalid.
      • longRange

        public static com.google.common.collect.Range<java.lang.Long> longRange​(java.lang.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:
        java.lang.NumberFormatException - when one of the bounds are invalid.
      • localDateTimeRange

        public static com.google.common.collect.Range<java.time.LocalDateTime> localDateTimeRange​(java.lang.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:
        java.time.format.DateTimeParseException - when one of the bounds are invalid.
      • localDateRange

        public static com.google.common.collect.Range<java.time.LocalDate> localDateRange​(java.lang.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:
        java.time.format.DateTimeParseException - when one of the bounds are invalid.
      • zonedDateTimeRange

        public static com.google.common.collect.Range<java.time.ZonedDateTime> zonedDateTimeRange​(java.lang.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:
        java.time.format.DateTimeParseException - when one of the bounds are invalid.
        java.lang.IllegalArgumentException - when bounds time zones are different.
      • offsetDateTimeRange

        public static com.google.common.collect.Range<java.time.OffsetDateTime> offsetDateTimeRange​(java.lang.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:
        java.time.format.DateTimeParseException - when one of the bounds are invalid.
        java.lang.IllegalArgumentException - when bounds time zones are different.
      • parseLocalDateTime

        private static java.util.function.Function<java.lang.String,​java.time.LocalDateTime> parseLocalDateTime()
      • parseZonedDateTime

        private static java.util.function.Function<java.lang.String,​java.time.ZonedDateTime> parseZonedDateTime()
      • parseOffsetDateTime

        private static java.util.function.Function<java.lang.String,​java.time.OffsetDateTime> parseOffsetDateTime()
      • unquote

        private static java.util.function.Function<java.lang.String,​java.lang.String> unquote()
      • asString

        public java.lang.String asString​(com.google.common.collect.Range range)
      • asString

        private java.lang.String asString​(java.lang.Object value)
      • setParameterValues

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

        public java.lang.Class<?> getElementType()