Class PostgreSQLGuavaRangeType
- java.lang.Object
-
- com.vladmihalcea.hibernate.type.ImmutableType<com.google.common.collect.Range>
-
- com.vladmihalcea.hibernate.type.range.guava.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 aRange
object type to a PostgreSQL range column type.Supported range types:
- int4range
- int8range
- numrange
- tsrange
- tstzrange
- daterange
- See Also:
- Serialized Form
-
-
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 satisfiesRange.isEmpty()
to map PostgreSQL'sempty
to.private static com.google.common.collect.Range<java.time.LocalDate>
EMPTY_DATE_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
to.private static com.google.common.collect.Range<java.lang.Integer>
EMPTY_INT_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
to.private static com.google.common.collect.Range<java.time.LocalDateTime>
EMPTY_LOCALDATETIME_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
to.private static com.google.common.collect.Range<java.lang.Long>
EMPTY_LONG_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
to.private static com.google.common.collect.Range<java.time.OffsetDateTime>
EMPTY_OFFSETDATETIME_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
to.private static com.google.common.collect.Range<java.time.ZonedDateTime>
EMPTY_ZONEDDATETIME_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
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
-
Constructor Summary
Constructors Constructor Description PostgreSQLGuavaRangeType()
PostgreSQLGuavaRangeType(java.lang.Class<?> elementType)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.String
asString(com.google.common.collect.Range range)
private java.lang.String
asString(java.lang.Object value)
static com.google.common.collect.Range<java.math.BigDecimal>
bigDecimalRange(java.lang.String range)
Creates theBigDecimal
range from provided string:private java.lang.String
determineRangeType(com.google.common.collect.Range<?> range)
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)
Get the column value from the JDBCResultSet
.java.lang.Class<?>
getElementType()
static com.google.common.collect.Range<java.lang.Integer>
integerRange(java.lang.String range)
Creates theInteger
range from provided string:static com.google.common.collect.Range<java.time.LocalDate>
localDateRange(java.lang.String range)
Creates theLocalDate
range from provided string:static com.google.common.collect.Range<java.time.LocalDateTime>
localDateTimeRange(java.lang.String range)
Creates theLocalDateTime
range from provided string:static com.google.common.collect.Range<java.lang.Long>
longRange(java.lang.String range)
Creates theLong
range from provided string:static com.google.common.collect.Range<java.time.OffsetDateTime>
offsetDateTimeRange(java.lang.String rangeStr)
Creates theOffsetDateTime
range from provided string: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)
private static java.util.function.Function<java.lang.String,java.time.LocalDateTime>
parseLocalDateTime()
private static java.util.function.Function<java.lang.String,java.time.OffsetDateTime>
parseOffsetDateTime()
private static java.util.function.Function<java.lang.String,java.time.ZonedDateTime>
parseZonedDateTime()
protected void
set(java.sql.PreparedStatement st, com.google.common.collect.Range range, int index, org.hibernate.engine.spi.SharedSessionContractImplementor session)
Set the column value on the provided JDBCPreparedStatement
.void
setParameterValues(java.util.Properties parameters)
int[]
sqlTypes()
private static java.util.function.Function<java.lang.String,java.lang.String>
unquote()
static com.google.common.collect.Range<java.time.ZonedDateTime>
zonedDateTimeRange(java.lang.String rangeStr)
Creates theZonedDateTime
range from provided string:-
Methods inherited from class com.vladmihalcea.hibernate.type.ImmutableType
assemble, assemble, beforeAssemble, compare, deepCopy, deepCopy, defaultSizes, dictatedSizes, disassemble, disassemble, equals, getColumnSpan, getConfiguration, getHashCode, getHashCode, getName, getReturnedClass, getSemiResolvedType, hashCode, hydrate, isAnyType, isAssociationType, isCollectionType, isComponentType, isDirty, isDirty, isDirty, isEntityType, isEqual, isEqual, isModified, isMutable, isSame, nullSafeGet, nullSafeGet, nullSafeSet, nullSafeSet, replace, replace, replace, resolve, returnedClass, semiResolve, sqlTypes, toColumnNullness, toLoggableString
-
-
-
-
Field Detail
-
EMPTY_INT_RANGE
private static final com.google.common.collect.Range<java.lang.Integer> EMPTY_INT_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
to.
-
EMPTY_LONG_RANGE
private static final com.google.common.collect.Range<java.lang.Long> EMPTY_LONG_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
to.
-
EMPTY_BIGDECIMAL_RANGE
private static final com.google.common.collect.Range<java.math.BigDecimal> EMPTY_BIGDECIMAL_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
to.
-
EMPTY_LOCALDATETIME_RANGE
private static final com.google.common.collect.Range<java.time.LocalDateTime> EMPTY_LOCALDATETIME_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
to.
-
EMPTY_OFFSETDATETIME_RANGE
private static final com.google.common.collect.Range<java.time.OffsetDateTime> EMPTY_OFFSETDATETIME_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
to.
-
EMPTY_ZONEDDATETIME_RANGE
private static final com.google.common.collect.Range<java.time.ZonedDateTime> EMPTY_ZONEDDATETIME_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
to.
-
EMPTY_DATE_RANGE
private static final com.google.common.collect.Range<java.time.LocalDate> EMPTY_DATE_RANGE
An empty int range that satisfiesRange.isEmpty()
to map PostgreSQL'sempty
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
-
INSTANCE
public static final PostgreSQLGuavaRangeType INSTANCE
-
type
private java.lang.reflect.Type type
-
elementType
private java.lang.Class<?> elementType
-
-
Method Detail
-
sqlTypes
public int[] sqlTypes()
- Specified by:
sqlTypes
in interfaceorg.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 JDBCResultSet
.- Specified by:
get
in classImmutableType<com.google.common.collect.Range>
- Parameters:
rs
- JDBCResultSet
names
- database column namesession
- current HibernateSession
owner
- current HibernateSessionFactoryImplementor
- 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 JDBCPreparedStatement
.- Specified by:
set
in classImmutableType<com.google.common.collect.Range>
- Parameters:
st
- JDBCPreparedStatement
range
- database column valueindex
- database column indexsession
- current HibernateSession
- 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 theBigDecimal
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
BigDecimal
s. - 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 theInteger
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
Integer
s. - 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 theLong
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
Long
s. - 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 theLocalDateTime
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
LocalDateTime
s. - 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 theLocalDate
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
LocalDate
s. - 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 theZonedDateTime
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
ZonedDateTime
s. - 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 theOffsetDateTime
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
OffsetDateTime
s. - 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 interfaceorg.hibernate.usertype.ParameterizedType
-
getElementType
public java.lang.Class<?> getElementType()
-
-