001/** Utilities for testing graphs.
002
003 Copyright (c) 2001-2013 The University of Maryland. All rights reserved.
004 Permission is hereby granted, without written agreement and without
005 license or royalty fees, to use, copy, modify, and distribute this
006 software and its documentation for any purpose, provided that the above
007 copyright notice and the following two paragraphs appear in all copies
008 of this software.
009
010 IN NO EVENT SHALL THE UNIVERSITY OF MARYLAND BE LIABLE TO ANY PARTY
011 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
012 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
013 THE UNIVERSITY OF MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF
014 SUCH DAMAGE.
015
016 THE UNIVERSITY OF MARYLAND SPECIFICALLY DISCLAIMS ANY WARRANTIES,
017 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
018 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
019 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
020 MARYLAND HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
021 ENHANCEMENTS, OR MODIFICATIONS.
022
023 PT_COPYRIGHT_VERSION_2
024 COPYRIGHTENDKEY
025
026 */
027package ptolemy.graph.test;
028
029import java.util.ArrayList;
030import java.util.Collection;
031import java.util.Iterator;
032
033//////////////////////////////////////////////////////////////////////////
034//// Utilities
035
036/**
037 Utilities for testing graphs.
038 This class provides utilities, in the form of static methods, for testing
039 graphs.
040
041 @author Shuvra S. Bhattacharyya
042 @version $Id$
043 @since Ptolemy II 2.0
044 @Pt.ProposedRating Red (cxh)
045 @Pt.AcceptedRating Red (cxh)
046 */
047public class Utilities {
048    // Private constructor to prevent instantiation.
049    private Utilities() {
050    }
051
052    /** Given a collection, return a string representation of the collection.
053     *  The representation returned is obtained by concatenating the
054     *  string representations of the individual elements in their sorted
055     *  order, as determined by the compareTo method of java.lang.String.
056     *  If the <code>recursive</code> argument is true, then elements of
057     *  the collection that are themselves collections are recursively
058     *  converted to sorted strings using this method. When printing
059     *  test results, this method can be used to guarantee a consistent
060     *  representation for a collection regardless of the order in which
061     *  elements are inserted and removed.
062     *  @param collection The collection.
063     *  @param recursive True if elements of the collection that are themselves
064     *  collections should be recursively converted to sorted strings.
065     *  @return The string representation.
066     */
067    public static String toSortedString(Collection collection,
068            boolean recursive) {
069        ArrayList result = new ArrayList(collection.size());
070        Iterator elements = collection.iterator();
071
072        while (elements.hasNext()) {
073            Object element = elements.next();
074            String elementString;
075
076            if (element instanceof Collection && recursive) {
077                elementString = toSortedString((Collection) element, recursive);
078            } else {
079                elementString = element.toString();
080            }
081
082            int i;
083
084            for (i = 0; i < result.size() && ((String) result.get(i))
085                    .compareTo(elementString) < 0; i++) {
086                ;
087            }
088
089            result.add(i, elementString);
090        }
091
092        return result.toString();
093    }
094}