blob: b042c8505434d5f49376af5e0ba9b741700736d4 [file] [log] [blame]
Simon Hunt3ee7f742015-05-05 10:18:20 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunt3ee7f742015-05-05 10:18:20 -07003 *
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.
Simon Hunt3ee7f742015-05-05 10:18:20 -070015 */
16
17package org.onosproject.ui.table.cell;
18
19import org.onosproject.ui.table.CellComparator;
20
21/**
22 * Base implementation of a {@link CellComparator}. This class takes care
23 * of dealing with null inputs; subclasses should implement their comparison
24 * knowing that both inputs are guaranteed to be non-null.
25 */
26public abstract class AbstractCellComparator implements CellComparator {
27
28 @Override
29 public int compare(Object o1, Object o2) {
30 if (o1 == null && o2 == null) {
31 return 0; // o1 == o2
32 }
33 if (o1 == null) {
34 return -1; // o1 < o2
35 }
36 if (o2 == null) {
37 return 1; // o1 > o2
38 }
39 return nonNullCompare(o1, o2);
40 }
41
42 /**
43 * Compares its two arguments for order. Returns a negative integer,
44 * zero, or a positive integer as the first argument is less than, equal
45 * to, or greater than the second.<p>
46 *
47 * Note that both objects are guaranteed to be non-null.
48 *
49 * @see java.util.Comparator#compare(Object, Object)
50 *
51 * @param o1 the first object to be compared.
52 * @param o2 the second object to be compared.
53 * @return a negative integer, zero, or a positive integer as the
54 * first argument is less than, equal to, or greater than the
55 * second.
56 * @throws ClassCastException if the arguments' types prevent them from
57 * being compared by this comparator.
58 */
59 protected abstract int nonNullCompare(Object o1, Object o2);
60}