Example usage for org.apache.commons.text StringTokenizer StringTokenizer

List of usage examples for org.apache.commons.text StringTokenizer StringTokenizer

Introduction

In this page you can find the example usage for org.apache.commons.text StringTokenizer StringTokenizer.

Prototype

public StringTokenizer(final char[] input, final StringMatcher delim, final StringMatcher quote) 

Source Link

Document

Constructs a tokenizer splitting using the specified delimiter matcher and handling quotes using the specified quote matcher.

Usage

From source file:org.lenskit.gradle.LenskitPlugin.java

public void apply(Project project) {
    final LenskitExtension lenskit = project.getExtensions().create("lenskit", LenskitExtension.class, project);

    for (MetaProperty prop : DefaultGroovyMethods.getMetaClass(lenskit).getProperties()) {
        String prjProp = "lenskit." + prop.getName();
        if (project.hasProperty(prjProp)) {
            Object val = project.findProperty(prjProp);
            String vstr = val != null ? val.toString() : null;
            logger.info("setting property {} to {}", prjProp, val);
            Class type = prop.getType();
            Consumer<Object> set = (v) -> prop.setProperty(lenskit, v);
            if (type.equals(Property.class)) {
                Method m = null;/*w ww.j a  v a 2s .  c om*/
                try {
                    m = lenskit.getClass().getMethod("get" + StringUtils.capitalize(prop.getName()));
                } catch (NoSuchMethodException e) {
                    throw new RuntimeException(e);
                }
                Type t = m.getGenericReturnType();
                Map<TypeVariable<?>, Type> args = TypeUtils.getTypeArguments(t, Property.class);
                Type rt = args.get(Property.class.getTypeParameters()[0]);
                type = TypeUtils.getRawType(rt, Object.class);
                set = (v) -> ((Property) prop.getProperty(lenskit)).set(v);
            }

            if (type.equals(List.class)) {// if the type is list update the val using strtokenizer
                StringTokenizer tok = new StringTokenizer(vstr, StringMatcherFactory.INSTANCE.splitMatcher(),
                        StringMatcherFactory.INSTANCE.quoteMatcher());
                val = DefaultGroovyMethods.toList(tok);
            } else if (type.equals(String.class)) {
                val = vstr;
            } else {
                val = StringConvert.INSTANCE.convertFromString(type, vstr);
            }

            set.accept(val);
        }

    }

}