classpath-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cp-patches] FYI: AttributedString/AttributedStringIterator


From: David Gilbert
Subject: [cp-patches] FYI: AttributedString/AttributedStringIterator
Date: Mon, 24 Oct 2005 11:08:36 +0100
User-agent: Mozilla Thunderbird 1.0.6 (X11/20050728)

This patch (committed) fixes the failing Mauve test cases for the AttributedString and AttributedStringIterator classes:

2005-10-24  David Gilbert  <address@hidden>

        * java/text/AttributedString.java
        (AttributedString(AttributedCharacterIterator, int, int,
        AttributedCharacterIterator.Attribute[])): renamed arguments, and
        updated IllegalArgumentException check,
        (addAttribute(AttrubytedCharacterIterator.Attribute, Object, int,
        int)): likewise,
        (addAttributes(Map, int, int)): changed IllegalArgumentException to
        NullPointerException, and modified check for illegal range,
        * java/text/AttributedStringIterator.java
        (getRunLimit(Set)): reimplemented,
        (getRunStart): added API docs,
        (getRunStart(AttributedCharacterIterator.Attribute)): handle null
        argument as a special case,
        (getRunStart(Set)): reimplemented,
        (getAttribute(AttributedCharacterIterator.Attribute, int)): new
        private method,
        (getAttribute(AttributedCharacterIterator.Attribute)): reimplemented
        using new private method.
        * java/text/CharacterIterator.java: minor API doc fix.

Regards,

Dave
Index: java/text/AttributedString.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/text/AttributedString.java,v
retrieving revision 1.14
diff -u -r1.14 AttributedString.java
--- java/text/AttributedString.java     27 Jul 2005 16:05:24 -0000      1.14
+++ java/text/AttributedString.java     24 Oct 2005 09:47:08 -0000
@@ -49,7 +49,7 @@
 /**
  * This class models a <code>String</code> with attributes over various
  * subranges of the string.  It allows applications to access this 
- * information via the <code>AttributedCharcterIterator</code> interface.
+ * information via the <code>AttributedCharacterIterator</code> interface.
  *
  * @author Aaron M. Renn (address@hidden)
  */
@@ -166,16 +166,16 @@
    *
    * @param aci The <code>AttributedCharacterIterator</code> containing the 
    *            text and attribute information.
-   * @param begin_index The beginning index of the text subrange.
-   * @param end_index The ending index of the text subrange.
+   * @param begin The beginning index of the text subrange.
+   * @param end The ending index of the text subrange.
    * @param attributes A list of attributes to include from the iterator, or 
    *                   <code>null</code> to include all attributes.
    */
-  public AttributedString(AttributedCharacterIterator aci, int begin_index, 
-          int end_index, AttributedCharacterIterator.Attribute[] attributes)
+  public AttributedString(AttributedCharacterIterator aci, int begin, int end, 
+                          AttributedCharacterIterator.Attribute[] attributes)
   {
     // Validate some arguments
-    if ((begin_index < 0) || (end_index < begin_index))
+    if ((begin < 0) || (end < begin) || end > aci.getEndIndex())
       throw new IllegalArgumentException("Bad index values");
 
     StringBuffer sb = new StringBuffer("");
@@ -186,7 +186,7 @@
       all_attribs.retainAll(Arrays.asList(attributes));
 
     // Loop through and extract the attributes
-    char c = aci.setIndex(begin_index);
+    char c = aci.setIndex(begin);
 
     ArrayList accum = new ArrayList();
     do
@@ -209,28 +209,28 @@
             int rl = aci.getRunLimit(attrib);
             if (rl == -1)
               continue;
-            if (rl > end_index)
-              rl = end_index;
-            rl -= begin_index;
+            if (rl > end)
+              rl = end;
+            rl -= begin;
 
             // Check to see if we already processed this one
             int rs = aci.getRunStart(attrib);
-            if ((rs < aci.getIndex()) && (aci.getIndex() != begin_index))
+            if ((rs < aci.getIndex()) && (aci.getIndex() != begin))
               continue;
 
             // If the attribute run starts before the beginning index, we
             // need to junk it if it is an Annotation.
             Object attrib_obj = aci.getAttribute(attrib);
-            if (rs < begin_index)
+            if (rs < begin)
               {
                 if (attrib_obj instanceof Annotation)
                    continue;
 
-                rs = begin_index;
+                rs = begin;
               }
             else
               {
-                rs -= begin_index;
+                rs -= begin;
               }
 
             // Create a map object.  Yes this will only contain one attribute
@@ -269,22 +269,23 @@
    *
    * @param attrib The attribute to add.
    * @param value The value of the attribute, which may be <code>null</code>.
-   * @param begin_index The beginning index of the subrange.
-   * @param end_index The ending index of the subrange.
+   * @param begin The beginning index of the subrange.
+   * @param end The ending index of the subrange.
    *
    * @exception IllegalArgumentException If attribute is <code>null</code> or 
    *            the subrange is not valid.
    */
   public void addAttribute(AttributedCharacterIterator.Attribute attrib, 
-          Object value, int begin_index, int end_index)
+          Object value, int begin, int end)
   {
     if (attrib == null)
       throw new IllegalArgumentException("null attribute");
-
+    if (end <= begin)
+      throw new IllegalArgumentException("Requires end > begin");
     HashMap hm = new HashMap();
     hm.put(attrib, value);
 
-    addAttributes(hm, begin_index, end_index);
+    addAttributes(hm, begin, end);
   }
 
   /**
@@ -295,16 +296,17 @@
    * @param begin_index The beginning index.
    * @param end_index The ending index
    *
-   * @throws IllegalArgumentException If the list is <code>null</code> or the 
-   * subrange is not valid.
+   * @throws NullPointerException if <code>attributes</code> is 
+   *         <code>null</code>.
+   * @throws IllegalArgumentException if the subrange is not valid.
    */
   public void addAttributes(Map attributes, int begin_index, int end_index)
   {
     if (attributes == null)
-      throw new IllegalArgumentException("null attribute");
+      throw new NullPointerException("null attribute");
 
     if ((begin_index < 0) || (end_index > sci.getEndIndex()) ||
-        (end_index < begin_index))
+        (end_index <= begin_index))
       throw new IllegalArgumentException("bad range");
 
     AttributeRange[] new_list = new AttributeRange[attribs.length + 1];
Index: java/text/AttributedStringIterator.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/text/AttributedStringIterator.java,v
retrieving revision 1.10
diff -u -r1.10 AttributedStringIterator.java
--- java/text/AttributedStringIterator.java     28 Jul 2005 09:27:39 -0000      
1.10
+++ java/text/AttributedStringIterator.java     24 Oct 2005 09:47:08 -0000
@@ -188,30 +188,42 @@
     return(getRunLimit(s));
   }
 
-  public synchronized int getRunLimit(Set attribute_set)
+  public synchronized int getRunLimit(Set attributeSet)
   {
-    boolean hit = false;
-    int runLimit = ci.getEndIndex ();
-    int pos = ci.getIndex ();
-
-    for (int i = 0; i < attribs.length; ++i)
+    if (attributeSet == null)
+      return ci.getEndIndex();
+    
+    int current = ci.getIndex();
+    int end = ci.getEndIndex();
+    int limit = current;
+    if (current == end) 
+      return end;
+    Map runValues = getAttributes();
+    while (limit < end) 
+    {
+      Iterator iterator = attributeSet.iterator();
+      while (iterator.hasNext()) 
       {
-        if (pos >= attribs[i].begin_index &&
-            pos < attribs[i].end_index)
+        Attribute attributeKey = (Attribute) iterator.next();
+        Object v1 = runValues.get(attributeKey);
+        Object v2 = getAttribute(attributeKey, limit + 1);
+        boolean changed = false;
+        // check for equal or both null, if NO return start
+        if (v1 != null) 
+          {
+            changed = !v1.equals(v2);
+          }
+        else 
           {
-            Iterator iter = attribute_set.iterator();
-            while(iter.hasNext()) 
-              if (attribs[i].attribs.containsKey(iter.next()))
-                {
-                  hit = true;
-                  runLimit = Math.min(runLimit, attribs[i].end_index);
-                }
+            changed = (v2 != null);  
           }
+        if (changed)
+          return limit + 1;
       }
-    if (hit)
-      return runLimit;
-    else
-      return ci.getEndIndex();
+      // no differences, so increment limit and next and loop again
+      limit++;
+    }
+    return end;
   }
 
   /*************************************************************************/
@@ -221,69 +233,126 @@
    * attribute combinations.
    */
 
+  /**
+   * Returns the index of the first character in the run containing the current
+   * character and defined by all the attributes defined for that character
+   * position.
+   * 
+   * @return The run start index.
+   */
   public int getRunStart()
   {
     return(getRunStart(getAttributes().keySet()));
   }
 
+  /**
+   * Returns the index of the first character in the run, defined by the 
+   * specified attribute, that contains the current character.
+   * 
+   * @param attrib  the attribute (<code>null</code> permitted).
+   * 
+   * return The index of the first character in the run.
+   */
   public int getRunStart(AttributedCharacterIterator.Attribute attrib)
   {
+    if (attrib == null)
+      return ci.getBeginIndex();
     HashSet s = new HashSet();
     s.add(attrib);
-
     return(getRunStart(s));
   }
 
-  public int getRunStart(Set attribute_set)
+  /**
+   * Returns the index of the first character in the run, defined by the 
+   * specified attribute set, that contains the current character.
+   * 
+   * @param attributeSet  the attribute set (<code>null</code> permitted).
+   * 
+   * return The index of the first character in the run.
+   */
+  public int getRunStart(Set attributeSet)
   {
-    boolean hit = false;
-    int runBegin = 0;
-    int pos = ci.getIndex();
-
-    for (int i = 0; i < attribs.length; ++i)
+    if (attributeSet == null)
+      return ci.getBeginIndex();
+    
+    int current = ci.getIndex();
+    int begin = ci.getBeginIndex();
+    int start = current;
+    if (start == begin) 
+      return begin;
+    Map runValues = getAttributes();
+    int prev = start - 1;
+    while (start > begin) 
+    {
+      Iterator iterator = attributeSet.iterator();
+      while (iterator.hasNext()) 
       {
-        if (pos >= attribs[i].begin_index &&
-            pos <= attribs[i].end_index)
+        Attribute attributeKey = (Attribute) iterator.next();
+        Object v1 = runValues.get(attributeKey);
+        Object v2 = getAttribute(attributeKey, prev);
+        boolean changed = false;
+        // check for equal or both null, if NO return start
+        if (v1 != null) 
+          {
+            changed = !v1.equals(v2);
+          }
+        else 
           {
-            Iterator iter = attribute_set.iterator();
-            while(iter.hasNext()) 
-              if (attribs[i].attribs.containsKey(iter.next()))
-                {
-                  hit = true;
-                  runBegin = Math.max(runBegin, attribs[i].begin_index);
-                }
+            changed = (v2 != null);  
           }
+        if (changed)
+          return start;
       }
-    if (hit)
-      return runBegin;
-    else
-      return -1;
+      // no differences, so decrement start and prev and loop again
+      start--;
+      prev--;
+    }
+    return start;
   }
 
   /*************************************************************************/
 
-  public Object getAttribute(AttributedCharacterIterator.Attribute attrib)
+  /**
+   * Returns the value for an attribute at the specified position.  If the
+   * attribute key (<code>key</code>) is <code>null</code>, the method returns
+   * <code>null</code>.
+   * 
+   * @param key  the key (<code>null</code> permitted).
+   * @param pos  the character position.
+   * 
+   * @return The attribute value (possibly <code>null</code>).
+   */
+  private Object getAttribute(AttributedCharacterIterator.Attribute key, 
+          int pos)
   {
     if (attribs == null)
-      return(null);
-
-    for (int i = 0; i < attribs.length; i++)
+      return null;
+    for (int i = attribs.length - 1; i >= 0; i--)
       {
-        Set key_set = attribs[i].attribs.keySet();
-        Iterator iter = key_set.iterator();
-        while (iter.hasNext())
+        if (pos >= attribs[i].begin_index && pos < attribs[i].end_index)
           {
-            Object obj = iter.next();
-
-            // Check for attribute match and range match
-            if (obj.equals(attrib))
-              if ((ci.getIndex() >= attribs[i].begin_index) &&
-                  (ci.getIndex() < attribs[i].end_index))
-                return(attribs[i].attribs.get(obj));
+            Set keys = attribs[i].attribs.keySet();
+            if (keys.contains(key)) 
+              {
+                return attribs[i].attribs.get(key);
+              }
           }
       }
-
-    return(null);
+    return null;   
+  }
+  
+  /**
+   * Returns the value for an attribute at the current position.  If the
+   * attribute key (<code>key</code>) is <code>null</code>, the method returns
+   * <code>null</code>.
+   * 
+   * @param key  the key (<code>null</code> permitted).
+   * 
+   * @return The attribute value (possibly <code>null</code>).
+   */
+  public Object getAttribute(AttributedCharacterIterator.Attribute key)
+  {
+    return getAttribute(key, ci.getIndex());
   }
 
   /*************************************************************************/
Index: java/text/CharacterIterator.java
===================================================================
RCS file: /cvsroot/classpath/classpath/java/text/CharacterIterator.java,v
retrieving revision 1.8
diff -u -r1.8 CharacterIterator.java
--- java/text/CharacterIterator.java    20 Jul 2005 08:10:07 -0000      1.8
+++ java/text/CharacterIterator.java    24 Oct 2005 09:47:09 -0000
@@ -68,7 +68,7 @@
    * <code>getEndIndex() - 1</code>, it will not be incremented.
    *
    * @return The character at the position of the incremented index value,
-   * or <code>DONE</code> if the index has reached getEndIndex() - 1
+   * or address@hidden #DONE} if the index has reached getEndIndex() - 1
    */
   char next();
 

reply via email to

[Prev in Thread] Current Thread [Next in Thread]