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:au.org.ala.delta.util.Utils.java

public static char GetNextChar(String RTFString, int[] startPos, int[] endPos) {
    char result = 0;
    int skipLevel = 0;
    endPos[0] = RTFString.length();/*  w  w w.  ja v  a 2  s. c  o m*/
    while (result == 0 && startPos[0] < endPos[0]) {
        char ch = RTFString.charAt(startPos[0]);
        if (ch == '{' || ch == '}') {
            ++startPos[0];
            if (skipLevel != 0) {
                if (ch == '{') {
                    ++skipLevel;
                } else {
                    --skipLevel;
                }
            }
        } else if (skipLevel != 0) {
            ++startPos[0];
        } else if (ch == '\\') {
            int cmdStart = startPos[0] + 1;
            if (cmdStart >= endPos[0]) {
                // A pathological case - not actually good RTF

                result = ch;
            } else {
                ch = RTFString.charAt(cmdStart);
                if (Character.isLetter(ch)) {
                    int[] curPos = new int[] { cmdStart };
                    while (++curPos[0] < endPos[0] && Character.isLetter(RTFString.charAt(curPos[0]))) {
                    }

                    String test = RTFString.substring(cmdStart, cmdStart + curPos[0] - cmdStart);

                    int numStart = curPos[0];
                    boolean hasParam = false;
                    if (curPos[0] < endPos[0] && (RTFString.charAt(curPos[0]) == '-'
                            || Character.isDigit(RTFString.charAt(curPos[0])))) {
                        hasParam = true;
                        while (++curPos[0] < endPos[0] && Character.isDigit(RTFString.charAt(curPos[0]))) {
                        }
                    }

                    if (curPos[0] < endPos[0] && RTFString.charAt(curPos[0]) == ' ') {
                        ++curPos[0];
                    }

                    for (int i = 0; i < nSkipWords; ++i) {
                        if (skipWords[i] == test) {
                            skipLevel = 1;
                            break;
                        }
                    }
                    if (skipLevel != 0) {

                    } else if (test == "u") {
                        // Actually had RTF unicode...
                        result = (char) Integer.parseInt(RTFString.substring(numStart, curPos[0] - numStart));
                        char ansiVal = GetNextChar(RTFString, curPos, endPos);
                        curPos[0] = endPos[0];
                        result |= ansiVal << 16;
                    } else if (!hasParam) {
                        // Currently match only parameter-less commands
                        for (int i = 0; i < nRTFCmds; ++i) {
                            if (RTFreps[i].cmdString == test) {
                                result = RTFreps[i].unicodeValue;
                                if (result > 0x100)
                                    result |= (char) RTFreps[i].repString.charAt(0) << 16;
                            }
                        }
                    }
                    if (result != 0) {
                        // && endPos == RTFString.size())

                        endPos[0] = curPos[0];
                    } else {
                        startPos[0] = curPos[0];
                    }
                } else if (ch == '{' || ch == '}' || ch == '\\') {
                    result = ch;
                    endPos[0] = cmdStart + 1;
                } else if (ch == '~') {
                    result = 0xa0;
                    endPos[0] = cmdStart + 1;
                } else if (ch == '-') {
                    result = 0xad;
                    endPos[0] = cmdStart + 1;
                } else if (ch == '\'' && cmdStart + 2 < endPos[0]) {
                    char[] buff = new char[2];
                    buff[0] = RTFString.charAt(cmdStart + 1);
                    buff[1] = RTFString.charAt(cmdStart + 2);

                    result = (char) Integer.parseInt(new String(buff), 16);
                    endPos[0] = cmdStart + 1 + 2;
                } else {
                    result = ch;
                    endPos[0] = cmdStart + 1;
                }
            }
        } else if (!Character.isISOControl(ch) || ch >= 0x80) {
            if (ch >= 0x80 && ch < 0xa0) {
                result = (char) (winANSIChars[ch - 0x80] | ch << 16);
            } else {
                result = ch;
            }
            endPos[0] = startPos[0] + 1;
        } else
            ++startPos[0];
    }

    if ((result >> 16) == 0)
        result |= (result << 16);

    return result;
}

From source file:com.flipkart.phantom.runtime.impl.server.netty.handler.command.CommandInterpreter.java

/**
 * Helper method to read and return a ProxyCommand from an input {@link InputStream}
 * @param inputStream the InputStream instance
 * @param isFramedTransport boolean indicator that defines mechanism for reporting errors - Exceptions vs a ProxyCommand with error description
 * @return the read ProxyCommand//w w w .  j a va  2  s  . co m
 * @throws Exception in case of errors
 */
private ProxyCommand interpretCommand(InputStream inputStream, boolean isFramedTransport) throws Exception {
    ProxyCommand readCommand = null;
    byte[] readBytes = new byte[MAX_COMMAND_INPUT];

    int byteReadIndex = 0, commandEndIndex = 0, dataStartIndex = 0, dataLength = 0;
    while (byteReadIndex < MAX_COMMAND_INPUT) {
        int bytesRead = inputStream.read(readBytes, byteReadIndex, MAX_COMMAND_INPUT - byteReadIndex); // try to read as much as is available into the byte array
        if (bytesRead <= 0) { // check if no data was read at all. Throw an IllegalArgumentException to indicate unexpected end of stream
            if (isFramedTransport) {
                throw new IllegalArgumentException(
                        "Invalid read. Encountered end of stream before reading a single byte");
            } else {
                return new ProxyCommand(ReadFailure.INSUFFICIENT_DATA,
                        "Invalid read. Encountered end of stream before reading a single byte");
            }
        }
        for (int i = 0; i < bytesRead; i++) { // look for the NEW_LINE character that signals end of command and params input
            if (readBytes[byteReadIndex + i] == LINE_FEED) {
                commandEndIndex = byteReadIndex + i;
                break;
            }
        }
        if (bytesRead > 0) {
            byteReadIndex += bytesRead; // skip the read bytes by moving the index for next read
        }
        if (commandEndIndex > 0 || bytesRead <= 0) { // break the read loop if end of command line is reached (or) EOS (end of stream is reached)                                                  
            break; // i.e. no more data available for read
        }
    }

    if (commandEndIndex == 0) { // report a suitable error if NEW_LINE was not encountered at all (or) if bytes read has exceeded MAX_COMMAND_INPUT
        if (byteReadIndex < MAX_COMMAND_INPUT) {
            if (isFramedTransport) {
                throw new IllegalArgumentException(
                        "Stream ended before encountering a \\n: " + new String(readBytes, 0, byteReadIndex));
            } else {
                return new ProxyCommand(ReadFailure.INSUFFICIENT_DATA,
                        "Stream ended before encountering a \\n: " + new String(readBytes, 0, byteReadIndex));
            }
        } else {
            throw new IllegalArgumentException("Maximum command line size allowed: " + MAX_COMMAND_INPUT
                    + " Command : " + new String(readBytes, 0, byteReadIndex));
        }
    }

    // The input data appears to adhere to the command protocol. Proceed to read the command, params and data
    dataStartIndex = commandEndIndex + 1;
    if (readBytes[commandEndIndex - 1] == CARRIAGE_RETURN) {
        commandEndIndex--; // handle the CR for people who still haven't moved on from telnet to netcat
    }

    byte delimiter = DEFAULT_DELIM;
    int fragmentStart = 0;
    if (!(readBytes[0] >= ASCII_LOW[0] && readBytes[0] <= ASCII_LOW[1])
            && !(readBytes[0] >= ASCII_HIGH[0] && readBytes[0] <= ASCII_HIGH[1])) {
        delimiter = readBytes[0]; // the delimiter is not DEFAULT_DELIM but the non-ascii character appearing as the first byte
        fragmentStart = 1;
    }
    int fragmentIndex = this.getNextCommandFragmentPosition(readBytes, fragmentStart, commandEndIndex,
            delimiter);
    readCommand = new ProxyCommand(new String(readBytes, fragmentStart, fragmentIndex - fragmentStart));

    Map<String, String> commandParams = new HashMap<String, String>();
    // gather params
    while (fragmentIndex < commandEndIndex) {
        // skip initial delims
        while (fragmentIndex < commandEndIndex && readBytes[fragmentIndex] == delimiter) {
            fragmentIndex++;
        }
        if (fragmentIndex == commandEndIndex) {
            break;
        }
        // read first char
        if (Character.isDigit((char) readBytes[fragmentIndex])) {
            // this is the datalen
            try {
                dataLength = Integer
                        .parseInt(new String(readBytes, fragmentIndex, commandEndIndex - fragmentIndex));
                break;
            } catch (Exception e) {
                throw new IllegalArgumentException("Invalid syntax in command: " + new String(readBytes), e);
            }
        } else {
            fragmentStart = fragmentIndex;
            fragmentIndex = getNextCommandFragmentPosition(readBytes, fragmentIndex + 1, commandEndIndex,
                    delimiter);
            int paramValueSepIndex = 0;
            for (int i = fragmentStart; i < fragmentIndex; i++) {
                if (readBytes[i] == PARAM_VALUE_SEP) {
                    paramValueSepIndex = i;
                    break;
                }
            }
            if (paramValueSepIndex > 0) {
                commandParams.put(new String(readBytes, fragmentStart, paramValueSepIndex - fragmentStart),
                        new String(readBytes, paramValueSepIndex + 1, fragmentIndex - paramValueSepIndex - 1));
            } else {
                commandParams.put(new String(readBytes, fragmentStart, fragmentIndex - fragmentStart),
                        DEFAULT_PARAM_VALUE); // initialize with default value if none specified
            }
            // set the params on the ProxyCommand object
            readCommand.setCommandParams(commandParams);
        }
    }

    if (dataLength > 0) {
        byte[] commandData = new byte[dataLength];
        int dataByteReadIndex = byteReadIndex - dataStartIndex;
        if (dataStartIndex < byteReadIndex) {
            System.arraycopy(readBytes, dataStartIndex, commandData, 0, dataByteReadIndex);
        }
        while (dataByteReadIndex < dataLength) {
            if (inputStream.available() < (dataLength - dataByteReadIndex)) {
                if (!isFramedTransport) { // check if all data bytes have been received. Return immediately for non framed transports
                    return new ProxyCommand(ReadFailure.INSUFFICIENT_DATA,
                            "Stream ended before all data was read. Length of data bytes needed : "
                                    + (dataLength - dataByteReadIndex));
                }
            }
            int actualBytesRead = inputStream.read(commandData, dataByteReadIndex,
                    dataLength - dataByteReadIndex);
            if (actualBytesRead <= 0) { // 0 bytes not possible because dataLength-dataByteReadIndex is non-zero, -1 is returned if no byte is available because the stream is at end of file (as per Javadocs)
                throw new IllegalArgumentException(
                        "Insufficient bytes read for command : " + readCommand.getCommand() + ". Expected : "
                                + (dataLength - dataByteReadIndex) + " but read : " + actualBytesRead);
            }
            dataByteReadIndex += actualBytesRead;
        }
        // set the command data on the ProxyCommand object
        readCommand.setCommandData(commandData);
    }
    return readCommand;
}

From source file:io.onedecision.engine.decisions.converter.DecisionModelConverter.java

private String toId(String name) {
    String id = name.replace(' ', '_').replace('-', '_');
    if (Character.isDigit(id.charAt(0))) {
        id = '_' + id;
    }/*www . jav a 2s  .  c  o  m*/
    return id;
}

From source file:de.alpharogroup.string.StringUtils.java

/**
 * Checks if the given String is an Number.
 *
 * @param testString// w  w w . ja va2s. c o  m
 *            The String to check.
 * @return true if the given String is a number otherwise false.
 * @deprecated use instead {@link StringExtensions#isNumber(String)}
 */
@Deprecated
public static final boolean isNumber(final String testString) {
    if (null == testString) {
        return false;
    }
    for (int i = 0; i < testString.length(); i++) {
        if (Character.isDigit(testString.charAt(i)) == false) {
            return false;
        }
    }
    return true;
}

From source file:herudi.controller.productController.java

@FXML
private void aksiQuantity(KeyEvent event) {
    char[] data = txtQuantityCode.getText().toCharArray();
    boolean valid = true;
    for (char c : data) {
        if (!Character.isDigit(c)) {
            valid = false;//from   w  w w. j av  a 2  s.c  om
            break;
        }
    }
    if (!valid) {
        config2.dialog(Alert.AlertType.ERROR, "Please, Fill With Number");
        txtQuantityCode.clear();
        txtQuantityCode.requestFocus();
    }
}

From source file:com.zb.jcseg.util.WordUnionUtils.java

private static boolean isEndWithDigit(String str) {
    if (StringUtils.isEmpty(str)) {
        return false;
    }// w w  w  .  j  a  va 2s  .  c  o m
    return Character.isDigit(str.charAt(str.length() - 1));
}

From source file:com.atomicleopard.thundr.ftp.commons.FTP.java

private boolean __lenientCheck(String line) {
    return (!(line.length() > REPLY_CODE_LEN && line.charAt(REPLY_CODE_LEN) != '-'
            && Character.isDigit(line.charAt(0))));
}

From source file:com.px100systems.util.SpringELCtx.java

/**
 * Format American phone as (xxx) yyy-zzzz
 *//*from   www. j a  v a  2s  . c om*/
public static String formatPhone(String phone) {
    if (phone == null)
        return null;

    StringBuilder ph = new StringBuilder();
    for (char ch : phone.toCharArray())
        if (Character.isDigit(ch))
            ph.append(ch);
    phone = ph.toString();

    if (phone.length() < LOCAL_PHONE_LENGTH)
        return phone;

    if (phone.length() == LOCAL_PHONE_LENGTH)
        return phone.substring(0, PHONE_DASH_POS) + "-" + phone.substring(PHONE_DASH_POS);

    int localPos = phone.length() - LOCAL_PHONE_LENGTH;
    return "(" + phone.substring(0, localPos) + ") " + formatPhone(phone.substring(localPos));
}

From source file:com.zb.jcseg.util.WordUnionUtils.java

public static boolean isStartWithDigit(String str) {
    if (StringUtils.isEmpty(str)) {
        return false;
    }/*from   w  w  w . j av a2  s .c om*/
    return Character.isDigit(str.charAt(0));
}

From source file:edu.ku.brc.specify.plugins.TaxonLabelFormatting.java

/**
 * Formats the same String./*from   w w w.j a v a  2s  . c  om*/
 */
protected void doFormatting() {
    drawList.clear();

    Object fmtObj = formatCBX.getValue();
    if (fmtObj == null) {
        return;
    }

    String format = fmtObj.toString();
    //System.out.println("["+format+"]");
    if (StringUtils.isNotEmpty(format) && format.length() > 0) {
        DefaultListModel model = (DefaultListModel) authorsList.getModel();

        StringBuilder chars = new StringBuilder();
        StringBuilder exp = new StringBuilder();
        int len = format.length();
        StringBuilder pat = new StringBuilder();
        for (int i = 0; i < len; i++) {
            char ch = format.charAt(i);
            if (ch == '%') {
                if (chars.length() > 0) {
                    drawList.add(new TextDrawInfo(FormatType.Plain, chars.toString()));
                    chars.setLength(0);
                }
                ch = format.charAt(++i);
                pat.setLength(0);
                pat.append('%');
                do {
                    if (Character.isLetter(ch) || Character.isDigit(ch)) {
                        pat.append(ch);
                    } else {
                        i--;
                        break;
                    }
                    i++;
                    if (i < len) {
                        ch = format.charAt(i);
                    }
                } while (i < len);

                FormatType ft = FormatType.Plain;
                String val = "";
                String token = pat.toString();
                if (token.equals("%S")) {
                    val = getByRank(taxon, 220).getName();
                    ft = FormatType.Italic;

                } else if (token.equals("%G")) {
                    val = getByRank(taxon, 180).getName();
                    ft = FormatType.Italic;

                } else if (token.charAt(1) == 'A') {
                    int authNum = Integer.parseInt(token.substring(2)) - 1;
                    if (authNum < model.getSize()) {
                        val = ((Agent) model.get(authNum)).getLastName();
                    } else {
                        val = token;
                    }
                }
                exp.append(val);
                drawList.add(new TextDrawInfo(ft, val));
            } else {
                exp.append(ch);
                chars.append(ch);
            }
        }
        if (chars.length() > 0) {
            drawList.add(new TextDrawInfo(FormatType.Plain, chars.toString()));
            chars.setLength(0);
        }
        specialLabel.repaint();
    }
}