Example usage for java.text CharacterIterator first

List of usage examples for java.text CharacterIterator first

Introduction

In this page you can find the example usage for java.text CharacterIterator first.

Prototype

public char first();

Source Link

Document

Sets the position to getBeginIndex() and returns the character at that position.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    CharacterIterator it = new StringCharacterIterator("abcd");

    char ch = it.first();
    ch = it.current();//from   w ww. j  av  a2s  . c  o  m
    ch = it.next();
    ch = it.current();
    ch = it.last();
    int pos = it.getIndex();
    ch = it.next();
    pos = it.getIndex();
    ch = it.previous();
    ch = it.setIndex(1);
}

From source file:Main.java

public static void main(String[] args) {
    String text = "this is a test";
    CharacterIterator it = new StringCharacterIterator(text, 4, 27, 5);

    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        System.out.print(ch);/*from w  ww . ja  v  a2  s . c  o  m*/
    }
}

From source file:Main.java

public static void main(String[] args) {
    String text = "The quick brown fox jumps over the lazy dog";
    CharacterIterator it = new StringCharacterIterator(text);

    int vowels = 0;
    int consonants = 0;

    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
            vowels = vowels + 1;//  ww  w. j av a 2  s .c  o  m
        } else if (ch != ' ') {
            consonants = consonants + 1;
        }
    }
    System.out.println("Number of vowels: " + vowels);
    System.out.println("Number of consonants: " + consonants);
}

From source file:Main.java

protected static boolean checkNamePart(String s) {
    if (s.length() == 0)
        return true;
    CharacterIterator cIter = new StringCharacterIterator(s);
    char ch = cIter.first();
    if (!checkNameStartChar(ch))
        return false;
    return checkNameTail(cIter);
}

From source file:Main.java

protected static boolean checkPrefixPart(String s) {
    if (s.length() == 0)
        return true;
    CharacterIterator cIter = new StringCharacterIterator(s);
    char ch = cIter.first();
    if (!checkNameStartChar(ch))
        return false;
    if (ch == '_') // Can't start with _ (bnodes labels handled separately) 
        return false;
    return checkNameTail(cIter);
}

From source file:Main.java

public static String escapeNonCustomRegex(String path) {
    /*//from  w  ww  .  j a  v  a  2 s.  com
     * TODO replace with a regular expression
     */
    StringBuilder sb = new StringBuilder();
    boolean inCustomRegion = false;
    CharacterIterator it = new StringCharacterIterator(path);
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {

        if (ch == CUSTOM_REGEX_START) {
            inCustomRegion = true;
        } else if (ch == CUSTOM_REGEX_END) {
            inCustomRegion = false;
        }

        if (REGEX_SPECIAL_CHARS.contains(ch) && !inCustomRegion) {
            sb.append('\\');
        }

        sb.append(ch);
    }

    return sb.toString();
}

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;
    }//from   w  w 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:Utils.java

/**
 * Determine whether the supplied string represents a well-formed fully-qualified Java classname. This utility method enforces
 * no conventions (e.g., packages are all lowercase) nor checks whether the class is available on the classpath.
 * // ww  w  .ja  va  2 s. c o m
 * @param classname
 * @return true if the string is a fully-qualified class name
 */
public static boolean isFullyQualifiedClassname(String classname) {
    if (classname == null)
        return false;
    String[] parts = classname.split("[\\.]");
    if (parts.length == 0)
        return false;
    for (String part : parts) {
        CharacterIterator iter = new StringCharacterIterator(part);
        // Check first character (there should at least be one character for each part) ...
        char c = iter.first();
        if (c == CharacterIterator.DONE)
            return false;
        if (!Character.isJavaIdentifierStart(c) && !Character.isIdentifierIgnorable(c))
            return false;
        c = iter.next();
        // Check the remaining characters, if there are any ...
        while (c != CharacterIterator.DONE) {
            if (!Character.isJavaIdentifierPart(c) && !Character.isIdentifierIgnorable(c))
                return false;
            c = iter.next();
        }
    }
    return true;
}

From source file:com.woonoz.proxy.servlet.UrlRewriterImpl.java

private static String removeLeadingSlashes(final String text) {
    if (text.isEmpty()) {
        return text;
    }// ww w. ja va  2s  .  c  o  m
    final CharacterIterator it = new StringCharacterIterator(text);
    Character c = it.first();
    while (c.equals('/')) {
        c = it.next();
    }
    return text.substring(it.getIndex());
}

From source file:org.mortbay.jetty.load.generator.jenkins.result.LoadResultProjectAction.java

public static List<RunInformations> searchRunInformations(String jettyVersion, ElasticHost elasticHost,
        int maxResult) throws IOException {

    String originalJettyVersion = jettyVersion;

    // jettyVersion 9.4.9*
    //in case jettyVersion is 9.4.9.v20180320 we need to replace with 9.4.9*
    if (StringUtils.contains(jettyVersion, 'v')) {
        jettyVersion = StringUtils.substringBeforeLast(jettyVersion, ".v");
    }//  w  w w .j  av  a2 s  . co  m
    // FIXME investigate elastic but query such 9.4.10-SNAPSHOT doesn't work...
    // so using 9.4.10* then filter response back....
    if (StringUtils.contains(jettyVersion, "-SNAPSHOT")) {
        jettyVersion = StringUtils.substringBeforeLast(jettyVersion, "-SNAPSHOT");
    }

    // in case of 9.4.11-NO-LOGGER-SNAPSHOT still not working with elastic
    // here we must have only number or . so remove everything else

    StringBuilder versionQuery = new StringBuilder();
    CharacterIterator ci = new StringCharacterIterator(jettyVersion);
    for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) {
        if (NumberUtils.isCreatable(Character.toString(c)) || c == '.') {
            versionQuery.append(c);
        }
    }

    jettyVersion = versionQuery.toString() + "*";

    try (ElasticResultStore elasticResultStore = elasticHost.buildElasticResultStore(); //
            InputStream inputStream = LoadResultProjectAction.class
                    .getResourceAsStream("/versionResult.json")) {
        String versionResultQuery = IOUtils.toString(inputStream);
        Map<String, String> map = new HashMap<>(1);
        map.put("jettyVersion", jettyVersion);
        map.put("maxResult", Integer.toString(maxResult));
        versionResultQuery = StrSubstitutor.replace(versionResultQuery, map);

        String results = elasticResultStore.search(versionResultQuery);

        List<LoadResult> loadResults = ElasticResultStore
                .map(new HttpContentResponse(null, results.getBytes(), null, null));

        List<RunInformations> runInformations = //
                loadResults.stream() //
                        .filter(loadResult -> StringUtils.equalsIgnoreCase(originalJettyVersion, //
                                loadResult.getServerInfo().getJettyVersion())) //
                        .map(loadResult -> new RunInformations(
                                loadResult.getServerInfo().getJettyVersion() + ":"
                                        + loadResult.getServerInfo().getGitHash(), //
                                loadResult.getCollectorInformations(),
                                StringUtils.lowerCase(loadResult.getTransport())) //
                                        .jettyVersion(loadResult.getServerInfo().getJettyVersion()) //
                                        .estimatedQps(LoadTestResultBuildAction.estimatedQps(
                                                LoadTestResultBuildAction.getLoaderConfig(loadResult))) //
                                        .serverInfo(loadResult.getServerInfo())) //
                        .collect(Collectors.toList());

        Collections.sort(runInformations, Comparator.comparing(o -> o.getStartTimeStamp()));
        return runInformations;
    }

}