I've created a StringUtil class which is used for some string validation across the application. The code for the StringUtil is as below,
public class StringUtil {
public static synchronized boolean isValidString(String string) {
return string!= null && string.trim().length() > 0;
}
}
In this class the method checks whether the string is a valid string or not. This method is thread safe. In an enterprise application, there might be multiple threads accessing this method. If a thread is accessing this method, then all the other threads have to wait for its turn. This method in turn will be used very frequently to check the string for null values. So which is the best option
- Making this a singleton and thread safety
- Making this as instance method
- Is there are any other way to organize a pool with objects of this type and each thread would pick up one and release the object to the pool once done.So thread safety is not a concern and object creation is also not done.
- Are are there any open source libraries for the same.
""is in fact a valid string (in the context of your use-case probably not). How aboutisPresentorisNotEmpty?