GUI -- Implemented table sorting in our simple table/row/cell model
- Introduced CellComparator (with default implementation)

Change-Id: I125f52c2c1ca219746b0e506e8837e24fb149038
diff --git a/core/api/src/main/java/org/onosproject/ui/table/CellComparator.java b/core/api/src/main/java/org/onosproject/ui/table/CellComparator.java
new file mode 100644
index 0000000..84d1134
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/table/CellComparator.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onosproject.ui.table;
+
+/**
+ * Defines a comparator for cell values.
+ */
+public interface CellComparator {
+
+    /**
+     * Compares its two arguments for order.  Returns a negative integer,
+     * zero, or a positive integer as the first argument is less than, equal
+     * to, or greater than the second.<p>
+     *
+     * Note that nulls are permitted, and should be sorted to the beginning
+     * of an ascending sort; i.e. null is considered to be "smaller" than
+     * non-null values.
+     *
+     * @see java.util.Comparator#compare(Object, Object)
+     *
+     * @param o1 the first object to be compared.
+     * @param o2 the second object to be compared.
+     * @return a negative integer, zero, or a positive integer as the
+     *         first argument is less than, equal to, or greater than the
+     *         second.
+     * @throws ClassCastException if the arguments' types prevent them from
+     *         being compared by this comparator.
+     */
+    int compare(Object o1, Object o2);
+
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/DefaultCellComparator.java b/core/api/src/main/java/org/onosproject/ui/table/DefaultCellComparator.java
new file mode 100644
index 0000000..c27be61
--- /dev/null
+++ b/core/api/src/main/java/org/onosproject/ui/table/DefaultCellComparator.java
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onosproject.ui.table;
+
+/**
+ * A default cell comparator. Implements a lexicographical compare function
+ * (i.e. string sorting). Uses the objects' toString() method and then
+ * compares the resulting strings. Note that null values are acceptable and
+ * are considered "smaller" than any non-null value.
+ */
+public class DefaultCellComparator implements CellComparator {
+    @Override
+    public int compare(Object o1, Object o2) {
+        if (o1 == null && o2 == null) {
+            return 0;       // o1 == o2
+        }
+        if (o1 == null) {
+            return -1;      // o1 < o2
+        }
+        if (o2 == null) {
+            return 1;       // o1 > o2
+        }
+        return o1.toString().compareTo(o2.toString());
+    }
+}
diff --git a/core/api/src/main/java/org/onosproject/ui/table/TableModel.java b/core/api/src/main/java/org/onosproject/ui/table/TableModel.java
index 3ff78d9..7fb336e 100644
--- a/core/api/src/main/java/org/onosproject/ui/table/TableModel.java
+++ b/core/api/src/main/java/org/onosproject/ui/table/TableModel.java
@@ -20,6 +20,8 @@
 
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -29,14 +31,25 @@
 import static com.google.common.base.Preconditions.checkNotNull;
 
 /**
- * A model of table data.
+ * A simple model of table data.
+ * <p>
+ * Note that this is not a full MVC type model; the expected usage pattern
+ * is to create an empty table, add rows (by consulting the business model),
+ * sort rows (based on client request parameters), and finally produce the
+ * sorted list of rows.
+ * <p>
+ * The table also provides a mechanism for defining how cell values for a
+ * particular column should be formatted into strings, to help facilitate
+ * the encoding of the table data into a JSON structure.
  */
 public class TableModel {
 
+    private static final CellComparator DEF_CMP = new DefaultCellComparator();
     private static final CellFormatter DEF_FMT = new DefaultCellFormatter();
 
     private final String[] columnIds;
     private final Set<String> idSet;
+    private final Map<String, CellComparator> comparators = new HashMap<>();
     private final Map<String, CellFormatter> formatters = new HashMap<>();
     private final List<Row> rows = new ArrayList<>();
 
@@ -88,6 +101,7 @@
      *
      * @return formatted table rows
      */
+    // TODO: still need to decide if we need this
     public TableRow[] getTableRows() {
         return new TableRow[0];
     }
@@ -97,11 +111,36 @@
      *
      * @return raw table rows
      */
+    // TODO: still need to decide if we should expose this
     public Row[] getRows() {
         return rows.toArray(new Row[rows.size()]);
     }
 
     /**
+     * Sets a cell comparator for the specified column.
+     *
+     * @param columnId column identifier
+     * @param comparator comparator to use
+     */
+    public void setComparator(String columnId, CellComparator comparator) {
+        checkNotNull(comparator, "must provide a comparator");
+        checkId(columnId);
+        comparators.put(columnId, comparator);
+    }
+
+    /**
+     * Returns the cell comparator to use on values in the specified column.
+     *
+     * @param columnId column identifier
+     * @return an appropriate cell comparator
+     */
+    private CellComparator getComparator(String columnId) {
+        checkId(columnId);
+        CellComparator cmp = comparators.get(columnId);
+        return cmp == null ? DEF_CMP : cmp;
+    }
+
+    /**
      * Sets a cell formatter for the specified column.
      *
      * @param columnId column identifier
@@ -137,6 +176,56 @@
     }
 
     /**
+     * Sorts the table rows based on the specified column, in the
+     * specified direction.
+     *
+     * @param columnId column identifier
+     * @param dir sort direction
+     */
+    public void sort(String columnId, SortDir dir) {
+        Collections.sort(rows, new RowComparator(columnId, dir));
+    }
+
+
+    /** Designates sorting direction. */
+    public enum SortDir {
+        /** Designates an ascending sort. */
+        ASC,
+        /** Designates a descending sort. */
+        DESC
+    }
+
+    /**
+     * Row comparator.
+     */
+    private class RowComparator implements Comparator<Row> {
+        private final String columnId;
+        private final SortDir dir;
+        private final CellComparator cellComparator;
+
+        /**
+         * Constructs a row comparator based on the specified
+         * column identifier and sort direction.
+         *
+         * @param columnId column identifier
+         * @param dir sort direction
+         */
+        public RowComparator(String columnId, SortDir dir) {
+            this.columnId = columnId;
+            this.dir = dir;
+            cellComparator = getComparator(columnId);
+        }
+
+        @Override
+        public int compare(Row a, Row b) {
+            Object cellA = a.get(columnId);
+            Object cellB = b.get(columnId);
+            int result = cellComparator.compare(cellA, cellB);
+            return dir == SortDir.ASC ? result : -result;
+        }
+    }
+
+    /**
      * Model of a row.
      */
     public class Row {
@@ -166,4 +255,20 @@
             return cells.get(columnId);
         }
     }
+
+    private static final String DESC = "desc";
+
+    /**
+     * Returns the appropriate sort direction for the given string.
+     * <p>
+     * The expected strings are "asc" for {@link SortDir#ASC ascending} and
+     * "desc" for {@link SortDir#DESC descending}. Any other value will
+     * default to ascending.
+     *
+     * @param s sort direction string encoding
+     * @return sort direction
+     */
+    public static SortDir sortDir(String s) {
+        return !DESC.equals(s) ? SortDir.ASC : SortDir.DESC;
+    }
 }
diff --git a/core/api/src/test/java/org/onosproject/ui/table/DefaultCellComparatorTest.java b/core/api/src/test/java/org/onosproject/ui/table/DefaultCellComparatorTest.java
new file mode 100644
index 0000000..86add93
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/ui/table/DefaultCellComparatorTest.java
@@ -0,0 +1,83 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.onosproject.ui.table;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Unit tests for {@link DefaultCellComparator}.
+ */
+public class DefaultCellComparatorTest {
+
+    private static class TestClass {
+        @Override
+        public String toString() {
+            return SOME;
+        }
+    }
+
+    private static final String SOME = "SoMeStRiNg";
+    private static final String OTHER = "OtherSTRING";
+    private static final int NUMBER = 42;
+    private static final TestClass OBJECT = new TestClass();
+
+    private CellComparator cmp = new DefaultCellComparator();
+
+    @Test
+    public void sameString() {
+        assertTrue("same string", cmp.compare(SOME, SOME) == 0);
+    }
+
+    @Test
+    public void someVsOther() {
+        assertTrue("some vs other", cmp.compare(SOME, OTHER) > 0);
+    }
+
+    @Test
+    public void otherVsSome() {
+        assertTrue("other vs some", cmp.compare(OTHER, SOME) < 0);
+    }
+
+    @Test
+    public void someVsObject() {
+        assertTrue("some vs object", cmp.compare(SOME, OBJECT) == 0);
+    }
+
+    @Test
+    public void otherVsObject() {
+        assertTrue("other vs object", cmp.compare(OTHER, OBJECT) < 0);
+    }
+
+    @Test
+    public void otherVsNumber() {
+        assertTrue("other vs 42", cmp.compare(OTHER, NUMBER) > 0);
+    }
+
+    @Test
+    public void someVsNull() {
+        assertTrue("some vs null", cmp.compare(SOME, null) > 0);
+    }
+
+    @Test
+    public void nullVsSome() {
+        assertTrue("null vs some", cmp.compare(null, SOME) < 0);
+    }
+
+}
diff --git a/core/api/src/test/java/org/onosproject/ui/table/TableModelTest.java b/core/api/src/test/java/org/onosproject/ui/table/TableModelTest.java
index c111597..33b8fb0 100644
--- a/core/api/src/test/java/org/onosproject/ui/table/TableModelTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/table/TableModelTest.java
@@ -17,20 +17,30 @@
 package org.onosproject.ui.table;
 
 import org.junit.Test;
+import org.onosproject.ui.table.TableModel.SortDir;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.*;
 
 /**
  * Unit tests for {@link TableModel}.
  */
 public class TableModelTest {
 
+    private static final String UNEX_SORT_ORDER = "unexpected sort: index ";
+
     private static final String FOO = "foo";
     private static final String BAR = "bar";
-    private static final String BAZ = "baz";
     private static final String ZOO = "zoo";
 
+    private static class TestCmpr implements CellComparator {
+        @Override
+        public int compare(Object o1, Object o2) {
+            int i1 = (int) o1;
+            int i2 = (int) o2;
+            return i1 - i2;
+        }
+    }
+
     private static class TestFmtr implements CellFormatter {
         @Override
         public String format(Object value) {
@@ -39,7 +49,9 @@
     }
 
     private TableModel tm;
-    private TableRow[] rows;
+    private TableModel.Row[] rows;
+    private TableModel.Row row;
+    private TableRow[] tableRows;
     private CellFormatter fmt;
 
     @Test(expected = NullPointerException.class)
@@ -63,8 +75,8 @@
         assertEquals("column count", 2, tm.columnCount());
         assertEquals("row count", 0, tm.rowCount());
 
-        rows = tm.getTableRows();
-        assertEquals("row count alt", 0, rows.length);
+        tableRows = tm.getTableRows();
+        assertEquals("row count alt", 0, tableRows.length);
     }
 
     @Test
@@ -118,8 +130,147 @@
         tm = new TableModel(FOO, BAR);
         tm.addRow().cell(FOO, 3).cell(BAR, true);
         assertEquals("bad row count", 1, tm.rowCount());
-        TableModel.Row r = tm.getRows()[0];
-        assertEquals("bad cell", 3, r.get(FOO));
-        assertEquals("bad cell", true, r.get(BAR));
+        row = tm.getRows()[0];
+        assertEquals("bad cell", 3, row.get(FOO));
+        assertEquals("bad cell", true, row.get(BAR));
     }
+
+
+    private static final String ONE = "one";
+    private static final String TWO = "two";
+    private static final String THREE = "three";
+    private static final String FOUR = "four";
+    private static final String ELEVEN = "eleven";
+    private static final String TWELVE = "twelve";
+    private static final String TWENTY = "twenty";
+    private static final String THIRTY = "thirty";
+
+    private static final String[] NAMES = {
+            FOUR,
+            THREE,
+            TWO,
+            ONE,
+            ELEVEN,
+            TWELVE,
+            THIRTY,
+            TWENTY,
+    };
+    private static final String[] SORTED_NAMES = {
+            ELEVEN,
+            FOUR,
+            ONE,
+            THIRTY,
+            THREE,
+            TWELVE,
+            TWENTY,
+            TWO,
+    };
+
+    private static final int[] NUMBERS = {
+        4, 3, 2, 1, 11, 12, 30, 20
+    };
+
+    private static final int[] SORTED_NUMBERS = {
+        1, 2, 3, 4, 11, 12, 20, 30
+    };
+
+    @Test
+    public void verifyTestData() {
+        // not a unit test per se, but will fail if we don't keep
+        // the three test arrays in sync
+        int nalen = NAMES.length;
+        int snlen = SORTED_NAMES.length;
+        int nulen = NUMBERS.length;
+
+        if (nalen != snlen || nalen != nulen) {
+            fail("test data array size discrepancy");
+        }
+    }
+
+    private void initUnsortedTable() {
+        tm = new TableModel(FOO, BAR);
+        for (int i = 0; i < NAMES.length; i++) {
+            tm.addRow().cell(FOO, NAMES[i]).cell(BAR, NUMBERS[i]);
+        }
+    }
+
+    @Test
+    public void tableStringSort() {
+        initUnsortedTable();
+
+        // sort by name
+        tm.sort(FOO, SortDir.ASC);
+
+        // verify results
+        rows = tm.getRows();
+        int nr = rows.length;
+        assertEquals("row count", NAMES.length, nr);
+        for (int i = 0; i < nr; i++) {
+            assertEquals(UNEX_SORT_ORDER + i, SORTED_NAMES[i], rows[i].get(FOO));
+        }
+
+        // now the other way
+        tm.sort(FOO, SortDir.DESC);
+
+        // verify results
+        rows = tm.getRows();
+        nr = rows.length;
+        assertEquals("row count", NAMES.length, nr);
+        for (int i = 0; i < nr; i++) {
+            assertEquals(UNEX_SORT_ORDER + i,
+                         SORTED_NAMES[nr - 1 - i], rows[i].get(FOO));
+        }
+    }
+
+    @Test
+    public void tableNumberSort() {
+        initUnsortedTable();
+
+        // first, tell the table to use an integer-based comparator
+        tm.setComparator(BAR, new TestCmpr());
+
+        // sort by number
+        tm.sort(BAR, SortDir.ASC);
+
+        // verify results
+        rows = tm.getRows();
+        int nr = rows.length;
+        assertEquals("row count", NUMBERS.length, nr);
+        for (int i = 0; i < nr; i++) {
+            assertEquals(UNEX_SORT_ORDER + i, SORTED_NUMBERS[i], rows[i].get(BAR));
+        }
+
+        // now the other way
+        tm.sort(BAR, SortDir.DESC);
+
+        // verify results
+        rows = tm.getRows();
+        nr = rows.length;
+        assertEquals("row count", NUMBERS.length, nr);
+        for (int i = 0; i < nr; i++) {
+            assertEquals(UNEX_SORT_ORDER + i,
+                         SORTED_NUMBERS[nr - 1 - i], rows[i].get(BAR));
+        }
+    }
+
+    @Test
+    public void sortDirAsc() {
+        assertEquals("asc sort dir", SortDir.ASC, TableModel.sortDir("asc"));
+    }
+
+    @Test
+    public void sortDirDesc() {
+        assertEquals("desc sort dir", SortDir.DESC, TableModel.sortDir("desc"));
+    }
+
+    @Test
+    public void sortDirOther() {
+        assertEquals("other sort dir", SortDir.ASC, TableModel.sortDir("other"));
+    }
+
+    @Test
+    public void sortDirNull() {
+        assertEquals("null sort dir", SortDir.ASC, TableModel.sortDir(null));
+    }
+
 }