KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > converters > TestCustomHashMapConverter


1 /*
2  * Copyright 2005-2007 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.dozer.util.mapping.converters;
17
18 import java.util.HashMap JavaDoc;
19
20 import net.sf.dozer.util.mapping.MappingException;
21 import net.sf.dozer.util.mapping.vo.TestCustomConverterHashMapObject;
22 import net.sf.dozer.util.mapping.vo.TestCustomConverterHashMapPrimeObject;
23 import net.sf.dozer.util.mapping.vo.TestObject;
24 import net.sf.dozer.util.mapping.vo.TestObjectPrime;
25
26 /**
27  * @author garsombke.franz
28  */

29 public class TestCustomHashMapConverter implements CustomConverter {
30
31   public Object JavaDoc convert(Object JavaDoc destination, Object JavaDoc source, Class JavaDoc destClass, Class JavaDoc sourceClass) {
32
33     if (source instanceof TestCustomConverterHashMapObject) {
34       TestCustomConverterHashMapPrimeObject dest = null;
35       TestCustomConverterHashMapObject testCustomConverterHashMapObject = (TestCustomConverterHashMapObject) source;
36       if (destination == null) {
37         dest = new TestCustomConverterHashMapPrimeObject();
38       } else {
39         dest = (TestCustomConverterHashMapPrimeObject) destination;
40       }
41       HashMap JavaDoc map = new HashMap JavaDoc();
42       map.put("object1", testCustomConverterHashMapObject.getTestObject());
43       map.put("object2", testCustomConverterHashMapObject.getTestObjectPrime());
44       dest.setTestObjects(map);
45       return dest;
46     } else if (source instanceof TestCustomConverterHashMapPrimeObject) {
47       TestCustomConverterHashMapObject dest = null;
48
49       TestCustomConverterHashMapPrimeObject testCustomConverterHashMapPrimeObject = (TestCustomConverterHashMapPrimeObject) source;
50       if (destination == null) {
51         dest = new TestCustomConverterHashMapObject();
52       } else {
53         dest = (TestCustomConverterHashMapObject) destination;
54       }
55       dest.setTestObject((TestObject) testCustomConverterHashMapPrimeObject.getTestObjects().get("object1"));
56       dest.setTestObjectPrime((TestObjectPrime) testCustomConverterHashMapPrimeObject.getTestObjects().get("object2"));
57       return dest;
58     } else {
59       throw new MappingException("Converter TestCustomHashMapConverter used incorrectly. Arguments passed in were:"
60           + destination + " and " + source);
61     }
62   }
63
64 }
65
Popular Tags