-/**
- @header@
+/*
+ * (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
*/
-package org.pcmm.gates.impl;
-import java.net.Inet4Address;
-import java.net.InetAddress;
-import java.net.UnknownHostException;
+package org.pcmm.gates.impl;
import org.pcmm.base.impl.PCMMBaseObject;
import org.pcmm.gates.ISubscriberID;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
/**
- *
+ * Implementation of the ISubscriberID interface
*/
public class SubscriberID extends PCMMBaseObject implements ISubscriberID {
/**
- *
+ * The source IPv4 or IPv6 address
*/
- public SubscriberID() {
- this(LENGTH, STYPE, SNUM);
- }
+ private final InetAddress srcIp;
/**
- * @param data
+ * Constructor
+ * @param srcIp - the source host address
*/
- public SubscriberID(byte[] data) {
- super(data);
+ public SubscriberID(final InetAddress srcIp) {
+ super(SNum.SUBSCRIBER_ID, STYPE);
+ if (srcIp == null) throw new IllegalArgumentException("srcIp must not be null");
+ this.srcIp = srcIp;
}
- /**
- * @param len
- * @param sType
- * @param sNum
- */
- public SubscriberID(short len, byte sType, byte sNum) {
- super(len, sType, sNum);
+ @Override
+ public InetAddress getSourceIPAddress() {
+ return srcIp;
}
- /*
- * (non-Javadoc)
- *
- * @see org.pcmm.gates.ISubscriberID#getSourceIPAddress()
- */
@Override
- public InetAddress getSourceIPAddress() {
- try {
- return Inet4Address.getByAddress(getBytes((short) 0, (short) 4));
- } catch (UnknownHostException e) {
- return null;
+ protected byte[] getBytes() {
+ return srcIp.getAddress();
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (!(o instanceof SubscriberID)) {
+ return false;
}
+ if (!super.equals(o)) {
+ return false;
+ }
+ final SubscriberID that = (SubscriberID) o;
+ return srcIp.equals(that.srcIp);
}
- /*
- * (non-Javadoc)
- *
- * @see
- * org.pcmm.gates.ISubscriberID#setSourceIPAddress(java.net.InetAddress)
- */
@Override
- public void setSourceIPAddress(InetAddress address) {
- setBytes(address.getAddress(), (short) 0);
+ public int hashCode() {
+ int result = super.hashCode();
+ result = 31 * result + srcIp.hashCode();
+ return result;
}
+ /**
+ * Returns a SubscriberID object from a byte array
+ * @param data - the data to parse
+ * @return - the object or null if cannot be parsed
+ * TODO - make me more robust as RuntimeExceptions can be thrown here.
+ */
+ public static SubscriberID parse(final byte[] data) {
+ try {
+ return new SubscriberID(InetAddress.getByAddress(data));
+ } catch (UnknownHostException e) {
+ return null;
+ }
+ }
}