Coverage Report - org.seasar.cubby.dxo.impl.BeanToStringArrayMapDxoCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
BeanToStringArrayMapDxoCommand
100%
21/21
88%
7/8
0
 
 1  
 /*
 2  
  * Copyright 2004-2008 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.cubby.dxo.impl;
 17  
 
 18  
 import java.lang.reflect.Array;
 19  
 import java.lang.reflect.Method;
 20  
 import java.util.LinkedHashMap;
 21  
 import java.util.Map;
 22  
 import java.util.Map.Entry;
 23  
 
 24  
 import org.seasar.extension.dxo.annotation.AnnotationReader;
 25  
 import org.seasar.extension.dxo.command.impl.BeanToMapDxoCommand;
 26  
 import org.seasar.extension.dxo.converter.ConversionContext;
 27  
 import org.seasar.extension.dxo.converter.Converter;
 28  
 import org.seasar.extension.dxo.converter.ConverterFactory;
 29  
 
 30  
 /**
 31  
  * 
 32  
  * @author baba
 33  
  * 
 34  
  */
 35  
 class BeanToStringArrayMapDxoCommand extends BeanToMapDxoCommand {
 36  
 
 37  
         /**
 38  
          * インスタンスを構築します。
 39  
          * 
 40  
          * @param dxoClass
 41  
          *            Dxoのインターフェースまたはクラス
 42  
          * @param method
 43  
          *            Dxoのメソッド
 44  
          * @param converterFactory
 45  
          *            {@link Converter}のファクトリ
 46  
          * @param annotationReader
 47  
          *            {@link AnnotationReader}のファクトリ
 48  
          * @param expression
 49  
          *            変換ルールを表すOGNL式
 50  
          */
 51  
         @SuppressWarnings("unchecked")
 52  
         public BeanToStringArrayMapDxoCommand(final Class dxoClass,
 53  
                         final Method method, final ConverterFactory converterFactory,
 54  
                         final AnnotationReader annotationReader, final String expression) {
 55  38
                 super(dxoClass, method, converterFactory, annotationReader, expression);
 56  38
                 valueType = String[].class;
 57  38
         }
 58  
 
 59  
         @Override
 60  
         @SuppressWarnings("unchecked")
 61  
         protected Map convertValueType(final Map from,
 62  
                         final ConversionContext context) {
 63  4
                 final Map<String, String[]> to = new LinkedHashMap<String, String[]>();
 64  
                 for (final Entry<String, Object> entry : castToObjectMap(from)
 65  4
                                 .entrySet()) {
 66  19
                         final String key = entry.getKey();
 67  19
                         final Object value = entry.getValue();
 68  19
                         if (value == null || value instanceof String[]) {
 69  12
                                 to.put(key, (String[]) value);
 70  
                         } else {
 71  7
                                 if (value.getClass().isArray()) {
 72  1
                                         final Converter converter = converterFactory.getConverter(
 73  
                                                         value.getClass(), String[].class);
 74  1
                                         final String[] convertedValue = (String[]) converter
 75  
                                                         .convert(value, String[].class, context);
 76  1
                                         to.put(key, convertedValue);
 77  1
                                 } else {
 78  6
                                         final Converter converter = converterFactory.getConverter(
 79  
                                                         value.getClass(), String.class);
 80  6
                                         final String[] array = (String[]) Array.newInstance(
 81  
                                                         String.class, 1);
 82  6
                                         Array.set(array, 0, converter.convert(value, String.class,
 83  
                                                         context));
 84  6
                                         to.put(key, array);
 85  
                                 }
 86  
                         }
 87  19
                 }
 88  4
                 return to;
 89  
         }
 90  
 
 91  
         @SuppressWarnings("unchecked")
 92  
         private Map<String, Object> castToObjectMap(final Map from) {
 93  4
                 return from;
 94  
         }
 95  
 
 96  
 }