]> git.basschouten.com Git - openhab-addons.git/blob
50fd8568dbdd075fb28e15da84a8aa1b92d2cdfd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.dwdunwetter.internal.dto;
14
15 import java.util.Comparator;
16 import java.util.Objects;
17
18 /**
19  * Comperator to sort a Warning first by Severity, second by the onSet date.
20  *
21  * @author Martin Koehler - Initial contribution
22  */
23 public class SeverityComparator implements Comparator<DwdWarningData> {
24
25     @Override
26     public int compare(DwdWarningData o1, DwdWarningData o2) {
27         Comparator.comparingInt(d -> ((DwdWarningData) d).getSeverity().getOrder());
28         Comparator.comparing(DwdWarningData::getOnset);
29
30         int result = Integer.compare(o1.getSeverity().getOrder(), o2.getSeverity().getOrder());
31         if (result == 0) {
32             if (Objects.equals(o1.getOnset(), o2.getOnset())) {
33                 return 0;
34             } else if (o1.getOnset() == null) {
35                 return -1;
36             } else if (o2.getOnset() == null) {
37                 return 1;
38             }
39             return o1.getOnset().compareTo(o2.getOnset());
40         }
41         return result;
42     }
43 }