001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2021, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 */
028
029package org.jfree.chart.text;
030
031import java.text.AttributedCharacterIterator;
032import java.text.AttributedString;
033import java.text.CharacterIterator;
034import java.util.Map;
035
036/**
037 * Some utility methods for working with {@code AttributedString} objects.
038 */
039public class AttributedStringUtils {
040
041    /**
042     * Private constructor prevents object creation.
043     */
044    private AttributedStringUtils() {
045    }
046
047    /**
048     * Tests two attributed strings for equality.
049     * 
050     * @param s1  string 1 ({@code null} permitted).
051     * @param s2  string 2 ({@code null} permitted).
052     * 
053     * @return {@code true} if {@code s1} and {@code s2} are
054     *         equal or both {@code null}, and {@code false} 
055     *         otherwise.
056     */
057    public static boolean equal(AttributedString s1, AttributedString s2) {
058        if (s1 == null) {
059            return (s2 == null);
060        }
061        if (s2 == null) {
062            return false;
063        }
064        AttributedCharacterIterator it1 = s1.getIterator();
065        AttributedCharacterIterator it2 = s2.getIterator();
066        char c1 = it1.first();
067        char c2 = it2.first();
068        int start = 0;
069        while (c1 != CharacterIterator.DONE) {
070            int limit1 = it1.getRunLimit();
071            int limit2 = it2.getRunLimit();
072            if (limit1 != limit2) {
073                return false;
074            }
075            // if maps aren't equivalent, return false
076            Map m1 = it1.getAttributes();
077            Map m2 = it2.getAttributes();
078            if (!m1.equals(m2)) {
079                return false;
080            }
081            // now check characters in the run are the same
082            for (int i = start; i < limit1; i++) {
083                if (c1 != c2) {
084                    return false;
085                }
086                c1 = it1.next();
087                c2 = it2.next();
088            }
089            start = limit1;
090        }
091        return c2 == CharacterIterator.DONE;
092    }
093    
094}