固定长度格式化


/**
* 固定长度格式化器
*
* @author wuyuhou
*
*/
public class FixedLengthFormatter {

//对齐方式
public static enum Alignment {
LEFT,//左对齐
MIDDLE,//居中
RIGHT//右对齐
}

//对齐方式,默认左对齐
private Alignment alignment = Alignment.LEFT;

//长度
private int fixedLength = 0;

//填充符号
private String fillSymbol = null;

//是否以字节长度计算,默认是以字符长度计算
private boolean isByteMode = false;

public FixedLengthFormatter() {
}

public Alignment getAlignment() {
return alignment;
}

public void setAlignment(Alignment alignment) {
if (alignment == null) {
throw new IllegalArgumentException("Alignment is null!");
}
this.alignment = alignment;
}

public String getFillSymbol() {
return fillSymbol;
}

public void setFillSymbol(String fillSymbol) {
if (fillSymbol == null) {
throw new IllegalArgumentException("fillSymbol is null!");
}
if (fillSymbol.length() > 1) {
throw new IllegalArgumentException("fillSymbol length > 1");
}
this.fillSymbol = fillSymbol;
}

public int getFixedLength() {
return fixedLength;
}

public void setFixedLength(int fixedLength) {
if (fixedLength == 0) {
throw new IllegalArgumentException("fixedLength is zero!");
}
this.fixedLength = fixedLength;
}

public boolean isByteMode() {
return isByteMode;
}

public void setByteMode(boolean isByteMode) {
this.isByteMode = isByteMode;
}

protected void doCheck() {
if (fixedLength == 0) {
throw new FormatRuntimeException("FixedLength is zero, please set it!");
}
if (fillSymbol == null) {
throw new FormatRuntimeException("FillSymbol is null, please set it!");
}
}

public String format(String data) {
int actualLength = 0;
if (isByteMode) {
actualLength = data.getBytes().length;
} else {
actualLength = data.length();
}

//需要填充的长度
int fillLength = fixedLength - actualLength;

if (fillLength == 0) {
return data;
} else if (fillLength < 0) {
//截取
if (isByteMode) {
if (actualLength / 2 > fixedLength) {
for (int i = 1; i < data.length(); i++) {
if (data.substring(0, i).getBytes().length > fixedLength) {
return data.substring(0, i - 1);
}
}
} else {
for (int i = data.length() - 2; i >= 0; i--) {
String result = data.substring(0, i);
if (result.getBytes().length <= fixedLength) {
return result;
}
}
}
} else {
return data.substring(0, fixedLength);
}
} else {
int fillSymbolByteLength = fillSymbol.getBytes().length;
int halfFillLength = fillLength / 2;
//取左边的一半
StringBuilder bufLeftHalf = new StringBuilder();
for (int i = 0; i < halfFillLength; i++) {
if (isByteMode) {
if (fillSymbolByteLength * (i + 1) > halfFillLength) {
break;
}
}
bufLeftHalf.append(fillSymbol);
}
int leftByteLength = bufLeftHalf.toString().getBytes().length;
//右边的一半
StringBuilder bufRightHalf = new StringBuilder();
for (int i = halfFillLength; i < fillLength; i++) {
if (isByteMode) {
if (fillSymbolByteLength * (i - halfFillLength + 1) + leftByteLength > fillLength) {
break;
}
}
bufRightHalf.append(fillSymbol);
}
//补齐
if (Alignment.LEFT.equals(alignment)) {
return data + bufLeftHalf.append(bufRightHalf.toString()).toString();
} else if (Alignment.RIGHT.equals(alignment)) {
return bufLeftHalf.append(bufRightHalf.toString()).append(data).toString();
} else {
return bufLeftHalf.append(data).append(bufRightHalf.toString()).toString();
}
}
throw new FormatRuntimeException("Does not format data[{0}]!", new Object[]{data});
}

public String unformat(String data) {
int actualLength = 0;
if (isByteMode) {
actualLength = data.getBytes().length;
} else {
actualLength = data.length();
}
if (actualLength > fixedLength) {
throw new FormatRuntimeException("Data[{0}] length[{1}] is greater than fixedLength[{2}]!",
new Object[]{data, actualLength, fixedLength});
}
if (Alignment.LEFT.equals(alignment)) {
int index = data.length() - 1;
for (; index >= 0; index--) {
if (!fillSymbol.equals(data.substring(index, index + 1))) {
break;
}
}
return data.substring(0, index + 1);
} else if (Alignment.RIGHT.equals(alignment)) {
int index = 0;
for (; index < data.length(); index++) {
if (!fillSymbol.equals(data.substring(index, index + 1))) {
break;
}
}
return data.substring(index);
} else {
int index = 0;
for (; index < data.length(); index++) {
if (!fillSymbol.equals(data.substring(index, index + 1))) {
break;
}
}
data = data.substring(index);
index = data.length() - 1;
for (; index >= 0; index--) {
if (!fillSymbol.equals(data.substring(index, index + 1))) {
break;
}
}
return data.substring(0, index + 1);
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值