garydgregory commented on a change in pull request #47: URL: https://github.com/apache/commons-beanutils/pull/47#discussion_r550223787
########## File path: src/main/java/org/apache/commons/beanutils2/converters/ColorConverter.java ########## @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.awt.Color; +import java.util.Objects; + +/** + * <p>Converts a configuration property into a Java {@link Color} object.</p> + * + * <p> + * This converter aims to be compatible with some some of the web color + * formats supported by browsers with CSS, rather than only support + * literal interpretations of numbers, such as: + * </p> + * + * <ul> + * <li>#RGB</li> + * <li>#RGBA</li> + * <li>#RRGGBBAA</li> + * </ul> + * + * <p> + * This converter will use the web based hexadecimal interpretations if + * the value is prefixed with {@link #HEX_COLOR_PREFIX}. + * + * If using a literal number, or {@link Color#decode(String)} is desired, you must + * prefix your value with <code>0x</code> instead of {@link #HEX_COLOR_PREFIX}. + * </p> + * + * @since 2.0.0 + */ +public class ColorConverter extends AbstractConverter { + + /** To be a web based hexadecimal color, it must be prefixed with this. */ + private static final String HEX_COLOR_PREFIX = "#"; + + /** + * Construct a <b>{@link Color}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public ColorConverter() { + super(); + } + + /** + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public ColorConverter(final Object defaultValue) { + super(defaultValue); + } + + @Override + protected Class<?> getDefaultType() { + return Color.class; + } + + /** + * <p> + * Convert the configuration value to a Java {@link Color} object, + * by reading the hexadecimal {@link String} and converting each component. + * </p> + * + * <p> + * This can also interpret raw color names based on the standard colors + * defined in Java, such as the following: + * </p> + * + * <ul> + * <li>{@link Color#WHITE}</li> + * <li>{@link Color#LIGHT_GRAY}</li> + * <li>{@link Color#GRAY}</li> + * <li>{@link Color#DARK_GRAY}</li> + * <li>{@link Color#BLACK}</li> + * <li>{@link Color#RED}</li> + * <li>{@link Color#PINK}</li> + * <li>{@link Color#ORANGE}</li> + * <li>{@link Color#YELLOW}</li> + * <li>{@link Color#GREEN}</li> + * <li>{@link Color#MAGENTA}</li> + * <li>{@link Color#CYAN}</li> + * <li>{@link Color#BLUE}</li> + * </ul> + * + * <small> + * Implementation Notes: We specifically avoid the use of {@link Color#decode(String)} + * for hexadecimal {@link String}s starting with {@link #HEX_COLOR_PREFIX} + * as it does not provide the desired result. + * The {@link Color#decode(String)} method uses {@link Integer#decode(String)} + * under the hood to convert the input to a number, which means input like + * <code>#FFF</code> gets interpreted incorrectly as it's literally converted + * to the number <code>0xFFF</code>, rather than the color, <code>#FFFFFF</code> which + * it is short hand for. It also doesn't work for <code>#FFFFFFFF</code> due to it + * being unable to parse as an {@link Integer}. + * If this is desired, then this method falls back to using {@link Color#decode(String)}, + * so for literal hexadecimal values you prefix it with <code>0x</code> instead of + * {@link #HEX_COLOR_PREFIX}. + * </small> + * + * @param value The String property value to convert. + * @return A {@link Color} which represents the compiled configuration property. + * @throws NullPointerException If the value is null. + * @throws NumberFormatException If an invalid number is provided. + */ + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + if (Color.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Value can't be null."); + final String stringValue = value.toString(); + + switch (stringValue.toLowerCase()) { + case "white": + return type.cast(Color.WHITE); + case "lightgray": + return type.cast(Color.LIGHT_GRAY); + case "gray": + return type.cast(Color.GRAY); + case "darkgray": + return type.cast(Color.DARK_GRAY); + case "black": + return type.cast(Color.BLACK); + case "red": + return type.cast(Color.RED); + case "pink": + return type.cast(Color.PINK); + case "orange": + return type.cast(Color.ORANGE); + case "yellow": + return type.cast(Color.YELLOW); + case "green": + return type.cast(Color.GREEN); + case "magenta": + return type.cast(Color.MAGENTA); + case "cyan": + return type.cast(Color.CYAN); + case "blue": + return type.cast(Color.BLUE); + default: + // Do nothing. + } + + if (stringValue.startsWith(HEX_COLOR_PREFIX)) { + return type.cast(parseWebColor(stringValue)); + } + + return type.cast(Color.decode(stringValue)); + } + + throw conversionException(type, value); + } + + /** + * <small> Review comment: Let's not use "small" formatting. In general, start Javadoc sentences with the action, in this case: "Parses ...". ########## File path: src/main/java/org/apache/commons/beanutils2/converters/DimensionConverter.java ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.awt.Dimension; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * <p> + * Allows converting configuration values to a {@link Dimension}. + * This can be used to load settings for default dimensions of {@link java.awt.Component}s + * {@link java.awt.Window}s, or {@link java.awt.Shape}s. + * </p> + * + * <p> + * Accepts a single {@link Integer} value, or two {@link Integer} values + * separated by the character <code>x</code>. + * </p> + * + * <p>The dimensions must not be negative, and must be {@link Integer} values.</p> + * + * @since 2.0.0 + */ +public class DimensionConverter extends AbstractConverter { + + /** Regular expression used to validate and tokenizer the {@link String}. */ + private static final Pattern DIMENSION_PATTERN = Pattern.compile("(?<x>\\d+)(?:x(?<y>\\d+))?"); + + /** + * Construct a <b>{@link Dimension}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public DimensionConverter() { + super(); + } + + /** + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public DimensionConverter(final Object defaultValue) { + super(defaultValue); + } + + @Override + protected Class<?> getDefaultType() { + return Dimension.class; + } + + /** + * @param value The String property value to convert. Review comment: Missing 1st sentence. ########## File path: src/main/java/org/apache/commons/beanutils2/converters/DimensionConverter.java ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.awt.Dimension; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * <p> Review comment: First sentence: "Allows converting..." -> "Converts...". All methods "allow" what they are coded to do, so it says nothing to "allow" ;-) ########## File path: src/main/java/org/apache/commons/beanutils2/ConvertUtilsBean.java ########## @@ -516,10 +534,13 @@ private void registerStandard(final boolean throwException, final boolean defaul private void registerOther(final boolean throwException) { Review comment: Note to self/community: We need to revisit all of the registration business before we release 2.0 and consider using a service loader instead of hard coding everything. ########## File path: src/main/java/org/apache/commons/beanutils2/converters/ColorConverter.java ########## @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.awt.Color; +import java.util.Objects; + +/** + * <p>Converts a configuration property into a Java {@link Color} object.</p> + * + * <p> + * This converter aims to be compatible with some some of the web color + * formats supported by browsers with CSS, rather than only support + * literal interpretations of numbers, such as: + * </p> + * + * <ul> + * <li>#RGB</li> + * <li>#RGBA</li> + * <li>#RRGGBBAA</li> + * </ul> + * + * <p> + * This converter will use the web based hexadecimal interpretations if + * the value is prefixed with {@link #HEX_COLOR_PREFIX}. + * + * If using a literal number, or {@link Color#decode(String)} is desired, you must + * prefix your value with <code>0x</code> instead of {@link #HEX_COLOR_PREFIX}. + * </p> + * + * @since 2.0.0 + */ +public class ColorConverter extends AbstractConverter { + + /** To be a web based hexadecimal color, it must be prefixed with this. */ + private static final String HEX_COLOR_PREFIX = "#"; + + /** + * Construct a <b>{@link Color}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public ColorConverter() { + super(); + } + + /** + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public ColorConverter(final Object defaultValue) { + super(defaultValue); + } + + @Override + protected Class<?> getDefaultType() { + return Color.class; + } + + /** + * <p> + * Convert the configuration value to a Java {@link Color} object, + * by reading the hexadecimal {@link String} and converting each component. + * </p> + * + * <p> + * This can also interpret raw color names based on the standard colors + * defined in Java, such as the following: + * </p> + * + * <ul> + * <li>{@link Color#WHITE}</li> + * <li>{@link Color#LIGHT_GRAY}</li> + * <li>{@link Color#GRAY}</li> + * <li>{@link Color#DARK_GRAY}</li> + * <li>{@link Color#BLACK}</li> + * <li>{@link Color#RED}</li> + * <li>{@link Color#PINK}</li> + * <li>{@link Color#ORANGE}</li> + * <li>{@link Color#YELLOW}</li> + * <li>{@link Color#GREEN}</li> + * <li>{@link Color#MAGENTA}</li> + * <li>{@link Color#CYAN}</li> + * <li>{@link Color#BLUE}</li> + * </ul> + * + * <small> + * Implementation Notes: We specifically avoid the use of {@link Color#decode(String)} + * for hexadecimal {@link String}s starting with {@link #HEX_COLOR_PREFIX} + * as it does not provide the desired result. + * The {@link Color#decode(String)} method uses {@link Integer#decode(String)} + * under the hood to convert the input to a number, which means input like + * <code>#FFF</code> gets interpreted incorrectly as it's literally converted + * to the number <code>0xFFF</code>, rather than the color, <code>#FFFFFF</code> which + * it is short hand for. It also doesn't work for <code>#FFFFFFFF</code> due to it + * being unable to parse as an {@link Integer}. + * If this is desired, then this method falls back to using {@link Color#decode(String)}, + * so for literal hexadecimal values you prefix it with <code>0x</code> instead of + * {@link #HEX_COLOR_PREFIX}. + * </small> + * + * @param value The String property value to convert. + * @return A {@link Color} which represents the compiled configuration property. + * @throws NullPointerException If the value is null. + * @throws NumberFormatException If an invalid number is provided. + */ + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + if (Color.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Value can't be null."); + final String stringValue = value.toString(); Review comment: Should we also call `String#trim()`? ########## File path: src/main/java/org/apache/commons/beanutils2/converters/ColorConverter.java ########## @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.awt.Color; +import java.util.Objects; + +/** + * <p>Converts a configuration property into a Java {@link Color} object.</p> + * + * <p> + * This converter aims to be compatible with some some of the web color + * formats supported by browsers with CSS, rather than only support + * literal interpretations of numbers, such as: + * </p> + * + * <ul> + * <li>#RGB</li> + * <li>#RGBA</li> + * <li>#RRGGBBAA</li> + * </ul> + * + * <p> + * This converter will use the web based hexadecimal interpretations if + * the value is prefixed with {@link #HEX_COLOR_PREFIX}. + * + * If using a literal number, or {@link Color#decode(String)} is desired, you must + * prefix your value with <code>0x</code> instead of {@link #HEX_COLOR_PREFIX}. + * </p> + * + * @since 2.0.0 + */ +public class ColorConverter extends AbstractConverter { + + /** To be a web based hexadecimal color, it must be prefixed with this. */ + private static final String HEX_COLOR_PREFIX = "#"; + + /** + * Construct a <b>{@link Color}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public ColorConverter() { + super(); + } + + /** + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public ColorConverter(final Object defaultValue) { + super(defaultValue); + } + + @Override + protected Class<?> getDefaultType() { + return Color.class; + } + + /** + * <p> + * Convert the configuration value to a Java {@link Color} object, + * by reading the hexadecimal {@link String} and converting each component. + * </p> + * + * <p> + * This can also interpret raw color names based on the standard colors + * defined in Java, such as the following: + * </p> + * + * <ul> + * <li>{@link Color#WHITE}</li> + * <li>{@link Color#LIGHT_GRAY}</li> + * <li>{@link Color#GRAY}</li> + * <li>{@link Color#DARK_GRAY}</li> + * <li>{@link Color#BLACK}</li> + * <li>{@link Color#RED}</li> + * <li>{@link Color#PINK}</li> + * <li>{@link Color#ORANGE}</li> + * <li>{@link Color#YELLOW}</li> + * <li>{@link Color#GREEN}</li> + * <li>{@link Color#MAGENTA}</li> + * <li>{@link Color#CYAN}</li> + * <li>{@link Color#BLUE}</li> + * </ul> + * + * <small> + * Implementation Notes: We specifically avoid the use of {@link Color#decode(String)} + * for hexadecimal {@link String}s starting with {@link #HEX_COLOR_PREFIX} + * as it does not provide the desired result. + * The {@link Color#decode(String)} method uses {@link Integer#decode(String)} + * under the hood to convert the input to a number, which means input like + * <code>#FFF</code> gets interpreted incorrectly as it's literally converted + * to the number <code>0xFFF</code>, rather than the color, <code>#FFFFFF</code> which + * it is short hand for. It also doesn't work for <code>#FFFFFFFF</code> due to it + * being unable to parse as an {@link Integer}. + * If this is desired, then this method falls back to using {@link Color#decode(String)}, + * so for literal hexadecimal values you prefix it with <code>0x</code> instead of + * {@link #HEX_COLOR_PREFIX}. + * </small> + * + * @param value The String property value to convert. + * @return A {@link Color} which represents the compiled configuration property. + * @throws NullPointerException If the value is null. + * @throws NumberFormatException If an invalid number is provided. + */ + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + if (Color.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Value can't be null."); + final String stringValue = value.toString(); + + switch (stringValue.toLowerCase()) { Review comment: Must pass in a locale here, at least, `Locale.ROOT` if not `Locale.English` since you have US English hard coded, please see https://garygregory.wordpress.com/2015/11/03/java-lowercase-conversion-turkey/ Be ready for a bug reports that "grey" and "DARK_GRAY" are not accepted (note the underscore). I would expect that all Color constant fields are allowed. Please sort the cases alphabetically to make it simpler to maintain and read. IMO this converter should accept as input, the output of `Color#toString()`. ########## File path: src/main/java/org/apache/commons/beanutils2/converters/ColorConverter.java ########## @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.awt.Color; +import java.util.Objects; + +/** + * <p>Converts a configuration property into a Java {@link Color} object.</p> + * + * <p> + * This converter aims to be compatible with some some of the web color + * formats supported by browsers with CSS, rather than only support + * literal interpretations of numbers, such as: + * </p> + * + * <ul> + * <li>#RGB</li> + * <li>#RGBA</li> + * <li>#RRGGBBAA</li> + * </ul> + * + * <p> + * This converter will use the web based hexadecimal interpretations if + * the value is prefixed with {@link #HEX_COLOR_PREFIX}. + * + * If using a literal number, or {@link Color#decode(String)} is desired, you must + * prefix your value with <code>0x</code> instead of {@link #HEX_COLOR_PREFIX}. + * </p> + * + * @since 2.0.0 + */ +public class ColorConverter extends AbstractConverter { + + /** To be a web based hexadecimal color, it must be prefixed with this. */ + private static final String HEX_COLOR_PREFIX = "#"; + + /** + * Construct a <b>{@link Color}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public ColorConverter() { + super(); + } + + /** + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public ColorConverter(final Object defaultValue) { + super(defaultValue); + } + + @Override + protected Class<?> getDefaultType() { + return Color.class; + } + + /** + * <p> + * Convert the configuration value to a Java {@link Color} object, + * by reading the hexadecimal {@link String} and converting each component. + * </p> + * + * <p> + * This can also interpret raw color names based on the standard colors + * defined in Java, such as the following: + * </p> + * + * <ul> + * <li>{@link Color#WHITE}</li> + * <li>{@link Color#LIGHT_GRAY}</li> + * <li>{@link Color#GRAY}</li> + * <li>{@link Color#DARK_GRAY}</li> + * <li>{@link Color#BLACK}</li> + * <li>{@link Color#RED}</li> + * <li>{@link Color#PINK}</li> + * <li>{@link Color#ORANGE}</li> + * <li>{@link Color#YELLOW}</li> + * <li>{@link Color#GREEN}</li> + * <li>{@link Color#MAGENTA}</li> + * <li>{@link Color#CYAN}</li> + * <li>{@link Color#BLUE}</li> + * </ul> + * + * <small> + * Implementation Notes: We specifically avoid the use of {@link Color#decode(String)} + * for hexadecimal {@link String}s starting with {@link #HEX_COLOR_PREFIX} + * as it does not provide the desired result. + * The {@link Color#decode(String)} method uses {@link Integer#decode(String)} + * under the hood to convert the input to a number, which means input like + * <code>#FFF</code> gets interpreted incorrectly as it's literally converted + * to the number <code>0xFFF</code>, rather than the color, <code>#FFFFFF</code> which + * it is short hand for. It also doesn't work for <code>#FFFFFFFF</code> due to it + * being unable to parse as an {@link Integer}. + * If this is desired, then this method falls back to using {@link Color#decode(String)}, + * so for literal hexadecimal values you prefix it with <code>0x</code> instead of + * {@link #HEX_COLOR_PREFIX}. + * </small> + * + * @param value The String property value to convert. + * @return A {@link Color} which represents the compiled configuration property. + * @throws NullPointerException If the value is null. + * @throws NumberFormatException If an invalid number is provided. + */ + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + if (Color.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Value can't be null."); + final String stringValue = value.toString(); Review comment: Please rebase on master, then reuse the new convenience method in `AbstractConverter` to replace: ``` Objects.requireNonNull(value, "Value must not be null."); final String stringValue = value.toString(); ``` with: ``` final String stringValue = toString(value); ``` ########## File path: src/main/java/org/apache/commons/beanutils2/converters/PointConverter.java ########## @@ -0,0 +1,102 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.awt.Point; +import java.util.Objects; +import java.util.regex.Pattern; + +/** + * @since 2.0.0 + */ +public class PointConverter extends AbstractConverter { + + /** Pattern used to split the {@link Point#x} and {@link Point#y} coordinate. */ + private static final Pattern POINT_SPLIT = Pattern.compile("\\s*,\\s*"); + + /** + * Construct a <b>{@link Point}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public PointConverter() { + super(); + } + + /** + * Constructs a {@link org.apache.commons.beanutils2.Converter} that will return + * the specified default value if a conversion error occurs. + * + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public PointConverter(final Object defaultValue) { + super(defaultValue); + } + + /** + * Gets the default type this {@code Converter} handles. + * + * @return The default type this {@code Converter} handles. + * @since 2.0.0 + */ + @Override + protected Class<?> getDefaultType() { + return Point.class; + } + + /** + * @param value The {@link String} property value to convert. + * @return A {@link Point} represented by the x and y coordinate of the input. + * @throws NullPointerException If the value is null. + * @throws IllegalArgumentException If the configuration value is an invalid representation of a {@link Point}. + * @throws NumberFormatException If a one of coordinates can not be parsed to an {@link Integer}. + */ + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + if (Point.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Value must not be null."); + final String stringValue = value.toString(); Review comment: See above. ########## File path: src/main/java/org/apache/commons/beanutils2/converters/DimensionConverter.java ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.awt.Dimension; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * <p> + * Allows converting configuration values to a {@link Dimension}. + * This can be used to load settings for default dimensions of {@link java.awt.Component}s + * {@link java.awt.Window}s, or {@link java.awt.Shape}s. + * </p> + * + * <p> + * Accepts a single {@link Integer} value, or two {@link Integer} values + * separated by the character <code>x</code>. + * </p> + * + * <p>The dimensions must not be negative, and must be {@link Integer} values.</p> + * + * @since 2.0.0 + */ +public class DimensionConverter extends AbstractConverter { + + /** Regular expression used to validate and tokenizer the {@link String}. */ + private static final Pattern DIMENSION_PATTERN = Pattern.compile("(?<x>\\d+)(?:x(?<y>\\d+))?"); + + /** + * Construct a <b>{@link Dimension}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public DimensionConverter() { + super(); + } + + /** + * @param defaultValue The default value to be returned Review comment: Missing 1st sentence. ########## File path: src/main/java/org/apache/commons/beanutils2/converters/DimensionConverter.java ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.awt.Dimension; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * <p> + * Allows converting configuration values to a {@link Dimension}. + * This can be used to load settings for default dimensions of {@link java.awt.Component}s + * {@link java.awt.Window}s, or {@link java.awt.Shape}s. + * </p> + * + * <p> + * Accepts a single {@link Integer} value, or two {@link Integer} values + * separated by the character <code>x</code>. + * </p> + * + * <p>The dimensions must not be negative, and must be {@link Integer} values.</p> + * + * @since 2.0.0 + */ +public class DimensionConverter extends AbstractConverter { + + /** Regular expression used to validate and tokenizer the {@link String}. */ + private static final Pattern DIMENSION_PATTERN = Pattern.compile("(?<x>\\d+)(?:x(?<y>\\d+))?"); + + /** + * Construct a <b>{@link Dimension}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public DimensionConverter() { + super(); + } + + /** + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public DimensionConverter(final Object defaultValue) { + super(defaultValue); + } + + @Override + protected Class<?> getDefaultType() { Review comment: Javadoc. ########## File path: src/main/java/org/apache/commons/beanutils2/converters/InetAddressConverter.java ########## @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Objects; + +/** + * For converting configuration property values to an IP address. Review comment: 1 sentence: "Converts..." ########## File path: src/test/java/org/apache/commons/beanutils2/converters/DimensionConverterTestCase.java ########## @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import org.apache.commons.beanutils2.ConversionException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.awt.Dimension; + +/** + * @since 2.0.0 Review comment: Not a requirement, but I like to start these test Javadocs with "Tests {@link ClasNameTested}." which makes is nice to click on that link to go to the code in your IDE (at least Eclipse does this.) ########## File path: src/main/java/org/apache/commons/beanutils2/converters/ColorConverter.java ########## @@ -0,0 +1,216 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.awt.Color; +import java.util.Objects; + +/** + * <p>Converts a configuration property into a Java {@link Color} object.</p> + * + * <p> + * This converter aims to be compatible with some some of the web color + * formats supported by browsers with CSS, rather than only support + * literal interpretations of numbers, such as: + * </p> + * + * <ul> + * <li>#RGB</li> + * <li>#RGBA</li> + * <li>#RRGGBBAA</li> + * </ul> + * + * <p> + * This converter will use the web based hexadecimal interpretations if + * the value is prefixed with {@link #HEX_COLOR_PREFIX}. + * + * If using a literal number, or {@link Color#decode(String)} is desired, you must + * prefix your value with <code>0x</code> instead of {@link #HEX_COLOR_PREFIX}. + * </p> + * + * @since 2.0.0 + */ +public class ColorConverter extends AbstractConverter { + + /** To be a web based hexadecimal color, it must be prefixed with this. */ + private static final String HEX_COLOR_PREFIX = "#"; + + /** + * Construct a <b>{@link Color}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public ColorConverter() { + super(); + } + + /** + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public ColorConverter(final Object defaultValue) { + super(defaultValue); + } + + @Override + protected Class<?> getDefaultType() { + return Color.class; + } + + /** + * <p> + * Convert the configuration value to a Java {@link Color} object, + * by reading the hexadecimal {@link String} and converting each component. + * </p> + * + * <p> + * This can also interpret raw color names based on the standard colors + * defined in Java, such as the following: + * </p> + * + * <ul> + * <li>{@link Color#WHITE}</li> + * <li>{@link Color#LIGHT_GRAY}</li> + * <li>{@link Color#GRAY}</li> + * <li>{@link Color#DARK_GRAY}</li> + * <li>{@link Color#BLACK}</li> + * <li>{@link Color#RED}</li> + * <li>{@link Color#PINK}</li> + * <li>{@link Color#ORANGE}</li> + * <li>{@link Color#YELLOW}</li> + * <li>{@link Color#GREEN}</li> + * <li>{@link Color#MAGENTA}</li> + * <li>{@link Color#CYAN}</li> + * <li>{@link Color#BLUE}</li> + * </ul> + * + * <small> Review comment: No "small". ########## File path: src/test/java/org/apache/commons/beanutils2/converters/DimensionConverterTestCase.java ########## @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import org.apache.commons.beanutils2.ConversionException; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.awt.Dimension; + +/** + * @since 2.0.0 + */ +public class DimensionConverterTestCase { + + private DimensionConverter converter; + + @Before + public void before() { + converter = new DimensionConverter(); + } + + @Test + public void testConvertingDimension() { + final Dimension expected = new Dimension(1920, 1080); + final Dimension actual = converter.convert(Dimension.class, "1920x1080"); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testConvertingSquare() { + final Dimension expected = new Dimension(512, 512); + final Dimension actual = converter.convert(Dimension.class, "512"); + + Assert.assertEquals(expected, actual); + } + + @Test + public void testInvalidDimensions() { + try { Review comment: Too verbose, since this component is not on JUnit 5 yet, you cannot use `assertThrows()`, so instead use `@Test(expected)` ########## File path: src/main/java/org/apache/commons/beanutils2/converters/LocaleConverter.java ########## @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.util.Locale; +import java.util.Objects; + +/** + * Converts a localization pattern into a Java {@link Locale} object. + * + * @since 2.0.0 + */ +public class LocaleConverter extends AbstractConverter { + + /** + * Construct a <b>{@link Locale}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public LocaleConverter() { + super(); + } + + /** + * Constructs a {@link org.apache.commons.beanutils2.Converter} that will return + * the specified default value if a conversion error occurs. + * + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public LocaleConverter(final Object defaultValue) { + super(defaultValue); + } + + /** + * Gets the default type this {@code Converter} handles. + * + * @return The default type this {@code Converter} handles. + * @since 2.0.0 + */ + @Override + protected Class<?> getDefaultType() { + return Locale.class; + } + + /** + * @param value The String property value to convert. + * @return A {@link Locale} which represents the configuration property value. + * @throws NullPointerException If the value is null. + */ + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + if (Locale.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Value can't be null."); + final String stringValue = value.toString(); + Review comment: See above. ########## File path: src/main/java/org/apache/commons/beanutils2/converters/InetAddressConverter.java ########## @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Objects; + +/** + * For converting configuration property values to an IP address. + * + * @since 2.0.0 + * @see <a href="https://en.wikipedia.org/wiki/Inet_address">IP Address on Wikipedia</a> + */ +public class InetAddressConverter extends AbstractConverter { + + /** + * Construct a <b>{@link InetAddress}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public InetAddressConverter() { + super(); + } + + /** + * Constructs a {@link org.apache.commons.beanutils2.Converter} that will return + * the specified default value if a conversion error occurs. + * + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public InetAddressConverter(final Object defaultValue) { + super(defaultValue); + } + + /** + * Gets the default type this {@code Converter} handles. + * + * @return The default type this {@code Converter} handles. + * @since 2.0.0 + */ + @Override + protected Class<?> getDefaultType() { + return InetAddress.class; + } + + /** + * @param value The String property value to convert. + * @return An {@link InetAddress} which represents the configuration property value. + * @throws NullPointerException If the value is null. + * @throws IllegalArgumentException If a host name was specified and the IP address couldn't be obtained. + */ + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + if (InetAddress.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Value can't be null."); + final String stringValue = value.toString(); Review comment: See above. ########## File path: src/main/java/org/apache/commons/beanutils2/converters/DimensionConverter.java ########## @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.awt.Dimension; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * <p> + * Allows converting configuration values to a {@link Dimension}. + * This can be used to load settings for default dimensions of {@link java.awt.Component}s + * {@link java.awt.Window}s, or {@link java.awt.Shape}s. + * </p> + * + * <p> + * Accepts a single {@link Integer} value, or two {@link Integer} values + * separated by the character <code>x</code>. + * </p> + * + * <p>The dimensions must not be negative, and must be {@link Integer} values.</p> + * + * @since 2.0.0 + */ +public class DimensionConverter extends AbstractConverter { + + /** Regular expression used to validate and tokenizer the {@link String}. */ + private static final Pattern DIMENSION_PATTERN = Pattern.compile("(?<x>\\d+)(?:x(?<y>\\d+))?"); + + /** + * Construct a <b>{@link Dimension}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public DimensionConverter() { + super(); + } + + /** + * Constructs a {@link org.apache.commons.beanutils2.Converter} that will return + * the specified default value if a conversion error occurs. + * + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public DimensionConverter(final Object defaultValue) { + super(defaultValue); + } + + /** + * Gets the default type this {@code Converter} handles. + * + * @return The default type this {@code Converter} handles. + * @since 2.0.0 + */ + @Override + protected Class<?> getDefaultType() { + return Dimension.class; + } + + /** + * @param value The String property value to convert. + * @return A {@link Dimension} which represents the configuration property value. + * @throws NullPointerException If the value is null. + * @throws NumberFormatException If the {@link Dimension} width or height is bigger than {@link Integer#MAX_VALUE}. + */ + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + if (Dimension.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Dimensions can not be null."); + final String stringValue = value.toString(); Review comment: Please rebase on master, then reuse the new convenience method in `AbstractConverter` to replace: ``` Objects.requireNonNull(value, "Value must not be null."); final String stringValue = value.toString(); ``` with: ``` final String stringValue = toString(value); ``` ########## File path: src/main/java/org/apache/commons/beanutils2/converters/PatternConverter.java ########## @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.commons.beanutils2.converters; + +import java.util.Objects; +import java.util.regex.Pattern; + +/** + * Converts a regular expression into a Java {@link Pattern} object. + * + * @since 2.0.0 + */ +public class PatternConverter extends AbstractConverter { + + /** + * Construct a <b>{@link Pattern}</b> <i>Converter</i> that throws + * a {@code ConversionException} if an error occurs. + */ + public PatternConverter() { + super(); + } + + /** + * Constructs a {@link org.apache.commons.beanutils2.Converter} that will return + * the specified default value if a conversion error occurs. + * + * @param defaultValue The default value to be returned + * if the value to be converted is missing or an error + * occurs converting the value. + */ + public PatternConverter(final Object defaultValue) { + super(defaultValue); + } + + /** + * Gets the default type this {@code Converter} handles. + * + * @return The default type this {@code Converter} handles. + * @since 2.0.0 + */ + @Override + protected Class<?> getDefaultType() { + return Pattern.class; + } + + /** + * @param value The String property value to convert. + * @return A {@link Pattern} which represents the compiled configuration property. + * @throws NullPointerException If the value is null. + * @throws java.util.regex.PatternSyntaxException If the regular expression {@link String} provided is malformed. + */ + @Override + protected <T> T convertToType(Class<T> type, Object value) throws Throwable { + if (Pattern.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Regular expression can't be null."); + final String stringValue = value.toString(); Review comment: See above. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
