oraclenumberformat.java

来自「mysql集群」· Java 代码 · 共 671 行 · 第 1/2 页

JAVA
671
字号
/*****************************************************************************      SQLJEP - Java SQL Expression Parser 0.2      November 1 2006         (c) Copyright 2006, Alexey Gaidukov      SQLJEP Author: Alexey Gaidukov      SQLJEP is based on JEP 2.24 (http://www.singularsys.com/jep/)           (c) Copyright 2002, Nathan Funk       See LICENSE.txt for license information.*****************************************************************************/package com.meidusa.amoeba.sqljep.function;import java.util.Hashtable;import java.text.*;import java.math.*;public final class OracleNumberFormat extends Format {	private static final long serialVersionUID = 1L;	private static final String PATTERN_EXCEPTION = "Wrong pattern";	public static final String NOT_IMPLIMENTED_EXCEPTION = "Not implimented";	final static char DOT = '.';	final static char GROUP = ' ';	private static final Hashtable<String, FORMAT> formatsCache = new Hashtable<String, FORMAT>();	private FORMAT format = null;		public static enum SIGN {		DEFAULT,		MI,		_S,		S_,		PR	};	public static enum CURRENCY {		NON,		DOLLARS,		LOCAL,		ISO	};	private static class FORMAT {		boolean sci;		boolean localGroups = true;		// for D and G  		String numbers = "";		int digits = 0; 				// number of digits in 'numbers' member		int firstNine = -1;			// position in 'numbers' member the first 9 digit		SIGN sign = SIGN.DEFAULT;		boolean fm = false;		boolean b = false;		CURRENCY cur = CURRENCY.NON;		int v = 0;		int dot = 0;		public void getPrefix(StringBuffer s, BigDecimal n) {			if (sci && !fm) {				s.append(' ');			}			if (n.signum() == 1) {				if (sign == SIGN.S_) {					s.append('+');				}				else if (!fm && sign != SIGN._S && sign != SIGN.MI) {					s.append(' ');				}			}			else if (n.signum() == -1) {				if (sign == SIGN.PR) {					s.append('<');				}				else if (sign != SIGN._S && sign != SIGN.MI) {					s.append('-');				}			}			else if (!fm) {				s.append(' ');			}			if (cur == CURRENCY.DOLLARS) {				s.append("$");			}	//			else if (cur == CURRENCY.LOCAL) {	//				s.append("L");	//			}			else if (cur == CURRENCY.ISO) {				s.append("RUR");			}		}			public void getSuffix(StringBuffer s, BigDecimal n) {			if (cur == CURRENCY.LOCAL) {				s.append("痼?");			}			if (n.signum() == 1) {				if (sign == SIGN._S) {					s.append('+');				}				else if (sign == SIGN.MI) {					s.append(' ');				}			}			else if (n.signum() == -1) {				if (sign == SIGN.PR) {					s.append('>');				}				else if (sign == SIGN.MI || sign == SIGN._S) {					s.append('-');				}			}		}				public String toString() {			StringBuilder s = new StringBuilder();			if (sign == SIGN.S_) {				s.append('S');			}			if (fm) {				s.append("FM");			}			if (b) {				s.append('B');			}			if (cur == CURRENCY.DOLLARS) {				s.append('$');			}			else if (cur == CURRENCY.LOCAL) {				s.append('L');			}			else if (cur == CURRENCY.ISO) {				s.append('C');			}			s.append(numbers);			if (dot > 0) {				s.append('D');				for (int i = 0; i < dot; i++) {					s.append('9');				}			}			else if (v > 0) {				s.append('V');				for (int i = 0; i < v; i++) {					s.append('9');				}			}			if (sci) {				s.append("EEEE");			}			if (sign == SIGN._S) {				s.append('S');			}			else if (sign == SIGN.MI) {				s.append("MI");			}			return s.toString();		}				public boolean equals(Comparable<?>  obj) {			if (obj == null) {				return false;			}			FORMAT other = (FORMAT)obj;			return (sci == other.sci && 					localGroups == other.localGroups && 					numbers.equals(other.numbers) &&					digits == other.digits &&					firstNine == other.firstNine &&					sign == other.sign &&					fm == other.fm &&					b == other.b &&					cur == other.cur &&					v == other.v &&					dot == other.dot);		}	};		public OracleNumberFormat(String pattern) throws java.text.ParseException {		format = formatsCache.get(pattern);		if (format == null) {			format = compilePattern(pattern);			formatsCache.put(pattern, format);		}	}		public String toString() {		return (format != null) ? format.toString() : "null";	}	public boolean equals(Object obj) {		if (obj == null) {			return false;		}		if (this == obj) {			return true;		}		if (getClass() != obj.getClass()) {			return false;		}		OracleNumberFormat other = (OracleNumberFormat)obj;		return (format != null) ? format.equals(other.format) : false;	}	private static FORMAT compilePattern(String pattern) throws java.text.ParseException {		if (pattern == null) {			throw new java.text.ParseException(PATTERN_EXCEPTION, 0);		}		if (pattern.equals("RN") || pattern.equals("rn")) {			throw new java.text.ParseException(NOT_IMPLIMENTED_EXCEPTION, 0);		}		FORMAT format = new FORMAT();		if (pattern.length() == 0) {			return format;		}		pattern = pattern.toUpperCase();		String prefix;		String suffix;		char c = pattern.charAt(0);		boolean n = (c == '0' || c == '9');		int i;		final int flen = pattern.length();		if (!n) {			for (i = 1; i < flen; i++) {				c = pattern.charAt(i);				if (c == '0' || c == '9')					break;			}			prefix = pattern.substring(0, i);		} else {			prefix = "";			i = 0;		}		StringBuilder numbers = new StringBuilder();		boolean stop = false;		boolean leadZero = true;		boolean definedGroups = false;		for (; i < flen; i++) {			c = pattern.charAt(i);			stop = true;			if (c == 'G') {				if (definedGroups && !format.localGroups) {					throw new java.text.ParseException(PATTERN_EXCEPTION, 0);				}				definedGroups = true;				format.localGroups = true;				stop = false;				numbers.append('G');			}			else if (c == ',') {				if (definedGroups && format.localGroups) {					throw new java.text.ParseException(PATTERN_EXCEPTION, i);				}				definedGroups = true;				format.localGroups = true;				stop = false;				numbers.append('G');			} else {				if (leadZero) {					if (c == '0') {						numbers.append('0');						format.digits++;						stop = false;					}					else if (c == '9') {						stop = false;						leadZero = false;						format.firstNine = numbers.length();						numbers.append('9');						format.digits++;					}				} else {					if (c == '0' || c == '9') {						stop = false;						numbers.append('9');						format.digits++;					}				}			}			if (stop) {				break;			}		}		if (leadZero) {			format.firstNine = numbers.length();		}		if (c == 'V') {			for (i++; i < flen; i++,format.v++) {				c = pattern.charAt(i);				if (c != '0' && c != '9')					break;			}		}		else if (c == '.' || c == 'D') {			if (definedGroups) {				if (format.localGroups && c == '.' || !format.localGroups && c == 'D') {					throw new java.text.ParseException(PATTERN_EXCEPTION, i);				}			} else {				format.localGroups = (c == 'D');			}			for (i++; i < flen; i++,format.dot++) {				c = pattern.charAt(i);				if (c != '0' && c != '9')					break;			}		}		format.numbers = numbers.toString();		if (i == prefix.length()) {			throw new java.text.ParseException(PATTERN_EXCEPTION, i);		}		suffix = pattern.substring(i);				int suf_offset = 0;		if (suffix.startsWith("EEEE")) {			format.sci = true;			suf_offset = 4;		}		if (suffix.startsWith("S", suf_offset)) {			format.sign = SIGN._S;			suf_offset += 1;		}		else if (suffix.startsWith("MI", suf_offset)) {			format.sign = SIGN.MI;			suf_offset += 2;		}		else if (suffix.startsWith("PR", suf_offset)) {			format.sign = SIGN.PR;			suf_offset += 2;		}				if (suf_offset < suffix.length()) {			throw new java.text.ParseException(PATTERN_EXCEPTION, i);		}				int pref_offset = 0;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?