From: Guilhem Lavaux Date: Thu, 27 Nov 2003 09:43:02 +0000 (+0000) Subject: FormatCharacterIterator.java: Documented the class and X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=76167dc0ff996db06526c1a0f445662a195876db;p=gcc.git FormatCharacterIterator.java: Documented the class and 2003-11-27 Guilhem Lavaux * java/text/FormatCharacterIterator.java: Documented the class and 2003-11-27 Guilhem Lavaux * java/text/FormatCharacterIterator.java: Fixed some typos. From-SVN: r73986 --- diff --git a/libjava/ChangeLog b/libjava/ChangeLog index c3a798b47af..544f2c48ef4 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,11 @@ +2003-11-27 Guilhem Lavaux + + * java/text/FormatCharacterIterator.java: Documented the class and + +2003-11-27 Guilhem Lavaux + + * java/text/FormatCharacterIterator.java: Fixed some typos. + 2003-11-27 Guilhem Lavaux * java/text/NumberFormat.java: diff --git a/libjava/java/text/FormatCharacterIterator.java b/libjava/java/text/FormatCharacterIterator.java index 8693707715c..a31154c531a 100644 --- a/libjava/java/text/FormatCharacterIterator.java +++ b/libjava/java/text/FormatCharacterIterator.java @@ -43,6 +43,18 @@ import java.util.Map; import java.util.HashMap; import java.util.Vector; + +/** + * This class should not be put public and it is only intended to the + * classes of the java.text package. Its aim is to build a segmented + * character iterator by appending strings and adding attributes to + * portions of strings. The code intends to do some optimization + * concerning memory consumption and attribute access but at the + * end it is only an AttributedCharacterIterator. + * + * @author Guilhem Lavaux + * @date November 22, 2003 + */ class FormatCharacterIterator implements AttributedCharacterIterator { private String formattedString; @@ -51,6 +63,11 @@ class FormatCharacterIterator implements AttributedCharacterIterator private int[] ranges; private HashMap[] attributes; + /** + * This constructor builds an empty iterated strings. The attributes + * are empty and so is the string. However you may append strings + * and attributes to this iterator. + */ FormatCharacterIterator() { formattedString = ""; @@ -58,18 +75,33 @@ class FormatCharacterIterator implements AttributedCharacterIterator attributes = new HashMap[0]; } - FormatCharacterIterator(String s, int[] ranges, HashMap[] attributes) + /** + * This constructor take a string s, a set of ranges + * and the corresponding attributes. This is used to build an iterator. + * The array ranges should be formatted as follow: + * each element of ranges specifies the index in the string + * until which the corresponding map of attributes at the same position + * is applied. For example, if you have: + *
+   *   s = "hello";
+   *   ranges = new int[] { 2, 6 };
+   *   attributes = new HashMap[2];
+   * 
+ * "he" will have the attributes attributes[0], + * "llo" the attributes[1]. + */ + FormatCharacterIterator (String s, int[] ranges, HashMap[] attributes) { formattedString = s; this.ranges = ranges; this.attributes = attributes; } - /* - * ----------------------------------- - * AttributedCharacterIterator methods - * ----------------------------------- + /* + * The following methods are inherited from AttributedCharacterIterator, + * and thus are already documented. */ + public Set getAllAttributeKeys() { if (attributes != null && attributes[attributeIndex] != null) @@ -86,10 +118,10 @@ class FormatCharacterIterator implements AttributedCharacterIterator return new HashMap(); } - public Object getAttribute(AttributedCharacterIterator.Attribute attrib) + public Object getAttribute (AttributedCharacterIterator.Attribute attrib) { if (attributes != null && attributes[attributeIndex] != null) - return attributes[attributeIndex].get(attrib); + return attributes[attributeIndex].get (attrib); else return null; } @@ -111,17 +143,17 @@ class FormatCharacterIterator implements AttributedCharacterIterator break; newKeys = attributes[currentAttrIndex].keySet(); } - while (newKeys.containsAll(reqAttrs)); + while (newKeys.containsAll (reqAttrs)); return ranges[currentAttrIndex-1]; } - public int getRunLimit(AttributedCharacterIterator.Attribute attribute) + public int getRunLimit (AttributedCharacterIterator.Attribute attribute) { Set s = new HashSet(); - s.add(attribute); - return getRunLimit(s); + s.add (attribute); + return getRunLimit (s); } public int getRunLimit() @@ -139,7 +171,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator return getRunLimit (attributes[attributeIndex].keySet()); } - public int getRunStart(Set reqAttrs) + public int getRunStart (Set reqAttrs) { if (attributes == null) return formattedString.length(); @@ -157,7 +189,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator break; newKeys = attributes[currentAttrIndex].keySet(); } - while (newKeys.containsAll(reqAttrs)); + while (newKeys.containsAll (reqAttrs)); return (currentAttrIndex > 0) ? ranges[currentAttrIndex-1] : 0; } @@ -175,37 +207,37 @@ class FormatCharacterIterator implements AttributedCharacterIterator return 0; } - return getRunStart(attributes[attributeIndex].keySet()); + return getRunStart (attributes[attributeIndex].keySet()); } - public int getRunStart(AttributedCharacterIterator.Attribute attribute) + public int getRunStart (AttributedCharacterIterator.Attribute attribute) { Set s = new HashSet(); - s.add(attribute); - return getRunStart(s); + s.add (attribute); + return getRunStart (s); } public Object clone() { - return new FormatCharacterIterator(formattedString, ranges, attributes); + return new FormatCharacterIterator (formattedString, ranges, attributes); } /* - * --------------------------------- - * CharacterIterator methods - * --------------------------------- + * The following methods are inherited from CharacterIterator and thus + * are already documented. */ + public char current() { - return formattedString.charAt(charIndex); + return formattedString.charAt (charIndex); } public char first() { charIndex = 0; attributeIndex = 0; - return formattedString.charAt(0); + return formattedString.charAt (0); } public int getBeginIndex() @@ -228,7 +260,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator charIndex = formattedString.length()-1; if (attributes != null) attributeIndex = attributes.length-1; - return formattedString.charAt(charIndex); + return formattedString.charAt (charIndex); } public char next() @@ -244,7 +276,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator if (charIndex >= ranges[attributeIndex]) attributeIndex++; } - return formattedString.charAt(charIndex); + return formattedString.charAt (charIndex); } public char previous() @@ -261,13 +293,13 @@ class FormatCharacterIterator implements AttributedCharacterIterator if (charIndex < ranges[attributeIndex]) attributeIndex--; } - return formattedString.charAt(charIndex); + return formattedString.charAt (charIndex); } - public char setIndex(int position) + public char setIndex (int position) { if (position < 0 || position > formattedString.length()) - throw new IllegalArgumentException("position is out of range"); + throw new IllegalArgumentException ("position is out of range"); charIndex = position; if (attributes != null) @@ -281,10 +313,19 @@ class FormatCharacterIterator implements AttributedCharacterIterator if (charIndex == formattedString.length()) return DONE; else - return formattedString.charAt(charIndex); + return formattedString.charAt (charIndex); } - protected void mergeAttributes(HashMap[] attributes, int[] ranges) + /** + * This method merge the specified attributes and ranges with the + * internal tables. This method is in charge of the optimization + * of tables. Two following sets of attributes are never the same. + * + * @see #FormatCharacterIterator() + * + * @param attributes the new array attributes to apply to the string. + */ + protected void mergeAttributes (HashMap[] attributes, int[] ranges) { Vector new_ranges = new Vector(); Vector new_attributes = new Vector(); @@ -294,28 +335,28 @@ class FormatCharacterIterator implements AttributedCharacterIterator { if (this.attributes[i] != null) { - new_attributes.add(this.attributes[i]); + new_attributes.add (this.attributes[i]); if (attributes[j] != null) - this.attributes[i].putAll(attributes[j]); + this.attributes[i].putAll (attributes[j]); } else { - new_attributes.add(attributes[j]); + new_attributes.add (attributes[j]); } if (this.ranges[i] == ranges[j]) { - new_ranges.add(new Integer(ranges[j])); + new_ranges.add (new Integer (ranges[j])); i++; j++; } else if (this.ranges[i] < ranges[j]) { - new_ranges.add(new Integer(this.ranges[i])); + new_ranges.add (new Integer (this.ranges[i])); i++; } else { - new_ranges.add(new Integer(ranges[j])); + new_ranges.add (new Integer (ranges[j])); j++; } } @@ -324,32 +365,39 @@ class FormatCharacterIterator implements AttributedCharacterIterator { for (;inull the string will simply have no + * attributes. + */ + protected void append (String text, HashMap local_attributes) { int[] new_ranges = new int[ranges.length+1]; HashMap[] new_attributes = new HashMap[attributes.length+1]; formattedString += text; - System.arraycopy(attributes, 0, new_attributes, 0, attributes.length); - System.arraycopy(ranges, 0, new_ranges, 0, ranges.length); + System.arraycopy (attributes, 0, new_attributes, 0, attributes.length); + System.arraycopy (ranges, 0, new_ranges, 0, ranges.length); new_ranges[ranges.length] = formattedString.length(); new_attributes[attributes.length] = local_attributes; @@ -398,8 +455,15 @@ class FormatCharacterIterator implements AttributedCharacterIterator attributes = new_attributes; } - protected void append(String text) + /** + * This method appends a string without attributes. It is completely + * equivalent to call {@link #append(String,HashMap)} with local_attributes + * equal to null. + * + * @param text The string to append to the iterator. + */ + protected void append (String text) { - append(text, null); + append (text, null); } }