固定長の書式設定

5912 ワード


/**
 *  
 * 
 * @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);
		}
	}
}