Skip to content

Commit

Permalink
Merge branch 'feature/err-polish' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Chan committed Apr 7, 2024
2 parents 6e5c79d + a1d8a6a commit d593a60
Show file tree
Hide file tree
Showing 6 changed files with 430 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class IntErr extends ErrValue<Integer> implements Err {

IntErr(int code, @Nullable String message, @Nullable Err source) {
protected IntErr(int code, @Nullable String message, @Nullable Err source) {
super(code, message, source);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class StrErr extends ErrValue<String> implements Err {

StrErr(String code, @Nullable String message, @Nullable Err source) {
protected StrErr(String code, @Nullable String message, @Nullable Err source) {
super(code, message, source);
}

Expand Down
34 changes: 17 additions & 17 deletions tile-util/src/main/java/com/power4j/tile/codec/BCD.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public class BCD {
* </pre>
* @param value Number that needs to be converted
* @return BCD encoded number
* @throws IllegalArgumentException if input is not a number
* @throws EncodeException if input is not a number
*/
public static byte[] encode(String value) {
if (!isBcdString(value)) {
throw new IllegalArgumentException("Can only encode numerical strings");
throw new EncodeException("Can only encode numerical strings");
}

final byte[] bcd = new byte[(value.length() + 1) / COMPRESS];
Expand All @@ -70,15 +70,15 @@ public static byte[] encode(String value) {
/**
* Encode value to BCD encoded bytes (big endian) <pre>
* encode(BigInteger.ZERO) -> [0x0]
* BCD.encode(BigInteger.ONE.negate()) -> error
* BCD.encode(BigInteger.ONE.negate()) -> EncodeException
* </pre>
* @param value number
* @return BCD encoded number
* @throws IllegalArgumentException if input is negative
* @throws EncodeException if input is negative
*/
public static byte[] encode(BigInteger value) {
if (value.signum() == -1) {
throw new IllegalArgumentException("Only non-negative values are supported");
throw new EncodeException("Only non-negative values are supported");
}
if (value.bitLength() > 63) {
return encode(value.toString());
Expand All @@ -92,19 +92,19 @@ public static byte[] encode(BigInteger value) {
* Encode value to BCD encoded bytes (big endian) in a byte array of a specific length
* <pre>
* encode(BigInteger.ZERO,2) -> [0x00, 0x00]
* BCD.encode(BigInteger.valueOf(100), 1) -> error
* BCD.encode(BigInteger.valueOf(100), 1) -> EncodeException
* </pre>
* @param value number
* @param length length of the byte array
* @return BCD encoded number
* @throws IllegalArgumentException if input is negative or does not fit in byte array
* @throws EncodeException if input is negative or does not fit in byte array
*/
public static byte[] encode(BigInteger value, int length) {
if (value.signum() == -1) {
throw new IllegalArgumentException("Only non-negative values are supported");
throw new EncodeException("Only non-negative values are supported");
}
else if (value.bitLength() > length * Byte.SIZE) {
throw new IllegalArgumentException("Value does not fit in byte array of length" + length);
throw new EncodeException("Value does not fit in byte array of length" + length);
}
if (value.bitLength() > 63) {
return encode(String.format("%0" + (length * 2) + "d", value));
Expand All @@ -123,11 +123,11 @@ else if (value.bitLength() > length * Byte.SIZE) {
* @param value number
* @param length length of the byte array
* @return BCD encoded number
* @throws IllegalArgumentException if input is negative or does not fit in byte array
* @throws EncodeException if input is negative or does not fit in byte array
*/
public static byte[] encode(long value, int length) {
if (value < 0) {
throw new IllegalArgumentException("Only non-negative values are supported");
throw new EncodeException("Only non-negative values are supported");
}
else if (value == 0) {
return new byte[length];
Expand Down Expand Up @@ -173,12 +173,12 @@ else if (value == 0) {
* BCD.decode(new byte[] { 0x02, 0x31 }).intValue() -> 231
* BCD.decode(new byte[] { 0x31 }).intValue() -> 31
* BCD.decode(new byte[] { 0x0 }).intValue() -> 0
* BCD.decode(new byte[] {-48 }) -> error
* BCD.decode(new byte[] { 0x0d }) -> error
* BCD.decode(new byte[] {-48 }) -> DecodeException
* BCD.decode(new byte[] { 0x0d }) -> DecodeException
* </pre>
* @param bcd BCD encoded bytes
* @return encoded number
* @throws IllegalArgumentException if an illegal byte is detected
* @throws DecodeException if an illegal byte is detected
*/
public static BigInteger decode(byte[] bcd) {
BigInteger value = BigInteger.ZERO;
Expand All @@ -187,7 +187,7 @@ public static BigInteger decode(byte[] bcd) {
final int low = (int) bcd[i] & 0xF;

if (high > MAX_NUMBER || low > MAX_NUMBER) {
throw new IllegalArgumentException(String.format("Illegal byte %x%x at %d", high, low, i));
throw new DecodeException(String.format("Illegal byte %x%x at %d", high, low, i));
}

value = value.multiply((BigInteger.TEN)).add(BigInteger.valueOf(high));
Expand All @@ -207,7 +207,7 @@ public static BigInteger decode(byte[] bcd) {
* @param bcd BCD encoded bytes
* @param stripLeadingZero strip leading zero if value is of odd length
* @return encoded number as String
* @throws IllegalArgumentException if an illegal byte is detected
* @throws DecodeException if an illegal byte is detected
*/
public static String decodeAsString(byte[] bcd, boolean stripLeadingZero) {
final StringBuilder buf = new StringBuilder(bcd.length * 2);
Expand All @@ -216,7 +216,7 @@ public static String decodeAsString(byte[] bcd, boolean stripLeadingZero) {
final int low = (int) bcd[i] & 0xF;

if (high > MAX_NUMBER || low > MAX_NUMBER) {
throw new IllegalArgumentException(String.format("Illegal byte %x%x at %d", high, low, i));
throw new DecodeException(String.format("Illegal byte %x%x at %d", high, low, i));
}
buf.append((char) (0x30 | high));
buf.append((char) (0x30 | low));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.power4j.tile.codec;

/**
* @author CJ ([email protected])
* @since 1.0
*/
public class DecodeException extends RuntimeException {

public DecodeException(String message) {
super(message);
}

public DecodeException(String message, Throwable cause) {
super(message, cause);
}

public DecodeException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2019-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.power4j.tile.codec;

/**
* @author CJ ([email protected])
* @since 1.0
*/
public class EncodeException extends RuntimeException {

public EncodeException(String message) {
super(message);
}

public EncodeException(String message, Throwable cause) {
super(message, cause);
}

public EncodeException(Throwable cause) {
super(cause);
}

}
Loading

0 comments on commit d593a60

Please sign in to comment.