package ru.micord.ervu.account_applications.util;

/**
 * @author Adel Kalimullin
 */
public final class IpAddressUtils {

  private IpAddressUtils() {}

  public static int compareIpAddresses(String ip1, String ip2) {
    long ip1Long = ipToLong(ip1);
    long ip2Long = ipToLong(ip2);
    return Long.compare(ip1Long, ip2Long);
  }

  private static long ipToLong(String ip) {
    if (ip == null || ip.isEmpty() || !ip.matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}")) {
      return 0L;
    }

    String[] parts = ip.split("\\.");
    long result = 0;
    for (int i = 0; i < 4; i++) {
      result = result * 256 + Integer.parseInt(parts[i]);
    }
    return result;
  }
}
