javautils.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 496 行 · 第 1/2 页

JAVA
496
字号
                        value.equals("1") ||
                        value.equalsIgnoreCase("yes"));
    }

    /**
     * Tests the Object 'value':
     * if its null, return default.
     * if its a Boolean, return booleanValue()
     * if its an Integer,  return 'false' if its '0' else 'true'
     * if its a String, return isTrueExplicitly((String)value).
     * All other types return 'true'
     */
    public static boolean isTrueExplicitly(Object value, boolean defaultVal) {
        if (value == null) {
            return defaultVal;
        }
        if (value instanceof Boolean) {
            return ((Boolean) value).booleanValue();
        }
        if (value instanceof Integer) {
            return ((Integer) value).intValue() != 0;
        }
        if (value instanceof String) {
            return isTrueExplicitly((String) value);
        }
        return true;
    }

    public static boolean isTrueExplicitly(Object value) {
        return isTrueExplicitly(value, false);
    }

    /**
     * Tests the Object 'value':
     * if its null, return default.
     * if its a Boolean, return booleanValue()
     * if its an Integer,  return 'false' if its '0' else 'true'
     * if its a String, return 'false' if its 'false', 'no', or '0' - else 'true'
     * All other types return 'true'
     */
    public static boolean isTrue(Object value, boolean defaultVal) {
        return !isFalseExplicitly(value, !defaultVal);
    }

    public static boolean isTrue(Object value) {
        return isTrue(value, false);
    }

    /**
     * Tests the String 'value':
     * return 'true' if its 'false', '0', or 'no' - else 'false'
     * <p/>
     * Follow in 'C' tradition of boolean values:
     * false is specific (0), everything else is true;
     */
    public static boolean isFalse(String value) {
        return isFalseExplicitly(value);
    }

    /**
     * Tests the String 'value':
     * return 'true' if its null, 'false', '0', or 'no' - else 'false'
     */
    public static boolean isFalseExplicitly(String value) {
        return value == null ||
                value.equalsIgnoreCase("false") ||
                value.equals("0") ||
                value.equalsIgnoreCase("no");
    }

    /**
     * Tests the Object 'value':
     * if its null, return default.
     * if its a Boolean, return !booleanValue()
     * if its an Integer,  return 'true' if its '0' else 'false'
     * if its a String, return isFalseExplicitly((String)value).
     * All other types return 'false'
     */
    public static boolean isFalseExplicitly(Object value, boolean defaultVal) {
        if (value == null) {
            return defaultVal;
        }
        if (value instanceof Boolean) {
            return !((Boolean) value).booleanValue();
        }
        if (value instanceof Integer) {
            return ((Integer) value).intValue() == 0;
        }
        if (value instanceof String) {
            return isFalseExplicitly((String) value);
        }
        return false;
    }

    public static boolean isFalseExplicitly(Object value) {
        return isFalseExplicitly(value, true);
    }

    /**
     * Tests the Object 'value':
     * if its null, return default.
     * if its a Boolean, return booleanValue()
     * if its an Integer,  return 'false' if its '0' else 'true'
     * if its a String, return 'false' if its 'false', 'no', or '0' - else 'true'
     * All other types return 'true'
     */
    public static boolean isFalse(Object value, boolean defaultVal) {
        return isFalseExplicitly(value, defaultVal);
    }

    public static boolean isFalse(Object value) {
        return isFalse(value, true);
    }

    public static boolean isJavaId(String id) {
        if (id == null || id.length() == 0 || isJavaKeyword(id)) {
            return false;
        }
        if (!Character.isJavaIdentifierStart(id.charAt(0))) {
            return false;
        }
        for (int i = 1; i < id.length(); i++) {
            if (!Character.isJavaIdentifierPart(id.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * An empty immutable <code>String</code> array.
     */
    public static final String[] EMPTY_STRING_ARRAY = new String[0];

    /**
     * <p>Splits the provided text into an array, separator specified.
     * This is an alternative to using StringTokenizer.</p>
     * <p/>
     * <p>The separator is not included in the returned String array.
     * Adjacent separators are treated as one separator.</p>
     * <p/>
     * <p>A <code>null</code> input String returns <code>null</code>.</p>
     * <p/>
     * <pre>
     * StringUtils.split(null, *)         = null
     * StringUtils.split("", *)           = []
     * StringUtils.split("a.b.c", '.')    = ["a", "b", "c"]
     * StringUtils.split("a..b.c", '.')   = ["a", "b", "c"]
     * StringUtils.split("a:b:c", '.')    = ["a:b:c"]
     * StringUtils.split("a\tb\nc", null) = ["a", "b", "c"]
     * StringUtils.split("a b c", ' ')    = ["a", "b", "c"]
     * </pre>
     *
     * @param str           the String to parse, may be null
     * @param separatorChar the character used as the delimiter,
     *                      <code>null</code> splits on whitespace
     * @return an array of parsed Strings, <code>null</code> if null String input
     */
    public static String[] split(String str, char separatorChar) {
        if (str == null) {
            return null;
        }
        int len = str.length();
        if (len == 0) {
            return EMPTY_STRING_ARRAY;
        }
        List list = new ArrayList();
        int i = 0, start = 0;
        boolean match = false;
        while (i < len) {
            if (str.charAt(i) == separatorChar) {
                if (match) {
                    list.add(str.substring(start, i));
                    match = false;
                }
                start = ++i;
                continue;
            }
            match = true;
            i++;
        }
        if (match) {
            list.add(str.substring(start, i));
        }
        return (String[]) list.toArray(new String[list.size()]);
    }

    public static Class getWrapperClass(Class primitive) {
        if (primitive == int.class) {
            return java.lang.Integer.class;
        } else if (primitive == short.class) {
            return java.lang.Short.class;
        } else if (primitive == boolean.class) {
            return java.lang.Boolean.class;
        } else if (primitive == byte.class) {
            return java.lang.Byte.class;
        } else if (primitive == long.class) {
            return java.lang.Long.class;
        } else if (primitive == double.class) {
            return java.lang.Double.class;
        } else if (primitive == float.class) {
            return java.lang.Float.class;
        } else if (primitive == char.class) {
            return java.lang.Character.class;
        }

        return null;
    }

    /**
     * Scans the parameter string for the parameter search ignoring case when
     * comparing characters.
     *
     * @param string
     * @param search If test is empty -1 is always returned.
     * @return -1 if the string was not found or the index of the first matching
     *         character
     */
    public static int indexOfIgnoreCase(final String string,
                                        final String search) {
        int index = -1;
        final int stringLength = string.length();
        final int testLength = search.length();
        if (stringLength > 1 || testLength > 1) {
            final char firstCharOfTest = Character.toLowerCase(search.charAt(0));
            final int lastStringCharacterToCheck = stringLength - testLength + 1;

            for (int i = 0; i < lastStringCharacterToCheck; i++) {
                if (firstCharOfTest == Character.toLowerCase(string.charAt(i))) {
                    index = i;
                    for (int j = 1; j < testLength; j++) {
                        final char c = string.charAt(i + j);
                        final char otherChar = search.charAt(j);
                        if (Character.toLowerCase(c) != Character.toLowerCase(otherChar)) {
                            index = -1;
                            break;
                        }
                    }
                    if (-1 != index) {
                        break;
                    }
                }
            }
        }
        return index;
    }
}

⌨️ 快捷键说明

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