You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2020/12/31 04:42:23 UTC

[GitHub] [commons-beanutils] SethFalco commented on a change in pull request #47: Add Converters for Additional Types

SethFalco commented on a change in pull request #47:
URL: https://github.com/apache/commons-beanutils/pull/47#discussion_r550395208



##########
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:
       Please let me know if there are any problems with the approach I'll be taking here.
   
   I agree handling `Color#toString()` would be nice. While I was at it, I opted to support some terser versions since I was using regular expression anyway, and it wasn't much additional work:
   
   Regex:
   `^(?:[A-Za-z\d._]+)??\[?(?:r=)?(\d{1,3}),(?:g=)?(\d{1,3}),(?:b=)?(\d{1,3})\]?$`
   
   `java.awt.Color[r=255,g=255,b=255]` - The normal `Color#toString()` result.  
   `org.apache.ExtendedColor[r=255,g=0,b=255]` - If the `Color` class is extended, but `#toString()` is not overridden.  
   `[r=0,g=255,b=0]` - Without the package.  
   `r=64,g=64,b=64` - Without the backets.  
   `255,175,175` - Without the color labels.  




----------------------------------------------------------------
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:
users@infra.apache.org