View Javadoc

1   /*
2    * Copyright 2004-2005, 2008 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.morph.transform.converters;
17  
18  import java.util.Date;
19  import java.util.Locale;
20  
21  import net.sf.morph.Defaults;
22  import net.sf.morph.transform.Converter;
23  import net.sf.morph.transform.DecoratedConverter;
24  import net.sf.morph.transform.TransformationException;
25  import net.sf.morph.transform.transformers.BaseTransformer;
26  
27  /**
28   * @author Matt Sgarlata
29   * @author Alexander Volanis
30   * @since Jan 8, 2005
31   */
32  public class TimeToNumberConverter extends BaseTransformer implements DecoratedConverter {
33  
34  	private Converter numberConverter;
35  	private Converter timeConverter;
36  
37  	/**
38  	 * {@inheritDoc}
39  	 */
40  	protected Object convertImpl(Class destinationClass, Object source,
41  		Locale locale) throws Exception {
42  
43  		if (destinationClass.isPrimitive() && source == null) {
44  			throw new TransformationException(destinationClass, source);
45  		}
46  
47  		Date date = (Date) getTimeConverter().convert(Date.class, source,
48  			locale);
49  		return getNumberConverter().convert(destinationClass,
50  			new Long(date.getTime()), locale);
51  	}
52  
53  	/**
54  	 * {@inheritDoc}
55  	 */
56  	protected boolean isAutomaticallyHandlingNulls() {
57  		return false;
58  	}
59  
60  	/**
61  	 * {@inheritDoc}
62  	 */
63  	protected boolean isWrappingRuntimeExceptions() {
64  	    return true;
65      }
66  
67  	/**
68  	 * {@inheritDoc}
69  	 */
70  	protected Class[] getSourceClassesImpl() throws Exception {
71  		return getTimeConverter().getSourceClasses();
72  	}
73  
74  	/**
75  	 * {@inheritDoc}
76  	 */
77  	protected Class[] getDestinationClassesImpl() throws Exception {
78  		return getNumberConverter().getDestinationClasses();
79  	}
80  
81  	/**
82  	 * Get the number converter used by this TimeToNumberConverter.
83  	 * @return Converter
84  	 */
85  	public Converter getNumberConverter() {
86  		if (numberConverter == null) {
87  			setNumberConverter(Defaults.createNumberConverter());
88  		}
89  		return numberConverter;
90  	}
91  
92  	/**
93  	 * Set the number converter used by this TimeToNumberConverter.
94  	 * @param numberConverter
95  	 */
96  	public void setNumberConverter(Converter numberConverter) {
97  		this.numberConverter = numberConverter;
98  	}
99  
100 	/**
101 	 * Get the time converter used by this TimeToNumberConverter.
102 	 * @return Converter
103 	 */
104 	public Converter getTimeConverter() {
105 		if (timeConverter == null) {
106 			setTimeConverter(Defaults.createTimeConverter());
107 		}
108 		return timeConverter;
109 	}
110 
111 	/**
112 	 * Set the time converter used by this TimeToNumberConverter.
113 	 * @param timeConverter
114 	 */
115 	public void setTimeConverter(Converter timeConverter) {
116 		this.timeConverter = timeConverter;
117 	}
118 
119 }