Skip to content

Commit

Permalink
HSEARCH-3099 Add method parse to ValueBridge
Browse files Browse the repository at this point in the history
  • Loading branch information
fax4ever committed Apr 19, 2019
1 parent 0cb2e85 commit 5248d87
Show file tree
Hide file tree
Showing 24 changed files with 463 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.hibernate.search.engine.cfg.spi.ConfigurationProperty;
import org.hibernate.search.engine.cfg.ConfigurationPropertySource;
import org.hibernate.search.engine.cfg.spi.ConvertUtils;
import org.hibernate.search.engine.logging.impl.Log;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.function.Function;

import org.hibernate.search.engine.cfg.spi.ConvertUtils;
import org.hibernate.search.engine.cfg.spi.KeyContext;
import org.hibernate.search.engine.cfg.spi.OptionalPropertyContext;
import org.hibernate.search.engine.environment.bean.BeanReference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.function.Supplier;
import java.util.regex.Pattern;

import org.hibernate.search.engine.cfg.spi.ConvertUtils;
import org.hibernate.search.engine.cfg.spi.DefaultedPropertyContext;
import org.hibernate.search.engine.cfg.spi.OptionalConfigurationProperty;
import org.hibernate.search.engine.cfg.spi.OptionalPropertyContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.engine.cfg.impl;
package org.hibernate.search.engine.cfg.spi;

import java.lang.invoke.MethodHandles;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -34,6 +36,7 @@ private ConvertUtils() {
* @param expectedType the expected type
* @param parser a parser from String to the expected type
* @param value the value to convert (a String)
* @param <T> The type of the returned value.
* @return the converted value
* @throws SearchException for invalid values.
*/
Expand Down Expand Up @@ -125,6 +128,150 @@ public static Long convertLong(Object value) {
throw log.invalidLongPropertyValue( "", null );
}

/**
* Convert a property value from String to byte if necessary.
*
* @param value the value to convert (a Number, or a String to be parsed)
* @return the converted byte
* @throws SearchException for invalid format or values.
*/
public static Byte convertByte(Object value) {
try {
if ( value instanceof Number ) {
return ( (Number) value ).byteValue();
}
if ( value instanceof String ) {
return Byte.parseByte( (String) value );
}
}
catch (RuntimeException e) {
throw log.invalidNumberPropertyValue( Byte.class, e.getMessage(), e );
}

throw log.invalidNumberPropertyValue( Byte.class, "", null );
}

/**
* Convert a property value from String to short if necessary.
*
* @param value the value to convert (a Number, or a String to be parsed)
* @return the converted short
* @throws SearchException for invalid format or values.
*/
public static Short convertShort(Object value) {
try {
if ( value instanceof Number ) {
return ( (Number) value ).shortValue();
}
if ( value instanceof String ) {
return Short.parseShort( (String) value );
}
}
catch (RuntimeException e) {
throw log.invalidNumberPropertyValue( Short.class, e.getMessage(), e );
}

throw log.invalidNumberPropertyValue( Short.class, "", null );
}

/**
* Convert a property value from String to float if necessary.
*
* @param value the value to convert (a Number, or a String to be parsed)
* @return the converted float
* @throws SearchException for invalid format or values.
*/
public static Float convertFloat(Object value) {
try {
if ( value instanceof Number ) {
return ( (Number) value ).floatValue();
}
if ( value instanceof String ) {
return Float.parseFloat( (String) value );
}
}
catch (RuntimeException e) {
throw log.invalidNumberPropertyValue( Float.class, e.getMessage(), e );
}

throw log.invalidNumberPropertyValue( Float.class, "", null );
}

/**
* Convert a property value from String to double if necessary.
*
* @param value the value to convert (a Number, or a String to be parsed)
* @return the converted double
* @throws SearchException for invalid format or values.
*/
public static Double convertDouble(Object value) {
try {
if ( value instanceof Number ) {
return ( (Number) value ).doubleValue();
}
if ( value instanceof String ) {
return Double.parseDouble( (String) value );
}
}
catch (RuntimeException e) {
throw log.invalidNumberPropertyValue( Double.class, e.getMessage(), e );
}

throw log.invalidNumberPropertyValue( Double.class, "", null );
}

/**
* Convert a property value from String to BigDecimal if necessary.
*
* @param value the value to convert (a BigDecimal, a Number, or a String to be parsed)
* @return the converted BigDecimal
* @throws SearchException for invalid format or values.
*/
public static BigDecimal convertBigDecimal(Object value) {
try {
if ( value instanceof BigDecimal ) {
return (BigDecimal) value;
}
if ( value instanceof Number ) {
return BigDecimal.valueOf( ( (Number) value ).doubleValue() );
}
if ( value instanceof String ) {
return new BigDecimal( (String) value );
}
}
catch (RuntimeException e) {
throw log.invalidNumberPropertyValue( BigDecimal.class, e.getMessage(), e );
}

throw log.invalidNumberPropertyValue( BigDecimal.class, "", null );
}

/**
* Convert a property value from String to BigInteger if necessary.
*
* @param value the value to convert (a BigInteger, a Number, or a String to be parsed)
* @return the converted BigInteger
* @throws SearchException for invalid format or values.
*/
public static BigInteger convertBigInteger(Object value) {
try {
if ( value instanceof BigInteger ) {
return (BigInteger) value;
}
if ( value instanceof Number ) {
return BigInteger.valueOf( ( (Number) value ).longValue() );
}
if ( value instanceof String ) {
return new BigInteger( (String) value );
}
}
catch (RuntimeException e) {
throw log.invalidNumberPropertyValue( BigInteger.class, e.getMessage(), e );
}

throw log.invalidNumberPropertyValue( BigInteger.class, "", null );
}

public static <T> BeanReference<? extends T> convertBeanReference(Class<T> expectedType, Object value) {
try {
if ( expectedType.isInstance( value ) ) {
Expand Down Expand Up @@ -174,7 +321,7 @@ else if ( value instanceof String ) {
}
}

static Object trimIfString(Object value) {
public static Object trimIfString(Object value) {
if ( value instanceof String ) {
String stringValue = (String) value;
String trimmed = stringValue.trim();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.engine.cfg.spi;

import static java.time.temporal.ChronoField.DAY_OF_MONTH;
import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
import static java.time.temporal.ChronoField.YEAR;

import java.lang.invoke.MethodHandles;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.format.SignStyle;

import org.hibernate.search.engine.logging.impl.Log;
import org.hibernate.search.util.common.impl.TimeHelper;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;

public final class ParseUtils {

private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );

private ParseUtils() {
// Private constructor, do not use
}

public static String parseString(String value) {
return value;
}

public static Instant parseInstant(String value) {
try {
// Using the default ISO format
return Instant.parse( value );
}
catch (DateTimeParseException e) {
throw log.unableToParseTemporal( Instant.class, value, DateTimeFormatter.ISO_INSTANT, e );
}
}

public static LocalDate parseLocalDate(String value) {
try {
// Using the default ISO format
return LocalDate.parse( value );
}
catch (DateTimeParseException e) {
throw log.unableToParseTemporal( LocalDate.class, value, DateTimeFormatter.ISO_LOCAL_DATE, e );
}
}

public static LocalDateTime parseLocalDateTime(String value) {
try {
// Using the default ISO format
return LocalDateTime.parse( value );
}
catch (DateTimeParseException e) {
throw log.unableToParseTemporal( LocalDateTime.class, value, DateTimeFormatter.ISO_LOCAL_DATE_TIME, e );
}
}

public static LocalTime parseLocalTime(String value) {
try {
// Using the default ISO format
return LocalTime.parse( value );
}
catch (DateTimeParseException e) {
throw log.unableToParseTemporal( LocalTime.class, value, DateTimeFormatter.ISO_LOCAL_TIME, e );
}
}

public static OffsetDateTime parseOffsetDateTime(String value) {
try {
// Using the default ISO format
return OffsetDateTime.parse( value );
}
catch (DateTimeParseException e) {
throw log.unableToParseTemporal( OffsetDateTime.class, value, DateTimeFormatter.ISO_OFFSET_DATE_TIME, e );
}
}

public static OffsetTime parseOffsetTime(String value) {
try {
return OffsetTime.parse( value );
}
catch (DateTimeParseException e) {
throw log.unableToParseTemporal( OffsetTime.class, value, DateTimeFormatter.ISO_OFFSET_TIME, e );
}
}

public static ZonedDateTime parseZonedDateTime(String value) {
// Using the default ISO format
DateTimeFormatter formatter = DateTimeFormatter.ISO_ZONED_DATE_TIME;

try {
return TimeHelper.parseZoneDateTime( value, formatter );
}
catch (DateTimeParseException e) {
throw log.unableToParseTemporal( ZonedDateTime.class, value, formatter, e );
}
}

public static Year parseYear(String value) {
// we prefer here to use a fixed format
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendValue( YEAR, 4, 10, SignStyle.EXCEEDS_PAD )
.toFormatter();

try {
return Year.parse( value );
}
catch (DateTimeParseException e) {
throw log.unableToParseTemporal( Year.class, value, formatter, e );
}
}

public static YearMonth parseYearMonth(String value) {
// we prefer here to use a fixed format
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendValue( YEAR, 4, 10, SignStyle.EXCEEDS_PAD )
.appendLiteral( '-' )
.appendValue( MONTH_OF_YEAR, 2 )
.toFormatter();

try {
return YearMonth.parse( value );
}
catch (DateTimeParseException e) {
throw log.unableToParseTemporal( YearMonth.class, value, formatter, e );
}
}

public static MonthDay parseMonthDay(String value) {
// we prefer here to use a fixed format
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendLiteral( "--" )
.appendValue( MONTH_OF_YEAR, 2 )
.appendLiteral( '-' )
.appendValue( DAY_OF_MONTH, 2 )
.toFormatter();

try {
return MonthDay.parse( value );
}
catch (DateTimeParseException e) {
throw log.unableToParseTemporal( MonthDay.class, value, formatter, e );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

package org.hibernate.search.engine.logging.impl;

import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Collection;
import java.util.List;

import org.hibernate.search.engine.environment.classpath.spi.ClassLoadingException;
import org.hibernate.search.engine.logging.spi.MappableTypeModelFormatter;
import org.hibernate.search.engine.logging.spi.MappingKeyFormatter;
import org.hibernate.search.engine.logging.spi.SimpleNameClassFormatter;
import org.hibernate.search.engine.mapper.mapping.spi.MappingKey;
import org.hibernate.search.engine.mapper.model.spi.MappableTypeModel;
import org.hibernate.search.engine.spatial.GeoPoint;
Expand Down Expand Up @@ -266,5 +269,10 @@ SearchException invalidBeanType(
value = "Invalid pattern: the pattern to match in wildcard predicates must be non-null.")
SearchException wildcardPredicateCannotMatchNullPattern(@Param EventContext context);

@Message(id = ID_OFFSET_2 + 57, value = "'%1$s' instance cannot be parsed from value: '%2$s', using the expected formatter: '%3$s'.")
SearchException unableToParseTemporal(@FormatWith(SimpleNameClassFormatter.class) Class<? extends TemporalAccessor> type, String value, DateTimeFormatter formatter,
@Cause Exception cause);

@Message(id = ID_OFFSET_2 + 58, value = "Invalid %1$s value: expected either a Number or a String that can be parsed into a %1$s. %2$s")
SearchException invalidNumberPropertyValue(@FormatWith(SimpleNameClassFormatter.class) Class<? extends Number> type, String nestedErrorMessage, @Cause Exception cause);
}
Loading

0 comments on commit 5248d87

Please sign in to comment.