All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openxma.dsl.common.Strings Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
package org.openxma.dsl.common;

public class Strings {
	public static final String KEY_COLUMN_PREFIX = "ID_";
	public static final int COLUMN_NAME_LENGTH = 30;
	public static final String EMPTY = "";
	public static final int INDEX_NOT_FOUND = -1;
	private static final int PAD_LIMIT = 8192;
	private static final String[] PADDING = new String[Character.MAX_VALUE];
	static {
		PADDING[32] = "                                                                ";
	}

	public static String toAttributeName(String name) {
		return uncapitalize(toClassName(name));
	}

	public static String toCamelCase(String name) {
		return toCamelCase(name, "");
	}

	public static String uncapitalize(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0) {
			return str;
		}
		return new StringBuffer(strLen).append(Character.toLowerCase(str.charAt(0))).append(str.substring(1)).toString();
	}

	public static String capitalize(String str) {
		int strLen;
		if (str == null || (strLen = str.length()) == 0) {
			return str;
		}
		return new StringBuffer(strLen).append(Character.toTitleCase(str.charAt(0))).append(str.substring(1)).toString();
	}

	public static String toUnderScoreString(String name) {
		StringBuffer stringBuffer = new StringBuffer(name.replace('.', '_'));
		for (int i = 1; i < stringBuffer.length() - 1; i++) {
			if ('_' != stringBuffer.charAt(i - 1)
					&& Character.isUpperCase(stringBuffer.charAt(i))
					&& (!Character.isUpperCase(stringBuffer.charAt(i + 1)) || stringBuffer.substring(i + 1).equals(
							stringBuffer.substring(i + 1).toUpperCase()))) {
				stringBuffer.insert(i++, '_');
			}
		}
		return stringBuffer.toString();
	}

	public static String removeVowelsAndUnderScore(String name) {
		StringBuffer stringBuffer = new StringBuffer(toUnderScoreString(name));
		int index = stringBuffer.length() - 1;
		while (index > 0) {
			char c = stringBuffer.charAt(index);
			if (isVowel(c) && !Character.isUpperCase(c) && index > 0) {
				stringBuffer.delete(index, index + 1);
			}
			--index;
		}
		return stringBuffer.toString();
	}

	public static String toColumnName(String name) {
		name = toUnderScoreString(name).toUpperCase();
		if (name.length() <= COLUMN_NAME_LENGTH)
			return name;
		StringBuffer newName = new StringBuffer(name);
		int index = newName.length() - 1;

		while (newName.length() > COLUMN_NAME_LENGTH && index > 0) {
			char c = newName.charAt(index);

			if (isVowel(c)) {
				newName.delete(index, index + 1);
			}
			--index;
		}
		return newName.toString();
	}
	
	public static String skipLastToken(String value, String separator) {
        int endIndex = value.lastIndexOf(separator);
        if (endIndex > 0)
            return value.substring(0, endIndex);
        return value;
    }
	
	public static String lastToken(String value, String separator) {
        int index = value.lastIndexOf(separator) + separator.length();
        if (index < value.length())
            return value.substring(index, value.length());
        return "";
    }

	public static String toClassName(String name) {
		return toCamelCase(unqualify(name));
	}

	public static String unqualify(String qualifiedName) {
		int loc = qualifiedName.lastIndexOf(".");
		return (loc < 0) ? qualifiedName : qualifiedName.substring(qualifiedName.lastIndexOf(".") + 1);
	}

	public static String repeat(String str, Integer repeat) {
		if (str == null) {
			return null;
		}
		if (repeat <= 0) {
			return EMPTY;
		}
		int inputLength = str.length();
		if (repeat == 1 || inputLength == 0) {
			return str;
		}
		if (inputLength == 1 && repeat <= PAD_LIMIT) {
			return padding(repeat, str.charAt(0));
		}

		int outputLength = inputLength * repeat;
		switch (inputLength) {
		case 1:
			char ch = str.charAt(0);
			char[] output1 = new char[outputLength];
			for (int i = repeat - 1; i >= 0; i--) {
				output1[i] = ch;
			}
			return new String(output1);
		case 2:
			char ch0 = str.charAt(0);
			char ch1 = str.charAt(1);
			char[] output2 = new char[outputLength];
			for (int i = repeat * 2 - 2; i >= 0; i--, i--) {
				output2[i] = ch0;
				output2[i + 1] = ch1;
			}
			return new String(output2);
		default:
			StringBuffer buf = new StringBuffer(outputLength);
			for (int i = 0; i < repeat; i++) {
				buf.append(str);
			}
			return buf.toString();
		}
	}

	private static String padding(int repeat, char padChar) {
		String pad = PADDING[padChar];
		if (pad == null) {
			pad = String.valueOf(padChar);
		}
		while (pad.length() < repeat) {
			pad = pad.concat(pad);
		}
		PADDING[padChar] = pad;
		return pad.substring(0, repeat);
	}

	public static String toCamelCase(String name, String separator) {
		StringBuffer buffer = new StringBuffer();
		String[] tokens = name.split("_");
		if (tokens != null && tokens.length > 0) {
			for (int ctr = 0; ctr < tokens.length; ctr++) {
				if (separator != null && !"".equals(buffer.toString())) {
					buffer.append(separator);
				}
				buffer.append(capitalize(tokens[ctr].toLowerCase()));
			}
		} else {
			buffer.append(capitalize(name.toLowerCase()));
		}
		return buffer.toString();
	}

	private static boolean isVowel(char c) {
		c = Character.toLowerCase(c);
		return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y';
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy