From 72722486ae49ce3e6c4eda755e0ad7066e16db93 Mon Sep 17 00:00:00 2001 From: Andrew John Hughes Date: Wed, 31 Dec 2008 08:27:23 +0000 Subject: Cleanup of java.text classes. 2008-12-31 Andrew John Hughes * 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. --- ChangeLog | 22 +++++++++ java/text/MessageFormat.java | 39 ++++++++------- java/text/NumberFormat.java | 1 + java/text/RuleBasedCollator.java | 104 +++++++++++++++++++-------------------- java/text/SimpleDateFormat.java | 18 +++---- 5 files changed, 104 insertions(+), 80 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8f5787acc..506b4a8be 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,25 @@ +2008-12-31 Andrew John Hughes + + * 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. + 2008-12-30 Andrew John Hughes * java/text/SimpleDateFormat.java: diff --git a/java/text/MessageFormat.java b/java/text/MessageFormat.java index c5579bff1..9019d1bb6 100644 --- a/java/text/MessageFormat.java +++ b/java/text/MessageFormat.java @@ -38,13 +38,17 @@ exception statement from your version. */ 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 @@ public class MessageFormat extends Format // 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 class MessageFormat extends Format public static final MessageFormat.Field ARGUMENT = new MessageFormat.Field("argument"); // For deserialization + @SuppressWarnings("unused") private Field() { super(""); @@ -194,7 +199,7 @@ public class MessageFormat extends Format // 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 @@ public class MessageFormat extends Format // 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 @@ public class MessageFormat extends Format // 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 elts, Locale locale) { MessageFormatElement mfe = new MessageFormatElement (); - elts.addElement(mfe); + elts.add(mfe); int max = pat.length(); @@ -342,17 +347,16 @@ public class MessageFormat extends Format { pattern = newPattern; - StringBuilder tempBuffer = new StringBuilder (); + CPStringBuilder tempBuffer = new CPStringBuilder (); int index = scanString (newPattern, 0, tempBuffer); leader = tempBuffer.toString(); - Vector elts = new Vector (); + List elts = new ArrayList(); 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 @@ public class MessageFormat extends Format if (output_iterator != null) { - HashMap hash_argument = new HashMap(); + HashMap hash_argument = + new HashMap(); int position = output_iterator.getEndIndex(); hash_argument.put (MessageFormat.Field.ARGUMENT, @@ -613,7 +618,7 @@ public class MessageFormat extends Format } index += leader.length(); - Vector results = new Vector (elements.length, 1); + ArrayList results = new ArrayList(elements.length); // Now check each format. for (int i = 0; i < elements.length; ++i) { @@ -681,15 +686,13 @@ public class MessageFormat extends Format } 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 diff --git a/java/text/NumberFormat.java b/java/text/NumberFormat.java index 0a436d004..294f8917c 100644 --- a/java/text/NumberFormat.java +++ b/java/text/NumberFormat.java @@ -177,6 +177,7 @@ public abstract class NumberFormat extends Format implements Cloneable * This constructor is only used by the deserializer. Without it, * it would fail to construct a valid object. */ + @SuppressWarnings("unused") private Field() { super(""); diff --git a/java/text/RuleBasedCollator.java b/java/text/RuleBasedCollator.java index fdd1446cc..b761039ad 100644 --- a/java/text/RuleBasedCollator.java +++ b/java/text/RuleBasedCollator.java @@ -151,13 +151,13 @@ public class RuleBasedCollator extends Collator */ 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 @@ public class RuleBasedCollator extends Collator * {@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 @@ public class RuleBasedCollator extends Collator 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 prefix_tree; /** * This is the value of the last sequence entered into @@ -306,7 +316,8 @@ public class RuleBasedCollator extends Collator * @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 main, + ArrayList patch) throws ParseException { int insertion_point = -1; @@ -324,8 +335,8 @@ public class RuleBasedCollator extends Collator 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 @@ public class RuleBasedCollator extends Collator // 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 @@ public class RuleBasedCollator extends Collator * 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 @@ public class RuleBasedCollator extends Collator * @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 v, int base_offset, String rules) throws ParseException { @@ -508,7 +517,7 @@ main_parse_loop: * indicated by the text element. */ String subrules = rules.substring(i); - ArrayList sorted_rules = new ArrayList(); + ArrayList sorted_rules = new ArrayList(); int idx; // Parse the subrules but do not iterate through all @@ -533,16 +542,12 @@ main_parse_loop: 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 @@ main_parse_loop: if (operator >= 0) { - CollationSorter sorter = new CollationSorter(); int pos = rules.length() + base_offset; if ((sb.length() != 0 && nextIsModifier) @@ -561,11 +565,8 @@ main_parse_loop: 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 @@ main_parse_loop: * @throws ParseException if something turned wrong during the parsing. To get details * decode the message. */ - private ArrayList parseString(String rules) + private ArrayList parseString(String rules) throws ParseException { - ArrayList v = new ArrayList(); + ArrayList v = new ArrayList(); // result of the first subParseString is not absolute (may be -1 or a // positive integer). But we do not care. @@ -612,7 +613,7 @@ main_parse_loop: * @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 parsedElements) throws ParseException { int primary_seq = 0; @@ -624,14 +625,13 @@ main_parse_loop: final boolean DECREASING = false; final boolean INCREASING = true; boolean secondaryType = INCREASING; - ArrayList v = new ArrayList(); + ArrayList v = new ArrayList(); // 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 @@ element_loop: 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 @@ element_loop: */ private void buildPrefixAccess() { - prefix_tree = new HashMap(); + prefix_tree = new HashMap(); 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 @@ element_loop: public CollationKey getCollationKey(String source) { CollationElementIterator cei = getCollationElementIterator(source); - ArrayList vect = new ArrayList(); + ArrayList vect = new ArrayList(); int ord = cei.next(); cei.reset(); //set to start of string @@ -969,16 +969,16 @@ element_loop: 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); diff --git a/java/text/SimpleDateFormat.java b/java/text/SimpleDateFormat.java index 7d57f67a0..cea599b82 100644 --- a/java/text/SimpleDateFormat.java +++ b/java/text/SimpleDateFormat.java @@ -159,13 +159,13 @@ public class SimpleDateFormat extends DateFormat } /** - * A list of CompiledFields, + * A list of CompiledFields and {@code String}s * representing the compiled version of the pattern. * * @see CompiledField * @serial Ignored. */ - private transient ArrayList tokens; + private transient ArrayList tokens; /** * The localised data used in formatting, @@ -274,7 +274,7 @@ public class SimpleDateFormat extends DateFormat set2DigitYearStart(defaultCenturyStart); // Set up items normally taken care of by the constructor. - tokens = new ArrayList(); + tokens = new ArrayList(); try { compileFormat(pattern); @@ -416,7 +416,7 @@ public class SimpleDateFormat extends DateFormat Locale locale = Locale.getDefault(); calendar = new GregorianCalendar(locale); computeCenturyStart(); - tokens = new ArrayList(); + tokens = new ArrayList(); formatData = new DateFormatSymbols(locale); pattern = (formatData.dateFormats[DEFAULT] + ' ' + formatData.timeFormats[DEFAULT]); @@ -454,7 +454,7 @@ public class SimpleDateFormat extends DateFormat super(); calendar = new GregorianCalendar(locale); computeCenturyStart(); - tokens = new ArrayList(); + tokens = new ArrayList(); formatData = new DateFormatSymbols(locale); compileFormat(pattern); this.pattern = pattern; @@ -479,7 +479,7 @@ public class SimpleDateFormat extends DateFormat super(); calendar = new GregorianCalendar(); computeCenturyStart (); - tokens = new ArrayList(); + tokens = new ArrayList(); if (formatData == null) throw new NullPointerException("formatData"); this.formatData = formatData; @@ -524,7 +524,7 @@ public class SimpleDateFormat extends DateFormat */ public void applyPattern(String pattern) { - tokens = new ArrayList(); + tokens.clear(); compileFormat(pattern); this.pattern = pattern; } @@ -698,11 +698,10 @@ public class SimpleDateFormat extends DateFormat 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 iter = tokens.iterator(); while (iter.hasNext()) { Object o = iter.next(); @@ -911,7 +910,6 @@ public class SimpleDateFormat extends DateFormat char ch = pattern.charAt(fmt_index); if (ch == '\'') { - int index = pos.getIndex(); if (fmt_index < fmt_max - 1 && pattern.charAt(fmt_index + 1) == '\'') { -- cgit v1.2.1