/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.perforce.p4java.extension.utility;
import com.perforce.p4java.exception.MessageSeverityCode;
import com.perforce.p4java.impl.mapbased.rpc.msg.RpcMessage;
public class ErrorId {
private final int sub;
private final int gen;
private final int sev;
private final int cod;
private final int arg;
private final String format;
/**
* server error code format is mostly returned by p4java.
* <p/>
* Bit format is:
*
* <pre>
* | severity(4) | arg COUNT(4) | generic code(8) | subsystem(6) | error code(10) |
* </pre>
*/
public ErrorId(String code) {
sev = RpcMessage.getSeverity(code); // 4 bits
arg = (Integer.valueOf(code) >> 24) & 0x0f; // 4 bits
gen = RpcMessage.getGeneric(code); // 8 bits
sub = RpcMessage.getSubsystem(code); // 6 bits
cod = Integer.valueOf(code) & 0x3ff; // 10bits
format = null;
}
public ErrorId(ErrorId e, String fmt) {
this.sub = e.sub;
this.arg = e.arg;
this.cod = e.cod;
this.sev = e.sev;
this.gen = e.gen;
format = fmt;
}
public ErrorId(int sub, int cod, int sev, int gen, int arg, String fmt) {
this.sub = sub;
this.arg = arg;
this.cod = cod;
this.sev = sev;
this.gen = gen;
format = fmt;
}
public int getSubsystem() {
return sub;
}
public int getSeverity() {
return sev;
}
public int getGeneric() {
return gen;
}
public int getCode() {
return cod;
}
public int getArg() {
return arg;
}
public String getFormat() {
return format;
}
@Override
public String toString() {
return String.valueOf(hashCode());
}
public boolean match(String code) {
if (code == null || !code.trim().matches("\\d+"))
return false;
return this.equals(new ErrorId(code.trim()));
}
@Override
public boolean equals(Object obj) {
if (obj instanceof ErrorId) {
// only compare the subsystem/code/generics, the rest can change
// from server version to server version
ErrorId id = (ErrorId) obj;
return sev != MessageSeverityCode.E_EMPTY
&& getSubsystem() == id.getSubsystem()
&& getCode() == id.getCode()
&& getGeneric() == id.getGeneric();
}
return false;
}
@Override
public int hashCode() {
return (sev << 28) | (arg << 24) | (gen << 16) | (sub << 10) | cod;
}
}