Java Code Examples for org.osgl.util.S#fastSplit()

The following examples show how to use org.osgl.util.S#fastSplit() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StringValueResolverManager.java    From actframework with Apache License 2.0 6 votes vote down vote up
public <T> StringValueResolver<T> arrayResolver(final Class<T> targetType, final char separator) {
    final Class<?> componentType = targetType.getComponentType();
    E.unexpectedIf(null == componentType, "specified target type is not array type: " + targetType);
    final StringValueResolver elementResolver = resolver(componentType);
    return new StringValueResolver<T>(targetType) {
        @Override
        public T resolve(String value) {
            List list = new ArrayList();
            List<String> parts = S.fastSplit(value, String.valueOf(separator));
            for (String part : parts) {
                list.add(elementResolver.resolve(part));
            }
            T array = (T) Array.newInstance(componentType, list.size());
            return (T)list.toArray((Object[])array);
        }
    };
}
 
Example 2
Source File: JavaNames.java    From actframework with Apache License 2.0 6 votes vote down vote up
/**
 * Check if a given string is a valid java package or class name.
 *
 * This method use the technique documented in
 * [this SO question](https://stackoverflow.com/questions/13557195/how-to-check-if-string-is-a-valid-class-identifier)
 * with the following extensions:
 *
 * * if the string does not contain `.` then assume it is not a valid package or class name
 *
 * @param s
 *      the string to be checked
 * @return
 *      `true` if `s` is a valid java package or class name
 */
public static boolean isPackageOrClassName(String s) {
    if (S.blank(s)) {
        return false;
    }
    S.List parts = S.fastSplit(s, ".");
    if (parts.size() < 2) {
        return false;
    }
    for (String part: parts) {
        if (!Character.isJavaIdentifierStart(part.charAt(0))) {
            return false;
        }
        for (int i = 1, len = part.length(); i < len; ++i) {
            if (!Character.isJavaIdentifierPart(part.charAt(i))) {
                return false;
            }
        }
    }
    return true;
}
 
Example 3
Source File: Config.java    From actframework with Apache License 2.0 6 votes vote down vote up
public Integer getInteger(ConfigKey key, Integer def) {
    Object retVal = get(key, def);
    if (null == retVal) {
        return null;
    }
    if (retVal instanceof Number) {
        return ((Number) retVal).intValue();
    }
    String s = S.string(retVal);
    if (s.contains("*")) {
        List<String> sl = S.fastSplit(s, "*");
        int n = 1;
        for (String sn : sl) {
            n *= Integer.parseInt(sn.trim());
        }
        return n;
    }
    return Integer.parseInt(s);
}
 
Example 4
Source File: JWT.java    From actframework with Apache License 2.0 5 votes vote down vote up
public Token deserialize(String tokenString) {
    List<String> parts = S.fastSplit(tokenString, ".");
    if (parts.size() != 3) {
        return null;
    }
    String encodedHeaders = parts.get(0);
    String encodedPayloads = parts.get(1);
    String hash = parts.get(2);

    if (!verifyHash(encodedHeaders, encodedPayloads, hash)) {
        return null;
    }

    String headerString = new String(Codec.decodeUrlSafeBase64(encodedHeaders));
    JSONObject headers = JSON.parseObject(headerString);
    if (!verifyArgo(headers)) {
        return null;
    }

    String payloadString = new String(Codec.decodeUrlSafeBase64(encodedPayloads));
    JSONObject payloads = JSON.parseObject(payloadString);
    if (!verifyIssuer(payloads)) {
        return null;
    }
    if (!verifyExpires(payloads)) {
        return null;
    }

    Token token = new Token(issuer);
    token.headers.putAll(headers);
    token.payloads.putAll(payloads);
    return token;
}
 
Example 5
Source File: StringValueResolverManager.java    From actframework with Apache License 2.0 5 votes vote down vote up
public <T extends Collection> StringValueResolver<T> collectionResolver(final Class<T> targetType, final Class<?> elementType, final char separator) {
    final StringValueResolver elementResolver = resolver(elementType);
    return new StringValueResolver<T>(targetType) {
        @Override
        public T resolve(String value) {
            T col = app().getInstance(targetType);
            List<String> parts = S.fastSplit(value, String.valueOf(separator));
            for (String part : parts) {
                col.add(elementResolver.resolve(part));
            }
            return col;
        }
    };
}
 
Example 6
Source File: YamlLoader.java    From actframework with Apache License 2.0 5 votes vote down vote up
private void addModelPackage(String packageSpec) {
    for (String pkg : S.fastSplit(packageSpec, ",")) {
        pkg = pkg.trim();
        if (S.empty(pkg)) {
            continue;
        }
        this.modelPackages.add(S.ensure(pkg).endWith("."));
    }
}
 
Example 7
Source File: ConfigKeyHelper.java    From actframework with Apache License 2.0 5 votes vote down vote up
private static Integer toInt(Object v) {
    if (null == v) return null;
    if (v instanceof Number) return ((Number) v).intValue();
    String s = v.toString();
    if (s.contains("*")) {
        List<String> sl = S.fastSplit(s, "*");
        int n = 1;
        for (String sn : sl) {
            n *= Integer.parseInt(sn.trim());
        }
        return n;
    }
    return Integer.parseInt(s);
}
 
Example 8
Source File: AppNameInferer.java    From actframework with Apache License 2.0 4 votes vote down vote up
private static List<String> tokenOf(String packageName) {
    return S.fastSplit(packageName, ".");
}
 
Example 9
Source File: FastSplitBenchmark.java    From java-tool with Apache License 2.0 4 votes vote down vote up
@Test
@BenchmarkOptions(warmupRounds = 1, benchmarkRounds = 1)
public void testFastSplit() {
    S.List sl = S.fastSplit(toBeSplited, "**");
    UtilTestBase.eq(sl, C.listOf(toBeSplited.split("\\*\\*")));
}
 
Example 10
Source File: FastSplitBenchmark.java    From java-tool with Apache License 2.0 4 votes vote down vote up
@Test
public void benchmarkFastSplit() {
    S.fastSplit(toBeSplited, "**");
}