f45df91b3606fbee0011d52721da63188881aeea
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / gates / impl / GateID.java
1 /*
2  * (c) 2015 Cable Television Laboratories, Inc.  All rights reserved.
3  */
4
5 package org.pcmm.gates.impl;
6
7 import org.pcmm.base.impl.PCMMBaseObject;
8 import org.pcmm.gates.IGateID;
9 import org.umu.cops.stack.COPSMsgParser;
10
11 /**
12  * Implementation of the IGateID interface
13  */
14 public class GateID extends PCMMBaseObject implements IGateID {
15
16     /**
17      * The gate ID value
18      */
19     final int gateId;
20
21     /**
22      * Constructor
23      * @param gateId - the ID value
24      */
25     public GateID(final int gateId) {
26         super(SNum.GATE_ID, STYPE);
27         this.gateId = gateId;
28     }
29
30     @Override
31     public int getGateID() {
32         return gateId;
33     }
34
35     @Override
36     protected byte[] getBytes() {
37         return COPSMsgParser.intToBytes(gateId);
38     }
39
40     @Override
41     public boolean equals(final Object o) {
42         if (this == o) {
43             return true;
44         }
45         if (!(o instanceof GateID)) {
46             return false;
47         }
48         if (!super.equals(o)) {
49             return false;
50         }
51         final GateID gateID = (GateID) o;
52         return gateId == gateID.gateId;
53     }
54
55     @Override
56     public int hashCode() {
57         int result = super.hashCode();
58         result = 31 * result + gateId;
59         return result;
60     }
61
62     /**
63      * Returns a GateID object from a byte array
64      * @param data - the data to parse
65      * @return - the object
66      * TODO - make me more robust as RuntimeExceptions can be thrown here.
67      */
68     public static GateID parse(final byte[] data) {
69         return new GateID(COPSMsgParser.bytesToInt(data[0], data[1], data[2], data[3]));
70     }
71 }