⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmldatatypeutil.java

📁 这是外国一个开源推理机
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
		StringTokenizer st = new StringTokenizer(s, "\t\r\n ");		if (!st.hasMoreTokens()) {			// Empty string or string containing white space only			return "";		}		else {			String firstToken = st.nextToken();			if (!st.hasMoreTokens()) {				// Single token only, no need to create a StringBuffer				return firstToken;			}			else {				StringBuffer buf = new StringBuffer(s.length());				buf.append(firstToken);				while (st.hasMoreTokens()) {					buf.append(' ').append(st.nextToken());				}				return buf.toString();			}		}	}/*--------------------+| Value comparison    |+--------------------*/	public static int compare(String value1, String value2, String datatype) {		if (datatype.equals(XmlSchema.DECIMAL)) {			return compareDecimals(value1, value2);		}		else if (datatype.equals(XmlSchema.INTEGER)) {			return compareIntegers(value1, value2);		}		else if (datatype.equals(XmlSchema.NEGATIVE_INTEGER)) {			return compareNegativeIntegers(value1, value2);		}		else if (datatype.equals(XmlSchema.NON_POSITIVE_INTEGER)) {			return compareNonPositiveIntegers(value1, value2);		}		else if (datatype.equals(XmlSchema.NON_NEGATIVE_INTEGER)) {			return compareNonNegativeIntegers(value1, value2);		}		else if (datatype.equals(XmlSchema.POSITIVE_INTEGER)) {			return comparePositiveIntegers(value1, value2);		}		else if (datatype.equals(XmlSchema.LONG)) {			return compareLongs(value1, value2);		}		else if (datatype.equals(XmlSchema.INT)) {			return compareInts(value1, value2);		}		else if (datatype.equals(XmlSchema.SHORT)) {			return compareShorts(value1, value2);		}		else if (datatype.equals(XmlSchema.BYTE)) {			return compareBytes(value1, value2);		}		else if (datatype.equals(XmlSchema.UNSIGNED_LONG)) {			return compareUnsignedLongs(value1, value2);		}		else if (datatype.equals(XmlSchema.UNSIGNED_INT)) {			return compareUnsignedInts(value1, value2);		}		else if (datatype.equals(XmlSchema.UNSIGNED_SHORT)) {			return compareUnsignedShorts(value1, value2);		}		else if (datatype.equals(XmlSchema.UNSIGNED_BYTE)) {			return compareUnsignedBytes(value1, value2);		}		else if (datatype.equals(XmlSchema.FLOAT)) {			return compareFloats(value1, value2);		}		else if (datatype.equals(XmlSchema.DOUBLE)) {			return compareDoubles(value1, value2);		}		else if (datatype.equals(XmlSchema.DATETIME)) {			return compareDateTime(value1, value2);		}		else {			_throwIAE("datatype is not ordered");			return 0; // required by compiler, never reached		}	}	/**	 * Compares two decimals to eachother.	 *	 * @return A negative number if <tt>dec1</tt> is smaller than	 * <tt>dec2</tt>, <tt>0</tt> if they are equal, or positive	 * (&gt;0) if <tt>dec1</tt> is larger than <tt>dec2</tt>.	 * @throws IllegalArgumentException If one of the supplied strings is	 * not a legal decimal.	 **/	public static int compareDecimals(String dec1, String dec2) {		dec1 = normalizeDecimal(dec1);		dec2 = normalizeDecimal(dec2);		return compareCanonicalDecimals(dec1, dec2);	}	/**	 * Compares two canonical decimals to eachother.	 *	 * @return A negative number if <tt>dec1</tt> is smaller than	 * <tt>dec2</tt>, <tt>0</tt> if they are equal, or positive	 * (&gt;0) if <tt>dec1</tt> is larger than <tt>dec2</tt>. The	 * result is undefined when one or both of the arguments is not	 * a canonical decimal.	 * @throws IllegalArgumentException If one of the supplied strings is	 * not a legal decimal.	 **/	public static int compareCanonicalDecimals(String dec1, String dec2) {		if (dec1.equals(dec2)) {			return 0;		}		// Check signs		if (dec1.charAt(0) == '-' && dec2.charAt(0) != '-') {			// dec1 is negative, dec2 is not			return -1;		}		if (dec2.charAt(0) == '-' && dec1.charAt(0) != '-') {			// dec2 is negative, dec1 is not			return 1;		}		int dotIdx1 = dec1.indexOf('.');		int dotIdx2 = dec2.indexOf('.');		// The decimal with the most digits before the dot is the largest		int result = dotIdx1 - dotIdx2;		if (result == 0) {			// equal number of digits before the dot, compare them			for (int i = 0; result == 0 && i < dotIdx1; i++) {				result = dec1.charAt(i) - dec2.charAt(i);			}			// Continue comparing digits after the dot if necessary			int dec1Length = dec1.length();			int dec2Length = dec2.length();			int lastIdx = dec1Length <= dec2Length ? dec1Length : dec2Length;			for (int i = dotIdx1 + 1; result == 0 && i < lastIdx; i++) {				result = dec1.charAt(i) - dec2.charAt(i);			}			// Still equal? The decimal with the most digits is the largest			if (result == 0) {				result = dec1Length - dec2Length;			}		}		if (dec1.charAt(0) == '-') {			// reverse result for negative values			result = -result;		}		return result;	}	/**	 * Compares two integers to eachother.	 *	 * @return A negative number if <tt>int1</tt> is smaller than	 * <tt>int2</tt>, <tt>0</tt> if they are equal, or positive	 * (&gt;0) if <tt>int1</tt> is larger than <tt>int2</tt>.	 * @throws IllegalArgumentException If one of the supplied strings is	 * not a legal integer.	 **/	public static int compareIntegers(String int1, String int2) {		int1 = normalizeInteger(int1);		int2 = normalizeInteger(int2);		return compareCanonicalIntegers(int1, int2);	}	/**	 * Compares two canonical integers to eachother.	 *	 * @return A negative number if <tt>int1</tt> is smaller than	 * <tt>int2</tt>, <tt>0</tt> if they are equal, or positive	 * (&gt;0) if <tt>int1</tt> is larger than <tt>int2</tt>. The	 * result is undefined when one or both of the arguments is not	 * a canonical integer.	 * @throws IllegalArgumentException If one of the supplied strings is	 * not a legal integer.	 **/	public static int compareCanonicalIntegers(String int1, String int2) {		if (int1.equals(int2)) {			return 0;		}		// Check signs		if (int1.charAt(0) == '-' && int2.charAt(0) != '-') {			// int1 is negative, int2 is not			return -1;		}		if (int2.charAt(0) == '-' && int1.charAt(0) != '-') {			// int2 is negative, int1 is not			return 1;		}		// The integer with the most digits is the largest		int result = int1.length() - int2.length();		if (result == 0) {			// equal number of digits, compare them			for (int i = 0; result == 0 && i < int1.length(); i++) {				result = int1.charAt(i) - int2.charAt(i);			}		}		if (int1.charAt(0) == '-') {			// reverse result for negative values			result = -result;		}		return result;	}	public static int compareNegativeIntegers(String int1, String int2) {		int1 = normalizeNegativeInteger(int1);		int2 = normalizeNegativeInteger(int2);		return compareCanonicalIntegers(int1, int2);	}	public static int compareNonPositiveIntegers(String int1, String int2) {		int1 = normalizeNonPositiveInteger(int1);		int2 = normalizeNonPositiveInteger(int2);		return compareCanonicalIntegers(int1, int2);	}	public static int compareNonNegativeIntegers(String int1, String int2) {		int1 = normalizeNonNegativeInteger(int1);		int2 = normalizeNonNegativeInteger(int2);		return compareCanonicalIntegers(int1, int2);	}	public static int comparePositiveIntegers(String int1, String int2) {		int1 = normalizePositiveInteger(int1);		int2 = normalizePositiveInteger(int2);		return compareCanonicalIntegers(int1, int2);	}	public static int compareLongs(String int1, String int2) {		int1 = normalizeLong(int1);		int2 = normalizeLong(int2);		return compareCanonicalIntegers(int1, int2);	}	public static int compareInts(String int1, String int2) {		int1 = normalizeInt(int1);		int2 = normalizeInt(int2);		return compareCanonicalIntegers(int1, int2);	}	public static int compareShorts(String int1, String int2) {		int1 = normalizeShort(int1);		int2 = normalizeShort(int2);		return compareCanonicalIntegers(int1, int2);	}	public static int compareBytes(String int1, String int2) {		int1 = normalizeByte(int1);		int2 = normalizeByte(int2);		return compareCanonicalIntegers(int1, int2);	}	public static int compareUnsignedLongs(String int1, String int2) {		int1 = normalizeUnsignedLong(int1);		int2 = normalizeUnsignedLong(int2);		return compareCanonicalIntegers(int1, int2);	}	public static int compareUnsignedInts(String int1, String int2) {		int1 = normalizeUnsignedInt(int1);		int2 = normalizeUnsignedInt(int2);		return compareCanonicalIntegers(int1, int2);	}	public static int compareUnsignedShorts(String int1, String int2) {		int1 = normalizeUnsignedShort(int1);		int2 = normalizeUnsignedShort(int2);		return compareCanonicalIntegers(int1, int2);	}	public static int compareUnsignedBytes(String int1, String int2) {		int1 = normalizeUnsignedByte(int1);		int2 = normalizeUnsignedByte(int2);		return compareCanonicalIntegers(int1, int2);	}	/**	 * Compares two floats to eachother.	 *	 * @return A negative number if <tt>float1</tt> is smaller than	 * <tt>float2</tt>, <tt>0</tt> if they are equal, or positive	 * (&gt;0) if <tt>float1</tt> is larger than <tt>float2</tt>.	 * @throws IllegalArgumentException If one of the supplied strings	 * is not a legal float or if <tt>NaN</tt> is compared to a float	 * other than <tt>NaN</tt>.	 **/	public static int compareFloats(String float1, String float2) {		float1 = normalizeFloat(float1);		float2 = normalizeFloat(float2);		return compareCanonicalFloats(float1, float2);	}	/**	 * Compares two canonical floats to eachother.	 *	 * @return A negative number if <tt>float1</tt> is smaller than	 * <tt>float2</tt>, <tt>0</tt> if they are equal, or positive	 * (&gt;0) if <tt>float1</tt> is larger than <tt>float2</tt>. The	 * result is undefined when one or both of the arguments is not	 * a canonical float.	 * @throws IllegalArgumentException If one of the supplied strings	 * is not a legal float or if <tt>NaN</tt> is compared to a	 * float other than <tt>NaN</tt>.	 **/	public static int compareCanonicalFloats(String float1, String float2) {		return compareCanonicalFPNumbers(float1, float2);	}	/**	 * Compares two doubles to eachother.	 *	 * @return A negative number if <tt>double1</tt> is smaller than	 * <tt>double2</tt>, <tt>0</tt> if they are equal, or positive	 * (&gt;0) if <tt>double1</tt> is larger than <tt>double2</tt>.	 * @throws IllegalArgumentException If one of the supplied strings	 * is not a legal double or if <tt>NaN</tt> is compared to a	 * double other than <tt>NaN</tt>.	 **/	public static int compareDoubles(String double1, String double2) {		double1 = normalizeDouble(double1);		double2 = normalizeDouble(double2);		return compareCanonicalDoubles(double1, double2);	}	/**	 * Compares two canonical doubles to eachother.	 *	 * @return A negative number if <tt>double1</tt> is smaller than	 * <tt>double2</tt>, <tt>0</tt> if they are equal, or positive	 * (&gt;0) if <tt>double1</tt> is larger than <tt>double2</tt>. The	 * result is undefined when one or both of the arguments is not	 * a canonical double.	 * @throws IllegalArgumentException If one of the supplied strings	 * is not a legal double or if <tt>NaN</tt> is compared to a	 * double other than <tt>NaN</tt>.	 **/	public static int compareCanonicalDoubles(String double1, String double2) {		return compareCanonicalFPNumbers(double1, double2);	}	/**	 * Compares two floating point numbers to eachother.	 *	 * @return A negative number if <tt>float1</tt> is smaller than	 * <tt>float2</tt>, <tt>0</tt> if they are equal, or positive	 * (&gt;0) if <tt>float1</tt> is larger than <tt>float2</tt>.	 * @throws IllegalArgumentException If one of the supplied strings	 * is not a legal floating point number or if <tt>NaN</tt> is	 * compared to a floating point number other than <tt>NaN</tt>.	 **/	public static int compareFPNumbers(String fp1, String fp2) {		fp1 = normalizeFPNumber(fp1);		fp2 = normalizeFPNumber(fp2);		return compareCanonicalFPNumbers(fp1, fp2);	}	/**	 * Compares two canonical floating point numbers to eachother.	 *	 * @return A negative number if <tt>float1</tt> is smaller than	 * <tt>float2</tt>, <tt>0</tt> if they are equal, or positive	 * (&gt;0) if <tt>float1</tt> is larger than <tt>float2</tt>. The	 * result is undefined when one or both of the arguments is not	 * a canonical floating point number.	 * @throws IllegalArgumentException If one of the supplied strings	 * is not a legal floating point number or if <tt>NaN</tt> is	 * compared to a floating point number other than <tt>NaN</tt>.	 **/	public static int compareCanonicalFPNumbers(String float1, String float2) {		// Handle special case NaN		if (float1.equals("NaN") || float2.equals("NaN")) {			if (float1.equals(float2)) {				// NaN is equal to itself				return 0;			}			else {				_throwIAE("NaN cannot be compared to other floats");			}		}		// Handle special case INF		if (float1.equals("INF")) {			return (float2.equals("INF")) ? 0 : 1;		}		else if (float2.equals("INF")) {			return -1;		}		// Handle special case -INF		if (float1.equals("-INF")) {			return (float2.equals("-INF")) ? 0 : -1;		}		else if (float2.equals("-INF")) {			return 1;		}		// Check signs		if (float1.charAt(0) == '-' && float2.charAt(0) != '-') {			// float1 is negative, float2 is not			return -1;		}		if (float2.charAt(0) == '-' && float1.charAt(0) != '-') {			// float2 is negative, float1 is not			return 1;		}		int eIdx1 = float1.indexOf('E');		String mantissa1 = float1.substring(0, eIdx1);		String exponent1 = float1.substring(eIdx1 + 1);		int eIdx2 = float2.indexOf('E');		String mantissa2 = float2.substring(0, eIdx2);		String exponent2 = float2.substring(eIdx2 + 1);		// Compare exponents		int result = compareCanonicalIntegers(exponent1, exponent2);		if (result != 0 && float1.charAt(0) == '-') {			// reverse result for negative values			result = -result;		}		if (result == 0) {			// Equal exponents, compare mantissas			result = compareCanonicalDecimals(mantissa1, mantissa2);		}		return result;	}	/**	 * Compares two dateTime objects. <b>Important:</b> The comparison only	 * works if both values have, or both values don't have specified a valid	 * value for the timezone.	 *	 * @param value1 An xsd:dateTime value.	 * @param value2 An xsd:dateTime value.	 * @return <tt>-1</tt> if <tt>value1</tt> is before <tt>value2</tt> (i.e. if	 * the dateTime object represented by value1 is before the dateTime object	 * represented by value2), <tt>0</tt> if both are equal and <tt>1</tt> if	 * <tt>value2</tt> is before <tt>value1</tt><br>.	 */	public static int compareDateTime(String value1, String value2) {		DateTime dateTime1 = new DateTime(value1);		DateTime dateTime2 = new DateTime(value2);		dateTime1.normalize();		dateTime2.normalize();		return dateTime1.compareTo(dateTime2);	}/*----------------+| Utility methods |+----------------*/	/**	 * Checks whether the supplied character is a digit.	 */	private static final boolean _isDigit(char c) {		return c >= '0' && c <= '9';	}	/**	 * Throws an IllegalArgumentException that contains the supplied	 * message.	 **/	private static final void _throwIAE(String msg) {		throw new IllegalArgumentException(msg);	}	/*	public static void main(String[] args) {		System.out.println(normalizeFloat(args[0]));	}	*/}

⌨️ 快捷键说明

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