public class SubjectTest
{
	static private String normalizeSubject(String subj, String prefix)
	{
		// JDK IMPLEMENTATION NOTE!  When we require JDK 1.4+, all
		// occurrences of subject.toString.().indexOf(...) can be
		// replaced by subject.indexOf(...).

		StringBuffer subject = new StringBuffer(subj);
		int prefixLength = prefix.length();

		System.err.println("In:  " + subject);

		//If the "prefix" is not at the beginning the subject line, remove it
		int index = subject.toString().indexOf(prefix);
		if (index != 0) {
//			System.err.println("(p) index: " + index + ", subject: " + subject);
			if (index > 0) subject.delete(index, index + prefixLength);
			subject.insert(0, prefix); // insert prefix at the front
		}

		//replace Re: with RE:
		String match = "Re:";
		index = subject.toString().indexOf(match, prefixLength);

		while(index > -1){
//			System.err.println("(a) index: " + index + ", subject: " + subject);
			subject.replace(index, index + match.length(), "RE:");
			index = subject.toString().indexOf(match, prefixLength);
//			System.err.println("(b) index: " + index + ", subject: " + subject);
		}

		//reduce them to one at the beginning
		match ="RE:";
		int indexRE = subject.toString().indexOf(match, prefixLength) + match.length();
		index = subject.toString().indexOf(match, indexRE);
		while(index > 0){
//			System.err.println("(c) index: " + index + ", subject: " + subject);
			subject.delete(index, index + match.length());
			index = subject.toString().indexOf(match, indexRE);
//			System.err.println("(d) index: " + index + ", subject: " + subject);
		}

		//reduce blanks
		match = "  ";
		index = subject.toString().indexOf(match, prefixLength);
		while(index > -1){
//			System.err.println("(e) index: " + index + ", subject: " + subject);
			subject.replace(index, index + match.length(), " ");
			index = subject.toString().indexOf(match, prefixLength);
//			System.err.println("(f) index: " + index + ", subject: " + subject);
		}


		System.err.println("Out: " + subject);
		System.err.println();

		return subject.toString();
	}

	static void test(String subj, String mustbe)
	{
		if (!subj.equals(mustbe)) System.err.println(subj + " != " + mustbe);
	}


	public static void main(String[] args)
	{
		test(normalizeSubject("A subject line", "[My Prefix] "), "[My Prefix] A subject line");
		test(normalizeSubject("[My Prefix] Re: A subject line", "[My Prefix] "), "[My Prefix] RE: A subject line");
		test(normalizeSubject("[My Prefix] Re:A subject line", "[My Prefix] "), "[My Prefix] RE:A subject line");

		test(normalizeSubject("Re: A subject line", "[My Prefix] "), "[My Prefix] RE: A subject line");
		test(normalizeSubject("Re: [My Prefix] Re: A subject line", "[My Prefix] "), "[My Prefix] RE: A subject line");
		test(normalizeSubject("Re: [My Prefix] Re:A subject line", "[My Prefix] "), "[My Prefix] RE: A subject line");

		test(normalizeSubject("Re: Re: A subject line", "[My Prefix] "), "[My Prefix] RE: A subject line");
		test(normalizeSubject("Re: [My Prefix] Re: Re: A subject line", "[My Prefix] "), "[My Prefix] RE: A subject line");
		test(normalizeSubject("Re: [My Prefix] Re: Re:A subject line", "[My Prefix] "), "[My Prefix] RE: A subject line");

		test(normalizeSubject("Re: Re: Re: A subject line", "[My Prefix] "), "[My Prefix] RE: A subject line");
		test(normalizeSubject("Re: [My Prefix] Re: Re: Re: A subject line", "[My Prefix] "), "[My Prefix] RE: A subject line");
		test(normalizeSubject("Re: [My Prefix] Re:Re:Re: A subject line", "[My Prefix] "), "[My Prefix] RE: A subject line");

		test(normalizeSubject("Re: [My Prefix] Re:Re:Re: A subject line (was re: Old subject)", "[My Prefix] "), "[My Prefix] RE: A subject line (was re: Old subject)");
		test(normalizeSubject("Re: [My Prefix] Re:Re:Re: A subject line (was Re: Old subject)", "[My Prefix] "), "[My Prefix] RE: A subject line (was Old subject)");
	}
}
