Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 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. |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package org.onosproject.ui.table.cell; |
| 18 | |
| 19 | import 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 | */ |
| 26 | public 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 |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 45 | * to, or greater than the second. |
| 46 | * <p> |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 47 | * Note that both objects are guaranteed to be non-null. |
| 48 | * |
| 49 | * @see java.util.Comparator#compare(Object, Object) |
| 50 | * |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 51 | * @param o1 the first object to be compared |
| 52 | * @param o2 the second object to be compared |
| 53 | * @return an integer representing relative ordering |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 54 | * @throws ClassCastException if the arguments' types prevent them from |
Simon Hunt | b058249 | 2016-09-20 18:26:38 -0700 | [diff] [blame] | 55 | * being compared by this comparator |
Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 56 | */ |
| 57 | protected abstract int nonNullCompare(Object o1, Object o2); |
| 58 | } |