Example usage for java.util Collections list

List of usage examples for java.util Collections list

Introduction

In this page you can find the example usage for java.util Collections list.

Prototype

public static <T> ArrayList<T> list(Enumeration<T> e) 

Source Link

Document

Returns an array list containing the elements returned by the specified enumeration in the order they are returned by the enumeration.

Usage

From source file:Main.java

@SuppressLint("DefaultLocale")
public static String getLocalHostAddress() {
    boolean useIPv4 = true;

    try {/*from  ww  w.jav a  2  s  .  com*/
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4) {
                            return sAddr;
                        }
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}

From source file:Main.java

public static String getLocalIpAddress(Context context) {
    try {/*from   ww  w .  j a  v a 2 s  .  c  o  m*/
        String ipv4;

        List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface ni : nilist) {
            if (ni.getName().toLowerCase().contains("usbnet"))
                continue;
            List<InetAddress> ialist = Collections.list(ni.getInetAddresses());
            for (InetAddress address : ialist) {
                if (!address.isLoopbackAddress()
                        && InetAddressUtils.isIPv4Address(ipv4 = address.getHostAddress())) {
                    return ipv4;
                }
            }

        }

    } catch (SocketException ex) {
        Log.d("socket_err", ex.toString());
    }
    return null;
}

From source file:Main.java

/**
 * Returns MAC address of the given interface name.
 *
 * @param interfaceName eth0, wlan0 or NULL=use first interface
 * @return mac address or empty string/* w  w  w  . j  a v  a2 s.  c  o m*/
 */
public static String getMACAddress(String interfaceName) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            byte[] mac = intf.getHardwareAddress();
            if (mac == null)
                return "";
            StringBuilder buf = new StringBuilder();
            for (int idx = 0; idx < mac.length; idx++)
                buf.append(String.format("%02X:", mac[idx]));
            if (buf.length() > 0)
                buf.deleteCharAt(buf.length() - 1);
            return buf.toString();
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
    /*try {
    // this is so Linux hack
    return loadFileAsString("/sys/class/net/" +interfaceName + "/address").toUpperCase().trim();
    } catch (IOException ex) {
    return null;
    }*/
}

From source file:Main.java

/**
 * Get IP address from first non-localhost interface
 *
 * @param useIPv4 true=return ipv4, false=return ipv6
 * @return address or empty string/*from   w w  w. j  a va 2s.com*/
 */
public static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress();
                    //boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    boolean isIPv4 = sAddr.indexOf(':') < 0;

                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 zone suffix
                            return delim < 0 ? sAddr.toUpperCase() : sAddr.substring(0, delim).toUpperCase();
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}

From source file:Main.java

/**
 * Get IP address from first non-localhost interface
 * @param ipv4  true=return ipv4, false=return ipv6
 * @return  address or empty string//www  . j ava2 s .c  o  m
 */
@SuppressLint("DefaultLocale")
public static String getIPAddress() {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (isIPv4)
                        return sAddr;
                }
            }
        }
    } catch (Exception ex) {
    }
    return "";
}

From source file:Main.java

/**
 * Get IP address from first non-localhost interface
 * @param useIPv4  true=return ipv4, false=return ipv6
 * @return  address or empty string/*w  w w. j  a va 2 s  .c o  m*/
 */
public static String getIPAddress(boolean useIPv4) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            List<InetAddress> addrs = Collections.list(intf.getInetAddresses());
            for (InetAddress addr : addrs) {
                if (!addr.isLoopbackAddress()) {
                    String sAddr = addr.getHostAddress().toUpperCase();
                    boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr);
                    if (useIPv4) {
                        if (isIPv4)
                            return sAddr;
                    } else {
                        if (!isIPv4) {
                            int delim = sAddr.indexOf('%'); // drop ip6 port suffix
                            return delim < 0 ? sAddr : sAddr.substring(0, delim);
                        }
                    }
                }
            }
        }
    } catch (Exception ex) {
    } // for now eat exceptions
    return "";
}

From source file:Main.java

/**
 * Convenience method for retrieving the UIDefault for a single property of
 * a particular class./*  w  ww .j  a v  a2 s  . c o m*/
 *
 * @param clazz the class of interest
 * @param property the property to query
 * @return the UIDefault property, or null if not found
 */
public static Object getUIDefaultOfClass(Class clazz, String property) {
    Object retVal = null;
    UIDefaults defaults = getUIDefaultsOfClass(clazz);
    List<Object> listKeys = Collections.list(defaults.keys());
    for (Object key : listKeys) {
        if (key.equals(property)) {
            return defaults.get(key);
        }
        if (key.toString().equalsIgnoreCase(property)) {
            retVal = defaults.get(key);
        }
    }
    return retVal;
}

From source file:Main.java

/**
 * Method by Adrian: [//from   www  . j av  a2s .  c  o  m
 * <a href="http://stackoverflow.com/a/15704264/5620200">StackOverflow</a> ]
 * & Mike: [ <a href=
 * "http://stackoverflow.com/questions/1542170/arranging-nodes-in-a-jtree">
 * StackOverflow</a> ]
 * 
 * @param node
 * @return
 */
@SuppressWarnings("unchecked")
public static DefaultMutableTreeNode sort(DefaultMutableTreeNode node) {
    List<DefaultMutableTreeNode> children = Collections.list(node.children());
    List<String> orgCnames = new ArrayList<String>();
    List<String> cNames = new ArrayList<String>();
    DefaultMutableTreeNode temParent = new DefaultMutableTreeNode();
    for (DefaultMutableTreeNode child : children) {
        DefaultMutableTreeNode ch = (DefaultMutableTreeNode) child;
        temParent.insert(ch, 0);
        String uppser = ch.toString().toUpperCase();
        // Not dependent on package name, so if duplicates are found
        // they will later on be confused. Adding this is of
        // very little consequence and fixes the issue.
        if (cNames.contains(uppser)) {
            uppser += "$COPY";
        }
        cNames.add(uppser);
        orgCnames.add(uppser);
        if (!child.isLeaf()) {
            sort(child);
        }
    }
    Collections.sort(cNames);
    for (String name : cNames) {
        int indx = orgCnames.indexOf(name);
        int insertIndex = node.getChildCount();
        node.insert(children.get(indx), insertIndex);
    }
    // Fixing folder placement
    for (int i = 0; i < node.getChildCount() - 1; i++) {
        DefaultMutableTreeNode child = (DefaultMutableTreeNode) node.getChildAt(i);
        for (int j = i + 1; j <= node.getChildCount() - 1; j++) {
            DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) node.getChildAt(j);
            if (!prevNode.isLeaf() && child.isLeaf()) {
                node.insert(child, j);
                node.insert(prevNode, i);
            }
        }
    }
    return node;
}

From source file:Main.java

private static void displayInterfaceInformation(NetworkInterface netint) throws SocketException {
    System.out.printf("Display name: %s%n", netint.getDisplayName());
    System.out.printf("Name: %s%n", netint.getName());
    Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
    for (InetAddress inetAddress : Collections.list(inetAddresses)) {
        System.out.printf("InetAddress: %s%n", inetAddress);
    }//from   w ww.  j a  v  a2s .co  m
    System.out.printf("%n");
}

From source file:eu.codebits.plasmas.util.NetworkInterfaces.java

/**
 *
 * @param interfaceName// w ww . ja va 2s  . c o m
 * @return
 */
public static String getMACAddress(String interfaceName) {
    try {
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (interfaceName != null) {
                if (!intf.getName().equalsIgnoreCase(interfaceName))
                    continue;
            }
            byte[] mac = intf.getHardwareAddress();
            if (mac == null)
                continue;//return "";
            StringBuilder buf = new StringBuilder();
            for (int idx = 0; idx < mac.length; idx++)
                buf.append(String.format("%02X:", mac[idx]));
            if (buf.length() > 0)
                buf.deleteCharAt(buf.length() - 1);
            return buf.toString();
        }
    } catch (SocketException ex) {
    }
    return "";
}