View Javadoc
1   /*
2    * Copyright 2005 Filipe Tavares
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 org.devyant.decorutils;
17  
18  import java.lang.reflect.InvocationTargetException;
19  import java.lang.reflect.Method;
20  import java.util.Collection;
21  import java.util.Vector;
22  
23  import org.apache.commons.beanutils.PropertyUtils;
24  
25  /***
26   * @author Filipe Tavares
27   * @version $Revision: 1.0$ ($Author: ftavares$)
28   * @since 2/Fev/2005 15:50:55
29   */
30  public class Utils {
31  
32      /***
33       * Not allowed.
34       */
35      protected Utils() {
36          // prevents calls from subclass
37          throw new UnsupportedOperationException();
38      }
39  
40      /***
41       * Split by delim character and ignore surrounding space.
42       * @param string The string
43       * @return Array of splited atoms
44       */
45      public static final String[] commaSplitToArray(final String string) {
46          return splitToArrayIgnoreSpace(string, ',');
47      }
48  
49      /***
50       * Split by delim character and ignore surrounding space.
51       * @param string The string
52       * @return Collection of splited atoms
53       */
54      public static final Collection commaSplitToCollection(final String string) {
55          return splitToCollectionIgnoreSpace(string, ',');
56      }
57  
58      /***
59       * Split by delim character and ignore surrounding space.
60       * @param string The string
61       * @param delim The delimiter character
62       * @return Collection of splited atoms
63       */
64      public static final Collection splitToCollectionIgnoreSpace(
65              final String string, final char delim) {
66          final String[] attr = splitToArrayIgnoreSpace(string, delim);
67  
68          final Vector tokens = new Vector(attr.length);
69          for (int i = 0; i < attr.length; i++) {
70              tokens.add(i, attr[i]);
71          }
72  
73          return tokens;
74      }
75  
76      /***
77       * Split by delim character and ignore surrounding space.
78       * @param string The string
79       * @param delim The delimiter character
80       * @return Array of splited atoms
81       */
82      public static final String[] splitToArrayIgnoreSpace(
83              final String string, final char delim) {
84          return string.trim().split("//s*" + delim + "+//s*");
85      }
86  
87      /***
88       * Capitalize first word of String.
89       * @param string The String
90       * @return Capitalized String
91       */
92      public static final String capitalize(final String string) {
93          int len;
94  
95          if ((string == null) || (len = string.length()) == 0) {
96              return string;
97          }
98  
99          final StringBuffer buf = new StringBuffer(len);
100         buf.append(Character.toTitleCase(string.charAt(0)));
101         buf.append(string.substring(1));
102 
103         return buf.toString();
104     }
105 
106     /***
107      * Decapitalize first word of String.
108      * @param string The String
109      * @return Decapitalized String
110      */
111     public static final String decapitalize(final String string) {
112         int len;
113 
114         if ((string == null) || (len = string.length()) == 0) {
115             return string;
116         }
117 
118         final StringBuffer buf = new StringBuffer(len);
119         buf.append(Character.toLowerCase(string.charAt(0)));
120         buf.append(string.substring(1));
121 
122         return buf.toString();
123     }
124 
125     /***
126      * The getter method for certain property from a certain
127      * class. The method can be an is or get method.
128      *
129      * @param theClass The property's class
130      * @param property The property
131      * @return The getter method
132      * @throws NoSuchMethodException Thrown if method does not exist
133      */
134     public static final Method getGetterMethod(
135             final Class theClass, final String property)
136         throws NoSuchMethodException {
137         Method decorateMethod;
138         final String propertyName = capitalize(property);
139         try {
140             decorateMethod = theClass
141                 .getMethod("is" + propertyName, new Class[]{});
142         } catch (Exception ex) {
143             // no is method, so look for getter
144             decorateMethod = theClass
145                 .getMethod("get" + propertyName, new Class[]{});
146         }
147         return decorateMethod;
148     }
149     /***
150      * The value for a certain property from the object.
151      * @param object The object
152      * @param prop The property
153      * @return The property's value
154      * @throws NoSuchMethodException Thrown if method does not exist
155      * @throws IllegalAccessException Thrown if is access protected
156      * @throws InvocationTargetException Thrown if target isn't valid
157      */
158     public static final Object getClassProperty(
159             final Object object, final String prop)
160         throws NoSuchMethodException,
161         IllegalAccessException,
162         InvocationTargetException {
163         
164         // the property
165         String property = prop;
166         // extra properties
167         String extra = null;
168         // the properties value
169         Object value;
170         
171         final int index = prop.indexOf(".");
172         if (index > 0) {
173             property = prop.substring(0, index);
174             extra = prop.substring(index + 1);
175         }
176         
177         value = getGetterMethod(object.getClass(), property)
178             .invoke(object, new Object[]{});
179         
180         if ((value != null) && (extra != null)) {
181             PropertyUtils.getProperty(value, extra);
182         }
183         
184         return value;
185     }
186 }