Simon Hunt | 3ee7f74 | 2015-05-05 10:18:20 -0700 | [diff] [blame] | 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 | } |