2 * Copyright (c) 2015 Cable Television Laboratories, Inc. and others. All rights reserved.
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
9 package org.pcmm.gates.impl;
11 import org.pcmm.base.impl.PCMMBaseObject;
12 import org.pcmm.gates.IPCMMError;
13 import org.umu.cops.stack.COPSMsgParser;
16 * Implementation of the IPCMMError interface
18 public class PCMMError extends PCMMBaseObject implements IPCMMError {
21 * The error code (cannot be NA)
23 private final ErrorCode errorCode;
26 * The error sub-code (defaults to NA)
28 private final ErrorCode subErrCode;
31 * Constructor without a sub-code which will then be set to NA
32 * @param errorCode - the error code (required and not NA)
34 public PCMMError(final ErrorCode errorCode) {
35 this(errorCode, null);
39 * Constructor with a sub-code
40 * @param errorCode - the error code (required and not NA)
41 * @param subErrCode - the sub-code (defaults to NA when null)
43 public PCMMError(final ErrorCode errorCode, final ErrorCode subErrCode) {
44 super(SNum.PCMM_ERROR, STYPE);
45 if (errorCode == null || errorCode.equals(ErrorCode.NA))
46 throw new IllegalArgumentException("ErrorCode is required and must not be NA");
47 this.errorCode = errorCode;
48 if (subErrCode == null) this.subErrCode = ErrorCode.NA;
49 else this.subErrCode = subErrCode;
53 public ErrorCode getErrorCode() {
58 public ErrorCode getErrorSubcode() {
65 * @see org.pcmm.gates.IPCError#getDescription()
68 public String getDescription() {
69 String hex = Integer.toHexString(subErrCode.getCode() & 0xFFFF);
70 return "Error Code: " + errorCode.getCode() + " Error Subcode : " + hex + " " + errorCode.getDescription();
74 public String toString() {
75 return getDescription();
79 protected byte[] getBytes() {
80 final byte[] errorCodeBytes = COPSMsgParser.shortToBytes(errorCode.getCode());
81 final byte[] subErrCodeBytes = COPSMsgParser.shortToBytes(subErrCode.getCode());
82 final byte[] data = new byte[errorCodeBytes.length + subErrCodeBytes.length];
83 System.arraycopy(errorCodeBytes, 0, data, 0, errorCodeBytes.length);
84 System.arraycopy(subErrCodeBytes, 0, data, errorCodeBytes.length, subErrCodeBytes.length);
89 public boolean equals(final Object o) {
93 if (!(o instanceof PCMMError)) {
96 if (!super.equals(o)) {
99 final PCMMError pcmmError = (PCMMError) o;
100 return errorCode == pcmmError.errorCode && subErrCode == pcmmError.subErrCode;
104 public int hashCode() {
105 int result = super.hashCode();
106 result = 31 * result + errorCode.hashCode();
107 result = 31 * result + subErrCode.hashCode();
112 * Returns a PCMMError object from a byte array
113 * @param data - the data to parse
114 * @return - the object
115 * TODO - make me more robust as RuntimeExceptions can be thrown here.
117 public static PCMMError parse(final byte[] data) {
118 return new PCMMError(ErrorCode.valueOf(COPSMsgParser.bytesToShort(data[0], data[1])),
119 ErrorCode.valueOf((COPSMsgParser.bytesToShort(data[2], data[3]))));