Example usage for org.apache.commons.lang3 CharSetUtils count

List of usage examples for org.apache.commons.lang3 CharSetUtils count

Introduction

In this page you can find the example usage for org.apache.commons.lang3 CharSetUtils count.

Prototype

public static int count(final String str, final String... set) 

Source Link

Document

Takes an argument in set-syntax, see evaluateSet, and returns the number of characters present in the specified string.

 CharSetUtils.count(null, *)        = 0 CharSetUtils.count("", *)          = 0 CharSetUtils.count(*, null)        = 0 CharSetUtils.count(*, "")          = 0 CharSetUtils.count("hello", "k-p") = 3 CharSetUtils.count("hello", "a-e") = 1 

Usage

From source file:com.quinsoft.zeidon.dbhandler.AbstractSqlHandler.java

/**
 * Parses the qualification object and puts the information in the qualMap.
 * The/*from  w  w  w . j a v a2  s.co m*/
 * @param view
 */
private void loadQualificationObject(LodDef lodDef) {
    qualMap = new HashMap<EntityDef, QualEntity>();
    if (qual == null)
        return;

    for (EntityInstance entitySpec : qual.cursor("EntitySpec").eachEntity()) {
        if (entitySpec.getAttribute("EntityName").isNull())
            throw new ZeidonException("Qualification view is missing entity name in EntitySpec");

        String entityName = entitySpec.getAttribute("EntityName").getString();
        if (StringUtils.isBlank(entityName))
            throw new ZeidonException("Qualification view is missing entity name in EntitySpec");

        EntityDef entityDef = lodDef.getEntityDef(entityName);
        QualEntity qualEntity = new QualEntity(entitySpec, entityDef);
        qualMap.put(entityDef, qualEntity);

        int parenCount = 0;

        EntityDef qualAttribDef = entitySpec.getEntityDef().getLodDef().getEntityDef("QualAttrib");
        for (EntityInstance qualAttribInstance : entitySpec.getChildren(qualAttribDef, true)) {
            //
            // Verify Oper
            //
            if (qualAttribInstance.getAttribute("Oper").isNull())
                throw new ZeidonException("QualAttrib for " + entityName + " is missing Oper");

            QualAttrib qualAttrib = new QualAttrib(qualAttribInstance.getAttribute("Oper").getString());
            if (qualAttrib.oper.equals("EXCLUDE")) {
                qualEntity.exclude = true;
                continue;
            }

            if (qualEntity.exclude)
                throw new ZeidonException("Entity '%s' has EXCLUDE but has additional qualification",
                        qualEntity.entityDef.getName());

            parenCount += CharSetUtils.count(qualAttrib.oper, "(");
            parenCount -= CharSetUtils.count(qualAttrib.oper, ")");

            //
            // Verify EntityName
            //
            if (!qualAttribInstance.getAttribute("EntityName").isNull()) {
                String qualEntityName = qualAttribInstance.getAttribute("EntityName").getString();
                qualAttrib.entityDef = lodDef.getEntityDef(qualEntityName);

                if (qualAttrib.entityDef.isDerived() || qualAttrib.entityDef.isDerivedPath()
                        || qualAttrib.entityDef.getDataRecord() == null) {
                    throw new ZeidonException(
                            "Entity " + entityName + " is derived or doesn't have DB information.");
                }

                // This entity better be a child of what we are qualifying.
                //                    EntityDef search = qualAttrib.entityDef.getParent();
                //                    while ( search != null && search != entityDef )
                //                        search = search.getParent();
                //                    if ( search == null )
                //                        throw new ZeidonException( "EntityName " + qualEntityName + " for QualAttrib " +
                //                                                   qualEntityName + " is not a child of " + entityName );
            }

            if (qualAttrib.oper.equals("EXISTS") || qualAttrib.oper.equals("NOT EXISTS")) {
                if (qualAttrib.entityDef == null)
                    throw new ZeidonException("Oper 'EXISTS'/'NOT EXISTS' requires an entity specification");

                // Change the oper to =/!=
                if (qualAttrib.oper.equals("EXISTS"))
                    qualAttrib.oper = "!=";
                else {
                    qualAttrib.oper = "=";
                    qualEntity.hasDoesNotExist = true;
                }

                qualAttrib.attributeDef = qualAttrib.entityDef.getKeys().get(0);
            }

            //
            // Verify AttribName
            //
            if (!qualAttribInstance.getAttribute("AttributeName").isNull() || qualAttrib.attributeDef != null) {
                if (qualAttrib.attributeDef == null) {
                    String attribName = qualAttribInstance.getAttribute("AttributeName").getString();
                    if (qualAttrib.entityDef == null)
                        throw new ZeidonException("QualAttrib has attribute defined but no valid entity");

                    qualAttrib.attributeDef = qualAttrib.entityDef.getAttribute(attribName);
                }

                // In some cases, we might be qualifying an entity using an attribute
                // from a child entity.  If the child attribute is a key AND that key
                // is the source attribute for a many-to-one relationship then the
                // attribute's value is also stored in the parent entity (the entity
                // we are qualifying) as a foreign key.  It will be much quicker to
                // perform qualification on just the foreign key, so change the
                // qualification to reference the foreign key.
                DataRecord dataRecord = qualAttrib.entityDef.getDataRecord();
                RelRecord relRecord = dataRecord.getRelRecord();
                while (qualAttrib.attributeDef.isKey() && qualAttrib.entityDef != qualEntity.entityDef
                        && relRecord != null && relRecord.getRelationshipType() == RelRecord.CHILD_IS_SOURCE) {
                    assert relRecord.getRelFields().size() == 1;

                    // Find the rel field for the qualifying attribute.
                    RelField relField = relRecord.getRelFields().get(0);

                    // Change the column we are qualifying on.
                    DataField dataField = relField.getRelDataField();
                    qualAttrib.attributeDef = dataField.getAttributeDef();
                    qualAttrib.entityDef = qualAttrib.attributeDef.getEntityDef();

                    dataRecord = qualAttrib.entityDef.getDataRecord();
                    relRecord = dataRecord.getRelRecord();
                }
            }

            if (qualAttrib.oper.equals("ORDERBY")) {
                if (qualAttrib.attributeDef == null)
                    throw new ZeidonException("Using Order By in qualification requires an attribute name");

                boolean descending = false;

                if (!qualAttribInstance.getAttribute("Value").isNull()) {
                    String str = qualAttribInstance.getAttribute("Value").getString().toLowerCase();
                    descending = str.startsWith("desc");
                }

                qualEntity.addOrderBy(qualAttrib.attributeDef, descending);
                continue;
            }

            //
            // Verify Value
            //
            if (!qualAttribInstance.getAttribute("Value").isNull()) {
                if (qualAttrib.attributeDef == null)
                    throw new ZeidonException("QualAttrib with value requires Entity.Attrib");

                String value = qualAttribInstance.getAttribute("Value").getString();

                if (qualAttrib.oper.equals("LIKE")) {
                    // If oper is "LIKE" then a qualification value that is invalid for the domain
                    // is possible.  E.g. "(617)%' is valid qualification for a phone number.  We'll
                    // assume the user knows what she's doing so we'll skip domain processing.
                    qualAttrib.value = value;
                } else {
                    // Get the string value from the qualification object, convert it to the
                    // domain's internal value, and then to an a string representation of the
                    // internal value.
                    Domain domain = qualAttrib.attributeDef.getDomain();
                    qualAttrib.value = domain.convertExternalValue(task, null, qualAttrib.attributeDef, null,
                            value);
                }
            }

            //
            // Verify KeyList
            //
            for (EntityInstance kl : qualAttribInstance.getChildren("KeyList")) {
                if (qualAttrib.valueList == null)
                    qualAttrib.valueList = new ArrayList<>();

                String value = null;
                if (!kl.getAttribute("StringValue").isNull())
                    value = kl.getAttribute("StringValue").getString();
                else if (!kl.getAttribute("IntegerValue").isNull())
                    value = kl.getAttribute("IntegerValue").getString();
                else
                    throw new ZeidonException("KeyList entity doesn't have IntegerValue or StringValue");

                Domain domain = qualAttrib.attributeDef.getDomain();
                Object objectValue = domain.convertExternalValue(task, null, qualAttrib.attributeDef, null,
                        value);

                qualAttrib.valueList.add(objectValue);
            }

            if (qualAttrib.valueList != null) {
                if (qualAttrib.value != null)
                    throw new ZeidonException(
                            "KeyList is specified for a QualAttrib that also has a value attribute");
            }

            //
            // Verify SourceViewName
            //
            if (!qualAttribInstance.getAttribute("SourceViewName").isNull())
                throw new ZeidonException("SourceViewName is currently unsupported for Activate Qualification");

            if (!qualAttribInstance.getAttribute("SourceViewID").isNull())
                throw new ZeidonException("SourceViewID is not supported by Java Object Engine");

            //
            // Verify SourceEntityName
            //
            if (!qualAttribInstance.getAttribute("SourceEntityName").isNull()) {
                if (qualAttribInstance.getAttribute("SourceAttributeName").isNull())
                    throw new ZeidonException("SourceEntityName is specified but SourceAttributeName is not.");

                loadQualAttributeColumn(lodDef, qualAttribInstance, qualAttrib);
            }

            // =================================================================
            // ===
            // ===  Validate Qualification attributes.
            // ===
            // =================================================================

            // TODO: Add the rest of the checks in fnSqlRetrieveQualAttrib.

            qualEntity.addQualAttrib(qualAttrib);
        } // for each QualAttrib...

        assert parenCount == 0;

    } // for each EntitySpec...

    // For pagination we *must* have the keys as part of the ordering, even if
    // it's the last value to order by.  This is so sorting will always have
    // the same results.
    if (pagingOptions != null) {
        EntityDef root = lodDef.getRoot();
        QualEntity qualRootEntity = qualMap.get(root);
        if (qualRootEntity == null) {
            qualRootEntity = new QualEntity(null, root);
            qualMap.put(root, qualRootEntity);
        }

        if (qualRootEntity.checkForKeysInOrderBy()) // Add the keys if they aren't there.
            getTask().log().trace("Key(s) added to ordering for pagination");

        qualRootEntity.activateLimit = pagingOptions.getPageSize();

        // Copy the root ordering back to the ActivateOptions so we can use
        // them later when attempting to load the next page.
        activateOptions.setRootActivateOrdering(qualRootEntity.ordering);
    }

}

From source file:yoyo.framework.standard.shared.commons.lang.CharSetUtilsTest.java

@Test
public void test() {
    assertThat(CharSetUtils.count(null, "a-z"), is(0));
    assertThat(CharSetUtils.count("", "a-z"), is(0));
    assertThat(CharSetUtils.count("hello", "k-p"), is(3));
    assertThat(CharSetUtils.count("hello", "a-e"), is(1));
    assertThat(CharSetUtils.count("hello", "a", "b", "c", "d", "e"), is(1));
    assertThat(CharSetUtils.delete(null, "a-z"), is(nullValue()));
    assertThat(CharSetUtils.delete("", "a-z"), is(""));
    assertThat(CharSetUtils.delete("hello", "hl"), is("eo"));
    assertThat(CharSetUtils.keep(null, "a-z"), is(nullValue()));
    assertThat(CharSetUtils.keep("", "a-z"), is(""));
    assertThat(CharSetUtils.keep("hello", "hl"), is("hll"));
    assertThat(CharSetUtils.squeeze(null, "a-z"), is(nullValue()));
    assertThat(CharSetUtils.squeeze("", "a-z"), is(""));
    assertThat(CharSetUtils.squeeze("hello", "k-p"), is("helo"));
}