2 * (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
5 package org.pcmm.gates.impl;
7 import org.pcmm.base.impl.PCMMBaseObject;
8 import org.pcmm.gates.IPCMMError;
9 import org.umu.cops.stack.COPSMsgParser;
12 * Implementation of the IPCMMError interface
14 public class PCMMError extends PCMMBaseObject implements IPCMMError {
17 * The error code (cannot be NA)
19 private final ErrorCode errorCode;
22 * The error sub-code (defaults to NA)
24 private final ErrorCode subErrCode;
27 * Constructor without a sub-code which will then be set to NA
28 * @param errorCode - the error code (required and not NA)
30 public PCMMError(final ErrorCode errorCode) {
31 this(errorCode, null);
35 * Constructor with a sub-code
36 * @param errorCode - the error code (required and not NA)
37 * @param subErrCode - the sub-code (defaults to NA when null)
39 public PCMMError(final ErrorCode errorCode, final ErrorCode subErrCode) {
40 super(SNum.PCMM_ERROR, STYPE);
41 if (errorCode == null || errorCode.equals(ErrorCode.NA))
42 throw new IllegalArgumentException("ErrorCode is required and must not be NA");
43 this.errorCode = errorCode;
44 if (subErrCode == null) this.subErrCode = ErrorCode.NA;
45 else this.subErrCode = subErrCode;
49 public ErrorCode getErrorCode() {
54 public ErrorCode getErrorSubcode() {
61 * @see org.pcmm.gates.IPCError#getDescription()
64 public String getDescription() {
65 String hex = Integer.toHexString(subErrCode.getCode() & 0xFFFF);
66 return "Error Code: " + errorCode.getCode() + " Error Subcode : " + hex + " " + errorCode.getDescription();
70 public String toString() {
71 return getDescription();
75 protected byte[] getBytes() {
76 final byte[] errorCodeBytes = COPSMsgParser.shortToBytes(errorCode.getCode());
77 final byte[] subErrCodeBytes = COPSMsgParser.shortToBytes(subErrCode.getCode());
78 final byte[] data = new byte[errorCodeBytes.length + subErrCodeBytes.length];
79 System.arraycopy(errorCodeBytes, 0, data, 0, errorCodeBytes.length);
80 System.arraycopy(subErrCodeBytes, 0, data, errorCodeBytes.length, subErrCodeBytes.length);
85 public boolean equals(final Object o) {
89 if (!(o instanceof PCMMError)) {
92 if (!super.equals(o)) {
95 final PCMMError pcmmError = (PCMMError) o;
96 return errorCode == pcmmError.errorCode && subErrCode == pcmmError.subErrCode;
100 public int hashCode() {
101 int result = super.hashCode();
102 result = 31 * result + errorCode.hashCode();
103 result = 31 * result + subErrCode.hashCode();
108 * Returns a PCMMError object from a byte array
109 * @param data - the data to parse
110 * @return - the object
111 * TODO - make me more robust as RuntimeExceptions can be thrown here.
113 public static PCMMError parse(final byte[] data) {
114 return new PCMMError(ErrorCode.valueOf(COPSMsgParser.bytesToShort(data[0], data[1])),
115 ErrorCode.valueOf((COPSMsgParser.bytesToShort(data[2], data[3]))));