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