Author: tfmorris Date: 2008-04-24 11:29:50-0700 New Revision: 14458 Modified: trunk/src/argouml-app/src/org/argouml/uml/util/PathComparator.java trunk/src/argouml-app/tests/org/argouml/uml/util/TestPathComparator.java
Log: Fix null handling. Add support for Strings. Modified: trunk/src/argouml-app/src/org/argouml/uml/util/PathComparator.java Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/src/org/argouml/uml/util/PathComparator.java?view=diff&rev=14458&p1=trunk/src/argouml-app/src/org/argouml/uml/util/PathComparator.java&p2=trunk/src/argouml-app/src/org/argouml/uml/util/PathComparator.java&r1=14457&r2=14458 ============================================================================== --- trunk/src/argouml-app/src/org/argouml/uml/util/PathComparator.java (original) +++ trunk/src/argouml-app/src/org/argouml/uml/util/PathComparator.java 2008-04-24 11:29:50-0700 @@ -53,7 +53,8 @@ /** * Compare two UML elements names, ignoring case, using names from the path - * as tie breakers. + * as tie breakers. As a convenience, we also compare simple strings using + * the same primary strength collator. * * @param o1 first model element * @param o2 second model element @@ -61,25 +62,37 @@ * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) */ public int compare(Object o1, Object o2) { + if (o1 == null) { + if (o2 == null) { + return 0; + } + return -1; + } + if (o2 == null) { + return 1; + } if (o1.equals(o2)) { return 0; } + if (o1 instanceof String && o2 instanceof String) { + return collator.compare((String) o1, (String) o2); + } // Elements are collated first by name hoping for a quick solution String name1, name2; try { name1 = Model.getFacade().getName(o1); name2 = Model.getFacade().getName(o2); } catch (IllegalArgumentException e) { - throw new ClassCastException("Model element required"); + throw new ClassCastException("Model element or String required"); } - int comparison = collator.compare(name1, name2); - if (comparison != 0) { - return comparison; - } else { - // and then by their enclosing path to fully distinguish them - return comparePaths(o1, o2); + if (name1 != null && name2 != null) { + int comparison = collator.compare(name1, name2); + if (comparison != 0) { + return comparison; + } } - + // and then by their enclosing path to fully distinguish them + return comparePaths(o1, o2); } /* @@ -108,17 +121,35 @@ return -1; } String name1 = i1.next(); + int comparison; if (name1 == null) { - return -1; + if (name2 == null) { + comparison = 0; + } else { + comparison = -1; + } + } else if (name2 == null) { + comparison = 1; + } else { + comparison = collator.compare(name1, name2); } - int comparison = collator.compare(name1, name2); if (comparison != 0) { return comparison; } // Keep track of first non-equal comparison to use in case the // case-insensitive comparisons all end up equal if (caseSensitiveComparison == 0) { - caseSensitiveComparison = name1.compareTo(name2); + if (name1 == null) { + if (name2 == null) { + caseSensitiveComparison = 0; + } else { + caseSensitiveComparison = -1; + } + } else if (name2 == null) { + caseSensitiveComparison = 1; + } else { + caseSensitiveComparison = name1.compareTo(name2); + } } } if (i2.hasNext()) { Modified: trunk/src/argouml-app/tests/org/argouml/uml/util/TestPathComparator.java Url: http://argouml.tigris.org/source/browse/argouml/trunk/src/argouml-app/tests/org/argouml/uml/util/TestPathComparator.java?view=diff&rev=14458&p1=trunk/src/argouml-app/tests/org/argouml/uml/util/TestPathComparator.java&p2=trunk/src/argouml-app/tests/org/argouml/uml/util/TestPathComparator.java&r1=14457&r2=14458 ============================================================================== --- trunk/src/argouml-app/tests/org/argouml/uml/util/TestPathComparator.java (original) +++ trunk/src/argouml-app/tests/org/argouml/uml/util/TestPathComparator.java 2008-04-24 11:29:50-0700 @@ -54,7 +54,11 @@ Object root = Model.getModelManagementFactory().createModel(); setName(root, "rootModel"); Object unnamed1 = Model.getCoreFactory().buildClass(root); + // Name is set to the empty string (yuck!) by default - fix it + setName(unnamed1, null); + Object unnamed2 = Model.getCoreFactory().buildClass(root); + setName(unnamed2, null); Object a = Model.getCoreFactory().buildClass("a", root); Object a1 = Model.getCoreFactory().buildClass("a", root); Object b = Model.getCoreFactory().buildClass("b", root); @@ -64,6 +68,16 @@ Object ba = Model.getCoreFactory().buildClass("b", a); Object bc = Model.getCoreFactory().buildClass("b", c); + assertEquals("Two nulls should be equal", 0, comp.compare(null, null)); + + assertEquals("Null should be less than anything", -1, + comp.compare(null, "")); + assertEquals("Null should be less than anything", 1, + comp.compare("", null)); + + assertEquals("Simple string compare failed", comp.compare("a","b"), + "a".compareTo("b")); + assertFalse("Two different unnamed elements should not be equal", comp.compare(unnamed1, unnamed2) == 0); assertEquals("Unnamed elements should collate before named", -1, --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
