📄 library.java
字号:
* <LI> input is converted to String form * <LI> input with a <code>String</code> form length greater than 16 returns * input unchaged * <LI> <code>String</code> form with last four characters of '...000x' where * x != '.' is converted to '...0000' * <LI> <code>String</code> form with last four characters of '...9999' is * converted to '...999999' * <LI> the <code>java.lang.Double.doubleValue</code> of the <code>String</code> * form is returned * </UL> * @param d the double value for which to retrieve the <em>magically</em> * rounded value * @return the <em>magically</em> rounded value produced */ public static double roundMagic(double d) { // this function rounds numbers in a good way but slow: // - special handling for numbers around 0 // - only numbers <= +/-1000000000000 // - convert to a string // - check the last 4 characters: // '000x' becomes '0000' // '999x' becomes '999999' (this is rounded automatically) if ((d < 0.0000000000001) && (d > -0.0000000000001)) { return 0.0; } if ((d > 1000000000000.) || (d < -1000000000000.)) { return d; } StringBuffer s = new StringBuffer(); s.append(d); int len = s.length(); if (len < 16) { return d; } char cx = s.charAt(len - 1); char c1 = s.charAt(len - 2); char c2 = s.charAt(len - 3); char c3 = s.charAt(len - 4); if ((c1 == '0') && (c2 == '0') && (c3 == '0') && (cx != '.')) { s.setCharAt(len - 1, '0'); } else if ((c1 == '9') && (c2 == '9') && (c3 == '9') && (cx != '.')) { s.setCharAt(len - 1, '9'); s.append('9'); s.append('9'); } return Double.valueOf(s.toString()).doubleValue(); } /** * Returns the cotangent of the given <code>double</code> value * expressed in radians. * @param d the angle, expressed in radians * @return the cotangent */ public static double cot(double d) { return 1. / Math.tan(d); } /** * Returns the remainder (modulus) of the first given integer divided * by the second. <p> * * @param i1 the numerator * @param i2 the divisor * @return <code>i1</code> % <code>i2</code>, as an <code>int</code> */ public static int mod(int i1, int i2) { return i1 % i2; } /** * Returns the constant value, pi. * @return pi as a <code>double</code> value */ public static double pi() { return Math.PI; } /** * Returns the given <code>double</code> value, rounded to the given * <code>int</code> places right of the decimal point. If * the supplied rounding place value is negative, rounding is performed * to the left of the decimal point, using its magnitude (absolute value). * @param d the value to be rounded * @param p the rounding place value * @return <code>d</code> rounded */ public static double round(double d, int p) { double f = Math.pow(10., p); return Math.round(d * f) / f; } /** * Returns an indicator of the sign of the given <code>double</code> * value. If the value is less than zero, -1 is returned. If the value * equals zero, 0 is returned. If the value is greater than zero, 1 is * returned. * @param d the value * @return the sign of <code>d</code> */ public static int sign(double d) { return (d < 0) ? -1 : ((d > 0) ? 1 : 0); } /** * Returns the given <code>double</code> value, truncated to * the given <code>int</code> places right of the decimal point. * If the given place value is negative, the given <code>double</code> * value is truncated to the left of the decimal point, using the * magnitude (aboslute value) of the place value. * @param d the value to truncate * @param p the places left or right of the decimal point at which to * truncate * @return <code>d</code>, truncated */ public static double truncate(double d, int p) { double f = Math.pow(10., p); double g = d * f; return ((d < 0) ? Math.ceil(g) : Math.floor(g)) / f; } /** * Returns the bit-wise logical <em>and</em> of the given * integer values. * @param i the first value * @param j the second value * @return the bit-wise logical <em>and</em> of * <code>i</code> and <code>j</code> */ public static int bitand(int i, int j) { return i & j; } /** * Returns the bit-wise logical <em>or</em> of the given * integer values. * * @param i the first value * @param j the second value * @return the bit-wise logical <em>or</em> of * <code>i</code> and <code>j</code> */ public static int bitor(int i, int j) { return i | j; } /** * Returns the bit-wise logical <em>xor</em> of the given * integer values. * * @param i the first value * @param j the second value * @return the bit-wise logical <em>xor</em> of * <code>i</code> and <code>j</code> * * @since 1.8.0 */ public static int bitxor(int i, int j) { return i ^ j; } // STRING FUNCTIONS /** * Returns the Unicode code value of the leftmost character of * <code>s</code> as an <code>int</code>. This is the same as the * ASCII value if the string contains only ASCII characters. * @param s the <code>String</code> to evaluate * @return the integer Unicode value of the * leftmost character */ public static Integer ascii(String s) { if ((s == null) || (s.length() == 0)) { return null; } return ValuePool.getInt(s.charAt(0)); } /** * Returns the character string corresponding to the given ASCII * (or Unicode) value. * * <b>Note:</b> <p> * * In some SQL CLI * implementations, a <code>null</code> is returned if the range is outside 0..255. * In HSQLDB, the corresponding Unicode character is returned * unchecked. * @param code the character code for which to return a String * representation * @return the String representation of the character */ public static String character(int code) { return String.valueOf((char) code); } /** * Returns a <code>String</code> object that is the result of an * concatenation of the given <code>String</code> objects. <p> * * <b>When only one string is NULL, the result is different from that * returned by an (string1 || string2) expression: * * <UL> * <LI> if both <code>String</code> objects are <code>null</code>, return * <code>null</code> * <LI> if only one string is <code>null</code>, return the other * <LI> if both <code>String</code> objects are non-null, return as a * <code>String</code> object the character sequence obtained by listing, * in left to right order, the characters of the first string followed by * the characters of the second * </UL> * @param s1 the first <code>String</code> * @param s2 the second <code>String</code> * @return <code>s1</code> concatentated with <code>s2</code> */ public static String concat(String s1, String s2) { if (s1 == null) { if (s2 == null) { return null; } return s2; } if (s2 == null) { return s1; } return s1.concat(s2); } /** * Returns a count of the characters that do not match when comparing * the 4 digit numeric SOUNDEX character sequences for the * given <code>String</code> objects. If either <code>String</code> object is * <code>null</code>, zero is returned. * @param s1 the first <code>String</code> * @param s2 the second <code>String</code> * @return the number of differences between the <code>SOUNDEX</code> of * <code>s1</code> and the <code>SOUNDEX</code> of <code>s2</code> */// fredt@users 20020305 - patch 460907 by fredt - soundex public static int difference(String s1, String s2) { // todo: check if this is the standard algorithm if ((s1 == null) || (s2 == null)) { return 0; } s1 = soundex(s1); s2 = soundex(s2); int e = 0; for (int i = 0; i < 4; i++) { if (s1.charAt(i) != s2.charAt(i)) { e++; } } return e; } /** * Converts a <code>String</code> of hexidecimal digit characters to a raw * binary value, represented as a <code>String</code>.<p> * * The given <code>String</code> object must consist of a sequence of * 4 digit hexidecimal character substrings.<p> If its length is not * evenly divisible by 4, <code>null</code> is returned. If any of * its 4 character subsequences cannot be parsed as a * 4 digit, base 16 value, then a NumberFormatException is thrown. * * This conversion has the effect of reducing the character count 4:1. * * @param s a <code>String</code> of hexidecimal digit characters * @return an equivalent raw binary value, represented as a * <code>String</code> */ public static String hexToRaw(String s) { if (s == null) { return null; } char raw; StringBuffer to = new StringBuffer(); int len = s.length(); if (len % 4 != 0) { return null; } for (int i = 0; i < len; i += 4) { raw = (char) Integer.parseInt(s.substring(i, i + 4), 16); to.append(raw); } return (to.toString()); } /** * Returns a character sequence which is the result of writing the * first <code>length</code> number of characters from the second * given <code>String</code> over the first string. The start position * in the first string where the characters are overwritten is given by * <code>start</code>.<p> * * <b>Note:</b> In order of precedence, boundry conditions are handled as * follows:<p> * * <UL> * <LI>if either supplied <code>String</code> is null, then the other is * returned; the check starts with the first given <code>String</code>. * <LI>if <code>start</code> is less than one, <code>s1</code> is returned * <LI>if <code>length</code> is less than or equal to zero, * <code>s1</code> is returned * <LI>if the length of <code>s2</code> is zero, <code>s1</code> is returned * <LI>if <code>start</code> is greater than the length of <code>s1</code>, * <code>s1</code> is returned * <LI>if <code>length</code> is such that, taken together with * <code>start</code>, the indicated interval extends * beyond the end of <code>s1</code>, then the insertion is performed * precisely as if upon a copy of <code>s1</code> extended in length * to just include the indicated interval * </UL> * @param s1 the <code>String</code> into which to insert <code>s2</code> * @param start the position, with origin one, at which to start the insertion * @param length the number of characters in <code>s1</code> to replace * @param s2 the <code>String</code> to insert into <code>s1</code> * @return <code>s2</code> inserted into <code>s1</code>, as indicated * by <code>start</code> and <code>length</code> and adjusted for * boundry conditions */ public static String insert(String s1, int start, int length, String s2) { if (s1 == null) { return s2; } if (s2 == null) { return s1; } int len1 = s1.length(); int len2 = s2.length(); start--; if (start < 0 || length <= 0 || len2 == 0 || start > len1) { return s1; } if (start + length > len1) { length = len1 - start; } return s1.substring(0, start) + s2 + s1.substring(start + length); } /** * Returns a copy of the given <code>String</code>, with all upper case * characters converted to lower case. This uses the default Java String * conversion.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -