Java Network Interface Get getNetworkInterfaces( Predicate predicate)

Here you can find the source of getNetworkInterfaces( Predicate predicate)

Description

Obtains the list of NetworkInterface s (of the machine on which this code is executing), ordered by NetworkInterface#getIndex() , that satisfy the specified Predicate .

License

CDDL license

Parameter

Parameter Description
predicate the NetworkInterface Predicate

Return

a list of s

Declaration

public static List<NetworkInterface> getNetworkInterfaces(
        Predicate<? super NetworkInterface> predicate) 

Method Source Code

//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;
    }
}

Related

  1. getNetworkInterface()
  2. getNetworkInterface(String name)
  3. getNetworkInterfaceArray(String name)
  4. getNetworkinterfaceByName(String name)
  5. getNetworkInterfaceInfo(NetworkInterface pNif)
  6. getNetworkInterfaces()
  7. getNetworkInterfaces()
  8. getNetworkInterfaces()
  9. getNetworkInterfaces()