Clover coverage report - SOJO - 1.0.0
Coverage timestamp: Mo Mai 4 2009 16:26:33 CEST
file stats: LOC: 189   Methods: 17
NCLOC: 135   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
SimpleKeyMapperInterceptor.java 100% 100% 100% 100%
coverage
 1    /*
 2    * Copyright 2002-2005 the original author or authors.
 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, either express or implied.
 13    * See the License for the specific language governing permissions and
 14    * limitations under the License.
 15    */
 16    package net.sf.sojo.core.conversion.interceptor;
 17   
 18    import java.io.Serializable;
 19    import java.util.Comparator;
 20    import java.util.Date;
 21    import java.util.Hashtable;
 22    import java.util.Iterator;
 23    import java.util.Map;
 24    import java.util.TreeMap;
 25    import java.util.Map.Entry;
 26   
 27    import net.sf.sojo.core.ConversionContext;
 28    import net.sf.sojo.core.ConversionException;
 29    import net.sf.sojo.core.ConverterInterceptorRecursive;
 30    import net.sf.sojo.core.reflect.ReflectionHelper;
 31   
 32    /**
 33    * A key from a map, where this key is extending to information of the number from inserting in the map
 34    * and the datatype from the key.
 35    *
 36    * @author Linke
 37    *
 38    */
 39    public class SimpleKeyMapperInterceptor implements ConverterInterceptorRecursive {
 40   
 41    public static final String DELIMITER = "~_-_~";
 42   
 43    private boolean makeSimple = false;
 44    private Map keyMapper = new TreeMap(new SimpleKeyComparator());
 45   
 46  807 public SimpleKeyMapperInterceptor() { }
 47   
 48  11 public SimpleKeyMapperInterceptor(boolean pvMakeSimple) {
 49  11 setMakeSimple(pvMakeSimple);
 50    }
 51   
 52  595 public void setMakeSimple(boolean pvMakeSimple) { makeSimple = pvMakeSimple; }
 53  81 public boolean getMakeSimple() { return makeSimple; }
 54   
 55   
 56  35 public void beforeConvertRecursion(ConversionContext pvContext) {
 57  35 if (getMakeSimple() == true) {
 58  24 toSimple(pvContext);
 59    }
 60    }
 61   
 62   
 63  46 public Object beforeConvert(final Object pvConvertObject, final Class pvToType) {
 64  46 Object lvReturn = null;
 65  46 if (getMakeSimple() == false) {
 66  20 Map lvMap = (Map) pvConvertObject;
 67   
 68  20 Map lvOrderedMap = map2SortedMap(lvMap);
 69  14 Iterator it = lvOrderedMap.entrySet().iterator();
 70  14 Hashtable lvHashtable = new Hashtable(lvMap.size());
 71  14 while (it.hasNext()) {
 72  11 Map.Entry lvMapEntry = (Entry) it.next();
 73  11 SimpleKeyComparator lvKeyComparator = (SimpleKeyComparator) lvMapEntry.getKey();
 74  11 Object lvKey = lvKeyComparator.getKey();
 75  11 Object lvValue = lvMapEntry.getValue();
 76  11 lvHashtable.put(lvKey, lvValue);
 77    }
 78  14 lvReturn = lvHashtable;
 79    } else {
 80  26 lvReturn = pvConvertObject;
 81    }
 82   
 83  40 keyMapper.clear();
 84  40 return lvReturn;
 85    }
 86   
 87  24 protected void toSimple (ConversionContext pvContext) {
 88  24 String lvNewKey = null;
 89  24 if (pvContext.key instanceof Date) {
 90  1 lvNewKey = Long.toString(((Date) pvContext.key).getTime());
 91    } else {
 92  23 lvNewKey = pvContext.key.toString();
 93    }
 94  24 int l = pvContext.key.toString().split(DELIMITER).length;
 95  24 if (l < 2) {
 96  22 if (pvContext.key.getClass().equals(String.class)) {
 97  17 lvNewKey = pvContext.numberOfRecursion + DELIMITER + lvNewKey;
 98    } else {
 99  5 lvNewKey = pvContext.numberOfRecursion + DELIMITER + lvNewKey + DELIMITER + pvContext.key.getClass().getName();
 100    }
 101    } else {
 102  2 lvNewKey = pvContext.key.toString();
 103    }
 104  24 pvContext.key = lvNewKey;
 105    }
 106   
 107  17 protected SimpleKeyComparator toComplex (Object pvKey) {
 108  17 if ( ! (pvKey instanceof String) ) {
 109  1 throw new IllegalArgumentException("Expected String, but key is: " + pvKey + " -> class: " + pvKey.getClass().getName());
 110    }
 111  16 String lvKeyStr = (String) pvKey;
 112  16 String lvKeyArray[] = lvKeyStr.split(DELIMITER);
 113  16 if (lvKeyArray.length < 2) {
 114  4 throw new IllegalArgumentException("The key must contains " +
 115    " the delimiter: " + DELIMITER + " - " + lvKeyStr + " (" + lvKeyArray.length + ")");
 116    }
 117   
 118  12 String lvPosStr = lvKeyArray[0];
 119  12 Integer integer = Integer.valueOf(lvPosStr);
 120  12 int lvPos = integer.intValue();
 121   
 122  12 String lvKeyValue = lvKeyArray[1];
 123  12 Object lvKey = null;
 124  12 if (lvKeyArray.length == 3) {
 125  4 String lvKeyClass = lvKeyArray[2];
 126  4 try {
 127  4 Class clazz = ReflectionHelper.forName(lvKeyClass);
 128  3 lvKey = ReflectionHelper.createNewSimpleObject(clazz, lvKeyValue);
 129    } catch (Exception e) {
 130  1 throw new ConversionException("Can't create a new instance of class: " + lvKeyClass + " with value: " + lvKeyValue);
 131    }
 132    } else {
 133  8 lvKey = lvKeyValue;
 134    }
 135  11 return new SimpleKeyComparator(lvPos, lvKey);
 136    }
 137   
 138  20 protected Map map2SortedMap (Map pvMap) {
 139  20 Iterator it = pvMap.entrySet().iterator();
 140  20 TreeMap lvTreeMap = new TreeMap(new SimpleKeyComparator());
 141  20 while (it.hasNext()) {
 142  17 Map.Entry lvEntry = (Entry) it.next();
 143  17 Object lvKey = lvEntry.getKey();
 144  17 Object lvValue = lvEntry.getValue();
 145  17 SimpleKeyComparator skm = toComplex(lvKey);
 146  11 lvTreeMap.put(skm, lvValue);
 147    }
 148  14 return lvTreeMap;
 149    }
 150   
 151   
 152   
 153  27 public void afterConvertRecursion(ConversionContext pvContext) { }
 154  36 public Object afterConvert(final Object pvResult, final Class pvToType) { return pvResult; }
 155  1 public void onError(Exception pvException) { }
 156   
 157   
 158   
 159   
 160    private static class SimpleKeyComparator implements Comparator, Serializable {
 161   
 162    private static final long serialVersionUID = 2873440526210140656L;
 163   
 164    private int pos = -1;
 165    private Object key = null;
 166   
 167  838 public SimpleKeyComparator() {
 168   
 169    }
 170  11 public SimpleKeyComparator(int pvPos, Object pvKey) {
 171  11 pos = pvPos;
 172  11 key = pvKey;
 173    }
 174   
 175  18 public int getPos() { return pos; }
 176  11 public Object getKey() { return key; }
 177   
 178  9 public int compare(Object pvO1, Object pvO2) {
 179  9 SimpleKeyComparator skm1 = (SimpleKeyComparator) pvO1;
 180  9 SimpleKeyComparator skm2 = (SimpleKeyComparator) pvO2;
 181  9 if (skm1.getPos() > skm2.getPos()) {
 182  2 return -1;
 183    }
 184    else {
 185  7 return 1;
 186    }
 187    }
 188    }
 189    }