Simon Hunt

GUI -- added getAsString() to TableModel.Row.

- Moved cell-related classes to new package.
- Created base classes for cell comparator and formatter.
- Created IntComparator and HexFormatter.

Change-Id: I3861bf3e0ca738a2d6eab9c70174db53f22960f8
...@@ -24,7 +24,8 @@ public interface CellFormatter { ...@@ -24,7 +24,8 @@ public interface CellFormatter {
24 24
25 /** 25 /**
26 * Formats the specified value into a string appropriate for displaying 26 * Formats the specified value into a string appropriate for displaying
27 - * in a table cell. 27 + * in a table cell. Note that null values are acceptable, and will result
28 + * in the empty string.
28 * 29 *
29 * @param value the value 30 * @param value the value
30 * @return the formatted string 31 * @return the formatted string
......
...@@ -17,6 +17,8 @@ ...@@ -17,6 +17,8 @@
17 package org.onosproject.ui.table; 17 package org.onosproject.ui.table;
18 18
19 import com.google.common.collect.Sets; 19 import com.google.common.collect.Sets;
20 +import org.onosproject.ui.table.cell.DefaultCellComparator;
21 +import org.onosproject.ui.table.cell.DefaultCellFormatter;
20 22
21 import java.util.ArrayList; 23 import java.util.ArrayList;
22 import java.util.Arrays; 24 import java.util.Arrays;
...@@ -254,6 +256,17 @@ public class TableModel { ...@@ -254,6 +256,17 @@ public class TableModel {
254 public Object get(String columnId) { 256 public Object get(String columnId) {
255 return cells.get(columnId); 257 return cells.get(columnId);
256 } 258 }
259 +
260 + /**
261 + * Returns the value of the cell as a string, using the
262 + * formatter appropriate for the column.
263 + *
264 + * @param columnId column identifier
265 + * @return formatted cell value
266 + */
267 + public String getAsString(String columnId) {
268 + return getFormatter(columnId).format(get(columnId));
269 + }
257 } 270 }
258 271
259 private static final String DESC = "desc"; 272 private static final String DESC = "desc";
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + *
16 + */
17 +
18 +package org.onosproject.ui.table.cell;
19 +
20 +import org.onosproject.ui.table.CellComparator;
21 +
22 +/**
23 + * Base implementation of a {@link CellComparator}. This class takes care
24 + * of dealing with null inputs; subclasses should implement their comparison
25 + * knowing that both inputs are guaranteed to be non-null.
26 + */
27 +public abstract class AbstractCellComparator implements CellComparator {
28 +
29 + @Override
30 + public int compare(Object o1, Object o2) {
31 + if (o1 == null && o2 == null) {
32 + return 0; // o1 == o2
33 + }
34 + if (o1 == null) {
35 + return -1; // o1 < o2
36 + }
37 + if (o2 == null) {
38 + return 1; // o1 > o2
39 + }
40 + return nonNullCompare(o1, o2);
41 + }
42 +
43 + /**
44 + * Compares its two arguments for order. Returns a negative integer,
45 + * zero, or a positive integer as the first argument is less than, equal
46 + * to, or greater than the second.<p>
47 + *
48 + * Note that both objects are guaranteed to be non-null.
49 + *
50 + * @see java.util.Comparator#compare(Object, Object)
51 + *
52 + * @param o1 the first object to be compared.
53 + * @param o2 the second object to be compared.
54 + * @return a negative integer, zero, or a positive integer as the
55 + * first argument is less than, equal to, or greater than the
56 + * second.
57 + * @throws ClassCastException if the arguments' types prevent them from
58 + * being compared by this comparator.
59 + */
60 + protected abstract int nonNullCompare(Object o1, Object o2);
61 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + *
16 + */
17 +
18 +package org.onosproject.ui.table.cell;
19 +
20 +import org.onosproject.ui.table.CellFormatter;
21 +
22 +/**
23 + * Base implementation of a {@link CellFormatter}. This class takes care of
24 + * dealing with null inputs; subclasses should implement their format method
25 + * knowing that the input is guaranteed to be non-null.
26 + */
27 +public abstract class AbstractCellFormatter implements CellFormatter {
28 +
29 + @Override
30 + public String format(Object value) {
31 + return value == null ? "" : nonNullFormat(value);
32 + }
33 +
34 + /**
35 + * Formats the specified value into a string appropriate for displaying
36 + * in a table cell. Note that value is guaranteed to be non-null.
37 + *
38 + * @param value the value
39 + * @return the formatted string
40 + */
41 + protected abstract String nonNullFormat(Object value);
42 +}
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
15 * 15 *
16 */ 16 */
17 17
18 -package org.onosproject.ui.table; 18 +package org.onosproject.ui.table.cell;
19 19
20 /** 20 /**
21 * A default cell comparator. Implements a lexicographical compare function 21 * A default cell comparator. Implements a lexicographical compare function
...@@ -23,18 +23,9 @@ package org.onosproject.ui.table; ...@@ -23,18 +23,9 @@ package org.onosproject.ui.table;
23 * compares the resulting strings. Note that null values are acceptable and 23 * compares the resulting strings. Note that null values are acceptable and
24 * are considered "smaller" than any non-null value. 24 * are considered "smaller" than any non-null value.
25 */ 25 */
26 -public class DefaultCellComparator implements CellComparator { 26 +public class DefaultCellComparator extends AbstractCellComparator {
27 @Override 27 @Override
28 - public int compare(Object o1, Object o2) { 28 + protected int nonNullCompare(Object o1, Object o2) {
29 - if (o1 == null && o2 == null) {
30 - return 0; // o1 == o2
31 - }
32 - if (o1 == null) {
33 - return -1; // o1 < o2
34 - }
35 - if (o2 == null) {
36 - return 1; // o1 > o2
37 - }
38 return o1.toString().compareTo(o2.toString()); 29 return o1.toString().compareTo(o2.toString());
39 } 30 }
40 } 31 }
......
...@@ -15,14 +15,14 @@ ...@@ -15,14 +15,14 @@
15 * 15 *
16 */ 16 */
17 17
18 -package org.onosproject.ui.table; 18 +package org.onosproject.ui.table.cell;
19 19
20 /** 20 /**
21 * A default cell formatter. Uses the object's toString() method. 21 * A default cell formatter. Uses the object's toString() method.
22 */ 22 */
23 -public class DefaultCellFormatter implements CellFormatter { 23 +public class DefaultCellFormatter extends AbstractCellFormatter {
24 @Override 24 @Override
25 - public String format(Object value) { 25 + public String nonNullFormat(Object value) {
26 - return value == null ? "" : value.toString(); 26 + return value.toString();
27 } 27 }
28 } 28 }
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + *
16 + */
17 +
18 +package org.onosproject.ui.table.cell;
19 +
20 +/**
21 + * Formats integer values as hex strings.
22 + */
23 +public class HexFormatter extends AbstractCellFormatter {
24 + @Override
25 + protected String nonNullFormat(Object value) {
26 + return "0x" + Integer.toHexString((Integer) value);
27 + }
28 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + *
16 + */
17 +
18 +package org.onosproject.ui.table.cell;
19 +
20 +/**
21 + * An integer-based cell comparator.
22 + * Note that null values are acceptable and are considered "smaller" than
23 + * any non-null value.
24 + */
25 +public class IntComparator extends AbstractCellComparator {
26 + @Override
27 + protected int nonNullCompare(Object o1, Object o2) {
28 + return ((int) o1) - ((int) o2);
29 + }
30 +}
...@@ -18,6 +18,9 @@ package org.onosproject.ui.table; ...@@ -18,6 +18,9 @@ package org.onosproject.ui.table;
18 18
19 import org.junit.Test; 19 import org.junit.Test;
20 import org.onosproject.ui.table.TableModel.SortDir; 20 import org.onosproject.ui.table.TableModel.SortDir;
21 +import org.onosproject.ui.table.cell.DefaultCellFormatter;
22 +import org.onosproject.ui.table.cell.HexFormatter;
23 +import org.onosproject.ui.table.cell.IntComparator;
21 24
22 import static org.junit.Assert.*; 25 import static org.junit.Assert.*;
23 26
...@@ -26,22 +29,13 @@ import static org.junit.Assert.*; ...@@ -26,22 +29,13 @@ import static org.junit.Assert.*;
26 */ 29 */
27 public class TableModelTest { 30 public class TableModelTest {
28 31
29 - private static final String UNEX_SORT_ORDER = "unexpected sort: index "; 32 + private static final String UNEX_SORT = "unexpected sort: index ";
30 33
31 private static final String FOO = "foo"; 34 private static final String FOO = "foo";
32 private static final String BAR = "bar"; 35 private static final String BAR = "bar";
33 private static final String ZOO = "zoo"; 36 private static final String ZOO = "zoo";
34 37
35 - private static class TestCmpr implements CellComparator { 38 + private static class ParenFormatter implements CellFormatter {
36 - @Override
37 - public int compare(Object o1, Object o2) {
38 - int i1 = (int) o1;
39 - int i2 = (int) o2;
40 - return i1 - i2;
41 - }
42 - }
43 -
44 - private static class TestFmtr implements CellFormatter {
45 @Override 39 @Override
46 public String format(Object value) { 40 public String format(Object value) {
47 return "(" + value + ")"; 41 return "(" + value + ")";
...@@ -95,14 +89,14 @@ public class TableModelTest { ...@@ -95,14 +89,14 @@ public class TableModelTest {
95 @Test 89 @Test
96 public void altFormatter() { 90 public void altFormatter() {
97 tm = new TableModel(FOO, BAR); 91 tm = new TableModel(FOO, BAR);
98 - tm.setFormatter(BAR, new TestFmtr()); 92 + tm.setFormatter(BAR, new ParenFormatter());
99 93
100 fmt = tm.getFormatter(FOO); 94 fmt = tm.getFormatter(FOO);
101 assertTrue("Wrong formatter", fmt instanceof DefaultCellFormatter); 95 assertTrue("Wrong formatter", fmt instanceof DefaultCellFormatter);
102 assertEquals("Wrong result", "2", fmt.format(2)); 96 assertEquals("Wrong result", "2", fmt.format(2));
103 97
104 fmt = tm.getFormatter(BAR); 98 fmt = tm.getFormatter(BAR);
105 - assertTrue("Wrong formatter", fmt instanceof TestFmtr); 99 + assertTrue("Wrong formatter", fmt instanceof ParenFormatter);
106 assertEquals("Wrong result", "(2)", fmt.format(2)); 100 assertEquals("Wrong result", "(2)", fmt.format(2));
107 } 101 }
108 102
...@@ -174,6 +168,10 @@ public class TableModelTest { ...@@ -174,6 +168,10 @@ public class TableModelTest {
174 1, 2, 3, 4, 11, 12, 20, 30 168 1, 2, 3, 4, 11, 12, 20, 30
175 }; 169 };
176 170
171 + private static final String[] SORTED_HEX = {
172 + "0x1", "0x2", "0x3", "0x4", "0xb", "0xc", "0x14", "0x1e"
173 + };
174 +
177 @Test 175 @Test
178 public void verifyTestData() { 176 public void verifyTestData() {
179 // not a unit test per se, but will fail if we don't keep 177 // not a unit test per se, but will fail if we don't keep
...@@ -206,7 +204,7 @@ public class TableModelTest { ...@@ -206,7 +204,7 @@ public class TableModelTest {
206 int nr = rows.length; 204 int nr = rows.length;
207 assertEquals("row count", NAMES.length, nr); 205 assertEquals("row count", NAMES.length, nr);
208 for (int i = 0; i < nr; i++) { 206 for (int i = 0; i < nr; i++) {
209 - assertEquals(UNEX_SORT_ORDER + i, SORTED_NAMES[i], rows[i].get(FOO)); 207 + assertEquals(UNEX_SORT + i, SORTED_NAMES[i], rows[i].get(FOO));
210 } 208 }
211 209
212 // now the other way 210 // now the other way
...@@ -217,7 +215,7 @@ public class TableModelTest { ...@@ -217,7 +215,7 @@ public class TableModelTest {
217 nr = rows.length; 215 nr = rows.length;
218 assertEquals("row count", NAMES.length, nr); 216 assertEquals("row count", NAMES.length, nr);
219 for (int i = 0; i < nr; i++) { 217 for (int i = 0; i < nr; i++) {
220 - assertEquals(UNEX_SORT_ORDER + i, 218 + assertEquals(UNEX_SORT + i,
221 SORTED_NAMES[nr - 1 - i], rows[i].get(FOO)); 219 SORTED_NAMES[nr - 1 - i], rows[i].get(FOO));
222 } 220 }
223 } 221 }
...@@ -227,7 +225,7 @@ public class TableModelTest { ...@@ -227,7 +225,7 @@ public class TableModelTest {
227 initUnsortedTable(); 225 initUnsortedTable();
228 226
229 // first, tell the table to use an integer-based comparator 227 // first, tell the table to use an integer-based comparator
230 - tm.setComparator(BAR, new TestCmpr()); 228 + tm.setComparator(BAR, new IntComparator());
231 229
232 // sort by number 230 // sort by number
233 tm.sort(BAR, SortDir.ASC); 231 tm.sort(BAR, SortDir.ASC);
...@@ -237,7 +235,7 @@ public class TableModelTest { ...@@ -237,7 +235,7 @@ public class TableModelTest {
237 int nr = rows.length; 235 int nr = rows.length;
238 assertEquals("row count", NUMBERS.length, nr); 236 assertEquals("row count", NUMBERS.length, nr);
239 for (int i = 0; i < nr; i++) { 237 for (int i = 0; i < nr; i++) {
240 - assertEquals(UNEX_SORT_ORDER + i, SORTED_NUMBERS[i], rows[i].get(BAR)); 238 + assertEquals(UNEX_SORT + i, SORTED_NUMBERS[i], rows[i].get(BAR));
241 } 239 }
242 240
243 // now the other way 241 // now the other way
...@@ -248,12 +246,33 @@ public class TableModelTest { ...@@ -248,12 +246,33 @@ public class TableModelTest {
248 nr = rows.length; 246 nr = rows.length;
249 assertEquals("row count", NUMBERS.length, nr); 247 assertEquals("row count", NUMBERS.length, nr);
250 for (int i = 0; i < nr; i++) { 248 for (int i = 0; i < nr; i++) {
251 - assertEquals(UNEX_SORT_ORDER + i, 249 + assertEquals(UNEX_SORT + i,
252 SORTED_NUMBERS[nr - 1 - i], rows[i].get(BAR)); 250 SORTED_NUMBERS[nr - 1 - i], rows[i].get(BAR));
253 } 251 }
254 } 252 }
255 253
256 @Test 254 @Test
255 + public void sortAndFormat() {
256 + initUnsortedTable();
257 +
258 + // set integer-based comparator and hex formatter
259 + tm.setComparator(BAR, new IntComparator());
260 + tm.setFormatter(BAR, new HexFormatter());
261 +
262 + // sort by number
263 + tm.sort(BAR, SortDir.ASC);
264 +
265 + // verify results
266 + rows = tm.getRows();
267 + int nr = rows.length;
268 + assertEquals("row count", SORTED_HEX.length, nr);
269 + for (int i = 0; i < nr; i++) {
270 + assertEquals(UNEX_SORT + i, SORTED_HEX[i], rows[i].getAsString(BAR));
271 + }
272 + }
273 +
274 +
275 + @Test
257 public void sortDirAsc() { 276 public void sortDirAsc() {
258 assertEquals("asc sort dir", SortDir.ASC, TableModel.sortDir("asc")); 277 assertEquals("asc sort dir", SortDir.ASC, TableModel.sortDir("asc"));
259 } 278 }
......
...@@ -15,9 +15,10 @@ ...@@ -15,9 +15,10 @@
15 * 15 *
16 */ 16 */
17 17
18 -package org.onosproject.ui.table; 18 +package org.onosproject.ui.table.cell;
19 19
20 import org.junit.Test; 20 import org.junit.Test;
21 +import org.onosproject.ui.table.CellComparator;
21 22
22 import static org.junit.Assert.assertTrue; 23 import static org.junit.Assert.assertTrue;
23 24
......
...@@ -15,9 +15,10 @@ ...@@ -15,9 +15,10 @@
15 * 15 *
16 */ 16 */
17 17
18 -package org.onosproject.ui.table; 18 +package org.onosproject.ui.table.cell;
19 19
20 import org.junit.Test; 20 import org.junit.Test;
21 +import org.onosproject.ui.table.CellFormatter;
21 22
22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertEquals;
23 24
......
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + *
16 + */
17 +
18 +package org.onosproject.ui.table.cell;
19 +
20 +import org.junit.Test;
21 +
22 +import static org.junit.Assert.assertEquals;
23 +
24 +/**
25 + * Unit tests for {@link HexFormatter}.
26 + */
27 +public class HexFormatterTest {
28 +
29 + private HexFormatter fmt = new HexFormatter();
30 +
31 + @Test
32 + public void nullValue() {
33 + assertEquals("null value", "", fmt.format(null));
34 + }
35 +
36 + @Test
37 + public void zero() {
38 + assertEquals("zero", "0x0", fmt.format(0));
39 + }
40 +
41 + @Test
42 + public void one() {
43 + assertEquals("one", "0x1", fmt.format(1));
44 + }
45 +
46 + @Test
47 + public void ten() {
48 + assertEquals("ten", "0xa", fmt.format(10));
49 + }
50 +
51 + @Test
52 + public void twenty() {
53 + assertEquals("twenty", "0x14", fmt.format(20));
54 + }
55 +
56 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + *
16 + */
17 +
18 +package org.onosproject.ui.table.cell;
19 +
20 +import org.junit.Test;
21 +import org.onosproject.ui.table.CellComparator;
22 +
23 +import static org.junit.Assert.assertTrue;
24 +
25 +/**
26 + * Unit tests for {@link IntComparator}.
27 + */
28 +public class IntComparatorTest {
29 +
30 + private CellComparator cmp = new IntComparator();
31 +
32 + @Test
33 + public void twoNulls() {
34 + assertTrue("two nulls", cmp.compare(null, null) == 0);
35 + }
36 +
37 + @Test
38 + public void nullVsNegValue() {
39 + assertTrue("null vs neg value", cmp.compare(null, -5) < 0);
40 + }
41 +
42 + @Test
43 + public void nullVsPosValue() {
44 + assertTrue("null vs pos value", cmp.compare(null, 5) < 0);
45 + }
46 +
47 + @Test
48 + public void negValueVsNull() {
49 + assertTrue("neg value vs null", cmp.compare(-5, null) > 0);
50 + }
51 +
52 + @Test
53 + public void posValueVsNull() {
54 + assertTrue("pos value vs null", cmp.compare(5, null) > 0);
55 + }
56 +
57 +
58 + @Test
59 + public void smallVsBig() {
60 + assertTrue("small vs big", cmp.compare(25, 75) < 0);
61 + }
62 +
63 + @Test
64 + public void bigVsSmall() {
65 + assertTrue("big vs small", cmp.compare(75, 25) > 0);
66 + }
67 +
68 + @Test
69 + public void sameValue() {
70 + assertTrue("same value", cmp.compare(50, 50) == 0);
71 + }
72 +}