Example usage for org.apache.commons.lang3 StringUtils countMatches

List of usage examples for org.apache.commons.lang3 StringUtils countMatches

Introduction

In this page you can find the example usage for org.apache.commons.lang3 StringUtils countMatches.

Prototype

public static int countMatches(final CharSequence str, final char ch) 

Source Link

Document

Counts how many times the char appears in the given string.

A null or empty ("") String input returns 0 .

 StringUtils.countMatches(null, *)       = 0 StringUtils.countMatches("", *)         = 0 StringUtils.countMatches("abba", 0)  = 0 StringUtils.countMatches("abba", 'a')   = 2 StringUtils.countMatches("abba", 'b')  = 2 StringUtils.countMatches("abba", 'x') = 0 

Usage

From source file:net.sourceforge.pmd.lang.java.ast.CommentTest.java

private int lineCount(String filtered) {
    return StringUtils.countMatches(filtered, PMD.EOL) + 1;
}

From source file:net.sourceforge.pmd.lang.java.rule.bestpractices.AvoidUsingHardCodedIPRule.java

protected boolean isIPv6(final char firstChar, String s, final boolean checkIPv6,
        final boolean checkIPv4MappedIPv6) {
    // Quick check before using Regular Expression
    // 1) At least 3 characters
    // 2) 1st must be a Hex number or a : (colon)
    // 3) Must contain at least 2 colons (:)
    if (s.length() < 3 || !(isHexCharacter(firstChar) || firstChar == ':')
            || StringUtils.countMatches(s, ':') < 2) {
        return false;
    }//from   w w w . ja v a  2  s  .  c  o  m

    Matcher matcher = IPV6_PATTERN.matcher(s);
    if (matcher.matches()) {
        // Account for leading or trailing :: before splitting on :
        boolean zeroSubstitution = false;
        if (s.startsWith("::")) {
            s = s.substring(2);
            zeroSubstitution = true;
        } else if (s.endsWith("::")) {
            s = s.substring(0, s.length() - 2);
            zeroSubstitution = true;
        }

        // String.split() doesn't produce an empty String in the trailing
        // case, but it does in the leading.
        if (s.endsWith(":")) {
            return false;
        }

        // All the intermediate parts must be hexidecimal, or
        int count = 0;
        boolean ipv4Mapped = false;
        String[] parts = s.split(":");
        for (int i = 0; i < parts.length; i++) {
            final String part = parts[i];
            // An empty part indicates :: was encountered. There can only be
            // 1 such instance.
            if (part.length() == 0) {
                if (zeroSubstitution) {
                    return false;
                } else {
                    zeroSubstitution = true;
                }
                continue;
            } else {
                count++;
            }
            // Should be a hexidecimal number in range [0, 65535]
            try {
                int value = Integer.parseInt(part, 16);
                if (value < 0 || value > 65535) {
                    return false;
                }
            } catch (NumberFormatException e) {
                // The last part can be a standard IPv4 address.
                if (i != parts.length - 1 || !isIPv4(firstChar, part)) {
                    return false;
                }
                ipv4Mapped = true;
            }
        }

        // IPv6 addresses are 128 bit, are we that long?
        if (zeroSubstitution) {
            if (ipv4Mapped) {
                return checkIPv4MappedIPv6 && 1 <= count && count <= 6;
            } else {
                return checkIPv6 && 1 <= count && count <= 7;
            }
        } else {
            if (ipv4Mapped) {
                return checkIPv4MappedIPv6 && count == 7;
            } else {
                return checkIPv6 && count == 8;
            }
        }
    } else {
        return false;
    }
}

From source file:net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryFullyQualifiedNameRule.java

private boolean isJavaLangImplicit(AbstractJavaTypeNode node) {
    String name = node.getImage();
    boolean isJavaLang = name != null && name.startsWith("java.lang.");

    if (isJavaLang && node.getType() != null && node.getType().getPackage() != null) {
        // valid would be ProcessBuilder.Redirect.PIPE but not java.lang.ProcessBuilder.Redirect.PIPE
        String packageName = node.getType().getPackage() // package might be null, if type is an array type...
                .getName();/* ww w  .  j  av  a  2s  . c o m*/
        return "java.lang".equals(packageName);
    } else if (isJavaLang) {
        // only java.lang.* is implicitly imported, but not e.g. java.lang.reflection.*
        return StringUtils.countMatches(name, '.') == 2;
    }
    return false;
}

From source file:net.sourceforge.pmd.lang.java.rule.errorprone.InvalidSlf4jMessageFormatRule.java

private int countPlaceholders(final ASTExpression node) {
    // zero means, no placeholders, or we could not analyze the message parameter
    int result = 0;

    try {/*  ww w  .  j a  v a 2 s . c om*/
        List<Node> literals = node.findChildNodesWithXPath(
                "AdditiveExpression/PrimaryExpression/PrimaryPrefix/Literal[@StringLiteral='true']"
                        + "|PrimaryExpression/PrimaryPrefix/Literal[@StringLiteral='true']");
        // if there are multiple literals, we just assume, they are concatenated
        // together...
        for (Node stringLiteral : literals) {
            result += StringUtils.countMatches(stringLiteral.getImage(), "{}");
        }
    } catch (JaxenException e) {
        LOG.log(Level.FINE, "Could not determine literals", e);
    }
    return result;
}

From source file:net.sourceforge.pmd.lang.java.rule.logging.InvalidSlf4jMessageFormatRule.java

private int countPlaceholders(final AbstractJavaTypeNode node) {
    int result = 0; // zero means, no placeholders, or we could not analyze the message parameter
    ASTLiteral stringLiteral = node.getFirstDescendantOfType(ASTLiteral.class);
    if (stringLiteral != null) {
        result = StringUtils.countMatches(stringLiteral.getImage(), "{}");
    }//from  w  w w.  j  ava2s. c o  m
    return result;
}

From source file:net.sourceforge.pmd.lang.xml.ast.DOMLineNumbers.java

private void calculateLinesMap() {
    lines = new TreeMap<Integer, Integer>();
    int index = -1;
    int count = StringUtils.countMatches(xmlString, "\n");
    for (int line = 1; line <= count; line++) {
        lines.put(line, index + 1);/* w w  w  .  j a  va2  s. co m*/
        index = xmlString.indexOf("\n", index + 1);
    }
    lines.put(count + 1, index + 1);
}

From source file:net.sourceforge.seqware.pipeline.plugins.BasicDeciderET.java

@Test
public void runBasicDecider() throws IOException {
    String listCommand = "-p net.sourceforge.seqware.pipeline.deciders.BasicDecider -- --all --wf-accession 6685 --parent-wf-accessions 4767 --test";
    String listOutput = ITUtility.runSeqWareJar(listCommand, ReturnValue.SUCCESS, null);
    Log.info(listOutput);/*from   w w  w  .j av a  2  s .  com*/
    Assert.assertTrue("expected to see 3 launches, found " + StringUtils.countMatches(listOutput, "java -jar"),
            StringUtils.countMatches(listOutput, "java -jar") == 3);
}

From source file:net.sourceforge.seqware.pipeline.plugins.BasicDeciderET.java

@Test
public void createDeciderFromArchetype() throws IOException {
    File createTempDir = Files.createTempDir();
    // generate , build and install the decider archetype
    String command = "mvn archetype:generate -DarchetypeCatalog=local -Dpackage=com.github.seqware -DgroupId=com.github.seqware "
            + "-DarchetypeArtifactId=seqware-archetype-decider -Dversion=1.0-SNAPSHOT -DarchetypeGroupId=com.github.seqware "
            + "-DartifactId=decider-HelloWorld -Dworkflow-name=HelloWorld " + "-B -Dgoals=install";
    String genOutput = ITUtility.runArbitraryCommand(command, 0, createTempDir);
    Log.info(genOutput);// w  ww.  j  a  v  a  2  s .  com
    // run the decider
    File seqwareJar = ITUtility.retrieveFullAssembledJar();
    String SEQWARE_VERSION = ReturnValue.class.getPackage().getImplementationVersion();
    command = "java -cp " + createTempDir.getAbsolutePath()
            + "/decider-HelloWorld/target/Decider_1.0-SNAPSHOT_HelloWorld_1.0_SeqWare_" + SEQWARE_VERSION
            + ".jar:" + seqwareJar.getAbsolutePath()
            + " net.sourceforge.seqware.pipeline.runner.PluginRunner -p com.github.seqware.HelloWorldDecider -- --all --wf-accession 6685 --parent-wf-accessions 4767 --test";
    genOutput = ITUtility.runArbitraryCommand(command, 0, createTempDir);
    Log.info(genOutput);
    Assert.assertTrue("expected to see 1 launches, found " + StringUtils.countMatches(genOutput, "java -jar"),
            StringUtils.countMatches(genOutput, "java -jar") == 1);
}

From source file:net.sourceforge.seqware.pipeline.plugins.CLI_ET.java

@Test
public void listBundles() throws IOException {
    String listCommand = " workflow list";
    String listOutput = ITUtility.runSeqwareCLI(listCommand, ReturnValue.SUCCESS, null);
    int countOccurrencesOf = StringUtils.countMatches(listOutput, "RECORD");
    Assert.assertTrue("incorrect number of expected bundles", countOccurrencesOf == 20);
}

From source file:net.sourceforge.seqware.pipeline.plugins.CLI_ET.java

@Test
public void workflowRunReport() throws IOException {
    String listCommand = " workflow-run report --accession 6603";
    String listOutput = ITUtility.runSeqwareCLI(listCommand, ReturnValue.SUCCESS, null);
    int countOccurrencesOf = StringUtils.countMatches(listOutput, "RECORD");
    Assert.assertTrue("incorrect number of expected bundles", countOccurrencesOf == 1);
}