Ignore serializable warnings after Phosphorus bump
[transportpce.git] / pce / src / main / java / org / opendaylight / transportpce / pce / SortPortsByName.java
1 /*
2  * Copyright © 2017 Orange, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.pce;
9
10 import java.io.Serializable;
11 import java.util.Comparator;
12
13 /**
14  * Sort Ports by Name.
15  *
16  * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
17  *
18  */
19 @SuppressWarnings("serial")
20 @edu.umd.cs.findbugs.annotations.SuppressFBWarnings(
21     value = "SE_NO_SERIALVERSIONID",
22     justification = "https://github.com/rzwitserloot/lombok/wiki/WHY-NOT:-serialVersionUID")
23 public class SortPortsByName implements Comparator<String>, Serializable {
24
25     @Override
26     public int compare(String port1, String port2) {
27         int num = extractInt(port1) - extractInt(port2);
28         int letter = extractString(port1).compareToIgnoreCase(extractString(port2));
29         int diff = port1.length() - port2.length();
30         if ((diff == 0) || (Math.abs(diff) == 1)) {
31             return num;
32         } else {
33             return letter;
34         }
35     }
36
37     int extractInt(String name) {
38         String num = name.replaceAll("\\D", "");
39         // return 0 if no digits found
40         return num.isEmpty() ? 0 : Integer.parseInt(num);
41     }
42
43     String extractString(String name) {
44         String letter = name.replaceAll("\\d", "");
45         return (letter != null) ? letter : "";
46     }
47 }
48