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:dk.dma.db.cassandra.CassandraConnection.java

private static boolean seedsContainPortNumbers(Collection<String> connectionPoints) {
    requireNonNull(connectionPoints);/*from w  w w.j av a  2  s . co  m*/
    return connectionPoints.stream().filter(cp -> StringUtils.countMatches(cp, ":") == 1).count() > 0;
}

From source file:com.cognifide.aet.job.common.collectors.accessibility.AccessibilityCollector.java

private void getElementsPositions(List<AccessibilityIssue> issues, final String html) {
    for (AccessibilityIssue issue : issues) {
        int indexOfElement = html.indexOf(issue.getElementString());
        if (indexOfElement >= 0) {
            String beforeOccurrence = html.substring(0, indexOfElement);
            int lineBreaks = StringUtils.countMatches(beforeOccurrence, "\n");
            int columnNumber;
            if (lineBreaks > 0) {
                int indexOfLastLineBreak = beforeOccurrence.lastIndexOf('\n');
                columnNumber = beforeOccurrence.substring(indexOfLastLineBreak).length();
            } else {
                columnNumber = beforeOccurrence.length();
            }//from   ww w  .j a v  a 2 s  . c  o  m
            issue.setLineNumber(lineBreaks + 1);
            issue.setColumnNumber(columnNumber);
        }
    }
}

From source file:hammingcode.HammingCode.java

Boolean checkParity(String parityString, String parityKind) {
    int ones = StringUtils.countMatches(parityString, "1");
    if (StringUtils.equalsIgnoreCase(parityKind, "even")) {
        if (ones % 2 == 0) {
            return true;
        }//from   www  .  jav a  2  s. co  m
        return false;
    } else if (StringUtils.equalsIgnoreCase(parityKind, "odd")) {
        if (ones % 2 == 1) {
            return true;
        }
        return false;
    }
    return null;
}

From source file:ambroafb.general.StagesContainer.java

private static boolean isDirectChildPath(String childPath, String ownerPath, String delimiter) {
    return childPath.startsWith(ownerPath) && !childPath.equals(ownerPath)
            && StringUtils.countMatches(childPath, delimiter) - 1 == StringUtils.countMatches(ownerPath,
                    delimiter);/*from  w  w w  .j  a  v a 2  s.co  m*/
}

From source file:net.ripe.ipresource.Ipv6Address.java

private static String expandMissingColons(String ipAddressString) {
    int colonCount = isInIpv4EmbeddedIpv6Format(ipAddressString) ? COLON_COUNT_FOR_EMBEDDED_IPV4
            : COLON_COUNT_IPV6;/*from  w  w w  .j  a v  a2  s  .c  o  m*/
    return ipAddressString.replace("::",
            StringUtils.repeat(":", colonCount - StringUtils.countMatches(ipAddressString, ":") + 2));
}

From source file:de.dennishoersch.web.css.parser.ParserTest.java

private TypeSafeMatcher<String> countOf(final String s, final int count) {
    return new TypeSafeMatcher<String>() {

        @Override/* w ww  . j av  a  2  s  .  c  om*/
        public void describeTo(Description description) {
            description.appendText("Expected that the String '" + s + "' occours " + count + " times!");
        }

        @Override
        protected boolean matchesSafely(String input) {
            return StringUtils.countMatches(input, s) == count;
        }
    };
}

From source file:com.blackducksoftware.integration.hub.detect.detector.gradle.GradleReportLine.java

public int getTreeLevel() {
    if (isRootLevel()) {
        return 0;
    }/*  w w w .  ja  v  a2  s . com*/

    String modifiedLine = removeDependencyIndicators();

    if (!modifiedLine.startsWith("|")) {
        modifiedLine = "|" + modifiedLine;
    }
    modifiedLine = modifiedLine.replace("     ", "    |");
    modifiedLine = modifiedLine.replace("||", "|");
    if (modifiedLine.endsWith("|")) {
        modifiedLine = modifiedLine.substring(0, modifiedLine.length() - 5);
    }
    final int matches = StringUtils.countMatches(modifiedLine, "|");

    return matches;
}

From source file:de.micromata.mgc.jpa.hibernatesearch.impl.SearchEmgrFactoryRegistryUtils.java

private static boolean includeNested(String name, SearchColumnMetadata scm, IndexedEmbedded indexedEmbedded,
        int curDepth, int maxDepth) {
    if (indexedEmbedded.includeEmbeddedObjectId() == false && scm.isIdField() == true) {
        return false;
    }//from   w  ww. j  a v a  2 s  . com
    int dotcount = StringUtils.countMatches(name, ".");
    if (dotcount > 0) {
        return false;
    }
    if (ArrayUtils.isEmpty(indexedEmbedded.includePaths()) == true) {
        return true;
    }
    for (String incp : indexedEmbedded.includePaths()) {
        String[] splittet = StringUtils.split(incp, '.');
        if (ArrayUtils.getLength(splittet) <= curDepth) {
            continue;
        }
        if (splittet[curDepth].equals(name) == true) {
            return true;
        }
    }
    return false;
}

From source file:com.adobe.cq.wcm.core.components.internal.models.v1.LanguageNavigationImpl.java

private Page getLocalizedPage(Page page, Page languageRoot) {
    Page localizedPage;//from   w  ww  .j  a  va 2s  . c  o m
    String path = languageRoot.getPath();
    String relativePath = page.getPath();
    if (relativePath.startsWith(path)) {
        localizedPage = page;
    } else {
        String separator = "/";
        int i = relativePath.indexOf(separator);
        int occurrence = StringUtils.countMatches(path, separator) + 1;
        while (--occurrence > 0 && i != -1) {
            i = relativePath.indexOf(separator, i + 1);
        }
        relativePath = (i > 0) ? relativePath.substring(i) : "";
        path = path.concat(relativePath);
        PageManager pageManager = page.getPageManager();
        localizedPage = pageManager.getPage(path);
    }
    return localizedPage;
}

From source file:com.neophob.sematrix.listener.TcpServer.java

/**
 * tcp server thread.//from w  ww.j a  v a 2 s. c om
 */
public void run() {
    LOG.log(Level.INFO, "Ready receiving messages...");
    while (Thread.currentThread() == runner) {

        if (tcpServer != null) {
            try {

                //check if client is available
                if (client != null && client.active()) {
                    //do not send sound status to gui - very cpu intensive!
                    //sendSoundStatus();

                    if ((count % 20) == 2 && Collector.getInstance().isRandomMode()) {
                        sendStatusToGui();
                    }
                }

                Client c = tcpServer.available();
                if (c != null && c.available() > 0) {

                    //clean message
                    String msg = lastMsg + StringUtils.replace(c.readString(), "\n", "");
                    //add replacement end string
                    msg = StringUtils.replace(msg, FUDI_ALTERNATIVE_END_MARKER, FUDI_MSG_END_MARKER);
                    msg = StringUtils.trim(msg);

                    int msgCount = StringUtils.countMatches(msg, FUDI_MSG_END_MARKER);
                    LOG.log(Level.INFO, "Got Message: {0} cnt: {1}", new Object[] { msg, msgCount });

                    //work around bug - the puredata gui sends back a message as soon we send one
                    long delta = System.currentTimeMillis() - lastMessageSentTimestamp;
                    if (delta < FLOODING_TIME) {
                        LOG.log(Level.INFO, "Ignore message, flooding protection ({0}<{1})",
                                new String[] { "" + delta, "" + FLOODING_TIME });
                        //delete message
                        msgCount = 0;
                        msg = "";
                    }

                    //ideal, one message receieved
                    if (msgCount == 1) {
                        msg = StringUtils.removeEnd(msg, FUDI_MSG_END_MARKER);
                        lastMsg = "";
                        processMessage(StringUtils.split(msg, ' '));
                    } else if (msgCount == 0) {
                        //missing end of message... save it
                        lastMsg = msg;
                    } else {
                        //more than one message receieved, split it
                        //TODO: reuse partial messages
                        lastMsg = "";
                        String[] msgs = msg.split(FUDI_MSG_END_MARKER);
                        for (String s : msgs) {
                            s = StringUtils.trim(s);
                            s = StringUtils.removeEnd(s, FUDI_MSG_END_MARKER);
                            processMessage(StringUtils.split(s, ' '));
                        }
                    }
                }
            } catch (Exception e) {
            }
        }

        count++;
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            //Ignored
        }

    }
}