Coverage Report - br.com.arsmachina.tapestrycrud.services.impl.PrimaryKeyEncoderSourceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PrimaryKeyEncoderSourceImpl
0%
0/25
0%
0/16
0
 
 1  
 // Copyright 2008 Thiago H. de Paula Figueiredo
 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 br.com.arsmachina.tapestrycrud.services.impl;
 16  
 
 17  
 import java.io.Serializable;
 18  
 import java.util.HashMap;
 19  
 import java.util.Map;
 20  
 
 21  
 import org.apache.tapestry5.PrimaryKeyEncoder;
 22  
 import org.apache.tapestry5.ioc.util.StrategyRegistry;
 23  
 
 24  
 import br.com.arsmachina.tapestrycrud.factory.PrimaryKeyEncoderFactory;
 25  
 import br.com.arsmachina.tapestrycrud.services.EncoderSource;
 26  
 import br.com.arsmachina.tapestrycrud.services.PrimaryKeyEncoderSource;
 27  
 
 28  
 /**
 29  
  * {@link PrimaryKeyEncoderSource} implementation.
 30  
  * 
 31  
  * @author Thiago H. de Paula Figueiredo
 32  
  */
 33  
 public class PrimaryKeyEncoderSourceImpl implements PrimaryKeyEncoderSource {
 34  
         
 35  
         @SuppressWarnings("unchecked")
 36  
         final private StrategyRegistry<PrimaryKeyEncoder> registry;
 37  
 
 38  
         final private EncoderSource encoderSource;
 39  
 
 40  
         final private PrimaryKeyEncoderFactory primaryKeyEncoderFactory;
 41  
         
 42  
         @SuppressWarnings("unchecked")
 43  0
         final private Map<Class, PrimaryKeyEncoder> additionalEncoders = new HashMap<Class, PrimaryKeyEncoder>();
 44  
         
 45  
         /**
 46  
          * Single constructor.
 47  
          * 
 48  
          * @param registrations a {@link Map}. It cannot be null.
 49  
          * @param encoderSource an {@link EncoderSource}. It cannot be null.
 50  
          * @param primaryKeyEncoderFactory an {@link PrimaryKeyEncoderFactory}. It cannot be null.
 51  
          */
 52  
         @SuppressWarnings("unchecked")
 53  0
         public PrimaryKeyEncoderSourceImpl(Map<Class, PrimaryKeyEncoder> registrations,
 54  
                         EncoderSource encoderSource, PrimaryKeyEncoderFactory primaryKeyEncoderFactory) {
 55  
 
 56  0
                 if (registrations == null) {
 57  0
                         throw new IllegalArgumentException("Parameter registrations cannot be null");
 58  
                 }
 59  
 
 60  0
                 if (encoderSource == null) {
 61  0
                         throw new IllegalArgumentException("Parameter encoderSource cannot be null");
 62  
                 }
 63  
 
 64  0
                 if (primaryKeyEncoderFactory == null) {
 65  0
                         throw new IllegalArgumentException("Parameter primaryKeyEncoderFactory cannot be null");
 66  
                 }
 67  
                 
 68  0
                 registry = StrategyRegistry.newInstance(PrimaryKeyEncoder.class, registrations, true);
 69  
 
 70  0
                 this.encoderSource = encoderSource;
 71  0
                 this.primaryKeyEncoderFactory = primaryKeyEncoderFactory;
 72  
 
 73  0
         }
 74  
 
 75  
         /**
 76  
          * @see br.com.arsmachina.tapestrycrud.services.PrimaryKeyEncoderSource#get(java.lang.Class)
 77  
          */
 78  
         @SuppressWarnings("unchecked")
 79  
         public <T, K extends Serializable> PrimaryKeyEncoder<K, T> get(Class<T> clasz) {
 80  
 
 81  0
                 PrimaryKeyEncoder<K, T> encoder = registry.get(clasz);
 82  
 
 83  0
                 if (encoder == null) {
 84  0
                         encoder = encoderSource.get(clasz);
 85  
                 }
 86  
 
 87  0
                 if (encoder == null) {
 88  
                         
 89  0
                         encoder = additionalEncoders.get(clasz);
 90  
                         
 91  0
                         if (encoder == null) {
 92  
                                 
 93  0
                                 encoder = primaryKeyEncoderFactory.build(clasz);
 94  
                                 
 95  0
                                 if (encoder != null) {
 96  0
                                         additionalEncoders.put(clasz, encoder);
 97  
                                 }
 98  
                                 
 99  
                         }
 100  
                         
 101  
                 }
 102  
 
 103  0
                 if (encoder == null) {
 104  0
                         throw new IllegalArgumentException(
 105  0
                                         "There is no PrimaryKeyEncoder configured for class " + clasz.getName());
 106  
                 }
 107  
 
 108  0
                 return encoder;
 109  
 
 110  
         }
 111  
         
 112  
 
 113  
 }