Commits

Scott Farquhar committed 3be26ef

Change some of the sorting interface and added testing

  • Participants
  • Parent commits 820d6da

Comments (0)

Files changed (3)

src/main/java/com/atlassian/lucene/stats/StatsMapComparators.java

+package com.atlassian.lucene.stats;
+
+import java.util.Collections;
+import java.util.Comparator;
+
+
+public class StatsMapComparators {
+
+    public final static String TOTAL_ORDER = "total";
+    public final static String NATURAL_ORDER = "natural";
+    public final static String DESC = "desc";
+    public final static String ASC = "asc";
+
+    public static Comparator getYAxis(String orderBy, String direction, final TwoDimensionalStatsMap statsMap)
+    {
+        Comparator comp;
+
+        if (orderBy != null && orderBy.equals(TOTAL_ORDER))
+        {
+            // Compare by total
+            comp = new Comparator()
+            {
+
+                public int compare(Object o1, Object o2)
+                {
+                    Long o1Long = (long) statsMap.getYAxisUniqueTotal(o1);
+                    Long o2Long = (long) statsMap.getYAxisUniqueTotal(o2);
+                    return o1Long.compareTo(o2Long);
+                }
+            };
+
+            // Only reverse total Comaparator, not field Comparator
+            if (direction != null && direction.equals(DESC))
+            {
+                comp = Collections.reverseOrder(comp);
+            }
+
+            // If totals are equal, delagate back to field comparator
+            comp = new DelegatingComparator(comp, statsMap.getyAxisMapper().getComparator());
+        }
+        else
+        {
+            comp = statsMap.getyAxisMapper().getComparator();
+            if (direction != null && direction.equals(DESC))
+            {
+                comp = Collections.reverseOrder(comp);
+            }
+        }
+
+        return comp;
+    }
+
+    public static Comparator getXAxis(String orderBy, String direction, final TwoDimensionalStatsMap statsMap)
+    {
+        Comparator comp;
+
+        if (orderBy != null && orderBy.equals(TOTAL_ORDER))
+        {
+            // Compare by total
+            comp = new Comparator()
+            {
+
+                public int compare(Object o1, Object o2)
+                {
+                    Long o1Long = (long) statsMap.getXAxisUniqueTotal(o1);
+                    Long o2Long = (long) statsMap.getXAxisUniqueTotal(o2);
+                    return o1Long.compareTo(o2Long);
+                }
+            };
+
+            // Only reverse total Comaparator, not field Comparator
+            if (direction != null && direction.equals(DESC))
+            {
+                comp = Collections.reverseOrder(comp);
+            }
+
+            // If totals are equal, delagate back to field comparator
+            comp = new DelegatingComparator(comp, statsMap.getxAxisMapper().getComparator());
+        }
+        else
+        {
+            comp = statsMap.getxAxisMapper().getComparator();
+            if (direction != null && direction.equals(DESC))
+            {
+                comp = Collections.reverseOrder(comp);
+            }
+        }
+
+        return comp;
+    }
+
+}

src/main/java/com/atlassian/lucene/stats/TwoDimensionalStatsMap.java

 
 public class TwoDimensionalStatsMap
 {
-    public final static String TOTAL_ORDER = "total";
-    public final static String NATURAL_ORDER = "natural";
-    public final static String DESC = "desc";
-    public final static String ASC = "asc";
-
     private final StatisticsMapper xAxisMapper;
     private final StatisticsMapper yAxisMapper;
     private final StatisticGatherer statisticGatherer;
     }
 
 
+    /**
+     * @see StatsMapComparators.getXAxis()
+     * @param comp  To sort the XAxis fields on
+     * @return  A collection of objects
+     */
     public Collection getXAxis()
     {
-        final Set xVals = new TreeSet(xAxisMapper.getComparator());
+        return getXAxis(xAxisMapper.getComparator());
+    }
+
+    public Collection getXAxis(Comparator comp)
+    {
+        final Set xVals = new TreeSet(comp);
         if (xAxis.keySet() != null)
         {
             xVals.addAll(xAxis.keySet());
 
     public Collection getYAxis()
     {
-        return getYAxis(NATURAL_ORDER, ASC);
+        return getYAxis(getyAxisMapper().getComparator());
     }
 
-    public Collection getYAxis(String orderBy, String direction)
-    {
-        Comparator comp;
-
-        if (orderBy != null && orderBy.equals(TOTAL_ORDER))
-        {
-            // Compare by total
-            comp = new Comparator()
-            {
-
-                public int compare(Object o1, Object o2)
-                {
-                    Long o1Long = (long) getYAxisUniqueTotal(o1);
-                    Long o2Long = (long) getYAxisUniqueTotal(o2);
-                    return o1Long.compareTo(o2Long);
-                }
-            };
-
-            // Only reverse total Comaparator, not field Comparator
-            if (direction != null && direction.equals(DESC))
-            {
-                comp = Collections.reverseOrder(comp);
-            }
-
-            // If totals are equal, delagate back to field comparator
-            comp = new DelegatingComparator(comp, yAxisMapper.getComparator());
-        }
-        else
-        {
-            comp = yAxisMapper.getComparator();
-            if (direction != null && direction.equals(DESC))
-            {
-                comp = Collections.reverseOrder(comp);
-            }
-        }
-
-        return getYAxis(comp);
-    }
-
+    /**
+     * @see StatsMapComparators.getYAxis()
+     * @param comp  To sort the YAxis fields on
+     * @return  A collection of objects
+     */
     public Collection getYAxis(Comparator comp)
     {
         Set yAxisKeys = new TreeSet(comp);

src/test/java/com/atlassian/lucene/stats/TestTwoDimensionalStatsMap.java

 import com.atlassian.lucene.stats.util.EasyList;
 import com.atlassian.lucene.stats.util.NullStatsMapper;
 import org.junit.Test;
+
+import java.util.Collection;
+
 import static org.junit.Assert.*;
 import static org.junit.Assert.assertEquals;
 
     }
 
 
-
     @Test
     public void testAdjustMapSomeInvalidValues()
     {
         assertEquals(7, statsMap.getUniqueTotal());
     }
 
+    @Test
     public void testAdjustMapUsingCountUnique()
     {
         TwoDimensionalStatsMap statsMap = new TwoDimensionalStatsMap(NULL_STATS_MAPPER, NULL_STATS_MAPPER, new StatisticGatherer.CountUnique());
         assertEquals(5, statsMap.getUniqueTotal());
     }
 
+    @Test
+    public void testSorting()
+    {
+        TwoDimensionalStatsMap statsMap = new TwoDimensionalStatsMap(NULL_STATS_MAPPER, NULL_STATS_MAPPER, new StatisticGatherer.Sum());
+
+        // add 5 unique values
+        statsMap.adjustMapForValues(EasyList.build("def"), EasyList.build("AAA"), 15);
+        statsMap.adjustMapForValues(EasyList.build("abc"), EasyList.build("BBB"), 20);
+        statsMap.adjustMapForValues(EasyList.build("xyz"), EasyList.build("CCC"), 30);
+        statsMap.adjustMapForValues(EasyList.build("abc"), EasyList.build("DDD"), 21);
+
+        // sorting natural order xaxis
+        Collection xAxisValues = statsMap.getXAxis(StatsMapComparators.getXAxis(StatsMapComparators.NATURAL_ORDER, StatsMapComparators.ASC, statsMap));
+        Object[] objects = xAxisValues.toArray();
+        assertEquals(3, objects.length);
+        assertEquals("abc", objects[0]);
+        assertEquals("def", objects[1]);
+        assertEquals("xyz", objects[2]);
+
+        // sorting natural order yaxis
+        Collection yAxisValues = statsMap.getYAxis(StatsMapComparators.getYAxis(StatsMapComparators.NATURAL_ORDER, StatsMapComparators.DESC, statsMap));
+        objects = yAxisValues.toArray();
+        assertEquals(4, objects.length);
+        assertEquals("DDD", objects[0]);
+        assertEquals("CCC", objects[1]);
+        assertEquals("BBB", objects[2]);
+        assertEquals("AAA", objects[3]);
+
+        yAxisValues = statsMap.getYAxis(StatsMapComparators.getYAxis(StatsMapComparators.TOTAL_ORDER, StatsMapComparators.ASC, statsMap));
+        objects = yAxisValues.toArray();
+        assertEquals(4, objects.length);
+        assertEquals("AAA", objects[0]);
+        assertEquals("BBB", objects[1]);
+        assertEquals("DDD", objects[2]);
+        assertEquals("CCC", objects[3]);
+
+        xAxisValues = statsMap.getXAxis(StatsMapComparators.getXAxis(StatsMapComparators.TOTAL_ORDER, StatsMapComparators.ASC, statsMap));
+        objects = xAxisValues.toArray();
+        assertEquals(3, objects.length);
+        assertEquals("def", objects[0]);
+        assertEquals("xyz", objects[1]);
+        assertEquals("abc", objects[2]);
+
+    }
 
 
 }