package fixjava;

public class StringUtils {

	/** Repeat the given string the requested number of times. */
	public static String repeat(String str, int numTimes) {
		StringBuilder buf = new StringBuilder(Math.max(0, str.length() * numTimes));
		for (int i = 0; i < numTimes; i++)
			buf.append(str);
		return buf.toString();
	}

	/**
	 * Pad the left hand side of a field with the given character. Always adds at least one pad character. If the length of str is
	 * greater than numPlaces-1, then the output string will be longer than numPlaces.
	 */
	public static String padLeft(String str, char padChar, int numPlaces) {
		int bufSize = Math.max(numPlaces, str.length() + 1);
		StringBuilder buf = new StringBuilder(Math.max(numPlaces, str.length() + 1));
		buf.append(padChar);
		for (int i = 1, mi = bufSize - str.length(); i < mi; i++)
			buf.append(padChar);
		buf.append(str);
		return buf.toString();
	}

	/**
	 * Pad the right hand side of a field with the given character. Always adds at least one pad character. If the length of str is
	 * greater than numPlaces-1, then the output string will be longer than numPlaces.
	 */
	public static String padRight(String str, char padChar, int numPlaces) {
		int bufSize = Math.max(numPlaces, str.length() + 1);
		StringBuilder buf = new StringBuilder(Math.max(numPlaces, str.length() + 1));
		buf.append(str);
		buf.append(padChar);
		while (buf.length() < bufSize)
			buf.append(padChar);
		return buf.toString();
	}

	/** Intern all strings in an array, skipping null elements. */
	public static String[] intern(String[] arr) {
		for (int i = 0; i < arr.length; i++)
			arr[i] = arr[i].intern();
		return arr;
	}

	/** Intern a string, ignoring null */
	public static String intern(String str) {
		return str == null ? null : str.intern();
	}

}

package fixjava;

import java.util.ArrayList;

/**
 * An ArrayList that allows values to be set at positions greater than or equal to size(), which causes the end of the list to be padded with an
 * identity element before the value is set
 */
public class ArrayListAutoPadded<T> extends ArrayList<T> {

	private T identity;

	public ArrayListAutoPadded(T identity) {
		super();
		this.identity = identity;
	}

	public ArrayListAutoPadded(T identity, int initialCapacity) {
		super(initialCapacity);
		this.identity = identity;
	}

	/**
	 * Set the value at the given location to the specified value, padding the list to the desired length if necessary. Returns the old value at the
	 * location, if there was one, otherwise the identity value that was passed in the constructor.
	 */
	public T set(int index, T element) {
		if (size() > index) {
			return super.set(index, element);
		} else {
			while (size() < index)
				add(identity);
			add(element);
			return identity;
		}
	};

	/**
	 * Combine the value at the given location with the new value, by calling labmda.apply(oldValue, newValue). The identity value that was passed in
	 * the constructor is used if the index >= size(). Returns the *old* value that was stored at the index before combining, or identity if there was
	 * no old value.
	 */
	public T combineAndSet(int index, Lambda2<T, T, T> combineFunc, T newValue) {
		T oldValue;
		if (size() > index) {
			oldValue = get(index);
		} else {
			while (size() <= index)
				add(identity);
			oldValue = identity;
		}
		return super.set(index, combineFunc.apply(oldValue, newValue));
	}

	private static final long serialVersionUID = 1L;
}
