Monday, September 8, 2014

JAVA UTILITY METHODS FOR ALL

import java.io.Closeable;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import junit.framework.Assert;

/*
 * Author: Raj Kirpalsinh
 * */

public class JavaUtility {

/* checking not null and not empty on the string object without trimming */
public static boolean isNotNullAndNotEmpty(String reference) {
return (reference != null && reference.trim().length()>0);
}


/* checking null/empty on the string object without trimming */
public static boolean isNullOrEmpty(String reference) {
return (reference == null || reference.trim().length()==0);
}

/* checking not null and  not empty on the Collection (interface) object */
public static boolean isNotNullAndNotEmpty(Collection reference) {
return (reference != null && reference.size()>0);
}

/* checking null/empty on the Collection (interface) object */
public static boolean isNullOrEmpty(Collection reference) {
return (reference == null || reference.size()==0);
}

/* checking not null and not empty on the Map (interface) object */
public static boolean isNotNullAndNotEmpty(Map reference) {
return (reference != null && reference.size()>0);
}

/* checking null/empty on the Map (interface) object */
public static boolean isNullOrEmpty(Map reference) {
return (reference == null || reference.size()==0);
}

/* trimming string array all elements values*/
public static String[] trim(String[] refArray) {
        if (refArray == null) {
            return refArray;
        }
        for (int i = 0, len = refArray.length; i < len; i++) {
            refArray[i] = refArray[i].trim();
        }
        return refArray;
    }

/* checking not null and not empty on the Array (Object[])*/
public static boolean isNotNullAndNotEmpty(Object[] reference) {
return (reference != null && reference.length>0);
}

/* checking null/empty on the Array (Object[])*/
public static boolean isNullOrEmpty(Object[] reference) {
return (reference == null || reference.length==0);
}

/* Closing all the objects/streams have Closeable interface nature */
public static void closeMe(Closeable reference) {
if(reference!=null) {
try {
reference.close();
} catch (IOException e) {
e.printStackTrace();
}
return;
}
}

/* Converting any string (i.e word/line) in Camel-Case
* i.e hello good morning buddy -->> Hello Good Morning Buddy
* */
public static String toCamelCase(String reference) {
if(isNullOrEmpty(reference) == false) {
String words[] = reference.split(" ");
StringBuilder line = new StringBuilder();
for(String word : words) {
word = word.trim().toLowerCase();
if(isNotNullAndNotEmpty(word)){
word = word.replaceFirst(String.valueOf(word.charAt(0)), String.valueOf(word.charAt(0)).toUpperCase());
line.append(word).append(" ");
}
}
return line.toString().trim();  
}
return reference;
}


public static void main(String dataBag[]) {
System.out.println("------BEGIN------");

String str = "  hello good morning buddy  ";
System.out.println("toCamelCase: "+toCamelCase(str));

String test = "";

Assert.assertEquals((isNotNullAndNotEmpty(test) == false), (isNullOrEmpty(test) == true));
Assert.assertEquals((isNotNullAndNotEmpty("") == false), (isNullOrEmpty("") == true));
Assert.assertEquals((isNotNullAndNotEmpty("adfa") == true), (isNullOrEmpty("") == false));

System.out.println("------END------");
     }
}

No comments:

Post a Comment