Example usage for org.apache.commons.lang StringUtils capitalize

List of usage examples for org.apache.commons.lang StringUtils capitalize

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils capitalize.

Prototype

public static String capitalize(String str) 

Source Link

Document

Capitalizes a String changing the first letter to title case as per Character#toTitleCase(char) .

Usage

From source file:org.apache.oodt.cas.cli.option.handler.ApplyToActionHandler.java

public void handleOption(CmdLineAction action, CmdLineOptionInstance optionInstance) {
    try {/*from ww w  .  java2 s  . co m*/
        Class<?> type = optionInstance.getOption().getType();
        List<?> vals = (optionInstance.getValues().isEmpty())
                ? convertToType(Arrays.asList(new String[] { "true" }), type = Boolean.TYPE)
                : convertToType(optionInstance.getValues(), type);
        String methodName = getMethodName(action.getName());
        if (methodName != null) {
            action.getClass().getMethod(methodName, type).invoke(action, vals.toArray(new Object[vals.size()]));
        } else {
            action.getClass()
                    .getMethod((optionInstance.getOption().isRepeating() ? "add" : "set")
                            + StringUtils.capitalize(optionInstance.getOption().getLongOption()), type)
                    .invoke(action, vals.toArray(new Object[vals.size()]));
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.oodt.cas.cli.option.handler.ApplyToActionHandler.java

public String getHelp(CmdLineOption option) {
    return "Will invoke '" + (option.isRepeating() ? "add" : "set")
            + StringUtils.capitalize(option.getLongOption())
            + "' on action selected, except for the following actions: "
            + (applyToActions != null ? applyToActions : Lists.newArrayList());
}

From source file:org.apache.openjpa.azure.AzureDistributionPolicy.java

@Override
public String distribute(final Object pc, final List<String> slices, final Object context) {

    final Broker broker = (Broker) context;
    final AzureConfiguration conf = (AzureConfiguration) broker.getConfiguration();

    log = conf.getLog(OpenJPAConfiguration.LOG_RUNTIME);

    final Table table = AzureUtils.getTable(conf, pc.getClass());

    final List<Federation> federations = conf.getFederations(table);

    if (federations.isEmpty()) {
        return "ROOT";
    } else {/*w w w. jav  a 2 s . co m*/
        // !!! IMPORTANT !!!
        // Every changes to this behavior must be verified against both bulk insert of objects with 
        // non auto generated id and insert of objects with auto generated id.

        final Federation fed = federations.get(0);

        Object id = null;

        try {
            final String rangeMappingName = fed.getRangeMappingName(table.getFullIdentifier().getName());

            if (StringUtils.isNotBlank(rangeMappingName)) {
                final Object objectId = broker.getObjectId(pc);

                final Object obj = objectId instanceof ObjectId ? ((ObjectId) objectId).getIdObject() : pc;

                final Method methodToInvoke = obj.getClass()
                        .getMethod("get" + StringUtils.capitalize(rangeMappingName), new Class[0]);

                id = methodToInvoke.invoke(obj, new Object[0]);

                if (id == null) {
                    return null;
                }
            }
        } catch (Exception e) {
            log.error("Error retrieving objectId", e);
            return null;
        }

        final StoreManager store = broker.getStoreManager().getDelegate() instanceof DataCacheStoreManager
                ? ((DataCacheStoreManager) broker.getStoreManager().getDelegate()).getDelegate()
                : broker.getStoreManager().getDelegate();

        return AzureUtils.getTargetSlice((DistributedJDBCStoreManager) store, slices, fed, id).get(0);
    }
}

From source file:org.apache.openjpa.enhance.ApplicationIdTool.java

/**
 * Return the getters and setters for all primary key fields.
 *//*ww w .jav  a 2 s .  c  o m*/
private String getProperties() {
    if (AccessCode.isExplicit(_meta.getAccessType()) && AccessCode.isField(_meta.getAccessType()))
        return "";

    CodeFormat code = newCodeFormat();
    String propName;
    String typeName;
    for (int i = 0; i < _fields.length; i++) {
        if (i > 0)
            code.afterSection();
        typeName = getTypeName(_fields[i]);
        propName = StringUtils.capitalize(_fields[i].getName());

        code.tab().append("public ").append(typeName).append(" ");
        if (_fields[i].getDeclaredTypeCode() == JavaTypes.BOOLEAN
                || _fields[i].getDeclaredTypeCode() == JavaTypes.BOOLEAN_OBJ)
            code.append("is");
        else
            code.append("get");
        code.append(propName).parens().openBrace(2).endl();
        code.tab(2).append("return ").append(_fields[i].getName()).append(";").endl();
        code.closeBrace(2);
        code.afterSection();

        code.tab().append("public void set").append(propName);
        code.openParen(true).append(typeName).append(" ").append(_fields[i].getName()).closeParen();
        code.openBrace(2).endl();
        code.tab(2).append("this.").append(_fields[i].getName()).append(" = ").append(_fields[i].getName())
                .append(";").endl();
        code.closeBrace(2);
    }
    return code.toString();
}

From source file:org.apache.openjpa.enhance.CodeGenerator.java

/**
 * Append the declaration and code for the given field to the given buffers.
 *//*from   w  w w  .  ja  v  a2 s .c om*/
private void appendFieldCode(FieldMetaData fmd, CodeFormat decs, CodeFormat code) {
    String fieldName = fmd.getName();
    String capFieldName = StringUtils.capitalize(fieldName);
    String propertyName = fieldName;
    if (propertyName.startsWith("_"))
        propertyName = propertyName.substring(1);
    String fieldType = Strings.getClassName(fmd.getDeclaredType());

    String keyType = null;
    String elementType = null;
    String paramType = "";
    if (useGenericCollections()) {
        if (fmd.getDeclaredTypeCode() == JavaTypes.COLLECTION) {
            Class elmCls = fmd.getElement().getDeclaredType();
            elementType = Strings.getClassName(elmCls);
            paramType = decs.getParametrizedType(new String[] { elementType });
        } else if (fmd.getDeclaredTypeCode() == JavaTypes.MAP) {
            Class keyCls = fmd.getKey().getDeclaredType();
            Class elmCls = fmd.getElement().getDeclaredType();
            keyType = Strings.getClassName(keyCls);
            elementType = Strings.getClassName(elmCls);
            paramType = decs.getParametrizedType(new String[] { keyType, elementType });
        }
    }

    String fieldValue = getInitialValue(fmd);
    if (fieldValue == null) {
        if ("Set".equals(fieldType))
            fieldValue = "new HashSet" + paramType + decs.getParens();
        else if ("TreeSet".equals(fieldType))
            fieldValue = "new TreeSet" + paramType + decs.getParens();
        else if ("Collection".equals(fieldType))
            fieldValue = "new ArrayList" + paramType + decs.getParens();
        else if ("Map".equals(fieldType))
            fieldValue = "new HashMap" + paramType + decs.getParens();
        else if ("TreeMap".equals(fieldType))
            fieldValue = "new TreeMap" + paramType + decs.getParens();
        else if (fmd.getDeclaredTypeCode() == JavaTypes.COLLECTION
                || fmd.getDeclaredTypeCode() == JavaTypes.MAP)
            fieldValue = "new " + fieldType + paramType + decs.getParens();
        else
            fieldValue = "";
    }
    if (fieldValue.length() > 0)
        fieldValue = " = " + fieldValue;

    boolean fieldAccess = !usePropertyBasedAccess();
    String custom = getDeclaration(fmd);
    if (decs.length() > 0)
        decs.endl();
    ParameterTemplate templ;
    if (custom != null) {
        templ = new ParameterTemplate();
        templ.append(custom);
        templ.setParameter("fieldName", fieldName);
        templ.setParameter("capFieldName", capFieldName);
        templ.setParameter("propertyName", propertyName);
        templ.setParameter("fieldType", fieldType);
        templ.setParameter("keyType", keyType);
        templ.setParameter("elementType", elementType);
        templ.setParameter("fieldValue", fieldValue);
        decs.append(templ.toString());
    } else {
        if (fieldAccess)
            writeAnnotations(decs, getFieldAnnotations(fmd), 1);
        decs.tab().append("private ").append(fieldType).append(paramType).append(" ").append(fieldName)
                .append(fieldValue).append(";");
        if (fieldAccess)
            decs.endl();
    }

    custom = getFieldCode(fmd);
    if (code.length() > 0)
        code.afterSection();
    if (custom != null) {
        templ = new ParameterTemplate();
        templ.append(custom);
        templ.setParameter("fieldName", fieldName);
        templ.setParameter("capFieldName", capFieldName);
        templ.setParameter("propertyName", propertyName);
        templ.setParameter("fieldType", fieldType);
        templ.setParameter("keyType", keyType);
        templ.setParameter("elementType", elementType);
        templ.setParameter("fieldValue", fieldValue);
        code.append(templ.toString());
    } else {
        // getter
        if (!fieldAccess)
            writeAnnotations(code, getFieldAnnotations(fmd), 1);
        code.tab().append("public ").append(fieldType).append(paramType).append(" ");
        if ("boolean".equalsIgnoreCase(fieldType))
            code.append("is");
        else
            code.append("get");
        code.append(capFieldName).parens();
        code.openBrace(2).endl();
        code.tab(2).append("return ").append(fieldName).append(";").endl();
        code.closeBrace(2).afterSection();

        // setter
        code.tab().append("public void set").append(capFieldName);
        code.openParen(true).append(fieldType).append(paramType).append(" ").append(propertyName).closeParen();
        code.openBrace(2).endl();
        code.tab(2);
        if (propertyName.equals(fieldName))
            code.append("this.");
        code.append(fieldName).append(" = ").append(propertyName).append(";").endl();
        code.closeBrace(2);
    }
}

From source file:org.apache.openjpa.enhance.DynamicStorageGenerator.java

/**
 * Add the typed set by index method.//  w ww.  j a  v  a2s .  co  m
 */
private void addSetMethod(int typeCode, BCClass bc, int[] types, int totalObjects) {
    int handle = getCreateFieldMethods(typeCode);
    if (handle == POLICY_EMPTY)
        return;
    Class type = forType(typeCode);
    // public void set<Type> (int field, <type> val)
    String name = Object.class.equals(type) ? "Object" : StringUtils.capitalize(type.getName());
    name = "set" + name;
    BCMethod method = bc.declareMethod(name, void.class, new Class[] { int.class, type });
    method.makePublic();
    Code code = method.getCode(true);
    // switch (field)
    code.aload().setParam(0);
    TableSwitchInstruction tabins = code.tableswitch();
    tabins.setLow(0);
    tabins.setHigh(types.length - 1);
    Instruction defaultIns;
    if (handle == POLICY_SILENT)
        defaultIns = code.vreturn();
    else
        defaultIns = throwException(code, IllegalArgumentException.class);
    tabins.setDefaultTarget(defaultIns);
    int objectCount = 0;
    for (int i = 0; i < types.length; i++) {
        // default: throw new IllegalArgumentException
        if (!isCompatible(types[i], typeCode)) {
            tabins.addTarget(tabins.getDefaultTarget());
            continue;
        }

        tabins.addTarget(code.aload().setThis());
        if (typeCode >= JavaTypes.OBJECT) {
            // if (objects == null)
            //       objects = new Object[totalObjects];
            code.aload().setThis();
            code.getfield().setField("objects", Object[].class);
            JumpInstruction ifins = code.ifnonnull();
            code.aload().setThis();
            code.constant().setValue(totalObjects);
            code.anewarray().setType(Object.class);
            code.putfield().setField("objects", Object[].class);

            // objects[objectCount] = val;
            ifins.setTarget(code.aload().setThis());
            code.getfield().setField("objects", Object[].class);
            code.constant().setValue(objectCount);
            code.aload().setParam(1);
            code.aastore();
            objectCount++;
        } else {
            // case i: fieldi = val;
            LoadInstruction load = code.xload();
            load.setType(type);
            load.setParam(1);
            code.putfield().setField("field" + i, type);
        }
        // return
        code.vreturn();
    }
    code.calculateMaxLocals();
    code.calculateMaxStack();
}

From source file:org.apache.openjpa.enhance.DynamicStorageGenerator.java

/**
 * Add typed get by index method./*from  www  .  j a v  a 2s. c o m*/
 */
private void addGetMethod(int typeCode, BCClass bc, int[] types) {
    int handle = getCreateFieldMethods(typeCode);
    if (handle == POLICY_EMPTY)
        return;
    Class type = forType(typeCode);
    // public <type> get<Type>Field (int field)
    String name = Object.class.equals(type) ? "Object" : StringUtils.capitalize(type.getName());
    name = "get" + name;
    BCMethod method = bc.declareMethod(name, type, new Class[] { int.class });
    method.makePublic();
    Code code = method.getCode(true);
    // switch (field)
    code.aload().setParam(0);
    TableSwitchInstruction tabins = code.tableswitch();
    tabins.setLow(0);
    tabins.setHigh(types.length - 1);
    Instruction defaultIns = null;
    if (typeCode == JavaTypes.OBJECT && handle == POLICY_SILENT) {
        defaultIns = code.constant().setNull();
        code.areturn();
    } else
        defaultIns = throwException(code, IllegalArgumentException.class);
    tabins.setDefaultTarget(defaultIns);
    int objectCount = 0;
    for (int i = 0; i < types.length; i++) {
        // default: throw new IllegalArgumentException
        if (!isCompatible(types[i], typeCode)) {
            tabins.addTarget(tabins.getDefaultTarget());
            continue;
        }

        tabins.addTarget(code.aload().setThis());
        if (typeCode >= JavaTypes.OBJECT) {
            // if (objects == null)
            //       return null;
            // return objects[objectCount];
            code.aload().setThis();
            code.getfield().setField("objects", Object[].class);
            JumpInstruction ifins = code.ifnonnull();
            code.constant().setNull();
            code.areturn();
            ifins.setTarget(code.aload().setThis());
            code.getfield().setField("objects", Object[].class);
            code.constant().setValue(objectCount);
            code.aaload();
            code.areturn();
            objectCount++;
        } else {
            // case i: return fieldi;
            code.getfield().setField("field" + i, type);
            code.xreturn().setType(type);
        }
    }
    code.calculateMaxLocals();
    code.calculateMaxStack();
}

From source file:org.apache.openjpa.enhance.DynamicStorageGenerator.java

/**
 * Add a bean field of the given name and type.
 *//*from w  ww  . j  av a2 s . c  om*/
protected BCField addBeanField(BCClass bc, String name, Class type) {
    if (name == null)
        throw new IllegalArgumentException("name == null");

    // private <type> <name>
    BCField field = bc.declareField(name, type);
    field.setAccessFlags(getFieldAccess());
    name = StringUtils.capitalize(name);

    // getter
    String prefix = (type == boolean.class) ? "is" : "get";
    BCMethod method = bc.declareMethod(prefix + name, type, null);
    method.makePublic();
    Code code = method.getCode(true);
    code.aload().setThis();
    code.getfield().setField(field);
    code.xreturn().setType(type);
    code.calculateMaxStack();
    code.calculateMaxLocals();

    // setter
    method = bc.declareMethod("set" + name, void.class, new Class[] { type });
    method.makePublic();
    code = method.getCode(true);
    code.aload().setThis();
    code.xload().setParam(0).setType(type);
    code.putfield().setField(field);
    code.vreturn();
    code.calculateMaxStack();
    code.calculateMaxLocals();
    return field;
}

From source file:org.apache.openjpa.enhance.PCDataGenerator.java

/**
 * Add the field load.//from w ww  . jav a2  s.c  o  m
 */
private Instruction addLoad(BCClass bc, Code code, FieldMetaData fmd, int objectCount, int local,
        boolean fields) {
    int index = fmd.getIndex();
    int typeCode = replaceType(fmd);
    Instruction first;
    if (typeCode < JavaTypes.OBJECT) {
        // sm.store<type>(i, field<i>)
        Class<?> type = forType(fmd.getTypeCode());
        first = code.aload().setParam(0);
        code.constant().setValue(index);
        code.aload().setThis();
        code.getfield().setField(getFieldName(index), type);
        code.invokeinterface().setMethod(OpenJPAStateManager.class,
                "store" + StringUtils.capitalize(type.getName()), void.class, new Class[] { int.class, type });
    } else {
        // fmd = sm.getMetaData().getField(i);
        int offset = fields ? 1 : 0;
        first = code.aload().setParam(0);
        code.invokeinterface().setMethod(OpenJPAStateManager.class, "getMetaData", ClassMetaData.class, null);
        code.constant().setValue(fmd.getIndex());
        code.invokevirtual().setMethod(ClassMetaData.class, "getField", FieldMetaData.class,
                new Class[] { int.class });
        code.astore().setLocal(local);
        // sm.storeField(i, toField(sm, fmd, objects[objectCount],
        //       fetch, context);
        code.aload().setParam(0);
        code.constant().setValue(index);
        code.aload().setThis();
        code.aload().setParam(0);
        code.aload().setLocal(local);
        code.aload().setThis();
        code.getfield().setField("objects", Object[].class);
        code.constant().setValue(objectCount);
        code.aaload();
        code.aload().setParam(1 + offset);
        code.aload().setParam(2 + offset);
        code.invokevirtual().setMethod(bc.getName(), "toField", Object.class.getName(),
                toStrings(new Class[] { OpenJPAStateManager.class, FieldMetaData.class, Object.class,
                        FetchConfiguration.class, Object.class }));
        code.invokeinterface().setMethod(OpenJPAStateManager.class, "storeField", void.class,
                new Class[] { int.class, Object.class });
    }
    return first;
}

From source file:org.apache.openjpa.enhance.PCDataGenerator.java

private void addStore(BCClass bc, Code code, FieldMetaData fmd, int objectCount) {
    int typeCode = replaceType(fmd);
    int index = fmd.getIndex();
    if (typeCode < JavaTypes.OBJECT) {
        Class<?> type = forType(typeCode);
        // field<i> = sm.fetch<Type>(index)
        code.aload().setThis();//w ww  .ja v  a  2 s. c  om
        code.aload().setParam(0);
        code.constant().setValue(index);
        code.invokeinterface().setMethod(OpenJPAStateManager.class,
                "fetch" + StringUtils.capitalize(type.getName()), type, new Class[] { int.class });
        code.putfield().setField(getFieldName(index), type);
        code.aload().setThis();
        code.getfield().setField("loaded", BitSet.class);
        code.constant().setValue(index);
        code.invokevirtual().setMethod(BitSet.class, "set", void.class, new Class[] { int.class });
    } else {
        // Object val = toData(sm.getMetaData().getField(index),
        //       sm.fetchField(index, false), sm.getContext());
        int local = code.getNextLocalsIndex();
        code.aload().setThis();
        code.aload().setParam(0);
        code.invokeinterface().setMethod(OpenJPAStateManager.class, "getMetaData", ClassMetaData.class, null);
        code.constant().setValue(fmd.getIndex());
        code.invokevirtual().setMethod(ClassMetaData.class, "getField", FieldMetaData.class,
                new Class[] { int.class });
        code.aload().setParam(0);
        code.constant().setValue(fmd.getIndex());
        code.constant().setValue(false);
        code.invokeinterface().setMethod(OpenJPAStateManager.class, "fetchField", Object.class,
                new Class[] { int.class, boolean.class });
        code.aload().setParam(0);
        code.invokeinterface().setMethod(OpenJPAStateManager.class, "getContext", StoreContext.class, null);
        code.invokevirtual().setMethod(bc.getName(), "toData", Object.class.getName(),
                toStrings(new Class[] { FieldMetaData.class, Object.class, StoreContext.class }));
        code.astore().setLocal(local);

        // if (val == NULL) {
        //       val = null;
        //       loaded.clear(index);
        //    } else
        //       loaded.set(index);
        //    objects[objectCount] = val;
        code.aload().setLocal(local);
        code.getstatic().setField(AbstractPCData.class, "NULL", Object.class);
        JumpInstruction ifins = code.ifacmpne();
        code.constant().setNull();
        code.astore().setLocal(local);
        code.aload().setThis();
        code.getfield().setField("loaded", BitSet.class);
        code.constant().setValue(index);
        code.invokevirtual().setMethod(BitSet.class, "clear", void.class, new Class[] { int.class });
        JumpInstruction go2 = code.go2();
        ifins.setTarget(code.aload().setThis());
        code.getfield().setField("loaded", BitSet.class);
        code.constant().setValue(index);
        code.invokevirtual().setMethod(BitSet.class, "set", void.class, new Class[] { int.class });
        go2.setTarget(code.aload().setThis());
        code.getfield().setField("objects", Object[].class);
        code.constant().setValue(objectCount);
        code.aload().setLocal(local);
        code.aastore();
    }
    if (!usesImplData(fmd))
        return;

    // storeImplData(sm, i, loaded.get(i);
    code.aload().setThis();
    code.aload().setParam(0);
    code.constant().setValue(index);
    code.aload().setThis();
    code.getfield().setField("loaded", BitSet.class);
    code.constant().setValue(index);
    code.invokevirtual().setMethod(BitSet.class, "get", boolean.class, new Class[] { int.class });
    code.invokevirtual().setMethod("storeImplData", void.class,
            new Class[] { OpenJPAStateManager.class, int.class, boolean.class });
}