KickJava   Java API By Example, From Geeks To Geeks.

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


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 net.sf.dozer.util.mapping.MappingException;
19 import net.sf.dozer.util.mapping.vo.CustomDoubleObject;
20 import net.sf.dozer.util.mapping.vo.CustomDoubleObjectIF;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 /**
26  * @author sullins.ben
27  */

28 public class TestCustomConverter implements CustomConverter {
29   
30   private static final Log log = LogFactory.getLog(TestCustomConverter.class);
31
32   public Object JavaDoc convert(Object JavaDoc destination, Object JavaDoc source, Class JavaDoc destClass, Class JavaDoc sourceClass) {
33     // show me the destClass and sourceClass
34
log.debug("Source Class is:" + sourceClass.getName());
35     log.debug("Dest Class is:" + destClass.getName());
36     
37     if (source == null) {
38       return null;
39     }
40     
41     CustomDoubleObjectIF dest = null;
42     if (source instanceof Double JavaDoc) {
43       // check to see if the object already exists
44
if (destination == null) {
45         dest = new CustomDoubleObject();
46       } else {
47         dest = (CustomDoubleObjectIF) destination;
48       }
49       dest.setTheDouble(((Double JavaDoc) source).doubleValue());
50       return dest;
51     } else if (source instanceof CustomDoubleObject) {
52       double sourceObj = ((CustomDoubleObjectIF) source).getTheDouble();
53       return new Double JavaDoc(sourceObj);
54     } else {
55       throw new MappingException("Converter TestCustomConverter used incorrectly. Arguments passed in were:"
56           + destination + " and " + source);
57     }
58   }
59 }
Popular Tags