|
1 | 1 | /* |
2 | | - * Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 | 4 | * |
5 | 5 | * This code is free software; you can redistribute it and/or modify it |
|
35 | 35 | import java.util.LinkedList; |
36 | 36 | import java.util.List; |
37 | 37 | import java.util.Map; |
| 38 | +import java.util.Objects; |
38 | 39 | import java.util.function.Predicate; |
39 | 40 | import java.util.stream.Collectors; |
40 | 41 | import java.util.stream.Stream; |
@@ -92,6 +93,38 @@ private static boolean isIPv6LinkLocal(InetAddress a) { |
92 | 93 | return Inet6Address.class.isInstance(a) && a.isLinkLocalAddress(); |
93 | 94 | } |
94 | 95 |
|
| 96 | + /** |
| 97 | + * Returns true if the two interfaces point to the same network interface. |
| 98 | + * |
| 99 | + * @implNote |
| 100 | + * This method first looks at whether the two interfaces are |
| 101 | + * {@linkplain NetworkInterface#equals(Object) equals}, and if so it returns |
| 102 | + * true. Otherwise, it looks at whether the two interfaces have the same |
| 103 | + * {@linkplain NetworkInterface#getName() name} and |
| 104 | + * {@linkplain NetworkInterface#getIndex() index}, and if so returns true. |
| 105 | + * Otherwise, it returns false. |
| 106 | + * |
| 107 | + * @apiNote |
| 108 | + * This method ignores differences in the addresses to which the network |
| 109 | + * interfaces are bound, to cater for possible reconfiguration that might |
| 110 | + * have happened between the time at which each interface configuration |
| 111 | + * was looked up. |
| 112 | + * |
| 113 | + * @param ni1 A network interface, may be {@code null} |
| 114 | + * @param ni2 An other network interface, may be {@code null} |
| 115 | + * @return {@code true} if the two network interfaces have the same name |
| 116 | + * and index, {@code false} otherwise. |
| 117 | + */ |
| 118 | + public static boolean isSameInterface(NetworkInterface ni1, NetworkInterface ni2) { |
| 119 | + if (Objects.equals(ni1, ni2)) return true; |
| 120 | + // Objects equals has taken care of the case where |
| 121 | + // ni1 == ni2 so either they are both non-null or only |
| 122 | + // one of them is null - in which case they can't be equal. |
| 123 | + if (ni1 == null || ni2 == null) return false; |
| 124 | + if (ni1.getIndex() != ni2.getIndex()) return false; |
| 125 | + return Objects.equals(ni1.getName(), ni2.getName()); |
| 126 | + } |
| 127 | + |
95 | 128 | public static boolean isTestable(NetworkInterface nif) { |
96 | 129 | if (Platform.isOSX()) { |
97 | 130 | if (nif.getName().contains("awdl")) { |
|
0 commit comments