commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dgra...@apache.org
Subject cvs commit: jakarta-commons/validator/src/share/org/apache/commons/validator ValidatorUtil.java
Date Tue, 29 Apr 2003 00:53:43 GMT
dgraham     2003/04/28 17:53:43

  Modified:    validator/src/share/org/apache/commons/validator
                        ValidatorUtil.java
  Log:
  Refactored to simplify if statement and catch specific exceptions.
  
  Revision  Changes    Path
  1.8       +79 -60    jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorUtil.java
  
  Index: ValidatorUtil.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/validator/src/share/org/apache/commons/validator/ValidatorUtil.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- ValidatorUtil.java	13 Mar 2003 02:11:39 -0000	1.7
  +++ ValidatorUtil.java	29 Apr 2003 00:53:43 -0000	1.8
  @@ -62,6 +62,7 @@
   
   package org.apache.commons.validator;
   
  +import java.lang.reflect.InvocationTargetException;
   import java.util.Iterator;
   
   import org.apache.commons.beanutils.PropertyUtils;
  @@ -74,13 +75,14 @@
    * <p>Basic utility methods.</p>
    *
    * @author David Winterfeldt
  + * @author David Graham
    * @version $Revision$ $Date$
   */
   public class ValidatorUtil  {
   
      /**
  -    * Logger
  -   */
  +    * Logger.
  +    */
      protected static Log log = LogFactory.getLog(ValidatorUtil.class);
      
      /**
  @@ -89,39 +91,53 @@
       * @param	value		<code>String</code> to perform the replacement on.
       * @param	key		The name of the constant.
       * @param	replaceValue	The value of the constant.
  -   */
  +    */
      public static String replace(String value, String key, String replaceValue) {
  -      if (value != null && key != null && replaceValue != null) {
  -         int pos = value.indexOf(key);
  -          
  -         if (pos >= 0) {
  -            int length = value.length();
  -            int start = pos;
  -            int end = pos + key.length();
  -         
  -            if (length == key.length()) {
  -               value = replaceValue;
  -            } else if (end == length) {
  -               value = value.substring(0, start) + replaceValue; //+ value.substring(end);
  -            } else {
  -            	value = value.substring(0, start) + replaceValue + replace(value.substring(end),
key, replaceValue);
  -            }
  -         }
  -      }
  -      return value;
  +		if (value == null || key == null || replaceValue == null) {
  +			return value;
  +		}
  +
  +		int pos = value.indexOf(key);
  +
  +		if (pos < 0) {
  +			return value;
  +		}
  +        
  +		int length = value.length();
  +		int start = pos;
  +		int end = pos + key.length();
  +
  +		if (length == key.length()) {
  +			value = replaceValue;
  +		} else if (end == length) {
  +			value = value.substring(0, start) + replaceValue;
  +		} else {
  +			value =
  +				value.substring(0, start)
  +					+ replaceValue
  +					+ replace(value.substring(end), key, replaceValue);
  +		}
  +
  +		return value;
      }
   
      /**
  -    * Convenience method for getting a value from a bean property as a <code>String</code>.
  -   */
  +    * Convenience method for getting a value from a bean property as a 
  +    * <code>String</code>.
  +    */
      public static String getValueAsString(Object bean, String property) {
         Object value = null;
   
  -      try {
  -         value = PropertyUtils.getProperty(bean, property);	
  -      } catch (Exception e) {
  -         log.error(e.getMessage(), e);
  -      }
  +         try {
  +			value = PropertyUtils.getProperty(bean, property);
  +            
  +		} catch (IllegalAccessException e) {
  +            log.error(e.getMessage(), e);
  +		} catch (InvocationTargetException e) {
  +            log.error(e.getMessage(), e);
  +		} catch (NoSuchMethodException e) {
  +            log.error(e.getMessage(), e);
  +		}	
      	
         return (value != null ? value.toString() : null);    	
      }
  @@ -131,33 +147,36 @@
       * are <code>String</code>, <code>Msg</code>, <code>Arg</code>,

       * or <code>Var</code>.  Otherwise it is a shallow copy.
       * 
  -    * @param	map		<code>FastHashMap</code> to copy.
  -    * @return 	FastHashMap	A copy of the <code>FastHashMap</code> 
  -    *				that was passed in.
  -   */
  -   public static FastHashMap copyFastHashMap(FastHashMap map) {
  -      FastHashMap hResults = new FastHashMap();
  -      
  -      for (Iterator i = map.keySet().iterator(); i.hasNext(); ) {
  -         String key = (String)i.next();
  -         Object value = map.get(key);
  -
  -      	  if (value instanceof String) {
  -      	     hResults.put(key, new String((String)value));
  -      	  } else if (value instanceof Msg) {
  -      	     hResults.put(key, ((Msg)value).clone());
  -      	  } else if (value instanceof Arg) {
  -      	     hResults.put(key, ((Arg)value).clone());
  -      	  } else if (value instanceof Var) {
  -      	     hResults.put(key, ((Var)value).clone());
  -         } else {
  -            hResults.put(key, value);	
  -         }
  -      }
  -      
  -      hResults.setFast(true);
  -      
  -      return hResults;
  -   }
  +    * @param map <code>FastHashMap</code> to copy.
  +    * @return FastHashMap A copy of the <code>FastHashMap</code> that was 
  +    * passed in.
  +    */
  +    public static FastHashMap copyFastHashMap(FastHashMap map) {
  +    	FastHashMap results = new FastHashMap();
  +        
  +		Iterator i = map.keySet().iterator();
  +		while (i.hasNext()) {
  +			String key = (String) i.next();
  +			Object value = map.get(key);
  +
  +			// TODO I don't think Strings need to be copied and do we really need to cast
  +			// before cloning?
  +			if (value instanceof String) {
  +				results.put(key, new String((String) value));
  +			} else if (value instanceof Msg) {
  +				results.put(key, ((Msg) value).clone());
  +			} else if (value instanceof Arg) {
  +				results.put(key, ((Arg) value).clone());
  +			} else if (value instanceof Var) {
  +				results.put(key, ((Var) value).clone());
  +			} else {
  +				results.put(key, value);
  +			}
  +		}
  +
  +		results.setFast(true);
  +
  +		return results;
  +	}
      
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message