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.transformers.BaseTransformer;
25  
26  /**
27   * @author Matt Sgarlata
28   * @author Alexander Volanis
29   * @since Jan 8, 2005
30   */
31  public class NumberToTimeConverter extends BaseTransformer implements DecoratedConverter {
32  
33  	private Converter numberConverter;
34  	private Converter timeConverter;
35  
36  	/**
37  	 * {@inheritDoc}
38  	 */
39  	protected Object convertImpl(Class destinationClass, Object source,
40  		Locale locale) throws Exception {
41  
42  		Long number =
43  			(Long) getNumberConverter().convert(Long.class, source, locale);
44  		Date date = new Date();
45  		date.setTime(number.longValue());
46  
47  		return getTimeConverter().convert(destinationClass, date, locale);
48  	}
49  
50  	/**
51  	 * {@inheritDoc}
52  	 */
53  	protected Class[] getSourceClassesImpl() throws Exception {
54  		return getNumberConverter().getSourceClasses();
55  	}
56  
57  	/**
58  	 * {@inheritDoc}
59  	 */
60  	protected Class[] getDestinationClassesImpl() throws Exception {
61  		return getTimeConverter().getDestinationClasses();
62  	}
63  
64  	/**
65  	 * {@inheritDoc}
66  	 */
67  	protected boolean isWrappingRuntimeExceptions() {
68  	    return true;
69      }
70  
71  	/**
72  	 * Get the number converter used by this NumberToTimeConverter.
73  	 * @return Converter
74  	 */
75  	public Converter getNumberConverter() {
76  		if (numberConverter == null) {
77  			setNumberConverter(Defaults.createNumberConverter());
78  		}
79  		return numberConverter;
80  	}
81  
82  	/**
83  	 * Set the number converter used by this NumberToTimeConverter.
84  	 * @param numberConverter
85  	 */
86  	public void setNumberConverter(Converter numberConverter) {
87  		this.numberConverter = numberConverter;
88  	}
89  
90  	/**
91  	 * Get the time converter used by this NumberToTimeConverter.
92  	 * @return Converter
93  	 */
94  	public Converter getTimeConverter() {
95  		if (timeConverter == null) {
96  			setTimeConverter(Defaults.createTimeConverter());
97  		}
98  		return timeConverter;
99  	}
100 
101 	/**
102 	 * Set the time converter used by this NumberToTimeConverter.
103 	 * @param timeConverter
104 	 */
105 	public void setTimeConverter(Converter timeConverter) {
106 		this.timeConverter = timeConverter;
107 	}
108 }