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.Locale;
19  
20  import net.sf.composite.util.ObjectUtils;
21  import net.sf.morph.transform.DecoratedConverter;
22  import net.sf.morph.transform.transformers.BaseTransformer;
23  
24  /**
25   * Converts any object to a Boolean.  Empty objects will be converted to
26   * <code>false</code> and non-empty objects will be converted to
27   * <code>true</code>. 
28   * 
29   * @author Matt Sgarlata
30   * @since Dec 31, 2004
31   * @see net.sf.morph.util.MorphUtils#isEmpty(Object)
32   */
33  public class ObjectToBooleanConverter extends BaseTransformer implements DecoratedConverter {
34  
35  	private static final Class[] DESTINATION_TYPES = { Boolean.class,
36  		boolean.class };
37  
38  	private static final Class[] SOURCE_TYPES = { Object.class, null };
39  
40  	/**
41  	 * {@inheritDoc}
42  	 */
43  	protected Object convertImpl(Class destinationClass, Object source,
44  		Locale locale) throws Exception {
45  
46  		return ObjectUtils.isEmpty(source) ? Boolean.FALSE : Boolean.TRUE;
47  	}
48  
49  	/**
50  	 * {@inheritDoc}
51  	 */
52  	protected boolean isWrappingRuntimeExceptions() {
53  	    return true;
54      }
55  
56  	/**
57  	 * {@inheritDoc}
58  	 */
59  	protected Class[] getSourceClassesImpl() throws Exception {
60  		return SOURCE_TYPES;
61  	}
62  
63  	/**
64  	 * {@inheritDoc}
65  	 */
66  	protected Class[] getDestinationClassesImpl() throws Exception {
67  		return DESTINATION_TYPES;
68  	}
69  
70  }