StringUtil.java :  » Google-tech » amazingapis » org » webscale » util » Java Open Source

Java Open Source » Google tech » amazingapis 
amazingapis » org » webscale » util » StringUtil.java
package org.webscale.util;

import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;

/**
 * @author Niraj 
 */
public class StringUtil {

    public static boolean contains(String key , String compare){
        int index = compare.indexOf(key);
        if(index < 0)
         return false;
        else 
            return true;
     }
    
    public static boolean StringEquals(String s1 , String s2){
      String ss1 = getSomeString(s1);
      String ss2 = getSomeString(s2);
      return ss1.equals(ss2);
    }
    
     
    
    public static int parseInt(String s){
      int i = -1;
      try {
        i = Integer.parseInt(s.trim());
    } catch (NumberFormatException ne) {
      
    }
      return i;
    }
    
    public static float parseFloat(String s){
      float f = -1f;
      try {
        f = Float.parseFloat(s.trim());
    } catch (NumberFormatException ne) {
      
    }
      return f;
    }
    
    /**
     * the Spring.split() method sometimes does not work if fields are empty. This method fixes that
     * @param s
     * @param delimeter
     * @return
     */
    public static String split(String s , String delimeter){
      int fromIndex = 0;
      while(s.indexOf(delimeter, fromIndex) > 0){
        fromIndex = s.indexOf(delimeter, fromIndex);
        
      }
      return null;
    }
    
    public static boolean checkExistenceLikeKeyInList(String key , List<String> patternList){
      Iterator<String> it= patternList.iterator();
      while(it.hasNext()){
        String compare = it.next();
        if(contains( compare , key)){
          return true;
        }
      }
      return false;
    }
    
    
    /**
     * Null Safe Trim()
     * @param str
     * @return
     */
  public static String trim(String str){
    if(!StringUtil.isValid(str))
        return str;
    else
       return str.trim();    
  }

  public static String floatToString(float f) {
    String str = f + "";
    int i = str.indexOf(".");
    if (i < 0)
      return str;
    String str1 = str.substring(i + 1);
    if (str1.length() > 1)
      return str;
    else if (str1.length() > 0)
      return str + "0";
    //else if(str1.length() == 0)
    return str + "00";
  }
  /**
   * for a.b.c.d will return b.c.d
   * @param str
   * @return
   */
  public static String getChildString(String str) {
    if (str.indexOf('.') == -1)
      return str;

    return str.substring(str.lastIndexOf('.') + 1);

  }

  /**
   * gets the Prent for the String 
   *  <br> eg : for  wizard.cutomerdetails.hello
   * will get wizard.cutomerdetails
   * <br> for wizard.customerdetails will get wizard
   *<br> for wizard will get wizard
   */
  public static String getParentString(String viewName) {
    if (viewName.indexOf('.') == -1)
      return viewName;

    return viewName.substring(0, viewName.lastIndexOf('.'));

  }

  /**
   * Null Check and Trim check
   * @param str
   * @return
   */
  public static boolean isValid(String str) {
    if (str == null)
      return false;
    if (str.trim().equals(""))
      return false;

    return true;
  }
/**
 * Chose a valid String from two strings
 * @param str1
 * @param str2
 * @return
 */
  public static String chooseFromString(String str1 , String str2){
    if(!StringUtil.isValid(str1)){
      if(!StringUtil.isValid(str2)){
              return "NA";        
      }else{
        return str2;
      }
    }
    return str1;
  }
  
  
  /**
   * Will parse a String like 
   * aaa=111,bbb=34d,fff=444
   * into a map
   */
  public static Map parseStringIntoMap(String str, String delimiter) {

    StringTokenizer st = new StringTokenizer(str, delimiter);

    TreeMap map = new TreeMap();
    while (st.hasMoreTokens()) {
      String pair = (String) st.nextElement();
      String key = pair.substring(0, pair.indexOf("="));
      String value = pair.substring(pair.indexOf("=") + 1);
      //        System.out.println("Key = " +  key + " value= " + value + " String = " +pair);
      map.put(key, value);
    }
    return map;

  }
  
  
    /**
     * Return "" or the actual string
     * @param str
     * @return
     */
  public static String getSomeString(String str) {
    if (str == null)
      return "";
    else
      return str.trim();
  }

  /**
   * Return null or the trimed String
   * @param str
   * @return
   */
  public static String getValidStringObject(String str) {
    if (str == null)
      return null;
    str = str.trim();
    if (str.equals(""))
      return null;

    return str;
  }
  
  public static String getStringRepresentation(Object obj){
    StringBuilder str = new StringBuilder("DETAILS=");
    Field[] fields = obj.getClass().getDeclaredFields();
    // System.out.println(" Filed Count =" + fields.length);

    for (int i = 0; i < fields.length; i++) {
      Field f = fields[i];
      String name = f.getName();
      String value = "{NA}";
      try {
        value = f.get(obj) + "";
      } catch (IllegalArgumentException e) {
        //e.printStackTrace();
      } catch (IllegalAccessException e) {
        //e.printStackTrace();
      }
      // System.out.println("\t" + name + " = " + value );
      str.append("\t" + name + " = " + value + " \n");
    }

    return str.toString();
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.