Here you can find the source of getNetworkInterfaces( Predicate super NetworkInterface> predicate)
Parameter | Description |
---|---|
predicate | the NetworkInterface Predicate |
public static List<NetworkInterface> getNetworkInterfaces( Predicate<? super NetworkInterface> predicate)
//package com.java2s; /*/*from w w w . j a va 2 s. co m*/ * File: NetworkHelper.java * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * The contents of this file are subject to the terms and conditions of * the Common Development and Distribution License 1.0 (the "License"). * * You may not use this file except in compliance with the License. * * You can obtain a copy of the License by consulting the LICENSE.txt file * distributed with this file, or by consulting https://oss.oracle.com/licenses/CDDL * * See the License for the specific language governing permissions * and limitations under the License. * * When distributing the software, include this License Header Notice in each * file and include the License file LICENSE.txt. * * MODIFICATIONS: * If applicable, add the following below the License Header, with the fields * enclosed by brackets [] replaced by your own identifying information: * "Portions Copyright [year] [name of copyright owner]" */ import java.net.NetworkInterface; import java.net.SocketException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Enumeration; import java.util.List; import java.util.function.Predicate; public class Main { /** * Obtains the list of {@link NetworkInterface}s (of the machine on which this code is executing), * ordered by {@link NetworkInterface#getIndex()}, that satisfy the specified {@link Predicate}. * * @param predicate the {@link NetworkInterface} {@link Predicate} * * @return a list of {@link NetworkInterface}s */ public static List<NetworkInterface> getNetworkInterfaces( Predicate<? super NetworkInterface> predicate) { ArrayList<NetworkInterface> networkInterfaces = new ArrayList<>(); try { // create the list of filtered network interfaces Enumeration<NetworkInterface> enumeration = NetworkInterface .getNetworkInterfaces(); while (enumeration.hasMoreElements()) { NetworkInterface networkInterface = enumeration .nextElement(); if (predicate.test(networkInterface)) { networkInterfaces.add(networkInterface); } } // sort the network interfaces by index Collections.sort(networkInterfaces, new Comparator<NetworkInterface>() { @Override public int compare( NetworkInterface networkInterface1, NetworkInterface networkInterface2) { return networkInterface1.getIndex() - networkInterface2.getIndex(); } }); } catch (SocketException e) { // nothing to do when we've had an exception } return networkInterfaces; } }