import java.util.StringTokenizer;

public class VariousCheck
{
 final static int max = 15;

 public static boolean checkException(String s)
 {
  if(s == null) return false;
  if(s.length()<2) return false;
  if(s.equalsIgnoreCase("CASH")) return true;
  return false;
 }

 public static boolean checkInclude(String[] o,String s)
 {
  int len = o.length;
  for(int i=0;i<len;i++)
   if(o[i].equalsIgnoreCase(s)) return true;
  return false;
 }

 public static boolean checkExist(int[] h, int x)
 {
  int len = h.length;
  for(int i=0;i<len;i++)
  if(h[i] == x) return true;
   return false;
 }

 public static int getZero(int[] h)
 {
  int len = h.length;
  for(int i=0;i<len;i++)
   if(h[i]==0) return i;
  return 0;
 }

 public static String formNumDelimiter(String str)
 {
  StringBuffer orig = new StringBuffer(str);
  boolean flag = false;
  int p = orig.indexOf(".");
  int l = orig.length();
  int r,d;
  if(p>0)
  {
   d = p/3;
   r = p%3;
   flag = true;
  }
  else
  {
   d = l/3;
   r = l%3;
  }
  int j=0;
  int i=0;
  if(flag && p<=3) { }
  else if(!flag && l<=3) { }
  else if(flag && str.charAt(0) == '-' && l <=7) { }
  else
  {
   if(r == 0) j = 3;
   else j = r;
   while(j<=l)
   {
    if(r==0&&i==d-1) break;
    else if(i==d) break;
    orig=orig.insert(j,',');
    j=j+4;
    i++;
   }
  }
  return orig.toString();
 }

/**
 * checkMax's method is to check whether the string exceeds the max allowed
 * @param String str - string to be check
 * @param boolean false if str exceeds max else true
 */
 public static boolean checkMax(String str)
 {
  if(str == null) return false;
  int pos = str.indexOf('.');
  int len = max - 3;
  if(pos<0 && str.length()>len) return false;
  if(pos>=0 && pos>len) return false;
  return true;
 }

 public static boolean checkDates(String tmp)
 {
  if(tmp.indexOf(',')<0) return false;
  int run = 1;
  StringTokenizer cut = new StringTokenizer(tmp); 
  StringBuffer mb = new StringBuffer(16);
  while(cut.hasMoreElements())
  {
   if(run>2) return true;
   if(checkDate(cut.nextToken(",")))
    run++; 
   else
    return false;
  }
  return true;
 }

/**
 * checkDate's method is to check Date in the String. It will check for
 * the lenght of the String - which has to be 10 characters long and the
 * month,day, and year. Date format: DD-MM-YYYY
 * @param String date
 * @return true if valid else false
 */
 public static boolean checkDate(String date)
 {
  if(date == null) return false;
  return DateUtil.verifyNormalDate(date);
 }

/**
 * checkInt's method is to check Integer in the String. It has 3 modes.
 * They are 1 - pure integer, 2 - date, 3 - double/float.
 * @param String str - String to be check
 *        int mode - 1 pure Integer
 *                 - 2 date 
 *                 - 3 double/float
 * @return true if everything is okay
 */
 public static boolean checkInt(String str, int mode)
 {
  if(str==null) return false;
  int size = str.length();
  for(int i=0;i<size;i++)
  {
   if(!Character.isDigit(str.charAt(i)))
   {
    switch(mode)
    {
     case 1 : return false; 
     case 2 : if(str.charAt(i)!='-') return false; break;
     case 3 : if(str.charAt(i)!='.') return false; 
              else if(i==(size-3)||i==(size-2)||i==(size-1))
              { }
              else return false;
	      break;	
    }
   }
  }
  return true;
 }

/**
 * formDate's method is to form the date from the private data
 * for each calling method.
 * @param normal date format 
 * @return String - date in SQL form
 */
 public static String formDate(String date)
 {
  if(date == null) return null;
  int pos = date.indexOf("-");
  int pos2= date.indexOf("-",pos+1);
  if((pos<0)||(pos2<0)) return null;
  return new String(date.substring(pos2+1,date.length())+"-"+
    date.substring(pos+1,pos2)+"-"+date.substring(0,pos));
  }

/**
 * formNDate's method is to form Normal date method. [DD-MM-YYYY]
 * @param String str - SQL date to be rearrange
 * @return String of DD-MM-YYYY
 */
 public static String formNDate(String str)
 {
  if(str == null) return new String("00-00-0000");
  int pos = str.indexOf('-');
  if(pos<0) return null;
  int pos2= str.indexOf('-',pos+1);
  if(pos2<0) return null;
  String yy = str.substring(0,pos);
  String mm = str.substring(pos+1,pos2);
  String dd = str.substring(pos2+1,str.length());
  return new String(dd+"-"+mm+"-"+yy);
 } 

/**
 * compareDate's method is to compare which date is earlier 
 * @param String d1 - first date, String d2 - second date
 * @return int -1 if d1 < d2, 1 if d1 > d2, else 0 if d1 == d2
 */
 public static int compareDate(String d1,String d2)
 {
  if(d1 == null && d2 != null) return -1;
  if(d1 != null && d2 == null) return 1;
  if(d1 == null && d2 == null) return 0;
  int pos11 = d1.indexOf('-');  
  int pos12 = d1.indexOf('-',pos11+1);
  int pos21 = d2.indexOf('-');
  int pos22 = d2.indexOf('-',pos21+1);
  int t1= Integer.parseInt(d1.substring(0,pos11));
  int t2= Integer.parseInt(d2.substring(0,pos21));
  if(t1 - t2 != 0) return compare(t1,t2);
  t1 = Integer.parseInt(d1.substring(pos11+1,pos12));
  t2 = Integer.parseInt(d2.substring(pos21+1,pos22));
  if(t1 - t2 != 0) return compare(t1,t2);
  t1 = Integer.parseInt(d1.substring(pos12+1,d1.length()));
  t2 = Integer.parseInt(d2.substring(pos22+1,d2.length()));
  if(t1 - t2 != 0) return compare(t1,t2);
  return 0;
 }

 public static int compareTime(String t1,String t2)
 {
  if(t1 == null && t2 == null) return 0;
  if(t1 != null && t2 == null) return 1;
  if(t1 == null && t2 != null) return -1;
  int pos11 = t1.indexOf(':');
  int pos12 = t1.indexOf(':',pos11+1);
  int pos21 = t2.indexOf(':');
  int pos22 = t2.indexOf(':',pos21+1);
  int v1 = Integer.parseInt(t1.substring(0,pos11));
  int v2 = Integer.parseInt(t2.substring(0,pos21));
  if(v1 - v2 != 0) return compare (v1,v2);
  v1 = Integer.parseInt(t1.substring(pos11+1,pos12));
  v2 = Integer.parseInt(t2.substring(pos21+1,pos22));
  if(v1 - v2 != 0) return compare(v1,v2);
  v1 = Integer.parseInt(t1.substring(pos12+1,t1.length()));
  v2 = Integer.parseInt(t2.substring(pos22+1,t2.length()));
  if(v1 - v2 != 0) return compare(v1,v2);
  return 0;
 }

 public static int compare(int n1,int n2)
 {
  if(n1 < n2) return -1;
  if(n1 > n2) return 1;
  return 0;
 }

}
