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  
  * Beanから{@link String}をキー、{@link String}の配列を値にもつ{@link Map}に変換するコマンドです。
 32  
  * 
 33  
  * @author baba
 34  
  * @since 1.0.0
 35  
  */
 36  
 class BeanToStringArrayMapDxoCommand extends BeanToMapDxoCommand {
 37  
 
 38  
         /**
 39  
          * インスタンスを構築します。
 40  
          * 
 41  
          * @param dxoClass
 42  
          *            Dxoのインターフェースまたはクラス
 43  
          * @param method
 44  
          *            Dxoのメソッド
 45  
          * @param converterFactory
 46  
          *            {@link Converter}のファクトリ
 47  
          * @param annotationReader
 48  
          *            {@link AnnotationReader}のファクトリ
 49  
          * @param expression
 50  
          *            変換ルールを表すOGNL式
 51  
          */
 52  
         @SuppressWarnings("unchecked")
 53  
         public BeanToStringArrayMapDxoCommand(final Class dxoClass,
 54  
                         final Method method, final ConverterFactory converterFactory,
 55  
                         final AnnotationReader annotationReader, final String expression) {
 56  36
                 super(dxoClass, method, converterFactory, annotationReader, expression);
 57  36
                 valueType = String[].class;
 58  36
         }
 59  
 
 60  
         @Override
 61  
         @SuppressWarnings("unchecked")
 62  
         protected Map convertValueType(final Map from,
 63  
                         final ConversionContext context) {
 64  4
                 final Map<String, String[]> to = new LinkedHashMap<String, String[]>();
 65  
                 for (final Entry<String, Object> entry : castToObjectMap(from)
 66  4
                                 .entrySet()) {
 67  19
                         final String key = entry.getKey();
 68  19
                         final Object value = entry.getValue();
 69  19
                         if (value == null || value instanceof String[]) {
 70  12
                                 to.put(key, (String[]) value);
 71  
                         } else {
 72  7
                                 if (value.getClass().isArray()) {
 73  1
                                         final Converter converter = converterFactory.getConverter(
 74  
                                                         value.getClass(), String[].class);
 75  1
                                         final String[] convertedValue = (String[]) converter
 76  
                                                         .convert(value, String[].class, context);
 77  1
                                         to.put(key, convertedValue);
 78  1
                                 } else {
 79  6
                                         final Converter converter = converterFactory.getConverter(
 80  
                                                         value.getClass(), String.class);
 81  6
                                         final String[] array = (String[]) Array.newInstance(
 82  
                                                         String.class, 1);
 83  6
                                         Array.set(array, 0, converter.convert(value, String.class,
 84  
                                                         context));
 85  6
                                         to.put(key, array);
 86  
                                 }
 87  
                         }
 88  19
                 }
 89  4
                 return to;
 90  
         }
 91  
 
 92  
         @SuppressWarnings("unchecked")
 93  
         private Map<String, Object> castToObjectMap(final Map from) {
 94  4
                 return from;
 95  
         }
 96  
 
 97  
 }