Coverage Report - org.apache.tapestry5.util.EnumValueEncoder
 
Classes in this File Line Coverage Branch Coverage Complexity
EnumValueEncoder
100%
9/9
100%
4/4
0
 
 1  
 // Copyright 2007, 2008 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry5.util;
 16  
 
 17  
 import org.apache.tapestry5.ValueEncoder;
 18  
 import static org.apache.tapestry5.ioc.internal.util.Defense.notNull;
 19  
 import org.apache.tapestry5.ioc.internal.util.InternalUtils;
 20  
 
 21  
 /**
 22  
  * A value encoder that can be used for aribrary Enum types. The enum name is stored as the client side value.
 23  
  */
 24  1314
 public class EnumValueEncoder<E extends Enum<E>> implements ValueEncoder<E>
 25  
 {
 26  
     private final Class<E> enumType;
 27  
 
 28  
     public EnumValueEncoder(final Class<E> enumType)
 29  66
     {
 30  66
         notNull(enumType, "enumType");
 31  
 
 32  66
         this.enumType = enumType;
 33  66
     }
 34  
 
 35  
     public String toClient(E value)
 36  
     {
 37  1178
         if (value == null) return null;
 38  
 
 39  1172
         return value.name();
 40  
     }
 41  
 
 42  
     @SuppressWarnings("unchecked")
 43  
     public E toValue(String clientValue)
 44  
     {
 45  136
         if (InternalUtils.isBlank(clientValue)) return null;
 46  
 
 47  134
         return Enum.valueOf(enumType, clientValue);
 48  
     }
 49  
 
 50  
 }