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