001    /* ========================================================================
002     * JCommon : a free general purpose class library for the Java(tm) platform
003     * ========================================================================
004     *
005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
006     * 
007     * Project Info:  http://www.jfree.org/jcommon/index.html
008     *
009     * This library is free software; you can redistribute it and/or modify it 
010     * under the terms of the GNU Lesser General Public License as published by 
011     * the Free Software Foundation; either version 2.1 of the License, or 
012     * (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but 
015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
017     * License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this library; if not, write to the Free Software
021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
022     * USA.  
023     *
024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
025     * in the United States and other countries.]
026     * 
027     * ------------------------------
028     * AttributedStringUtilities.java
029     * ------------------------------
030     * (C)opyright 2005, by Object Refinery Limited and Contributors.
031     *
032     * Original Author:  David Gilbert (for Object Refinery Limited);
033     * Contributor(s):   -;
034     *
035     * $Id: AttributedStringUtilities.java,v 1.2 2005/10/18 13:24:19 mungady Exp $
036     *
037     * Changes
038     * -------
039     * 29-Jul-2005 : Version 1(DG);
040     * 
041     */
042    
043    package org.jfree.util;
044    
045    import java.text.AttributedCharacterIterator;
046    import java.text.AttributedString;
047    import java.text.CharacterIterator;
048    import java.util.Map;
049    
050    /**
051     * Some utility methods for working with <code>AttributedString</code> objects.
052     * 
053     * @author David Gilbert
054     */
055    public class AttributedStringUtilities {
056    
057        /**
058         * Private constructor prevents object creation.
059         */
060        private AttributedStringUtilities() {
061        }
062    
063        /**
064         * Tests two attributed strings for equality.
065         * 
066         * @param s1  string 1 (<code>null</code> permitted).
067         * @param s2  string 2 (<code>null</code> permitted).
068         * 
069         * @return <code>true</code> if <code>s1</code> and <code>s2</code> are
070         *         equal or both <code>null</code>, and <code>false</code> 
071         *         otherwise.
072         */
073        public static boolean equal(AttributedString s1, AttributedString s2) {
074            if (s1 == null) {
075                return (s2 == null);
076            }
077            if (s2 == null) {
078                return false;
079            }
080            AttributedCharacterIterator it1 = s1.getIterator();
081            AttributedCharacterIterator it2 = s2.getIterator();
082            char c1 = it1.first();
083            char c2 = it2.first();
084            int start = 0;
085            while (c1 != CharacterIterator.DONE) {
086                int limit1 = it1.getRunLimit();
087                int limit2 = it2.getRunLimit();
088                if (limit1 != limit2) {
089                    return false;
090                }
091                // if maps aren't equivalent, return false
092                Map m1 = it1.getAttributes();
093                Map m2 = it2.getAttributes();
094                if (!m1.equals(m2)) {
095                    return false;
096                }
097                // now check characters in the run are the same
098                for (int i = start; i < limit1; i++) {
099                    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    }