package string;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class MyStringUtil
{
public static final String RETURN_ESCAPE_SEQUENCE;
static
{
RETURN_ESCAPE_SEQUENCE = System.getProperty("line.separator");
}
private MyStringUtil()
{
}
public static final CharSequence getMultipleReturnEscapeSequences(final int quantity)
{
String ret = "";
for (int cnt = 0; cnt < quantity; cnt++)
{
ret = ret + MyStringUtil.RETURN_ESCAPE_SEQUENCE;
}
return ret;
}
public static String getValue(String content, final String name)
{
final int pos = MyStringUtil.getIndexAfterKeywordAndWhitespaces(content, name);
for (int cnt = pos; cnt < content.length(); cnt++)
{
final char c = content.charAt(cnt);
if (c == ' ')
{
content = content.substring(pos, cnt);
break;
}
}
return content;
}
public static boolean stringIsEmptyOrNull(final String caString)
{
if (!((caString == null) || (caString.intern() == "")))
{
return false;
}
return true;
}
public static String setNullStringToEmpty(String aString)
{
if (aString == null)
{
aString = "";
}
return aString;
}
// replace method for small strings, <= 512 bytes...
public static String replaceSmallString(final String orig, final String search, final String replace)
{
final int cap = replace.length() > search.length() ? orig.length() + (replace.length() - search.length()) * 20 : orig.length();
final StringWriter out = new StringWriter(cap);
int prev = 0;
CharSequence okPart;
for (int i = orig.indexOf(search); i != -1; i = orig.indexOf(search, prev))
{
okPart = orig.subSequence(prev, i);
out.append(okPart).append(replace);
prev = i + search.length();
}
if (prev < orig.length())
{
okPart = orig.subSequence(prev, orig.length());
out.append(okPart);
}
return out.toString();
}
private static int[] initNext(final String pat)
{
final int[] next = new int[pat.length()];
int i = 0;
int j = -1;
next[0] = j;
while (i < pat.length() - 1)
{
while ((j >= 0) && (pat.charAt(i) != pat.charAt(j)))
{
j = next[j];
}
i++;
j++;
next[i] = j;
}
return next;
}
public static int getIndexBeforeKeyword(final String text, final String pat)
{
final int[] next = MyStringUtil.initNext(pat);
int i = 0;
int j = i;
while (i < text.length())
{
while ((j >= 0) && (pat.charAt(j) != text.charAt(i)))
{
j = next[j];
}
i++;
j++;
if (j == pat.length())
{
return i - pat.length();
}
}
return -1;
}
public static int getIndexAfterKeywordAndWhitespaces(final String text, String pat)
{
pat = pat.trim();
int pos = MyStringUtil.getIndexAfterKeyword(text, pat);
if (text.length() > pos)
{
while (true)
{
pos++;
if (text.charAt(pos) != ' ')
{
break;
}
}
}
return pos;
}
public static int getIndexAfterKeyword(final String text, final String pat)
{
int pos = MyStringUtil.getIndexBeforeKeyword(text, pat);
if (pos != -1)
{
pos = pos + pat.length();
}
return pos;
}
public static int getTokenPosition(final String aTokenizedString, final String aToken)
{
int lPosition = 0;
final StringTokenizer lStringTokenizer = new StringTokenizer(aTokenizedString);
while (lStringTokenizer.hasMoreTokens())
{
lPosition++;
if (lStringTokenizer.nextToken(",").trim().intern() == aToken.trim().intern())
{
break;
}
}
return lPosition;
}
public static String removeToken(final int aDelimiterPosition, final String aTokenizedString)
{
int lCounter = 0;
String lToken = "";
final StringTokenizer lStringTokenizer = new StringTokenizer(aTokenizedString, ",");
final int lQuantityOfTokens = lStringTokenizer.countTokens();
while (lStringTokenizer.hasMoreTokens())
{
if (lCounter > (aDelimiterPosition - 1))
{
lToken += lStringTokenizer.nextToken().trim();
if (lCounter < (lQuantityOfTokens - 1))
{
lToken += ", ";
}
}
else
{
lStringTokenizer.nextToken(",");
}
lCounter++;
}
return lToken;
}
public static int countTokens(final String str, final char token)
{
int ret = 0;
if (MyStringUtil.stringIsEmptyOrNull(str))
{
return ret;
}
final StringTokenizer st = new StringTokenizer(str);
while (st.hasMoreTokens())
{
st.nextToken(",");
ret++;
}
return ret;
}
public static String myToString(final Object object)
{
Class<?> c = null;
try
{
c = Class.forName(object.getClass().getCanonicalName());
}
catch (final Exception e)
{
// never thrown --> this.getClass().getCanonicalName() can't be a non-existing class...
}
final Field field[] = c.getDeclaredFields();
final List<String> classFields = new ArrayList<String>(field.length);
for (final Field element : field)
{
element.setAccessible(true);
final String cf = element.toString();
final String fieldName = cf.substring(cf.lastIndexOf(".") + 1);
Object value = null;
try
{
value = element.get(object);
}
catch (final Exception e)
{
e.printStackTrace();
}
classFields.add(fieldName + " = " + value);
}
return classFields.toString();
}
}