class StringUtilities {

	public static String trimMiddle(String s, int max) {

		int len = s.length();
		if (len <= max) {
			return s;
		}

		int minimum = ELLIPSES.length() + 2; // +2 for one char on each side
		if (max < minimum) {
			throw new IllegalArgumentException("Max cannot be less than " + minimum);
		}

		int toRemove = (len - max) + ELLIPSES.length();
		int toKeep = len - toRemove;
		int lhsSize = toKeep / 2;
		int rhsSize = lhsSize;

		if (toKeep % 2 != 0) {
			rhsSize++; // compensate for roundoff on odd size
		}

		StringBuilder buffy = new StringBuilder();
		buffy.append(s.substring(0, lhsSize));
		buffy.append(ELLIPSES);
		buffy.append(s.substring(len - rhsSize, len));

		return buffy.toString();
	}

	private  StringUtilities();

}