Coverage Report - org.apache.tapestry5.internal.util.PrimaryKeyEncoder2ValueEncoder
 
Classes in this File Line Coverage Branch Coverage Complexity
PrimaryKeyEncoder2ValueEncoder
100%
12/12
100%
4/4
0
PrimaryKeyEncoder2ValueEncoder$1
83%
5/6
N/A
0
 
 1  
 // Copyright 2009 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.internal.util;
 16  
 
 17  
 import org.apache.tapestry5.PrimaryKeyEncoder;
 18  
 import org.apache.tapestry5.ValueEncoder;
 19  
 import org.apache.tapestry5.ioc.services.Coercion;
 20  
 import org.apache.tapestry5.ioc.services.TypeCoercer;
 21  
 import org.apache.tapestry5.util.DefaultPrimaryKeyEncoder;
 22  
 
 23  
 import java.io.Serializable;
 24  
 
 25  
 /**
 26  
  * This is a key part of the plan to eliminate {@link org.apache.tapestry5.PrimaryKeyEncoder}.
 27  
  *
 28  
  * @since 5.1.0.0
 29  
  */
 30  
 @SuppressWarnings({ "unchecked" })
 31  144
 public class PrimaryKeyEncoder2ValueEncoder implements Coercion<PrimaryKeyEncoder, ValueEncoder>
 32  
 {
 33  
     // The magic of proxies: a coercion within TypeCoercer can use TypeCoercer as part of its job!
 34  
     private final TypeCoercer coercer;
 35  
 
 36  
     public PrimaryKeyEncoder2ValueEncoder(TypeCoercer coercer)
 37  62
     {
 38  62
         this.coercer = coercer;
 39  62
     }
 40  
 
 41  
     public ValueEncoder coerce(final PrimaryKeyEncoder input)
 42  
     {
 43  46
         final Class keyType = input.getKeyType();
 44  
 
 45  46
         if (keyType == null)
 46  
         {
 47  4
             String message = String.format("Unable to extract primary key type from %s. " +
 48  
                     "This represents a change from Tapestry 5.0 to Tapestry 5.1.", input);
 49  
 
 50  4
             if (input instanceof DefaultPrimaryKeyEncoder)
 51  2
                 message +=
 52  
                         " Class DefaultPrimaryKeyEncoder now includes a constructor for specifying the key type. " +
 53  
                                 "You should change the code that instantiates the encoder.";
 54  
             else
 55  2
                 message += " You should ensure that the getKeyType() method returns the correct Class.";
 56  
 
 57  4
             throw new RuntimeException(message);
 58  
         }
 59  
 
 60  42
         return new ValueEncoder()
 61  
         {
 62  
             public String toClient(Object value)
 63  
             {
 64  78
                 Object key = input.toKey(value);
 65  
 
 66  78
                 return coercer.coerce(key, String.class);
 67  
             }
 68  
 
 69  
             public Object toValue(String clientValue)
 70  
             {
 71  26
                 Serializable key = (Serializable) coercer.coerce(clientValue, keyType);
 72  
 
 73  26
                 return input.toValue(key);
 74  
             }
 75  
 
 76  
             @Override
 77  42
             public String toString()
 78  
             {
 79  0
                 return String.format("<ValueEncoder coercion wrapper around PrimaryKeyEncoder[%s]>",
 80  
                                      keyType.getName());
 81  
             }
 82  
         };
 83  
     }
 84  
 }