Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 106   Methods: 3
NCLOC: 62   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FqnComparator.java 90% 91.7% 100% 91.5%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7    package org.jboss.cache;
 8   
 9    import net.jcip.annotations.Immutable;
 10   
 11    import java.util.Comparator;
 12   
 13    /**
 14    * Compares the order of two FQN.
 15    * Sorts by name, then by depth, e.g.
 16    * <pre>
 17    * aaa/bbb
 18    * xxx
 19    * xxx/ccc
 20    * </pre>
 21    *
 22    * @author Manik Surtani (<a href="mailto:manik@jboss.org">manik@jboss.org</a>)
 23    * @author Steve Woodcock (<a href="mailto:stevew@jofti.com">stevew@jofti.com</a>)
 24    */
 25    @Immutable
 26    public class FqnComparator implements Comparator<Fqn>
 27    {
 28    public static final FqnComparator INSTANCE = new FqnComparator();
 29   
 30    /**
 31    * Sorts by name, then depth.
 32    */
 33  112 public FqnComparator()
 34    {
 35    }
 36   
 37    /**
 38    * Returns -1 if the first comes before; 0 if they are the same; 1 if the
 39    * second Fqn comes before. <code>null</code> always comes first.
 40    */
 41  90174 public int compare(Fqn fqn1, Fqn fqn2)
 42    {
 43  90174 int s1 = fqn1.size();
 44  90174 int s2 = fqn2.size();
 45   
 46  90174 if (s1 == 0)
 47    {
 48  998 return (s2 == 0) ? 0 : -1;
 49    }
 50   
 51  89176 if (s2 == 0)
 52    {
 53  352 return 1;
 54    }
 55   
 56  88824 int size = Math.min(s1, s2);
 57   
 58  88824 for (int i = 0; i < size; i++)
 59    {
 60  314080 Object e1 = fqn1.get(i);
 61  314080 Object e2 = fqn2.get(i);
 62  314080 if (e1 == e2)
 63    {
 64  132603 continue;
 65    }
 66  181477 if (e1 == null)
 67    {
 68  0 return 0;
 69    }
 70  181477 if (e2 == null)
 71    {
 72  0 return 1;
 73    }
 74  181477 if (!e1.equals(e2))
 75    {
 76  42992 int c = compareElements(e1, e2);
 77  42992 if (c != 0)
 78    {
 79  42985 return c;
 80    }
 81    }
 82    }
 83   
 84  45839 return s1 - s2;
 85    }
 86   
 87    /**
 88    * Compares two Fqn elements.
 89    * If e1 and e2 are the same class and e1 implements Comparable,
 90    * returns e1.compareTo(e2).
 91    * Otherwise, returns e1.toString().compareTo(e2.toString()).
 92    */
 93  42992 private int compareElements(Object e1, Object e2)
 94    {
 95  42992 if (e1.getClass() == e2.getClass() && e1 instanceof Comparable)
 96    {
 97  42980 return ((Comparable) e1).compareTo(e2);
 98    }
 99    else
 100    {
 101  12 return e1.toString().compareTo(e2.toString());
 102    }
 103    }
 104   
 105   
 106    }