View Javadoc

1   /*
2    * $Id: ValueUtils.java,v 1.3 2004/05/23 10:26:57 eelco12 Exp $
3    * $Revision: 1.3 $
4    * $Date: 2004/05/23 10:26:57 $
5    *
6    * ====================================================================
7    * Copyright (c) 2003, Open Edge B.V.
8    * All rights reserved.
9    * Redistribution and use in source and binary forms, with or without 
10   * modification, are permitted provided that the following conditions are met:
11   * Redistributions of source code must retain the above copyright notice, 
12   * this list of conditions and the following disclaimer. Redistributions 
13   * in binary form must reproduce the above copyright notice, this list of 
14   * conditions and the following disclaimer in the documentation and/or other 
15   * materials provided with the distribution. Neither the name of OpenEdge B.V. 
16   * nor the names of its contributors may be used to endorse or promote products 
17   * derived from this software without specific prior written permission.
18   * 
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
20   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
21   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
22   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
23   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
24   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
25   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
26   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
27   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
28   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
29   * THE POSSIBILITY OF SUCH DAMAGE.
30   */
31  package nl.openedge.baritus.util;
32  
33  import java.lang.reflect.Array;
34  import java.util.Collection;
35  import java.util.Map;
36  
37  import org.apache.commons.logging.Log;
38  import org.apache.commons.logging.LogFactory;
39  
40  import nl.openedge.baritus.ConverterRegistry;
41  import nl.openedge.baritus.converters.ConversionException;
42  import nl.openedge.baritus.converters.Converter;
43  
44  /***
45   * Misc utility methods for handling values
46   * 
47   * @author Eelco Hillenius
48   */
49  public final class ValueUtils
50  {
51      
52      /* logger */
53      private static Log log = LogFactory.getLog(ValueUtils.class);
54  
55  	/***
56  	 * check if the value is null or empty
57  	 * @param value object to check on
58  	 * @return true if value is not null AND not empty (e.g. 
59  	 * in case of a String or Collection)
60  	 */
61  	public static boolean isNullOrEmpty(Object value)
62  	{
63  		if (value instanceof String)
64  		{
65  			return (value == null || (((String)value).trim().equals("")));
66  		}
67  		if (value instanceof Object[])
68  		{
69  			if (value == null)
70  			{
71  				return true;
72  			}
73  			else if (((Object[])value).length == 0)
74  			{
75  				return true;
76  			}
77  			else if (((Object[])value).length == 1)
78  			{
79  				return isNullOrEmpty(((Object[])value)[0]);
80  			}
81  			else
82  			{
83  				return false;
84  			}
85  		}
86  		else if (value instanceof Collection)
87  		{
88  			return (value == null || (((Collection)value).isEmpty()));
89  		}
90  		else if (value instanceof Map)
91  		{
92  			return (value == null || (((Map)value).isEmpty()));
93  		}
94  		else
95  		{
96  			return (value == null);
97  		}
98  	}
99  
100 	/***
101 	 * Convert the specified value into a String.  If the specified value
102 	 * is an array, the first element (converted to a String) will be
103 	 * returned.  The registered {@link Converter} for the
104 	 * <code>java.lang.String</code> class will be used, which allows
105 	 * applications to customize Object->String conversions (the default
106 	 * implementation simply uses toString()).
107 	 *
108 	 * @param value Value to be converted (may be null)
109 	 */
110 	public static String convertToString(Object value)
111 	{
112 
113 		if (value == null)
114 		{
115 			return ((String) null);
116 		}
117 		else if (value.getClass().isArray())
118 		{
119 			if (Array.getLength(value) < 1)
120 			{
121 				return (null);
122 			}
123 			value = Array.get(value, 0);
124 			if (value == null)
125 			{
126 				return ((String) null);
127 			}
128 			else
129 			{
130 				try
131 				{
132 					Converter converter = ConverterRegistry.getInstance().lookup(String.class);
133 					Object converted = converter.convert(String.class, value);
134 					return (converted instanceof String) ? 
135 						(String)converted : String.valueOf(converted);
136 				}
137 				catch (Exception e)
138 				{
139 					e.printStackTrace();
140 					throw new ConversionException(e);
141 				}
142 			}
143 		}
144 		else
145 		{
146 			try
147 			{
148 				Converter converter = ConverterRegistry.getInstance().lookup(String.class);
149 				Object converted = converter.convert(String.class, value);
150 				return (converted instanceof String) ? 
151 					(String)converted : String.valueOf(converted);
152 			}
153 			catch (Exception e)
154 			{
155 				log.error(e.getMessage(), e);
156 				throw new ConversionException(e);
157 			}
158 		}
159 
160 	}
161 
162 }