com.github.rxyor.common.util.lang2.RadixUtil Maven / Gradle / Ivy
package com.github.rxyor.common.util.lang2;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
/**
*
*
*
*
* @author liuyang
* @date 2019-06-04 Tue 16:52:00
* @since 1.0.0
*/
public class RadixUtil {
public final static char[] DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z'};
/**
* 支持的最小进制数
*/
public static final int MIN_RADIX = 2;
private char[] digits = DIGITS;
private Map digitMap = new HashMap<>(64);
private RadixUtil() {
}
private RadixUtil(char[] digits, Map digitMap) {
this.digits = digits;
this.digitMap = digitMap;
}
/**
* 支持的最大进制数
*/
public int getMaxRadix() {
return digits.length;
}
public static Builder builder() {
return new Builder();
}
public String convert2String(long i) {
return this.convert2String(i, this.getMaxRadix());
}
public String convert2String(long i, int radix) {
final int defaultRadix = 10;
if (radix < MIN_RADIX || radix > this.getMaxRadix()) {
radix = defaultRadix;
}
if (radix == defaultRadix) {
return Long.toString(i);
}
final int size = 65;
int charPos = 64;
char[] buf = new char[size];
boolean negative = (i < 0);
if (!negative) {
i = -i;
}
while (i <= -radix) {
buf[charPos--] = digits[(int) (-(i % radix))];
i = i / radix;
}
buf[charPos] = digits[(int) (-i)];
if (negative) {
buf[--charPos] = '-';
}
return new String(buf, charPos, (size - charPos));
}
public long convert2Number(String s) {
return this.convert2Number(s, this.getMaxRadix());
}
public long convert2Number(String s, int radix) {
Optional.ofNullable(s).orElseThrow(() -> new NumberFormatException("for input string: null"));
if (radix < MIN_RADIX) {
throw new NumberFormatException("radix " + radix
+ " less than " + this.getMaxRadix());
}
if (radix > this.getMaxRadix()) {
throw new NumberFormatException("radix " + radix
+ " greater than " + RadixUtil.MIN_RADIX);
}
long result = 0;
boolean negative = false;
int i = 0, len = s.length();
long limit = -Long.MAX_VALUE;
long multiplyMin;
Integer digit;
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') {
if (firstChar == '-') {
negative = true;
limit = Long.MIN_VALUE;
} else if (firstChar != '+') {
throw numberFormatException(s);
}
if (len == 1) {
throw numberFormatException(s);
}
i++;
}
multiplyMin = limit / radix;
while (i < len) {
digit = this.digitMap.get(s.charAt(i++));
if (digit == null) {
throw numberFormatException(s);
}
if (digit < 0) {
throw numberFormatException(s);
}
if (result < multiplyMin) {
throw numberFormatException(s);
}
result *= radix;
if (result < limit + digit) {
throw numberFormatException(s);
}
result -= digit;
}
} else {
throw numberFormatException(s);
}
return negative ? result : -result;
}
private NumberFormatException numberFormatException(String s) {
return new NumberFormatException("For input string: \"" + s + "\"");
}
/**
*
*Builder
*
*
* @author liuyang
* @date 2019-12-09 周一 21:39:43
* @since 1.0.0
*/
public static class Builder {
private char[] digits = DIGITS;
private Map digitMap = new HashMap<>(64);
public Builder digits(char[] digits) {
if (digits == null || digits.length == 0) {
throw new IllegalArgumentException("digits can't be empty");
}
this.digits = digits;
return this;
}
public RadixUtil build() {
this.buildDigitMap();
return new RadixUtil(digits, digitMap);
}
private void buildDigitMap() {
digitMap.clear();
for (int i = 0; i < digits.length; i++) {
digitMap.put(digits[i], i);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy