Skip to content

Commit 1bf386f

Browse files
committed
Add NetworkInterface.fromJava
1 parent b7bbf6c commit 1bf386f

File tree

4 files changed

+59
-16
lines changed

4 files changed

+59
-16
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2018 Comcast Cable Communications Management, LLC
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+
17+
package com.comcast.ip4s
18+
19+
private[ip4s] trait NetworkInterfaceCompanionPlatform
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2018 Comcast Cable Communications Management, LLC
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+
17+
package com.comcast.ip4s
18+
19+
import java.net.{InterfaceAddress as JInterfaceAddress, NetworkInterface as JNetworkInterface}
20+
21+
import CollectionCompat.*
22+
23+
private[ip4s] trait NetworkInterfaceCompanionPlatform {
24+
25+
def fromJava(jni: JNetworkInterface): NetworkInterface =
26+
NetworkInterface(
27+
jni.getName,
28+
jni.getDisplayName,
29+
Option(jni.getHardwareAddress).flatMap(MacAddress.fromBytes),
30+
jni.getInterfaceAddresses.asScala.toList.map(fromJavaInterfaceAddress),
31+
jni.isLoopback,
32+
jni.isUp
33+
)
34+
35+
private def fromJavaInterfaceAddress(ifa: JInterfaceAddress): Cidr[IpAddress] =
36+
Cidr(IpAddress.fromInetAddress(ifa.getAddress), ifa.getNetworkPrefixLength.toInt)
37+
}

jvm/src/main/scala/com/comcast/ip4s/NetworkInterfacesPlatform.scala

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
package com.comcast.ip4s
1818

1919
import cats.effect.Async
20-
import java.net.{InterfaceAddress as JInterfaceAddress, NetworkInterface as JNetworkInterface}
20+
import java.net.NetworkInterface as JNetworkInterface
2121

2222
import CollectionCompat.*
2323

@@ -29,21 +29,8 @@ private[ip4s] trait NetworkInterfacesCompanionPlatform {
2929
F.blocking {
3030
collection.immutable.ListMap.empty[String, NetworkInterface] ++
3131
JNetworkInterface.getNetworkInterfaces.asScala.toList.map(jni =>
32-
jni.getName -> fromJavaNetworkInterface(jni)
32+
jni.getName -> NetworkInterface.fromJava(jni)
3333
)
3434
}
35-
36-
private def fromJavaNetworkInterface(jni: JNetworkInterface): NetworkInterface =
37-
NetworkInterface(
38-
jni.getName,
39-
jni.getDisplayName,
40-
Option(jni.getHardwareAddress).flatMap(MacAddress.fromBytes),
41-
jni.getInterfaceAddresses.asScala.toList.map(fromJavaInterfaceAddress),
42-
jni.isLoopback,
43-
jni.isUp
44-
)
45-
46-
private def fromJavaInterfaceAddress(ifa: JInterfaceAddress): Cidr[IpAddress] =
47-
Cidr(IpAddress.fromInetAddress(ifa.getAddress), ifa.getNetworkPrefixLength.toInt)
4835
}
4936
}

shared/src/main/scala/com/comcast/ip4s/NetworkInterface.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ sealed trait NetworkInterface {
4343
def isUp: Boolean
4444
}
4545

46-
object NetworkInterface {
46+
object NetworkInterface extends NetworkInterfaceCompanionPlatform {
4747

4848
def apply(
4949
name: String,

0 commit comments

Comments
 (0)