Example usage for java.lang Character isDigit

List of usage examples for java.lang Character isDigit

Introduction

In this page you can find the example usage for java.lang Character isDigit.

Prototype

public static boolean isDigit(int codePoint) 

Source Link

Document

Determines if the specified character (Unicode code point) is a digit.

Usage

From source file:com.joliciel.talismane.tokeniser.filters.TokenRegexFilterImpl.java

Pattern getPattern() {
    if (pattern == null) {
        // we may need to replace WordLists by the list contents
        String myRegex = this.regex;

        if (LOG.isTraceEnabled()) {
            LOG.trace("Regex: " + myRegex);
        }//from  w  ww.jav a  2  s.  c om

        if (this.autoWordBoundaries) {
            Boolean startsWithLetter = null;
            for (int i = 0; i < myRegex.length() && startsWithLetter == null; i++) {
                char c = myRegex.charAt(i);
                if (c == '\\') {
                    i++;
                    c = myRegex.charAt(i);
                    if (c == 'd' || c == 'w') {
                        startsWithLetter = true;
                    } else if (c == 's' || c == 'W' || c == 'b' || c == 'B') {
                        startsWithLetter = false;
                    } else if (c == 'p') {
                        i += 2; // skip the open curly brackets
                        int closeCurlyBrackets = myRegex.indexOf('}', i);
                        int openParentheses = myRegex.indexOf('(', i);
                        int endIndex = closeCurlyBrackets;
                        if (openParentheses > 0 && openParentheses < closeCurlyBrackets)
                            endIndex = openParentheses;
                        if (endIndex > 0) {
                            String specialClass = myRegex.substring(i, endIndex);
                            if (specialClass.equals("WordList")) {
                                startsWithLetter = true;
                            }
                        }
                    }
                    break;
                } else if (c == '[' || c == '(') {
                    // do nothing
                } else if (Character.isLetter(c) || Character.isDigit(c)) {
                    startsWithLetter = true;
                } else {
                    startsWithLetter = false;
                }
            }

            Boolean endsWithLetter = null;
            for (int i = myRegex.length() - 1; i >= 0 && endsWithLetter == null; i--) {
                char c = myRegex.charAt(i);
                char prevC = ' ';
                if (i >= 1)
                    prevC = myRegex.charAt(i - 1);
                if (prevC == '\\') {
                    if (c == 'd' || c == 'w') {
                        endsWithLetter = true;
                    } else if (c == 's' || c == 'W' || c == 'b' || c == 'B') {
                        endsWithLetter = false;
                    } else if (c == 'p') {
                        i += 2; // skip the open curly brackets
                        int closeCurlyBrackets = myRegex.indexOf('}', i);
                        int openParentheses = myRegex.indexOf('(', i);
                        int endIndex = closeCurlyBrackets;
                        if (openParentheses < closeCurlyBrackets)
                            endIndex = openParentheses;
                        if (endIndex > 0) {
                            String specialClass = myRegex.substring(i, endIndex);
                            if (specialClass.equals("WordList") || specialClass.equals("Alpha")
                                    || specialClass.equals("Lower") || specialClass.equals("Upper")
                                    || specialClass.equals("ASCII") || specialClass.equals("Digit")) {
                                startsWithLetter = true;
                            }
                        }
                    }
                    break;
                } else if (c == ']' || c == ')' || c == '+') {
                    // do nothing
                } else if (c == '}') {
                    int startIndex = myRegex.lastIndexOf('{') + 1;
                    int closeCurlyBrackets = myRegex.indexOf('}', startIndex);
                    int openParentheses = myRegex.indexOf('(', startIndex);
                    int endIndex = closeCurlyBrackets;
                    if (openParentheses > 0 && openParentheses < closeCurlyBrackets)
                        endIndex = openParentheses;
                    if (endIndex > 0) {
                        String specialClass = myRegex.substring(startIndex, endIndex);
                        if (specialClass.equals("WordList") || specialClass.equals("Alpha")
                                || specialClass.equals("Lower") || specialClass.equals("Upper")
                                || specialClass.equals("ASCII") || specialClass.equals("Digit")) {
                            endsWithLetter = true;
                        }
                    }
                    break;
                } else if (Character.isLetter(c) || Character.isDigit(c)) {
                    endsWithLetter = true;
                } else {
                    endsWithLetter = false;
                }
            }

            if (startsWithLetter != null && startsWithLetter) {
                myRegex = "\\b" + myRegex;
            }
            if (endsWithLetter != null && endsWithLetter) {
                myRegex = myRegex + "\\b";
            }
            if (LOG.isTraceEnabled()) {
                LOG.trace("After autoWordBoundaries: " + myRegex);
            }
        }

        if (!this.caseSensitive || !this.diacriticSensitive) {
            StringBuilder regexBuilder = new StringBuilder();
            for (int i = 0; i < myRegex.length(); i++) {
                char c = myRegex.charAt(i);
                if (c == '\\') {
                    // escape - skip next
                    regexBuilder.append(c);
                    i++;
                    c = myRegex.charAt(i);
                    regexBuilder.append(c);
                } else if (c == '[') {
                    // character group, don't change it
                    regexBuilder.append(c);
                    while (c != ']' && i < myRegex.length()) {
                        i++;
                        c = myRegex.charAt(i);
                        regexBuilder.append(c);
                    }
                } else if (c == '{') {
                    // command, don't change it
                    regexBuilder.append(c);
                    while (c != '}' && i < myRegex.length()) {
                        i++;
                        c = myRegex.charAt(i);
                        regexBuilder.append(c);
                    }
                } else if (Character.isLetter(c)) {
                    Set<String> chars = new TreeSet<String>();
                    chars.add("" + c);
                    char noAccent = diacriticPattern.matcher(Normalizer.normalize("" + c, Form.NFD))
                            .replaceAll("").charAt(0);

                    if (!this.caseSensitive) {
                        chars.add("" + Character.toUpperCase(c));
                        chars.add("" + Character.toLowerCase(c));
                        chars.add("" + Character.toUpperCase(noAccent));
                    }
                    if (!this.diacriticSensitive) {
                        chars.add("" + noAccent);
                        if (!this.caseSensitive) {
                            chars.add("" + Character.toLowerCase(noAccent));
                        }
                    }
                    if (chars.size() == 1) {
                        regexBuilder.append(c);
                    } else {
                        regexBuilder.append('[');
                        for (String oneChar : chars) {
                            regexBuilder.append(oneChar);
                        }
                        regexBuilder.append(']');
                    }
                } else {
                    regexBuilder.append(c);
                }
            }
            myRegex = regexBuilder.toString();
            if (LOG.isTraceEnabled()) {
                LOG.trace("After caseSensitive: " + myRegex);
            }
        }

        Matcher matcher = wordListPattern.matcher(myRegex);
        StringBuilder regexBuilder = new StringBuilder();

        int lastIndex = 0;
        while (matcher.find()) {
            String[] params = matcher.group(1).split(",");
            int start = matcher.start();
            int end = matcher.end();
            regexBuilder.append(myRegex.substring(lastIndex, start));

            String wordListName = params[0];
            boolean uppercaseOptional = false;
            boolean diacriticsOptional = false;
            boolean lowercaseOptional = false;
            boolean firstParam = true;
            for (String param : params) {
                if (firstParam) {
                    /* word list name */ } else if (param.equals("diacriticsOptional"))
                    diacriticsOptional = true;
                else if (param.equals("uppercaseOptional"))
                    uppercaseOptional = true;
                else if (param.equals("lowercaseOptional"))
                    lowercaseOptional = true;
                else
                    throw new TalismaneException(
                            "Unknown parameter in word list " + matcher.group(1) + ": " + param);
                firstParam = false;
            }

            ExternalWordList wordList = externalResourceFinder.getExternalWordList(wordListName);
            if (wordList == null)
                throw new TalismaneException("Unknown word list: " + wordListName);

            StringBuilder sb = new StringBuilder();

            boolean firstWord = true;
            for (String word : wordList.getWordList()) {
                if (!firstWord)
                    sb.append("|");
                word = Normalizer.normalize(word, Form.NFC);
                if (uppercaseOptional || diacriticsOptional) {
                    String wordNoDiacritics = Normalizer.normalize(word, Form.NFD)
                            .replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
                    String wordLowercase = word.toLowerCase(Locale.ENGLISH);
                    String wordLowercaseNoDiacritics = Normalizer.normalize(wordLowercase, Form.NFD)
                            .replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
                    String wordUppercase = wordNoDiacritics.toUpperCase(Locale.ENGLISH);

                    boolean needsGrouping = false;
                    if (uppercaseOptional && !word.equals(wordLowercase))
                        needsGrouping = true;
                    if (diacriticsOptional && !word.equals(wordNoDiacritics))
                        needsGrouping = true;
                    if (lowercaseOptional && !word.equals(wordUppercase))
                        needsGrouping = true;
                    if (needsGrouping) {
                        for (int i = 0; i < word.length(); i++) {
                            char c = word.charAt(i);

                            boolean grouped = false;
                            if (uppercaseOptional && c != wordLowercase.charAt(i))
                                grouped = true;
                            if (diacriticsOptional && c != wordNoDiacritics.charAt(i))
                                grouped = true;
                            if (lowercaseOptional && c != wordUppercase.charAt(i))
                                grouped = true;

                            if (!grouped)
                                sb.append(c);
                            else {
                                sb.append("[");
                                String group = "" + c;
                                if (uppercaseOptional && group.indexOf(wordLowercase.charAt(i)) < 0)
                                    group += (wordLowercase.charAt(i));
                                if (lowercaseOptional && group.indexOf(wordUppercase.charAt(i)) < 0)
                                    group += (wordUppercase.charAt(i));
                                if (diacriticsOptional && group.indexOf(wordNoDiacritics.charAt(i)) < 0)
                                    group += (wordNoDiacritics.charAt(i));
                                if (uppercaseOptional && diacriticsOptional
                                        && group.indexOf(wordLowercaseNoDiacritics.charAt(i)) < 0)
                                    group += (wordLowercaseNoDiacritics.charAt(i));

                                sb.append(group);
                                sb.append("]");
                            } // does this letter need grouping?
                        } // next letter
                    } else {
                        sb.append(word);
                    } // any options activated?
                } else {
                    sb.append(word);
                }
                firstWord = false;
            } // next word in list

            regexBuilder.append(sb.toString());
            lastIndex = end;
        } // next match
        regexBuilder.append(myRegex.substring(lastIndex));
        myRegex = regexBuilder.toString();
        this.pattern = Pattern.compile(myRegex, Pattern.UNICODE_CHARACTER_CLASS);
    }
    return pattern;
}

From source file:Data.Storage.java

/**
 * adding a data Storage to the collection
 * @param key index item is stored by//w w  w. j  ava 2  s  .  c  om
 * @param value actual data storage data to be stored
 */
public void put(String key, Storage value) {
    try {
        //find out if it is an array or json
        //if the collection is empty then hashmap
        boolean isArray = true;
        for (String storageKey : value.keys())
            for (char k : storageKey.toCharArray())
                //if there is just one key not a digit then not array
                if (!Character.isDigit(k))
                    isArray = false;

        //if the collection is empty then hashmap
        if (value.keys().size() > 0 && isArray) {
            HashMap<String, Object> newJson = new HashMap<>();
            newJson.put(key, value.toJsonArray());
            addMap(newJson);
        } else {
            JSONObject newJson = new JSONObject();
            newJson.put(key, value.toJson());
            addJson(newJson);
        }

    } catch (JSONException e) {
        Utils.debug("unable to add to Data Storage:\nStorage: " + toString() + "\nAdding: " + value.toString(),
                "error");
        e.printStackTrace();
    }
}

From source file:com.duowan.common.spring.jdbc.BeanPropertyRowMapper.java

/**
 * Convert a name in camelCase to an underscored name in lower case.
 * Any upper case letters are converted to lower case with a preceding underscore.
 * @param name the string containing original name
 * @return the converted name// www. j  ava 2s  . c  om
 */
private String underscoreName(String name) {
    StringBuilder result = new StringBuilder();
    if (name != null && name.length() > 0) {
        result.append(name.substring(0, 1).toLowerCase());
        for (int i = 1; i < name.length(); i++) {
            char c = name.charAt(i);
            if (Character.isUpperCase(c) && !Character.isDigit(c)) {
                result.append("_");
                result.append(Character.toLowerCase(c));
            } else {
                result.append(c);
            }
        }
    }
    return result.toString();
}

From source file:au.org.ala.delta.intkey.directives.IntkeyDirectiveParser.java

@Override
protected void processTrailing(StringBuilder data, IntkeyContext context) throws DirectiveException {
    if (data.length() > 0) {
        char ch = data.charAt(0);
        if (Character.isDigit(ch)) {
            DirectiveSearchResult r = getDirectiveRegistry()
                    .findDirective(new ArrayList<String>(Arrays.asList("use")));
            IntkeyDirective useDirective = (IntkeyDirective) r.getMatches().get(0);
            try {
                useDirective.parseAndProcess(context, data.toString());
            } catch (Exception ex) {
                handleDirectiveProcessingException(context, useDirective, ex);
            }//w  ww  . ja v  a2s  .c  om
        } else {
            super.processTrailing(data, context);
        }
    }
}

From source file:UTFEncodingUtil.java

/**
 * Decodes the given string using the provided encoding. The encoding must be a valid
 * Java-encoding.//  w w  w. j  a v  a 2 s . com
 *
 * @param s        the string that should be encoded.
 * @param encoding the encoding to tranform the bytes into a string.
 * @return the encoded string.
 * @throws UnsupportedEncodingException if the specified encoding is not recognized.
 */
public static String decode(final String s, final String encoding) throws UnsupportedEncodingException {
    if ("utf-8".equalsIgnoreCase(encoding)) {
        return decodeUTF(s);
    }
    // the resulting string will never be greater than the encoded string
    final byte[] result = new byte[s.length()];
    final char[] chars = s.toCharArray();
    int position = 0;

    for (int i = 0; i < chars.length; i++) {
        final char ch = chars[i];
        final int b;
        switch (ch) {
        case '%':
            final char lch = s.charAt(++i);
            final int hb = (Character.isDigit(lch) ? lch - '0' : 10 + Character.toLowerCase(lch) - 'a') & 0xF;
            final char hch = s.charAt(++i);
            final int lb = (Character.isDigit(hch) ? hch - '0' : 10 + Character.toLowerCase(hch) - 'a') & 0xF;
            b = (hb << 4) | lb;
            break;
        case '+':
            b = ' ';
            break;
        default:
            b = ch;
        }
        result[position] = (byte) b;
        position += 1;
    }
    return new String(result, 0, position, encoding);
}

From source file:com.netflix.dyno.contrib.ElasticConnectionPoolConfigurationPublisher.java

int findVersionStartIndex(String nameWithVersion) {
    if (nameWithVersion != null) {
        char[] chars = nameWithVersion.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '-') {
                if (i < chars.length && Character.isDigit(chars[i + 1])) {
                    return i + 1;
                }//from ww  w  .  j a v  a 2s  .  co m
            }
        }
    }

    return -1;
}

From source file:FormatTest.java

public void replace(FilterBypass fb, int offset, int length, String string, AttributeSet attr)
        throws BadLocationException {
    if (string != null) {
        StringBuilder builder = new StringBuilder(string);
        for (int i = builder.length() - 1; i >= 0; i--) {
            int cp = builder.codePointAt(i);
            if (!Character.isDigit(cp) && cp != '-') {
                builder.deleteCharAt(i);
                if (Character.isSupplementaryCodePoint(cp)) {
                    i--;//from   ww w . j  a va 2s  .  c  o m
                    builder.deleteCharAt(i);
                }
            }
        }
        string = builder.toString();
    }
    super.replace(fb, offset, length, string, attr);
}

From source file:com.flexive.shared.FxFormatUtils.java

/**
 * Check if a color value is valid./*from w  w w  .  j av a 2 s  .com*/
 * <p/>
 * The color may be a RGB value (recognized by a starting '#') or a css class name.<br>
 * The function returns the default color if value is empty.<br>
 * If the value defines an invalid RGB value a FxInvalidParameterException is thrown.<br>
 *
 * @param paramName the name of the parameter that is used when a FxInvalidParameterException is thrown
 * @param value     the color alue
 * @return the (corrected) color
 * @throws FxInvalidParameterException if the color is invalid
 */
public static String processColorString(String paramName, String value) throws FxInvalidParameterException {
    if (value == null || value.length() == 0) {
        return DEFAULT_COLOR;
    }
    if (value.charAt(0) == '#') {
        if (value.length() != 7) {
            throw new FxInvalidParameterException("Invalid color for property [" + paramName + "]", paramName);
        }
        for (int i = 1; i < 7; i++) {
            char aChar = value.charAt(i);
            if (!Character.isDigit(aChar) && (aChar != 'A') && (aChar != 'B') && (aChar != 'C')
                    && (aChar != 'D') && (aChar != 'E') && (aChar != 'F')) {
                throw new FxInvalidParameterException("Invalid color for property [" + paramName + "]",
                        paramName);
            }
        }
    } else if (StringUtils.containsOnly(value.toUpperCase(), "0123456789ABCDEF")) {
        // prefix with '#'
        value = "#" + value;
    }
    return value;
}

From source file:io.techcode.logbulk.pipeline.input.SyslogInput.java

/**
 * Default implementation returns the date as a String (if present).
 *
 * @param reader the reader./*from w ww . j a v  a  2s  .c o m*/
 * @return the timestamp.
 */
private String getTimestamp(Reader reader) {
    int c = reader.getc();

    if (c == NILVALUE) {
        return null;
    }

    if (!Character.isDigit(c)) {
        throw new IllegalStateException("Year expected @" + reader.idx);
    }

    StringBuilder dateBuilder = new StringBuilder(64);
    dateBuilder.append((char) c);
    while ((c = reader.getc()) != SPACE) {
        dateBuilder.append((char) c);
    }
    return dateBuilder.toString();
}

From source file:com.github.tteofili.p2h.Par2HierTest.java

private String asStrings(Map<String, INDArray> pvs, Map<String, INDArray> hvs) {
    StringBuilder builder = new StringBuilder();
    builder.append("doc, depth, Paragraph, PV x, PV y, HV x, HV y\n");
    for (Map.Entry<String, INDArray> entry : pvs.entrySet()) {
        String key = entry.getKey();
        String depth = String.valueOf(key.split("\\.").length - 1);
        String c = String.valueOf(key.charAt(3));
        if (Character.isDigit(key.charAt(4))) {
            c += key.charAt(4);/*from  w ww  .jav  a2  s.c om*/
        }
        String doc = String.valueOf(c);
        builder.append(doc).append(',').append(depth).append(", ")
                .append(entry.toString().replace("=[", ",").replace("]", ","));
        String s = hvs.get(key).toString();
        builder.append(s.replace("[", "").replace("]", "")).append("\n");
    }
    return builder.toString();
}