8a386f0e30b2934cdc19a68e681fb45786d54c7a
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / objects / SyncOptions.java
1 /*
2  * (c) 2015 Cable Television Laboratories, Inc.  All rights reserved.
3  */
4
5 package org.pcmm.objects;
6
7 import org.pcmm.base.impl.PCMMBaseObject;
8 import org.umu.cops.stack.COPSMsgParser;
9
10 /**
11  * PCMM SyncOptions object
12  */
13 public class SyncOptions extends PCMMBaseObject {
14
15         /**
16          * The requested report type
17          */
18         private final ReportType reportType;
19
20         /**
21          * The requested type of synchronization
22          */
23         private final SyncType syncType;
24
25         /**
26          * Constructor
27          * @param reportType - the requested report type
28          * @param syncType - the requested synchronization type
29          */
30         public SyncOptions(final ReportType reportType, final SyncType syncType) {
31                 super(SNum.SYNC_OPTS, (byte)1);
32                 if (reportType == null) throw new IllegalArgumentException("Report type must not be null");
33                 if (syncType == null) throw new IllegalArgumentException("Synchronization type must not be null");
34                 this.reportType = reportType;
35                 this.syncType = syncType;
36         }
37
38         /**
39          * @return the syncType
40          */
41         public SyncType getSyncType() {
42                 return syncType;
43         }
44
45         /**
46          * @return the reportType
47          */
48         public ReportType getReportType() {
49                 return reportType;
50         }
51
52         @Override
53         protected byte[] getBytes() {
54                 final byte[] rptTypeBytes = COPSMsgParser.shortToBytes(reportType.getValue());
55                 final byte[] syncTypeBytes = COPSMsgParser.shortToBytes(syncType.getValue());
56                 final byte[] data = new byte[rptTypeBytes.length + syncTypeBytes.length];
57                 System.arraycopy(rptTypeBytes, 0, data, 0, rptTypeBytes.length);
58                 System.arraycopy(syncTypeBytes, 0, data, rptTypeBytes.length, syncTypeBytes.length);
59                 return data;
60         }
61
62         @Override
63         public boolean equals(final Object o) {
64                 if (this == o) {
65                         return true;
66                 }
67                 if (!(o instanceof SyncOptions)) {
68                         return false;
69                 }
70                 if (!super.equals(o)) {
71                         return false;
72                 }
73                 final SyncOptions that = (SyncOptions) o;
74                 return reportType == that.reportType && syncType == that.syncType;
75         }
76
77         @Override
78         public int hashCode() {
79                 int result = super.hashCode();
80                 result = 31 * result + reportType.hashCode();
81                 result = 31 * result + syncType.hashCode();
82                 return result;
83         }
84
85         /**
86          * Returns a SyncOptions object from a byte array
87          * @param data - the data to parse
88          * @return - the object
89          * TODO - make me more robust as RuntimeExceptions can be thrown here.
90          */
91         public static SyncOptions parse(final byte[] data) {
92                 return new SyncOptions(ReportType.valueOf(COPSMsgParser.bytesToShort(data[0], data[1])),
93                                 SyncType.valueOf(COPSMsgParser.bytesToShort(data[2], data[3])));
94         }
95
96         /**
97          * The supported Report types
98          */
99         public enum ReportType {
100
101                 STANDARD_REPORT_DATA((short) 0), COMPLETE_GATE_DATA((short) 1);
102
103                 ReportType(short value) {
104                         this.value = value;
105                 }
106
107                 public short getValue() {
108                         return value;
109                 }
110
111                 public static ReportType valueOf(short v) {
112                         switch (v) {
113                                 case 0:
114                                         return ReportType.STANDARD_REPORT_DATA;
115                                 case 1:
116                                         return ReportType.COMPLETE_GATE_DATA;
117                                 default:
118                                         throw new IllegalArgumentException("not supported value");
119                         }
120                 }
121
122                 private short value;
123
124         }
125
126         /**
127          * The supported Synchronization types
128          */
129         public enum SyncType {
130
131                 FULL_SYNCHRONIZATION((short) 0), INCREMENTAL_SYNCHRONIZATION((short) 1);
132
133                 SyncType(short value) {
134                         this.value = value;
135                 }
136
137                 public short getValue() {
138                         return value;
139                 }
140
141                 public static SyncType valueOf(short v) {
142                         switch (v) {
143                                 case 0:
144                                         return SyncType.FULL_SYNCHRONIZATION;
145                                 case 1:
146                                         return SyncType.INCREMENTAL_SYNCHRONIZATION;
147                                 default:
148                                         throw new IllegalArgumentException("not supported value");
149                         }
150                 }
151
152                 private short value;
153
154         }
155
156 }