Skip to content

Commit

Permalink
#Closes 77
Browse files Browse the repository at this point in the history
  • Loading branch information
rrighi committed Jul 24, 2019
1 parent 750439d commit 7e3672c
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ public interface NumberInputConfigurator<T extends Number, C extends NumberInput
* @return this
*/
C allowNegative(boolean allowNegative);

/**
* Set whether to use the grouping symbol for numbers representation and for input validation.
* @param useGrouping Whether to use the grouping symbol
* @return this
*/
C useGrouping(boolean useGrouping);

/**
* Sets the minimum number of digits allowed in the fraction portion of a number.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ public interface StringToNumberConverter<T extends Number> extends Converter<Str
*/
void setAllowNegatives(boolean allowNegatives);

/**
* Get whether to use the grouping character.
* @return Whether to use the grouping character.
*/
boolean isUseGrouping();

/**
* Set whether to use the grouping character.
* @param useGrouping Whether to use the grouping character.
*/
void setUseGrouping(boolean useGrouping);

/**
* Get the minimum decimal digits to display.
* @return the minimum decimal digits, <code>-1</code> if not configured
Expand Down Expand Up @@ -77,6 +89,12 @@ public interface StringToNumberConverter<T extends Number> extends Converter<Str
*/
Optional<Character> getDecimalSymbol();

/**
* Get the grouping separator character, if available
* @return the grouping separator character, if available
*/
Optional<Character> getGroupingSymbol();

/**
* Get the regex validation pattern which corresponds to the converter configuration.
* @return the regex validation pattern
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.Optional;
import java.util.function.Function;

import org.apache.commons.lang3.exception.ExceptionUtils;

import com.holonplatform.core.Registration;
import com.holonplatform.core.internal.utils.ObjectUtils;
import com.holonplatform.vaadin.flow.components.HasLabel;
Expand Down Expand Up @@ -84,7 +86,16 @@ public void setValue(V value) {
*/
@Override
public V getValue() {
return convertToModel(input.getValue());
try {
return convertToModel(input.getValue());
} catch (Exception e) {
// notify error
input.hasValidation().ifPresent(v -> {
v.setInvalid(true);
v.setErrorMessage(ExceptionUtils.getRootCauseMessage(e));
});
throw e;
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ public C allowNegative(boolean allowNegative) {
return getConfigurator();
}

@Override
public C useGrouping(boolean useGrouping) {
getConverter().setUseGrouping(useGrouping);
return getConfigurator();
}

/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.flow.components.builders.NumberInputBuilder#minDecimals(int)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public class DefaultStringToNumberConverter<T extends Number> extends AbstractLo
*/
private boolean allowNegatives = true;

/**
* Whether to use grouping
*/
private boolean useGrouping = false;

/**
* Minimum decimals
*/
Expand Down Expand Up @@ -154,6 +159,24 @@ protected Optional<String> getNumberFormatPattern() {
return Optional.ofNullable(numberFormatPattern);
}

/**
* Get whether to use the grouping character.
* @return Whether to use the grouping character.
*/
@Override
public boolean isUseGrouping() {
return useGrouping;
}

/**
* Set whether to use the grouping character.
* @param useGrouping Whether to use the grouping character.
*/
@Override
public void setUseGrouping(boolean useGrouping) {
this.useGrouping = useGrouping;
}

/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.flow.components.converters.StringToNumberConverter#isAllowNegatives()
Expand Down Expand Up @@ -231,11 +254,18 @@ protected NumberFormat getNumberFormat(ValueContext context) {
numberFormat.setMaximumFractionDigits(defaultMaximumFractionDigits);
}
}
// disable grouping
numberFormat.setGroupingUsed(false);
// check grouping
if (!isUseGrouping()) {
numberFormat.setGroupingUsed(false);
}
return numberFormat;
}

/**
* Create the {@link NumberFormat} instance to use for conversions.
* @param context Value context
* @return The {@link NumberFormat} instance to use for conversions
*/
private NumberFormat buildNumberFormat(ValueContext context) {
// check fixed pattern
if (getNumberFormatPattern().isPresent()) {
Expand Down Expand Up @@ -301,6 +331,15 @@ public Optional<Character> getDecimalSymbol() {
return getDecimalFormatSymbols().map(dfs -> dfs.getDecimalSeparator());
}

/**
* Get the grouping separator character, if available
* @return the grouping separator character, if available
*/
@Override
public Optional<Character> getGroupingSymbol() {
return getDecimalFormatSymbols().map(dfs -> dfs.getGroupingSeparator());
}

/*
* (non-Javadoc)
* @see com.holonplatform.vaadin.flow.components.converters.StringToNumberConverter#getValidationPattern()
Expand All @@ -321,6 +360,10 @@ public String getValidationPattern() {
}
return decimalPattern;
} else {
if (isUseGrouping() && getGroupingSymbol().isPresent()) {
final String pattern = "^([0-9])(" + escape(getGroupingSymbol().get()) + "|[0-9])*";
return isAllowNegatives() ? PATTERN_NEGATIVE_PREFIX + pattern : pattern;
}
return isAllowNegatives() ? PATTERN_NEGATIVE_PREFIX + PATTERN_INTEGER : PATTERN_INTEGER;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,17 @@ public class TestStringToNumberConverter {
public void testDoubleConverter() {

final Double dbl = 12345.67d;

StringToNumberConverter<Double> converter = StringToNumberConverter.create(Double.class);

String text = converter.convertToPresentation(dbl, new ValueContext(Locale.US));
assertNotNull(text);
assertEquals("12345.67", text);

StringToNumberConverter<Double> converter1 = StringToNumberConverter.create(Double.class);
converter1.setUseGrouping(true);

String text = converter1.convertToPresentation(dbl, new ValueContext(Locale.US));
text = converter1.convertToPresentation(dbl, new ValueContext(Locale.US));
assertNotNull(text);
assertEquals("12,345.67", text);

Expand Down Expand Up @@ -73,6 +80,7 @@ public void testDoubleConverter() {

// fixed Locale
StringToNumberConverter<Double> converter2 = StringToNumberConverter.create(Double.class, Locale.US);
converter2.setUseGrouping(true);

text = converter2.convertToPresentation(dbl, new ValueContext(Locale.US));
assertNotNull(text);
Expand Down Expand Up @@ -154,10 +162,17 @@ public void testMinMaxDecimals() {
public void testIntegerConverter() {

final Integer itg = 12345;

StringToNumberConverter<Integer> converter = StringToNumberConverter.create(Integer.class);

String text = converter.convertToPresentation(itg, new ValueContext(Locale.US));
assertNotNull(text);
assertEquals("12345", text);

StringToNumberConverter<Integer> converter1 = StringToNumberConverter.create(Integer.class);
converter1.setUseGrouping(true);

String text = converter1.convertToPresentation(itg, new ValueContext(Locale.US));
text = converter1.convertToPresentation(itg, new ValueContext(Locale.US));
assertNotNull(text);
assertEquals("12,345", text);

Expand Down

0 comments on commit 7e3672c

Please sign in to comment.