001: /**
002: * Copyright (C) 2006, 2007 David Bulmore, Software Sensation Inc.
003: * All Rights Reserved.
004: *
005: * This file is part of jCommonTk.
006: *
007: * jCommonTk is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License (Version 2) as published by
009: * the Free Software Foundation.
010: *
011: * jCommonTk is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with jCommonTk; if not, write to the Free Software Foundation,
018: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
019: */package jcommontk.utils;
020:
021: public class StringUtils {
022: public static String emptyToNull(String val) {
023: return emptyToDefault(val, null);
024: }
025:
026: public static String nullToEmpty(String val) {
027: return emptyToDefault(val, "");
028: }
029:
030: public static String emptyToDefault(String val, String defaultValue) {
031: if (val == null || val.length() == 0)
032: return defaultValue;
033:
034: return val;
035: }
036:
037: public static String lowerCaseUnderlineToCamelCase(String name) {
038: StringBuffer newName = new StringBuffer();
039: boolean nextIsUpper = false;
040:
041: name = name.toLowerCase();
042:
043: for (int i = 0; i < name.length(); i++) {
044: if ((name.charAt(i)) == '_')
045: nextIsUpper = true;
046: else {
047: newName.append(nextIsUpper ? Character.toUpperCase(name
048: .charAt(i)) : name.charAt(i));
049:
050: nextIsUpper = false;
051: }
052: }
053:
054: return newName.toString();
055: }
056:
057: public static String camelCaseToLowerCaseUnderline(String name) {
058: StringBuffer newName = new StringBuffer();
059:
060: for (int i = 0; i < name.length(); i++)
061: if (Character.isUpperCase(name.charAt(i))) {
062: if (i == 0)
063: newName.append(Character
064: .toLowerCase(name.charAt(i)));
065: else
066: newName.append("_").append(
067: Character.toLowerCase(name.charAt(i)));
068: } else
069: newName.append(name.charAt(i));
070:
071: return newName.toString();
072: }
073:
074: public static String upperCaseUnderlineToCamelCase(String name) {
075: return lowerCaseUnderlineToCamelCase(name);
076: }
077:
078: public static String camelCaseToUpperCaseUnderline(String name) {
079: StringBuffer newName = new StringBuffer();
080:
081: for (int i = 0; i < name.length(); i++)
082: if (Character.isUpperCase(name.charAt(i))) {
083: if (i == 0)
084: newName.append(Character
085: .toUpperCase(name.charAt(i)));
086: else
087: newName.append("_").append(
088: Character.toUpperCase(name.charAt(i)));
089: } else
090: newName.append(Character.toUpperCase(name.charAt(i)));
091:
092: return newName.toString();
093: }
094:
095: public static String toString(Object[] objects) {
096: String str = "";
097:
098: if (objects != null)
099: for (int i = 0; i < objects.length; i++)
100: str += "[" + objects[i] + "]"
101: + (i < objects.length - 1 ? ", " : "");
102:
103: return str;
104: }
105: }
|