2 * Copyright © 2017 Orange, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.pce;
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import java.io.Serializable;
12 import java.util.Comparator;
17 * @author Martial Coulibaly ( martial.coulibaly@gfi.com ) on behalf of Orange
20 @SuppressWarnings("serial")
22 value = "SE_NO_SERIALVERSIONID",
23 justification = "https://github.com/rzwitserloot/lombok/wiki/WHY-NOT:-serialVersionUID")
24 public class SortPortsByName implements Comparator<String>, Serializable {
27 public int compare(String port1, String port2) {
28 int num = extractInt(port1) - extractInt(port2);
29 int letter = extractString(port1).compareToIgnoreCase(extractString(port2));
30 int diff = port1.length() - port2.length();
31 if ((diff == 0) || (Math.abs(diff) == 1)) {
38 int extractInt(String name) {
39 String num = name.replaceAll("\\D", "");
40 // return 0 if no digits found
41 return num.isEmpty() ? 0 : Integer.parseInt(num);
44 String extractString(String name) {
45 String letter = name.replaceAll("\\d", "");
46 return (letter != null) ? letter : "";