Example usage for org.apache.commons.lang3 CharUtils isAsciiNumeric

List of usage examples for org.apache.commons.lang3 CharUtils isAsciiNumeric

Introduction

In this page you can find the example usage for org.apache.commons.lang3 CharUtils isAsciiNumeric.

Prototype

public static boolean isAsciiNumeric(final char ch) 

Source Link

Document

Checks whether the character is ASCII 7 bit numeric.

 CharUtils.isAsciiNumeric('a')  = false CharUtils.isAsciiNumeric('A')  = false CharUtils.isAsciiNumeric('3')  = true CharUtils.isAsciiNumeric('-')  = false CharUtils.isAsciiNumeric('\n') = false CharUtils.isAsciiNumeric('©') = false 

Usage

From source file:com.thinkbiganalytics.feedmgr.rest.support.SystemNamingService.java

public static String generateSystemName(String name) {
    //first trim it
    String systemName = StringUtils.trimToEmpty(name);
    if (StringUtils.isBlank(systemName)) {
        return systemName;
    }/*  ww w.j  a  v  a 2s . c o  m*/
    systemName = systemName.replaceAll(" +", "_");
    systemName = systemName.replaceAll("[^\\w_]", "");

    int i = 0;

    StringBuilder s = new StringBuilder();
    CharacterIterator itr = new StringCharacterIterator(systemName);
    for (char c = itr.first(); c != CharacterIterator.DONE; c = itr.next()) {
        if (Character.isUpperCase(c)) {
            //if it is upper, not at the start and not at the end check to see if i am surrounded by upper then lower it.
            if (i > 0 && i != systemName.length() - 1) {
                char prevChar = systemName.charAt(i - 1);
                char nextChar = systemName.charAt(i + 1);

                if (Character.isUpperCase(prevChar) && (Character.isUpperCase(nextChar)
                        || CharUtils.isAsciiNumeric(nextChar) || '_' == nextChar || '-' == nextChar)) {
                    char lowerChar = Character.toLowerCase(systemName.charAt(i));
                    s.append(lowerChar);
                } else {
                    s.append(c);
                }
            } else if (i > 0 && i == systemName.length() - 1) {
                char prevChar = systemName.charAt(i - 1);
                if (Character.isUpperCase(prevChar) && !CharUtils.isAsciiNumeric(systemName.charAt(i))) {
                    char lowerChar = Character.toLowerCase(systemName.charAt(i));
                    s.append(lowerChar);
                } else {
                    s.append(c);
                }
            } else {
                s.append(c);
            }
        } else {
            s.append(c);
        }

        i++;
    }

    systemName = s.toString();
    systemName = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, systemName);
    systemName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_UNDERSCORE, systemName);
    systemName = StringUtils.replace(systemName, "__", "_");
    // Truncate length if exceeds Hive limit
    systemName = (systemName.length() > 128 ? systemName.substring(0, 127) : systemName);
    return systemName;
}

From source file:net.siegmar.japtproxy.packages.rpm.RpmPackageVersionComparator.java

/**
 * Returns the order for a single character. The order is digits,
 * then alpha, then non-ascii. If {@code pos} >=
 * {@code ca.length}, then the order is the same as for digits.
 *
 * @param ca  the character array//from  w w w .ja  va  2 s  .  c o  m
 * @param pos the position in the character array
 * @return the order for the given character
 */
@Override
protected int order(final char[] ca, final int pos) {
    if (pos >= ca.length) {
        return 0;
    }

    final char c = ca[pos];

    return CharUtils.isAsciiNumeric(c) ? 0 : CharUtils.isAsciiAlpha(c) ? c : c + NON_ASCII_OFFSET;
}

From source file:net.siegmar.japtproxy.packages.debian.DebianPackageVersionComparator.java

/**
 * Returns the order for a single character. The order is ~, then digits,
 * then alpha, then non-ascii. If {@code pos} >=
 * {@code ca.length}, then the order is the same as for digits.
 *
 * @param ca  the character array/*  w w w  . j  ava  2  s . c o  m*/
 * @param pos the position in the character array
 * @return the order for the given character
 */
@Override
protected int order(final char[] ca, final int pos) {
    if (pos >= ca.length) {
        return 0;
    }

    final char c = ca[pos];

    return c == '~' ? -1
            : CharUtils.isAsciiNumeric(c) ? 0 : CharUtils.isAsciiAlpha(c) ? c : c + NON_ASCII_OFFSET;
}

From source file:ductive.parse.parsers.LongParser.java

@Override
public Result<Long> doApply(ParseContext ctx) {
    int pos = 0;/*w  ww.  java 2 s.co m*/

    if (ctx.length() == 0)
        throw new UnexpectedEofException(String.format(LongParser.UNEXPECTED_EOF_MESSAGE, pos), ctx, 0);

    StringBuffer buf = new StringBuffer();

    char c = ctx.charAt(pos);
    while (true) {
        if (!CharUtils.isAsciiNumeric(c))
            break;

        buf.append(c);

        if (++pos >= ctx.length())
            break;

        c = ctx.charAt(pos);
    }

    if (buf.length() == 0)
        throw new UnexpectedCharacterException(String.format(UNEXPECTED_CHARACTER_MESSAGE, c, 0), ctx, 0);

    String str = buf.toString();
    return Result.make(Long.parseLong(str), ctx.eatInput(str.length()));
}

From source file:net.siegmar.japtproxy.packages.AbstractRepoPackageVersionComparator.java

/**
 * Compares two version strings and returns and {@code int} that
 * specifies the order.//from   www.j  av a 2 s  . co m
 *
 * @param s1 version string 1
 * @param s2 version string 2
 * @return the order
 */
protected int strCompare(final String s1, final String s2) {
    final char[] c1 = s1 == null ? new char[0] : s1.toCharArray();
    final char[] c2 = s2 == null ? new char[0] : s2.toCharArray();

    int p1 = 0;
    int p2 = 0;

    while (p1 < c1.length || p2 < c2.length) {
        int firstDiff = 0;

        // compare leading non-digits
        for (; p1 < c1.length && !CharUtils.isAsciiNumeric(c1[p1])
                || p2 < c2.length && !CharUtils.isAsciiNumeric(c2[p2]); p1++, p2++) {
            final int diff = order(c1, p1) - order(c2, p2);

            if (diff != 0) {
                return diff;
            }
        }

        // strip 0's
        while (p1 < c1.length && c1[p1] == '0') {
            p1++;
        }
        while (p2 < c2.length && c2[p2] == '0') {
            p2++;
        }

        // compare digits
        for (; p1 < c1.length && CharUtils.isAsciiNumeric(c1[p1]) && p2 < c2.length
                && CharUtils.isAsciiNumeric(c2[p2]); p1++, p2++) {
            if (firstDiff == 0) {
                firstDiff = c1[p1] - c2[p2];
            }
        }

        if (p1 < c1.length && CharUtils.isAsciiNumeric(c1[p1])) {
            return 1;
        }

        if (p2 < c2.length && CharUtils.isAsciiNumeric(c2[p2])) {
            return -1;
        }

        if (firstDiff != 0) {
            return firstDiff;
        }
    }

    return 0;
}

From source file:com.kegare.caveworld.plugin.mceconomy.GuiShopEntry.java

@Override
protected void keyTyped(char c, int code) {
    if (editMode) {
        for (GuiTextField textField : editFieldList) {
            if (code == Keyboard.KEY_ESCAPE) {
                textField.setFocused(false);
            } else if (textField.isFocused()) {
                if (textField != itemField) {
                    if (!CharUtils.isAsciiControl(c) && !CharUtils.isAsciiNumeric(c)) {
                        continue;
                    }/*from w  w  w.j  av a2s .  c  om*/
                }

                textField.textboxKeyTyped(c, code);
            }
        }
    } else {
        if (filterTextField.isFocused()) {
            if (code == Keyboard.KEY_ESCAPE) {
                filterTextField.setFocused(false);
            }

            String prev = filterTextField.getText();

            filterTextField.textboxKeyTyped(c, code);

            String text = filterTextField.getText();
            boolean changed = text != prev;

            if (Strings.isNullOrEmpty(text) && changed) {
                productList.setFilter(null);
            } else if (instantFilter.isChecked() && changed || code == Keyboard.KEY_RETURN) {
                productList.setFilter(text);
            }
        } else {
            if (code == Keyboard.KEY_ESCAPE) {
                actionPerformed(doneButton);
            } else if (code == Keyboard.KEY_BACK) {
                productList.selected = null;
            } else if (code == Keyboard.KEY_TAB) {
                if (++productList.nameType > 2) {
                    productList.nameType = 0;
                }
            } else if (code == Keyboard.KEY_UP) {
                if (isCtrlKeyDown()) {
                    productList.contents.swapTo(productList.contents.indexOf(productList.selected), -1);
                    productList.products.swapTo(productList.products.indexOf(productList.selected), -1);

                    productList.scrollToSelected();
                } else {
                    productList.scrollUp();
                }
            } else if (code == Keyboard.KEY_DOWN) {
                if (isCtrlKeyDown()) {
                    productList.contents.swapTo(productList.contents.indexOf(productList.selected), 1);
                    productList.products.swapTo(productList.products.indexOf(productList.selected), 1);

                    productList.scrollToSelected();
                } else {
                    productList.scrollDown();
                }
            } else if (code == Keyboard.KEY_HOME) {
                productList.scrollToTop();
            } else if (code == Keyboard.KEY_END) {
                productList.scrollToEnd();
            } else if (code == Keyboard.KEY_SPACE) {
                productList.scrollToSelected();
            } else if (code == Keyboard.KEY_PRIOR) {
                productList.scrollToPrev();
            } else if (code == Keyboard.KEY_NEXT) {
                productList.scrollToNext();
            } else if (code == Keyboard.KEY_F || code == mc.gameSettings.keyBindChat.getKeyCode()) {
                filterTextField.setFocused(true);
            } else if (code == Keyboard.KEY_DELETE && productList.selected != null) {
                actionPerformed(removeButton);
            } else if (code == Keyboard.KEY_C && isCtrlKeyDown()) {
                productList.copied = productList.selected == null ? null
                        : new ShopProduct(productList.selected.getProductItem(),
                                productList.selected.getcost());
            } else if (code == Keyboard.KEY_X && isCtrlKeyDown()) {
                keyTyped(Character.MIN_VALUE, Keyboard.KEY_C);
                actionPerformed(removeButton);
            } else if (code == Keyboard.KEY_V && isCtrlKeyDown() && productList.copied != null) {
                ShopProduct entry = new ShopProduct(productList.copied.getProductItem(),
                        productList.copied.getcost());

                if (productList.products.addIfAbsent(entry)) {
                    productList.contents.addIfAbsent(entry);

                    if (productList.selected != null) {
                        productList.contents.swap(productList.contents.indexOf(productList.selected) + 1,
                                productList.contents.size() - 1);
                        productList.products.swap(productList.products.indexOf(productList.selected) + 1,
                                productList.products.size() - 1);
                    }

                    productList.selected = entry;
                    productList.scrollToSelected();
                }
            }
        }
    }
}

From source file:com.kegare.caveworld.client.config.GuiBiomesEntry.java

@Override
protected void keyTyped(char c, int code) {
    if (editMode) {
        for (GuiTextField textField : editFieldList) {
            if (textField.isFocused()) {
                if (code == Keyboard.KEY_ESCAPE) {
                    textField.setFocused(false);
                }/*from  w w  w  .j  a v  a  2s  . c om*/

                if (textField == weightField || textField == terrainMetaField || textField == topMetaField) {
                    if (CharUtils.isAsciiControl(c) || CharUtils.isAsciiNumeric(c)) {
                        textField.textboxKeyTyped(c, code);
                    }
                } else {
                    textField.textboxKeyTyped(c, code);
                }
            }
        }
    } else {
        if (filterTextField.isFocused()) {
            if (code == Keyboard.KEY_ESCAPE) {
                filterTextField.setFocused(false);
            }

            String prev = filterTextField.getText();

            filterTextField.textboxKeyTyped(c, code);

            String text = filterTextField.getText();
            boolean changed = text != prev;

            if (Strings.isNullOrEmpty(text) && changed) {
                biomeList.setFilter(null);
            } else if (instantFilter.isChecked() && changed || code == Keyboard.KEY_RETURN) {
                biomeList.setFilter(text);
            }
        } else {
            if (code == Keyboard.KEY_ESCAPE) {
                actionPerformed(doneButton);
            } else if (code == Keyboard.KEY_BACK) {
                biomeList.selected = null;
            } else if (code == Keyboard.KEY_UP) {
                biomeList.scrollUp();
            } else if (code == Keyboard.KEY_DOWN) {
                biomeList.scrollDown();
            } else if (code == Keyboard.KEY_HOME) {
                biomeList.scrollToTop();
            } else if (code == Keyboard.KEY_END) {
                biomeList.scrollToEnd();
            } else if (code == Keyboard.KEY_SPACE) {
                biomeList.scrollToSelected();
            } else if (code == Keyboard.KEY_PRIOR) {
                biomeList.scrollToPrev();
            } else if (code == Keyboard.KEY_NEXT) {
                biomeList.scrollToNext();
            } else if (code == Keyboard.KEY_F || code == mc.gameSettings.keyBindChat.getKeyCode()) {
                filterTextField.setFocused(true);
            } else if (code == Keyboard.KEY_DELETE && biomeList.selected != null) {
                actionPerformed(removeButton);
            }
        }
    }
}

From source file:com.kegare.caveworld.client.config.GuiVeinsEntry.java

@Override
protected void keyTyped(char c, int code) {
    if (editMode) {
        for (GuiTextField textField : editFieldList) {
            if (code == Keyboard.KEY_ESCAPE) {
                textField.setFocused(false);
            } else if (textField.isFocused()) {
                if (textField != blockField && textField != targetField && textField != biomesField) {
                    if (!CharUtils.isAsciiControl(c) && !CharUtils.isAsciiNumeric(c)) {
                        continue;
                    }/*  w ww . j a v a2s. com*/
                }

                textField.textboxKeyTyped(c, code);
            }
        }
    } else {
        if (filterTextField.isFocused()) {
            if (code == Keyboard.KEY_ESCAPE) {
                filterTextField.setFocused(false);
            }

            String prev = filterTextField.getText();

            filterTextField.textboxKeyTyped(c, code);

            String text = filterTextField.getText();
            boolean changed = text != prev;

            if (Strings.isNullOrEmpty(text) && changed) {
                veinList.setFilter(null);
            } else if (instantFilter.isChecked() && changed || code == Keyboard.KEY_RETURN) {
                veinList.setFilter(text);
            }
        } else {
            if (code == Keyboard.KEY_ESCAPE) {
                actionPerformed(doneButton);
            } else if (code == Keyboard.KEY_BACK) {
                veinList.selected = null;
            } else if (code == Keyboard.KEY_TAB) {
                if (++veinList.nameType > 2) {
                    veinList.nameType = 0;
                }
            } else if (code == Keyboard.KEY_UP) {
                if (isCtrlKeyDown()) {
                    veinList.contents.swapTo(veinList.contents.indexOf(veinList.selected), -1);
                    veinList.veins.swapTo(veinList.veins.indexOf(veinList.selected), -1);

                    veinList.scrollToSelected();
                } else {
                    veinList.scrollUp();
                }
            } else if (code == Keyboard.KEY_DOWN) {
                if (isCtrlKeyDown()) {
                    veinList.contents.swapTo(veinList.contents.indexOf(veinList.selected), 1);
                    veinList.veins.swapTo(veinList.veins.indexOf(veinList.selected), 1);

                    veinList.scrollToSelected();
                } else {
                    veinList.scrollDown();
                }
            } else if (code == Keyboard.KEY_HOME) {
                veinList.scrollToTop();
            } else if (code == Keyboard.KEY_END) {
                veinList.scrollToEnd();
            } else if (code == Keyboard.KEY_SPACE) {
                veinList.scrollToSelected();
            } else if (code == Keyboard.KEY_PRIOR) {
                veinList.scrollToPrev();
            } else if (code == Keyboard.KEY_NEXT) {
                veinList.scrollToNext();
            } else if (code == Keyboard.KEY_F || code == mc.gameSettings.keyBindChat.getKeyCode()) {
                filterTextField.setFocused(true);
            } else if (code == Keyboard.KEY_DELETE && veinList.selected != null) {
                actionPerformed(removeButton);
            } else if (code == Keyboard.KEY_C && isCtrlKeyDown()) {
                veinList.copied = veinList.selected == null ? null
                        : new CaveVein(veinList.selected.getBlock(), veinList.selected.getGenBlockCount(),
                                veinList.selected.getGenWeight(), veinList.selected.getGenRate(),
                                veinList.selected.getGenMinHeight(), veinList.selected.getGenMaxHeight(),
                                veinList.selected.getGenTargetBlock(), veinList.selected.getGenBiomes());
            } else if (code == Keyboard.KEY_X && isCtrlKeyDown()) {
                keyTyped(Character.MIN_VALUE, Keyboard.KEY_C);
                actionPerformed(removeButton);
            } else if (code == Keyboard.KEY_V && isCtrlKeyDown() && veinList.copied != null) {
                ICaveVein entry = new CaveVein(veinList.copied.getBlock(), veinList.copied.getGenBlockCount(),
                        veinList.copied.getGenWeight(), veinList.selected.getGenRate(),
                        veinList.copied.getGenMinHeight(), veinList.copied.getGenMaxHeight(),
                        veinList.copied.getGenTargetBlock(), veinList.copied.getGenBiomes());

                if (veinList.veins.addIfAbsent(entry)) {
                    veinList.contents.addIfAbsent(entry);

                    if (veinList.selected != null) {
                        veinList.contents.swap(veinList.contents.indexOf(veinList.selected) + 1,
                                veinList.contents.size() - 1);
                        veinList.veins.swap(veinList.veins.indexOf(veinList.selected) + 1,
                                veinList.veins.size() - 1);
                    }

                    veinList.selected = entry;
                    veinList.scrollToSelected();
                }
            }
        }
    }
}

From source file:org.cryptomator.ui.controllers.SettingsController.java

private void filterNumericKeyEvents(KeyEvent t) {
    if (t.getCharacter() == null || t.getCharacter().length() == 0) {
        return;//w  ww  .  j  a  v  a 2s .  c o m
    }
    char c = CharUtils.toChar(t.getCharacter());
    if (!(CharUtils.isAsciiNumeric(c) || c == '_')) {
        t.consume();
    }
}

From source file:org.evosuite.junit.naming.variables.DefaultNamingStrategy.java

@Override
public String getVariableName(TestCase testCase, VariableReference var) {
    if (!variableNames.containsKey(var)) {
        String className = var.getSimpleClassName();

        String variableName = className.substring(0, 1).toLowerCase() + className.substring(1);
        if (variableName.contains("[]")) {
            variableName = variableName.replace("[]", "Array");
        }//ww w  .j a  v  a2  s  .  c o  m
        variableName = variableName.replace(".", "_");

        if (CharUtils.isAsciiNumeric(variableName.charAt(variableName.length() - 1)))
            variableName += "_";

        if (!indices.containsKey(variableName)) {
            indices.put(variableName, 0);
        }

        int index = indices.get(variableName);
        indices.put(variableName, index + 1);

        variableName += index;

        put(var, variableName);
    }

    return variableNames.get(var).name;
}