//**************************************************** // Copyright (c) 2009 Cisco Systems, Inc. // All rights reserved. //**************************************************** package com.sheer.types.ipaddrfactory; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import com.sheer.framework.DataVersion; import com.sheer.types.IPAddress; import com.sheer.types.IPAddress.IpPart2String; public class InternalIPv4Address implements InternalIPAddress { private static final long serialVersionUID = DataVersion.CURRENT_VERSION; private static transient IpPart2String[] ipPart2StringMap = loadIpPart2String(); private static transient int type = IPAddress.IP_ADDR_TYPE_v4; int address = 0; //no need to serialize, is reconstructed upon first call to toString() private transient String strAddress = null; public InternalIPv4Address() { this(0); } public InternalIPv4Address(int address) { this.address = address; } public InternalIPv4Address(String addressString) { // check addressString on null if (addressString == null) { return; } if (addressString.equalsIgnoreCase("localhost")) { throw new IllegalArgumentException("IPAddress cannot resolve localhost. use IPAddressFactory instead"); } this.address = IPAddress.nameToInt(addressString); } public int getAddressAsInt() throws UnsupportedOperationException { return this.getAddress(); } public byte[] getHexAddress() { int temp = address; byte[] hex = new byte[4]; int i = 3; while (i >= 0) { hex[i] = (byte) (temp & 0xFF); i--; temp >>= 8; } return hex; } public int getType() { return type; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } InternalIPv4Address otherInternal = (InternalIPv4Address)obj; return otherInternal.getAddress() == this.getAddress(); } protected int getAddress() { return address; } @Override public int hashCode() { return this.getAddress(); } @Override public int compareTo(InternalIPAddress addr) { return compare(address, ((InternalIPv4Address)addr).getAddress()); } /** * @author Guy_Aloni * @param address1 * first IP address * @param address2 * second IP address * @return 0 if equal, 1 if address1 is greater then address2, -1 if address1 * is lower then address2 */ public static int compare(int address1, int address2) { if (address1 == address2) return 0;// equality if (((address1 ^ address2) >>> 31) == 0) { // both numbers have the same sign return (address1 > address2) ? 1 : -1; } // different sign return (address1 > address2) ? -1 : 1; } public static int compare(Object address1, Object address2) { return compare(((InternalIPv4Address)address1).getAddress(), ((InternalIPv4Address)address2).getAddress()); } public String toString() { if (strAddress == null) {// the first toString -> calc the string and hold it in strAddress strAddress = ipPart2StringMap[address >>> 24].first + // First octet ipPart2StringMap[(address >> 16) & 0xff].rest + // Second octet ipPart2StringMap[(address >> 8) & 0xff].rest + // Third octet ipPart2StringMap[address & 0xff].rest;// Fourth octet //brussabr - removing call to intern() - this call was found to badly hit performance //strAddress = strAddress.intern(); } return strAddress; } private static IpPart2String[] loadIpPart2String() { IpPart2String[] map = new IpPart2String[256]; for (int i = 0; i < 256; i++) map[i] = new IpPart2String(Integer.toString(i)); return map; } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.address = in.readInt(); } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeInt(address); } }