This fixes a number of warnings in some java.text classes and adds a few minor optimisations (use of final fields, ArrayLists in place of Vectors where synchronisation is not needed).
ChangeLog: 2008-12-31 Andrew John Hughes <gnu_and...@member.fsf.org> * java/text/MessageFormat.java: Convert variables to use generic types, use CPStringBuilder in place of StringBuilder. (Field()): Suppress warning due to only being used by deserialization. (scanFormat(String,int,CPStringBuilder,List,Locale)): Use ArrayList instead of Vector as no synchronisation needed. (parse(String,ParsePosition)): Likewise. * java/text/NumberFormat.java: (Field()): Suppress warning due to only being used by deserialization. * java/text/RuleBasedCollator.java: Convert variables to use generic types. (CollationElement): Make fields final. (CollationSorter): Likewise. (CollationSorter(int,String,int,boolean)): New constructor. * java/text/SimpleDateFormat.java, Convert variables to use generic types. (applyPattern(String)): Clear list rather than creating a new instance. -- Andrew :) Support Free Java! Contribute to GNU Classpath and the OpenJDK http://www.gnu.org/software/classpath http://openjdk.java.net PGP Key: 94EFD9D8 (http://subkeys.pgp.net) Fingerprint = F8EF F1EA 401E 2E60 15FA 7927 142C 2591 94EF D9D8
Index: java/text/MessageFormat.java =================================================================== RCS file: /sources/classpath/classpath/java/text/MessageFormat.java,v retrieving revision 1.27 diff -u -u -r1.27 MessageFormat.java --- java/text/MessageFormat.java 6 May 2008 22:20:41 -0000 1.27 +++ java/text/MessageFormat.java 31 Dec 2008 07:59:27 -0000 @@ -38,13 +38,17 @@ package java.text; +import gnu.java.lang.CPStringBuilder; + import gnu.java.text.FormatCharacterIterator; import java.io.InvalidObjectException; + +import java.util.ArrayList; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Locale; -import java.util.Vector; public class MessageFormat extends Format { @@ -69,7 +73,7 @@ // Argument will be checked to make sure it is an instance of this // class. - Class formatClass; + Class<?> formatClass; // Formatter type. String type; @@ -165,6 +169,7 @@ public static final MessageFormat.Field ARGUMENT = new MessageFormat.Field("argument"); // For deserialization + @SuppressWarnings("unused") private Field() { super(""); @@ -194,7 +199,7 @@ // Helper that returns the text up to the next format opener. The // text is put into BUFFER. Returns index of character after end of // string. Throws IllegalArgumentException on error. - private static int scanString(String pat, int index, StringBuilder buffer) + private static int scanString(String pat, int index, CPStringBuilder buffer) { int max = pat.length(); buffer.setLength(0); @@ -234,7 +239,7 @@ // This helper retrieves a single part of a format element. Returns // the index of the terminating character. private static int scanFormatElement(String pat, int index, - StringBuilder buffer, char term) + CPStringBuilder buffer, char term) { int max = pat.length(); buffer.setLength(0); @@ -281,11 +286,11 @@ // This is used to parse a format element and whatever non-format // text might trail it. - private static int scanFormat(String pat, int index, StringBuilder buffer, - Vector elts, Locale locale) + private static int scanFormat(String pat, int index, CPStringBuilder buffer, + List<MessageFormatElement> elts, Locale locale) { MessageFormatElement mfe = new MessageFormatElement (); - elts.addElement(mfe); + elts.add(mfe); int max = pat.length(); @@ -342,17 +347,16 @@ { pattern = newPattern; - StringBuilder tempBuffer = new StringBuilder (); + CPStringBuilder tempBuffer = new CPStringBuilder (); int index = scanString (newPattern, 0, tempBuffer); leader = tempBuffer.toString(); - Vector elts = new Vector (); + List<MessageFormatElement> elts = new ArrayList<MessageFormatElement>(); while (index < newPattern.length()) index = scanFormat (newPattern, index, tempBuffer, elts, locale); - elements = new MessageFormatElement[elts.size()]; - elts.copyInto(elements); + elements = elts.toArray(new MessageFormatElement[elts.size()]); } /** @@ -494,7 +498,8 @@ if (output_iterator != null) { - HashMap hash_argument = new HashMap(); + HashMap<MessageFormat.Field, Integer> hash_argument = + new HashMap<MessageFormat.Field, Integer>(); int position = output_iterator.getEndIndex(); hash_argument.put (MessageFormat.Field.ARGUMENT, @@ -613,7 +618,7 @@ } index += leader.length(); - Vector results = new Vector (elements.length, 1); + ArrayList<Object> results = new ArrayList<Object>(elements.length); // Now check each format. for (int i = 0; i < elements.length; ++i) { @@ -681,15 +686,13 @@ } if (elements[i].argNumber >= results.size()) - results.setSize(elements[i].argNumber + 1); - results.setElementAt(value, elements[i].argNumber); + results.ensureCapacity(elements[i].argNumber + 1); + results.set(elements[i].argNumber, value); index += elements[i].trailer.length(); } - Object[] r = new Object[results.size()]; - results.copyInto(r); - return r; + return results.toArray(new Object[results.size()]); } public Object[] parse (String sourceStr) throws ParseException Index: java/text/NumberFormat.java =================================================================== RCS file: /sources/classpath/classpath/java/text/NumberFormat.java,v retrieving revision 1.24 diff -u -u -r1.24 NumberFormat.java --- java/text/NumberFormat.java 12 Mar 2008 23:39:51 -0000 1.24 +++ java/text/NumberFormat.java 31 Dec 2008 07:59:27 -0000 @@ -177,6 +177,7 @@ * This constructor is only used by the deserializer. Without it, * it would fail to construct a valid object. */ + @SuppressWarnings("unused") private Field() { super(""); Index: java/text/RuleBasedCollator.java =================================================================== RCS file: /sources/classpath/classpath/java/text/RuleBasedCollator.java,v retrieving revision 1.34 diff -u -u -r1.34 RuleBasedCollator.java --- java/text/RuleBasedCollator.java 6 May 2008 22:20:41 -0000 1.34 +++ java/text/RuleBasedCollator.java 31 Dec 2008 07:59:28 -0000 @@ -151,13 +151,13 @@ */ static final class CollationElement { - String key; - int primary; - short secondary; - short tertiary; - short equality; - boolean ignore; - String expansion; + final String key; + final int primary; + final short secondary; + final short tertiary; + final short equality; + final boolean ignore; + final String expansion; CollationElement(String key, int primary, short secondary, short tertiary, short equality, String expansion, boolean ignore) @@ -185,7 +185,7 @@ * {...@link #mergeRules(int,java.lang.String,java.util.ArrayList,java.util.ArrayList)}) * as a temporary state while merging two sets of instructions. */ - static final class CollationSorter + private static final class CollationSorter { static final int GREATERP = 0; static final int GREATERS = 1; @@ -194,29 +194,39 @@ static final int RESET = 4; static final int INVERSE_SECONDARY = 5; - int comparisonType; - String textElement; - int hashText; - int offset; - boolean ignore; + final int comparisonType; + final String textElement; + final int hashText; + final int offset; + final boolean ignore; String expansionOrdering; + + private CollationSorter(final int comparisonType, final String textElement, + final int offset, final boolean ignore) + { + this.comparisonType = comparisonType; + this.textElement = textElement; + this.offset = offset; + this.ignore = ignore; + hashText = textElement.hashCode(); + } } /** - * This the the original rule string. + * This is the original rule string. */ private String rules; /** * This is the table of collation element values */ - private Object[] ce_table; + private CollationElement[] ce_table; /** * Quick-prefix finder. */ - HashMap prefix_tree; + HashMap<String,CollationElement> prefix_tree; /** * This is the value of the last sequence entered into @@ -306,7 +316,8 @@ * @param patch Rules to be merged into the repository. * @throws ParseException if it is impossible to find an anchor point for the new rules. */ - private void mergeRules(int offset, String starter, ArrayList main, ArrayList patch) + private void mergeRules(int offset, String starter, ArrayList<CollationSorter> main, + ArrayList<CollationSorter> patch) throws ParseException { int insertion_point = -1; @@ -324,8 +335,8 @@ while (j < main.size()) { - CollationSorter rule1 = (CollationSorter) patch.get(i); - CollationSorter rule2 = (CollationSorter) main.get(j); + CollationSorter rule1 = patch.get(i); + CollationSorter rule2 = main.get(j); if (rule1.textElement.equals(rule2.textElement)) main.remove(j); @@ -337,7 +348,7 @@ // Find the insertion point... O(N) for (int i = 0; i < main.size(); i++) { - CollationSorter sorter = (CollationSorter) main.get(i); + CollationSorter sorter = main.get(i); int length = findPrefixLength(starter, sorter.textElement); if (length > max_length) @@ -363,9 +374,7 @@ * sequence. The rest of the subsequence must be appended * to the end of the sequence. */ - CollationSorter sorter = (CollationSorter) patch.get(0); - CollationSorter expansionPrefix = - (CollationSorter) main.get(insertion_point-1); + CollationSorter sorter = patch.get(0); sorter.expansionOrdering = starter.substring(max_length); // Skip the first good prefix element @@ -398,7 +407,7 @@ * @throws ParseException if something turned wrong during the parsing. To get details * decode the message. */ - private int subParseString(boolean stop_on_reset, ArrayList v, + private int subParseString(boolean stop_on_reset, ArrayList<CollationSorter> v, int base_offset, String rules) throws ParseException { @@ -508,7 +517,7 @@ * indicated by the text element. */ String subrules = rules.substring(i); - ArrayList sorted_rules = new ArrayList(); + ArrayList<CollationSorter> sorted_rules = new ArrayList<CollationSorter>(); int idx; // Parse the subrules but do not iterate through all @@ -533,16 +542,12 @@ break main_parse_loop; } - CollationSorter sorter = new CollationSorter(); - + String textElement = sb.toString(); if (operator == CollationSorter.GREATERP) ignoreChars = false; - - sorter.comparisonType = operator; - sorter.textElement = sb.toString(); - sorter.hashText = sorter.textElement.hashCode(); - sorter.offset = base_offset+rules.length(); - sorter.ignore = ignoreChars; + CollationSorter sorter = new CollationSorter(operator, textElement, + base_offset + rules.length(), + ignoreChars); sb.setLength(0); v.add(sorter); @@ -551,7 +556,6 @@ if (operator >= 0) { - CollationSorter sorter = new CollationSorter(); int pos = rules.length() + base_offset; if ((sb.length() != 0 && nextIsModifier) @@ -561,11 +565,8 @@ if (operator == CollationSorter.GREATERP) ignoreChars = false; - sorter.comparisonType = operator; - sorter.textElement = sb.toString(); - sorter.hashText = sorter.textElement.hashCode(); - sorter.offset = base_offset+pos; - sorter.ignore = ignoreChars; + CollationSorter sorter = new CollationSorter(operator, sb.toString(), + base_offset+pos, ignoreChars); v.add(sorter); } @@ -593,10 +594,10 @@ * @throws ParseException if something turned wrong during the parsing. To get details * decode the message. */ - private ArrayList parseString(String rules) + private ArrayList<CollationSorter> parseString(String rules) throws ParseException { - ArrayList v = new ArrayList(); + ArrayList<CollationSorter> v = new ArrayList<CollationSorter>(); // result of the first subParseString is not absolute (may be -1 or a // positive integer). But we do not care. @@ -612,7 +613,7 @@ * @param parsedElements Parsed instructions stored in a ArrayList. * @throws ParseException if the order of the instructions are not valid. */ - private void buildCollationVector(ArrayList parsedElements) + private void buildCollationVector(ArrayList<CollationSorter> parsedElements) throws ParseException { int primary_seq = 0; @@ -624,14 +625,13 @@ final boolean DECREASING = false; final boolean INCREASING = true; boolean secondaryType = INCREASING; - ArrayList v = new ArrayList(); + ArrayList<CollationElement> v = new ArrayList<CollationElement>(); // elts is completely sorted. element_loop: for (int i = 0; i < parsedElements.size(); i++) { - CollationSorter elt = (CollationSorter) parsedElements.get(i); - boolean ignoreChar = false; + CollationSorter elt = parsedElements.get(i); switch (elt.comparisonType) { @@ -686,7 +686,7 @@ this.inverseAccentComparison = inverseComparisons; - ce_table = v.toArray(); + ce_table = v.toArray(new CollationElement[v.size()]); last_primary_value = primary_seq+1; last_tertiary_value = last_tertiary_seq+1; @@ -699,11 +699,11 @@ */ private void buildPrefixAccess() { - prefix_tree = new HashMap(); + prefix_tree = new HashMap<String,CollationElement>(); for (int i = 0; i < ce_table.length; i++) { - CollationElement e = (CollationElement) ce_table[i]; + CollationElement e = ce_table[i]; prefix_tree.put(e.key, e); } @@ -941,7 +941,7 @@ public CollationKey getCollationKey(String source) { CollationElementIterator cei = getCollationElementIterator(source); - ArrayList vect = new ArrayList(); + ArrayList<Integer> vect = new ArrayList<Integer>(); int ord = cei.next(); cei.reset(); //set to start of string @@ -969,16 +969,16 @@ break; } - vect.add(new Integer(ord)); + vect.add(Integer.valueOf(ord)); ord = cei.next(); //increment to next key } - Object[] objarr = vect.toArray(); + Integer[] objarr = vect.toArray(new Integer[vect.size()]); byte[] key = new byte[objarr.length * 4]; for (int i = 0; i < objarr.length; i++) { - int j = ((Integer) objarr[i]).intValue(); + int j = objarr[i].intValue(); key [i * 4] = (byte) ((j & 0xFF000000) >> 24); key [i * 4 + 1] = (byte) ((j & 0x00FF0000) >> 16); key [i * 4 + 2] = (byte) ((j & 0x0000FF00) >> 8); Index: java/text/SimpleDateFormat.java =================================================================== RCS file: /sources/classpath/classpath/java/text/SimpleDateFormat.java,v retrieving revision 1.61 diff -u -u -r1.61 SimpleDateFormat.java --- java/text/SimpleDateFormat.java 31 Dec 2008 01:41:16 -0000 1.61 +++ java/text/SimpleDateFormat.java 31 Dec 2008 07:59:29 -0000 @@ -159,13 +159,13 @@ } /** - * A list of <code>CompiledField</code>s, + * A list of <code>CompiledField</code>s and {...@code String}s * representing the compiled version of the pattern. * * @see CompiledField * @serial Ignored. */ - private transient ArrayList tokens; + private transient ArrayList<Object> tokens; /** * The localised data used in formatting, @@ -274,7 +274,7 @@ set2DigitYearStart(defaultCenturyStart); // Set up items normally taken care of by the constructor. - tokens = new ArrayList(); + tokens = new ArrayList<Object>(); try { compileFormat(pattern); @@ -416,7 +416,7 @@ Locale locale = Locale.getDefault(); calendar = new GregorianCalendar(locale); computeCenturyStart(); - tokens = new ArrayList(); + tokens = new ArrayList<Object>(); formatData = new DateFormatSymbols(locale); pattern = (formatData.dateFormats[DEFAULT] + ' ' + formatData.timeFormats[DEFAULT]); @@ -454,7 +454,7 @@ super(); calendar = new GregorianCalendar(locale); computeCenturyStart(); - tokens = new ArrayList(); + tokens = new ArrayList<Object>(); formatData = new DateFormatSymbols(locale); compileFormat(pattern); this.pattern = pattern; @@ -479,7 +479,7 @@ super(); calendar = new GregorianCalendar(); computeCenturyStart (); - tokens = new ArrayList(); + tokens = new ArrayList<Object>(); if (formatData == null) throw new NullPointerException("formatData"); this.formatData = formatData; @@ -524,7 +524,7 @@ */ public void applyPattern(String pattern) { - tokens = new ArrayList(); + tokens.clear(); compileFormat(pattern); this.pattern = pattern; } @@ -698,11 +698,10 @@ private void formatWithAttribute(Date date, FormatBuffer buffer, FieldPosition pos) { String temp; - AttributedCharacterIterator.Attribute attribute; calendar.setTime(date); // go through vector, filling in fields where applicable, else toString - Iterator iter = tokens.iterator(); + Iterator<Object> iter = tokens.iterator(); while (iter.hasNext()) { Object o = iter.next(); @@ -911,7 +910,6 @@ char ch = pattern.charAt(fmt_index); if (ch == '\'') { - int index = pos.getIndex(); if (fmt_index < fmt_max - 1 && pattern.charAt(fmt_index + 1) == '\'') {