Marc De Leenheer | b0fb41d | 2015-12-03 22:16:53 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Marc De Leenheer | b0fb41d | 2015-12-03 22:16:53 -0800 | [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. |
| 15 | */ |
| 16 | package org.onosproject.net; |
| 17 | |
| 18 | import java.util.Comparator; |
HIGUCHI Yuta | ca8cb4e | 2015-12-11 13:57:05 -0800 | [diff] [blame] | 19 | import java.util.TreeSet; |
Marc De Leenheer | b0fb41d | 2015-12-03 22:16:53 -0800 | [diff] [blame] | 20 | |
| 21 | import static com.google.common.base.Preconditions.checkArgument; |
| 22 | import static com.google.common.base.Preconditions.checkNotNull; |
| 23 | |
| 24 | /** |
| 25 | * Comparator implementation for OchSignal. Assumes identical grid type and channel spacing. |
| 26 | */ |
| 27 | public class DefaultOchSignalComparator implements Comparator<OchSignal> { |
HIGUCHI Yuta | ca8cb4e | 2015-12-11 13:57:05 -0800 | [diff] [blame] | 28 | |
| 29 | private static final DefaultOchSignalComparator INSTANCE = new DefaultOchSignalComparator(); |
| 30 | |
| 31 | /** |
| 32 | * Creates a new instance of {@link TreeSet} using this Comparator. |
| 33 | * @return {@link TreeSet} |
| 34 | */ |
| 35 | public static TreeSet<OchSignal> newOchSignalTreeSet() { |
| 36 | return new TreeSet<>(INSTANCE); |
| 37 | } |
| 38 | |
Marc De Leenheer | b0fb41d | 2015-12-03 22:16:53 -0800 | [diff] [blame] | 39 | @Override |
| 40 | public int compare(OchSignal o1, OchSignal o2) { |
| 41 | checkNotNull(o1.gridType()); |
| 42 | checkNotNull(o1.channelSpacing()); |
| 43 | |
| 44 | checkArgument(o1.gridType().equals(o2.gridType())); |
| 45 | checkArgument(o1.channelSpacing().equals(o2.channelSpacing())); |
| 46 | |
| 47 | return o1.spacingMultiplier() * o1.slotGranularity() - o2.spacingMultiplier() * o2.slotGranularity(); |
| 48 | } |
| 49 | } |