KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > util > AttributedStringUtilities


1 /* ========================================================================
2  * JCommon : a free general purpose class library for the Java(tm) platform
3  * ========================================================================
4  *
5  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
6  *
7  * Project Info: http://www.jfree.org/jcommon/index.html
8  *
9  * This library is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17  * License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22  * USA.
23  *
24  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25  * in the United States and other countries.]
26  *
27  * ------------------------------
28  * AttributedStringUtilities.java
29  * ------------------------------
30  * (C)opyright 2005, by Object Refinery Limited and Contributors.
31  *
32  * Original Author: David Gilbert (for Object Refinery Limited);
33  * Contributor(s): -;
34  *
35  * $Id: AttributedStringUtilities.java,v 1.2 2005/10/18 13:24:19 mungady Exp $
36  *
37  * Changes
38  * -------
39  * 29-Jul-2005 : Version 1(DG);
40  *
41  */

42
43 package org.jfree.util;
44
45 import java.text.AttributedCharacterIterator JavaDoc;
46 import java.text.AttributedString JavaDoc;
47 import java.text.CharacterIterator JavaDoc;
48 import java.util.Map JavaDoc;
49
50 /**
51  * Some utility methods for working with <code>AttributedString</code> objects.
52  *
53  * @author David Gilbert
54  */

55 public class AttributedStringUtilities {
56
57     /**
58      * Private constructor prevents object creation.
59      */

60     private AttributedStringUtilities() {
61     }
62
63     /**
64      * Tests two attributed strings for equality.
65      *
66      * @param s1 string 1 (<code>null</code> permitted).
67      * @param s2 string 2 (<code>null</code> permitted).
68      *
69      * @return <code>true</code> if <code>s1</code> and <code>s2</code> are
70      * equal or both <code>null</code>, and <code>false</code>
71      * otherwise.
72      */

73     public static boolean equal(AttributedString JavaDoc s1, AttributedString JavaDoc s2) {
74         if (s1 == null) {
75             return (s2 == null);
76         }
77         if (s2 == null) {
78             return false;
79         }
80         AttributedCharacterIterator JavaDoc it1 = s1.getIterator();
81         AttributedCharacterIterator JavaDoc it2 = s2.getIterator();
82         char c1 = it1.first();
83         char c2 = it2.first();
84         int start = 0;
85         while (c1 != CharacterIterator.DONE) {
86             int limit1 = it1.getRunLimit();
87             int limit2 = it2.getRunLimit();
88             if (limit1 != limit2) {
89                 return false;
90             }
91             // if maps aren't equivalent, return false
92
Map JavaDoc m1 = it1.getAttributes();
93             Map JavaDoc m2 = it2.getAttributes();
94             if (!m1.equals(m2)) {
95                 return false;
96             }
97             // now check characters in the run are the same
98
for (int i = start; i < limit1; i++) {
99                 if (c1 != c2) {
100                     return false;
101                 }
102                 c1 = it1.next();
103                 c2 = it2.next();
104             }
105             start = limit1;
106         }
107         return c2 == CharacterIterator.DONE;
108     }
109     
110 }
111
Popular Tags