All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bardframework.commons.utils.IpUtils Maven / Gradle / Ivy

There is a newer version: 3.6.1
Show newest version
package org.bardframework.commons.utils;

import lombok.experimental.UtilityClass;

/**
 * @author [email protected]
 */
@UtilityClass
public class IpUtils {

    public static String formatIp(String ip) {
        StringBuilder builder = new StringBuilder();
        try {
            for (String num : ip.split("\\.")) {
                short part = Short.parseShort(num);
                if (part < 0) {
                    throw new IllegalArgumentException(ip);
                }
                builder.append(String.format("%03d", part));
                builder.append(".");
            }
            String finalIp = builder.substring(0, builder.length() - 1);
            if (finalIp.length() != 15) {
                throw new IllegalArgumentException(ip);
            }
            return finalIp;
        } catch (NumberFormatException | NullPointerException ex) {
            throw new IllegalArgumentException(String.format("error validating ip %s", ip), ex);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy