Commit 76167dc0 by Guilhem Lavaux Committed by Michael Koch

FormatCharacterIterator.java: Documented the class and

2003-11-27  Guilhem Lavaux <guilhem@kaffe.org>

	* java/text/FormatCharacterIterator.java: Documented the class and

2003-11-27  Guilhem Lavaux <guilhem@kaffe.org>

	* java/text/FormatCharacterIterator.java: Fixed some typos.

From-SVN: r73986
parent a01387dd
2003-11-27 Guilhem Lavaux <guilhem@kaffe.org> 2003-11-27 Guilhem Lavaux <guilhem@kaffe.org>
* java/text/FormatCharacterIterator.java: Documented the class and
2003-11-27 Guilhem Lavaux <guilhem@kaffe.org>
* java/text/FormatCharacterIterator.java: Fixed some typos.
2003-11-27 Guilhem Lavaux <guilhem@kaffe.org>
* java/text/NumberFormat.java: * java/text/NumberFormat.java:
(getIntegerInstance) Added the java version in the comments. (getIntegerInstance) Added the java version in the comments.
......
...@@ -43,6 +43,18 @@ import java.util.Map; ...@@ -43,6 +43,18 @@ import java.util.Map;
import java.util.HashMap; import java.util.HashMap;
import java.util.Vector; 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 <guilhem@kaffe.org>
* @date November 22, 2003
*/
class FormatCharacterIterator implements AttributedCharacterIterator class FormatCharacterIterator implements AttributedCharacterIterator
{ {
private String formattedString; private String formattedString;
...@@ -51,6 +63,11 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -51,6 +63,11 @@ class FormatCharacterIterator implements AttributedCharacterIterator
private int[] ranges; private int[] ranges;
private HashMap[] attributes; 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() FormatCharacterIterator()
{ {
formattedString = ""; formattedString = "";
...@@ -58,18 +75,33 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -58,18 +75,33 @@ class FormatCharacterIterator implements AttributedCharacterIterator
attributes = new HashMap[0]; attributes = new HashMap[0];
} }
FormatCharacterIterator(String s, int[] ranges, HashMap[] attributes) /**
* This constructor take a string <code>s</code>, a set of ranges
* and the corresponding attributes. This is used to build an iterator.
* The array <code>ranges</code> should be formatted as follow:
* each element of <code>ranges</code> specifies the index in the string
* until which the corresponding map of attributes at the same position
* is applied. For example, if you have:
* <pre>
* s = "hello";
* ranges = new int[] { 2, 6 };
* attributes = new HashMap[2];
* </pre>
* <code>"he"</code> will have the attributes <code>attributes[0]</code>,
* <code>"llo"</code> the <code>attributes[1]</code>.
*/
FormatCharacterIterator (String s, int[] ranges, HashMap[] attributes)
{ {
formattedString = s; formattedString = s;
this.ranges = ranges; this.ranges = ranges;
this.attributes = attributes; this.attributes = attributes;
} }
/* /*
* ----------------------------------- * The following methods are inherited from AttributedCharacterIterator,
* AttributedCharacterIterator methods * and thus are already documented.
* -----------------------------------
*/ */
public Set getAllAttributeKeys() public Set getAllAttributeKeys()
{ {
if (attributes != null && attributes[attributeIndex] != null) if (attributes != null && attributes[attributeIndex] != null)
...@@ -86,10 +118,10 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -86,10 +118,10 @@ class FormatCharacterIterator implements AttributedCharacterIterator
return new HashMap(); return new HashMap();
} }
public Object getAttribute(AttributedCharacterIterator.Attribute attrib) public Object getAttribute (AttributedCharacterIterator.Attribute attrib)
{ {
if (attributes != null && attributes[attributeIndex] != null) if (attributes != null && attributes[attributeIndex] != null)
return attributes[attributeIndex].get(attrib); return attributes[attributeIndex].get (attrib);
else else
return null; return null;
} }
...@@ -111,17 +143,17 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -111,17 +143,17 @@ class FormatCharacterIterator implements AttributedCharacterIterator
break; break;
newKeys = attributes[currentAttrIndex].keySet(); newKeys = attributes[currentAttrIndex].keySet();
} }
while (newKeys.containsAll(reqAttrs)); while (newKeys.containsAll (reqAttrs));
return ranges[currentAttrIndex-1]; return ranges[currentAttrIndex-1];
} }
public int getRunLimit(AttributedCharacterIterator.Attribute attribute) public int getRunLimit (AttributedCharacterIterator.Attribute attribute)
{ {
Set s = new HashSet(); Set s = new HashSet();
s.add(attribute); s.add (attribute);
return getRunLimit(s); return getRunLimit (s);
} }
public int getRunLimit() public int getRunLimit()
...@@ -139,7 +171,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -139,7 +171,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator
return getRunLimit (attributes[attributeIndex].keySet()); return getRunLimit (attributes[attributeIndex].keySet());
} }
public int getRunStart(Set reqAttrs) public int getRunStart (Set reqAttrs)
{ {
if (attributes == null) if (attributes == null)
return formattedString.length(); return formattedString.length();
...@@ -157,7 +189,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -157,7 +189,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator
break; break;
newKeys = attributes[currentAttrIndex].keySet(); newKeys = attributes[currentAttrIndex].keySet();
} }
while (newKeys.containsAll(reqAttrs)); while (newKeys.containsAll (reqAttrs));
return (currentAttrIndex > 0) ? ranges[currentAttrIndex-1] : 0; return (currentAttrIndex > 0) ? ranges[currentAttrIndex-1] : 0;
} }
...@@ -175,37 +207,37 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -175,37 +207,37 @@ class FormatCharacterIterator implements AttributedCharacterIterator
return 0; 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(); Set s = new HashSet();
s.add(attribute); s.add (attribute);
return getRunStart(s); return getRunStart (s);
} }
public Object clone() public Object clone()
{ {
return new FormatCharacterIterator(formattedString, ranges, attributes); return new FormatCharacterIterator (formattedString, ranges, attributes);
} }
/* /*
* --------------------------------- * The following methods are inherited from CharacterIterator and thus
* CharacterIterator methods * are already documented.
* ---------------------------------
*/ */
public char current() public char current()
{ {
return formattedString.charAt(charIndex); return formattedString.charAt (charIndex);
} }
public char first() public char first()
{ {
charIndex = 0; charIndex = 0;
attributeIndex = 0; attributeIndex = 0;
return formattedString.charAt(0); return formattedString.charAt (0);
} }
public int getBeginIndex() public int getBeginIndex()
...@@ -228,7 +260,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -228,7 +260,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator
charIndex = formattedString.length()-1; charIndex = formattedString.length()-1;
if (attributes != null) if (attributes != null)
attributeIndex = attributes.length-1; attributeIndex = attributes.length-1;
return formattedString.charAt(charIndex); return formattedString.charAt (charIndex);
} }
public char next() public char next()
...@@ -244,7 +276,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -244,7 +276,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator
if (charIndex >= ranges[attributeIndex]) if (charIndex >= ranges[attributeIndex])
attributeIndex++; attributeIndex++;
} }
return formattedString.charAt(charIndex); return formattedString.charAt (charIndex);
} }
public char previous() public char previous()
...@@ -261,13 +293,13 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -261,13 +293,13 @@ class FormatCharacterIterator implements AttributedCharacterIterator
if (charIndex < ranges[attributeIndex]) if (charIndex < ranges[attributeIndex])
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()) if (position < 0 || position > formattedString.length())
throw new IllegalArgumentException("position is out of range"); throw new IllegalArgumentException ("position is out of range");
charIndex = position; charIndex = position;
if (attributes != null) if (attributes != null)
...@@ -281,10 +313,19 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -281,10 +313,19 @@ class FormatCharacterIterator implements AttributedCharacterIterator
if (charIndex == formattedString.length()) if (charIndex == formattedString.length())
return DONE; return DONE;
else 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_ranges = new Vector();
Vector new_attributes = new Vector(); Vector new_attributes = new Vector();
...@@ -294,28 +335,28 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -294,28 +335,28 @@ class FormatCharacterIterator implements AttributedCharacterIterator
{ {
if (this.attributes[i] != null) if (this.attributes[i] != null)
{ {
new_attributes.add(this.attributes[i]); new_attributes.add (this.attributes[i]);
if (attributes[j] != null) if (attributes[j] != null)
this.attributes[i].putAll(attributes[j]); this.attributes[i].putAll (attributes[j]);
} }
else else
{ {
new_attributes.add(attributes[j]); new_attributes.add (attributes[j]);
} }
if (this.ranges[i] == ranges[j]) if (this.ranges[i] == ranges[j])
{ {
new_ranges.add(new Integer(ranges[j])); new_ranges.add (new Integer (ranges[j]));
i++; i++;
j++; j++;
} }
else if (this.ranges[i] < ranges[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++; i++;
} }
else else
{ {
new_ranges.add(new Integer(ranges[j])); new_ranges.add (new Integer (ranges[j]));
j++; j++;
} }
} }
...@@ -324,32 +365,39 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -324,32 +365,39 @@ class FormatCharacterIterator implements AttributedCharacterIterator
{ {
for (;i<this.ranges.length;i++) for (;i<this.ranges.length;i++)
{ {
new_attributes.add(this.attributes[i]); new_attributes.add (this.attributes[i]);
new_ranges.add(new Integer(this.ranges[i])); new_ranges.add (new Integer (this.ranges[i]));
} }
} }
if (j != ranges.length) if (j != ranges.length)
{ {
for (;j<ranges.length;j++) for (;j<ranges.length;j++)
{ {
new_attributes.add(attributes[j]); new_attributes.add (attributes[j]);
new_ranges.add(new Integer(ranges[j])); new_ranges.add (new Integer (ranges[j]));
} }
} }
this.attributes = new HashMap[new_attributes.size()]; this.attributes = new HashMap[new_attributes.size()];
this.ranges = new int[new_ranges.size()]; this.ranges = new int[new_ranges.size()];
System.arraycopy(new_attributes.toArray(), 0, this.attributes, System.arraycopy (new_attributes.toArray(), 0, this.attributes,
0, this.attributes.length); 0, this.attributes.length);
for (i=0;i<new_ranges.size();i++) for (i=0;i<new_ranges.size();i++)
{ {
this.ranges[i] = ((Integer)new_ranges.elementAt(i)).intValue(); this.ranges[i] = ((Integer)new_ranges.elementAt (i)).intValue();
} }
} }
protected void append(AttributedCharacterIterator iterator) /**
* This method appends to the internal attributed string the attributed
* string contained in the specified iterator.
*
* @param iterator the iterator which contains the attributed string to
* append to this iterator.
*/
protected void append (AttributedCharacterIterator iterator)
{ {
char c = iterator.first(); char c = iterator.first();
Vector more_ranges = new Vector(); Vector more_ranges = new Vector();
...@@ -360,7 +408,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -360,7 +408,7 @@ class FormatCharacterIterator implements AttributedCharacterIterator
formattedString = formattedString + String.valueOf(c); formattedString = formattedString + String.valueOf(c);
// TODO: Reduce the size of the output array. // TODO: Reduce the size of the output array.
more_attributes.add (iterator.getAttributes()); more_attributes.add (iterator.getAttributes());
more_ranges.add(new Integer(formattedString.length())); more_ranges.add (new Integer (formattedString.length()));
// END TOOD // END TOOD
c = iterator.next(); c = iterator.next();
} }
...@@ -370,11 +418,11 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -370,11 +418,11 @@ class FormatCharacterIterator implements AttributedCharacterIterator
+ more_attributes.size()]; + more_attributes.size()];
int[] new_ranges = new int[ranges.length + more_ranges.size()]; int[] new_ranges = new int[ranges.length + more_ranges.size()];
System.arraycopy(attributes, 0, new_attributes, 0, attributes.length); System.arraycopy (attributes, 0, new_attributes, 0, attributes.length);
System.arraycopy(more_attributes.toArray(), 0, new_attributes, System.arraycopy (more_attributes.toArray(), 0, new_attributes,
attributes.length, more_attributes.size()); attributes.length, more_attributes.size());
System.arraycopy(ranges, 0, new_ranges, 0, ranges.length); System.arraycopy (ranges, 0, new_ranges, 0, ranges.length);
Object[] new_ranges_array = more_ranges.toArray(); Object[] new_ranges_array = more_ranges.toArray();
for (int i=0;i<more_ranges.size();i++) for (int i=0;i<more_ranges.size();i++)
new_ranges[i+ranges.length] = ((Integer)new_ranges_array[i]).intValue(); new_ranges[i+ranges.length] = ((Integer)new_ranges_array[i]).intValue();
...@@ -383,14 +431,23 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -383,14 +431,23 @@ class FormatCharacterIterator implements AttributedCharacterIterator
ranges = new_ranges; ranges = new_ranges;
} }
protected void append(String text, HashMap local_attributes) /**
* This method appends an attributed string which attributes are specified
* directly in the calling parameters.
*
* @param text The string to append.
* @param local_attributes The attributes to put on this string in the
* iterator. If it is <code>null</code> the string will simply have no
* attributes.
*/
protected void append (String text, HashMap local_attributes)
{ {
int[] new_ranges = new int[ranges.length+1]; int[] new_ranges = new int[ranges.length+1];
HashMap[] new_attributes = new HashMap[attributes.length+1]; HashMap[] new_attributes = new HashMap[attributes.length+1];
formattedString += text; formattedString += text;
System.arraycopy(attributes, 0, new_attributes, 0, attributes.length); System.arraycopy (attributes, 0, new_attributes, 0, attributes.length);
System.arraycopy(ranges, 0, new_ranges, 0, ranges.length); System.arraycopy (ranges, 0, new_ranges, 0, ranges.length);
new_ranges[ranges.length] = formattedString.length(); new_ranges[ranges.length] = formattedString.length();
new_attributes[attributes.length] = local_attributes; new_attributes[attributes.length] = local_attributes;
...@@ -398,8 +455,15 @@ class FormatCharacterIterator implements AttributedCharacterIterator ...@@ -398,8 +455,15 @@ class FormatCharacterIterator implements AttributedCharacterIterator
attributes = new_attributes; 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 <code>null</code>.
*
* @param text The string to append to the iterator.
*/
protected void append (String text)
{ {
append(text, null); append (text, null);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment