You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by hl...@apache.org on 2010/10/21 01:46:14 UTC

svn commit: r1025792 - in /tapestry/tapestry5/trunk/tapestry-core/src: main/java/org/apache/tapestry5/util/StringToEnumCoercion.java test/java/org/apache/tapestry5/util/StringToEnumCoercionTest.java

Author: hlship
Date: Wed Oct 20 23:46:14 2010
New Revision: 1025792

URL: http://svn.apache.org/viewvc?rev=1025792&view=rev
Log:
TAP5-538: Allow StringToEnumCoercion to support aliases for values

Modified:
    tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/util/StringToEnumCoercion.java
    tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/util/StringToEnumCoercionTest.java

Modified: tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/util/StringToEnumCoercion.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/util/StringToEnumCoercion.java?rev=1025792&r1=1025791&r2=1025792&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/util/StringToEnumCoercion.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/main/java/org/apache/tapestry5/util/StringToEnumCoercion.java Wed Oct 20 23:46:14 2010
@@ -57,13 +57,25 @@ public final class StringToEnumCoercion<
         T result = stringToEnum.get(input);
 
         if (result == null)
-            throw new UnknownValueException(PublicUtilMessages
-                    .missingEnumValue(input, enumClass, stringToEnum.keySet()), new AvailableValues(enumClass.getName()
-                    + " enum constants", stringToEnum));
+            throw new UnknownValueException(
+                    PublicUtilMessages.missingEnumValue(input, enumClass, stringToEnum.keySet()), new AvailableValues(
+                            enumClass.getName() + " enum constants", stringToEnum));
 
         return result;
     }
 
+    /**
+     * Allows an alias value (alternate) string to reference a value.
+     * 
+     * @since 5.2.2
+     */
+    public StringToEnumCoercion<T> addAlias(String alias, T value)
+    {
+        stringToEnum.put(alias, value);
+
+        return this;
+    }
+
     public static <T extends Enum> StringToEnumCoercion<T> create(Class<T> enumClass)
     {
         return new StringToEnumCoercion<T>(enumClass);

Modified: tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/util/StringToEnumCoercionTest.java
URL: http://svn.apache.org/viewvc/tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/util/StringToEnumCoercionTest.java?rev=1025792&r1=1025791&r2=1025792&view=diff
==============================================================================
--- tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/util/StringToEnumCoercionTest.java (original)
+++ tapestry/tapestry5/trunk/tapestry-core/src/test/java/org/apache/tapestry5/util/StringToEnumCoercionTest.java Wed Oct 20 23:46:14 2010
@@ -1,10 +1,10 @@
-// Copyright 2007 The Apache Software Foundation
+// Copyright 2007, 2010 The Apache Software Foundation
 //
 // Licensed 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
+// 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,
@@ -32,6 +32,14 @@ public class StringToEnumCoercionTest ex
     }
 
     @Test
+    public void value_found_by_alias()
+    {
+        StringToEnumCoercion<Stooge> coercion = new StringToEnumCoercion<Stooge>(Stooge.class);
+
+        assertSame(coercion.addAlias("Shemp", Stooge.CURLY_JOE).coerce("shemp"), Stooge.CURLY_JOE);
+    }
+
+    @Test
     public void blank_is_null()
     {
         StringToEnumCoercion<Stooge> coercion = new StringToEnumCoercion<Stooge>(Stooge.class);