Commit 1cd932b4 authored by Administrator's avatar Administrator

update

parent c352e39c
package com.virjar.superappium.util;
public class BooleanUtils {
/**
* <p>Converts a String to a Boolean.</p>
*
* <p>{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'}
* (case insensitive) will return {@code true}.
* {@code 'false'}, {@code 'off'}, {@code 'n'}, {@code 'f'} or {@code 'no'}
* (case insensitive) will return {@code false}.
* Otherwise, {@code null} is returned.</p>
*
* <p>NOTE: This returns null and will throw a NullPointerException if autoboxed to a boolean. </p>
*
* <pre>
* // N.B. case is not significant
* BooleanUtils.toBooleanObject(null) = null
* BooleanUtils.toBooleanObject("true") = Boolean.TRUE
* BooleanUtils.toBooleanObject("T") = Boolean.TRUE // i.e. T[RUE]
* BooleanUtils.toBooleanObject("false") = Boolean.FALSE
* BooleanUtils.toBooleanObject("f") = Boolean.FALSE // i.e. f[alse]
* BooleanUtils.toBooleanObject("No") = Boolean.FALSE
* BooleanUtils.toBooleanObject("n") = Boolean.FALSE // i.e. n[o]
* BooleanUtils.toBooleanObject("on") = Boolean.TRUE
* BooleanUtils.toBooleanObject("ON") = Boolean.TRUE
* BooleanUtils.toBooleanObject("off") = Boolean.FALSE
* BooleanUtils.toBooleanObject("oFf") = Boolean.FALSE
* BooleanUtils.toBooleanObject("yes") = Boolean.TRUE
* BooleanUtils.toBooleanObject("Y") = Boolean.TRUE // i.e. Y[ES]
* BooleanUtils.toBooleanObject("blue") = null
* BooleanUtils.toBooleanObject("true ") = null // trailing space (too long)
* BooleanUtils.toBooleanObject("ono") = null // does not match on or no
* </pre>
*
* @param str the String to check; upper and lower case are treated as the same
* @return the Boolean value of the string, {@code null} if no match or {@code null} input
*/
public static Boolean toBooleanObject(final String str) {
// Previously used equalsIgnoreCase, which was fast for interned 'true'.
// Non interned 'true' matched 15 times slower.
//
// Optimisation provides same performance as before for interned 'true'.
// Similar performance for null, 'false', and other strings not length 2/3/4.
// 'true'/'TRUE' match 4 times slower, 'tRUE'/'True' 7 times slower.
if (str == "true") {
return Boolean.TRUE;
}
if (str == null) {
return null;
}
switch (str.length()) {
case 1: {
final char ch0 = str.charAt(0);
if (ch0 == 'y' || ch0 == 'Y' ||
ch0 == 't' || ch0 == 'T') {
return Boolean.TRUE;
}
if (ch0 == 'n' || ch0 == 'N' ||
ch0 == 'f' || ch0 == 'F') {
return Boolean.FALSE;
}
break;
}
case 2: {
final char ch0 = str.charAt(0);
final char ch1 = str.charAt(1);
if ((ch0 == 'o' || ch0 == 'O') &&
(ch1 == 'n' || ch1 == 'N') ) {
return Boolean.TRUE;
}
if ((ch0 == 'n' || ch0 == 'N') &&
(ch1 == 'o' || ch1 == 'O') ) {
return Boolean.FALSE;
}
break;
}
case 3: {
final char ch0 = str.charAt(0);
final char ch1 = str.charAt(1);
final char ch2 = str.charAt(2);
if ((ch0 == 'y' || ch0 == 'Y') &&
(ch1 == 'e' || ch1 == 'E') &&
(ch2 == 's' || ch2 == 'S') ) {
return Boolean.TRUE;
}
if ((ch0 == 'o' || ch0 == 'O') &&
(ch1 == 'f' || ch1 == 'F') &&
(ch2 == 'f' || ch2 == 'F') ) {
return Boolean.FALSE;
}
break;
}
case 4: {
final char ch0 = str.charAt(0);
final char ch1 = str.charAt(1);
final char ch2 = str.charAt(2);
final char ch3 = str.charAt(3);
if ((ch0 == 't' || ch0 == 'T') &&
(ch1 == 'r' || ch1 == 'R') &&
(ch2 == 'u' || ch2 == 'U') &&
(ch3 == 'e' || ch3 == 'E') ) {
return Boolean.TRUE;
}
break;
}
case 5: {
final char ch0 = str.charAt(0);
final char ch1 = str.charAt(1);
final char ch2 = str.charAt(2);
final char ch3 = str.charAt(3);
final char ch4 = str.charAt(4);
if ((ch0 == 'f' || ch0 == 'F') &&
(ch1 == 'a' || ch1 == 'A') &&
(ch2 == 'l' || ch2 == 'L') &&
(ch3 == 's' || ch3 == 'S') &&
(ch4 == 'e' || ch4 == 'E') ) {
return Boolean.FALSE;
}
break;
}
default:
break;
}
return null;
}
/**
* <p>Converts a String to a boolean (optimised for performance).</p>
*
* <p>{@code 'true'}, {@code 'on'}, {@code 'y'}, {@code 't'} or {@code 'yes'}
* (case insensitive) will return {@code true}. Otherwise,
* {@code false} is returned.</p>
*
* <p>This method performs 4 times faster (JDK1.4) than
* {@code Boolean.valueOf(String)}. However, this method accepts
* 'on' and 'yes', 't', 'y' as true values.
*
* <pre>
* BooleanUtils.toBoolean(null) = false
* BooleanUtils.toBoolean("true") = true
* BooleanUtils.toBoolean("TRUE") = true
* BooleanUtils.toBoolean("tRUe") = true
* BooleanUtils.toBoolean("on") = true
* BooleanUtils.toBoolean("yes") = true
* BooleanUtils.toBoolean("false") = false
* BooleanUtils.toBoolean("x gti") = false
* BooleanUtils.toBooleanObject("y") = true
* BooleanUtils.toBooleanObject("n") = false
* BooleanUtils.toBooleanObject("t") = true
* BooleanUtils.toBooleanObject("f") = false
* </pre>
*
* @param str the String to check
* @return the boolean value of the string, {@code false} if no match or the String is null
*/
public static boolean toBoolean(final String str) {
return toBooleanObject(str) == Boolean.TRUE;
}
}
package com.virjar.superappium.util;
import com.sun.xml.internal.xsom.impl.scd.Iterators;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* some utils migrated from guava
......@@ -44,5 +49,40 @@ public class Lists {
return new LinkedList<E>();
}
static <T> Collection<T> cast(Iterable<T> iterable) {
return (Collection<T>) iterable;
}
public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList(
Iterable<? extends E> elements) {
// We copy elements to an ArrayList first, rather than incurring the
// quadratic cost of adding them to the COWAL directly.
Collection<? extends E> elementsCollection =
(elements instanceof Collection)
? cast(elements)
: newArrayList(elements);
return new CopyOnWriteArrayList<E>(elementsCollection);
}
public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements) {
// Let ArrayList's sizing logic work, if possible
return (elements instanceof Collection)
? new ArrayList<E>(cast(elements))
: newArrayList(elements.iterator());
}
public static <E> ArrayList<E> newArrayList(Iterator<? extends E> elements) {
ArrayList<E> list = newArrayList();
addAll(list, elements);
return list;
}
public static <T> boolean addAll(Collection<T> addTo, Iterator<? extends T> iterator) {
boolean wasModified = false;
while (iterator.hasNext()) {
wasModified |= addTo.add(iterator.next());
}
return wasModified;
}
}
package com.virjar.superappium.util;
public class NumberUtils {
/**
* <p>Convert a <code>String</code> to a <code>double</code>, returning
* <code>0.0d</code> if the conversion fails.</p>
*
* <p>If the string <code>str</code> is <code>null</code>,
* <code>0.0d</code> is returned.</p>
*
* <pre>
* NumberUtils.toDouble(null) = 0.0d
* NumberUtils.toDouble("") = 0.0d
* NumberUtils.toDouble("1.5") = 1.5d
* </pre>
*
* @param str the string to convert, may be <code>null</code>
* @return the double represented by the string, or <code>0.0d</code>
* if conversion fails
* @since 2.1
*/
public static double toDouble(final String str) {
return toDouble(str, 0.0d);
}
/**
* <p>Convert a <code>String</code> to a <code>double</code>, returning a
* default value if the conversion fails.</p>
*
* <p>If the string <code>str</code> is <code>null</code>, the default
* value is returned.</p>
*
* <pre>
* NumberUtils.toDouble(null, 1.1d) = 1.1d
* NumberUtils.toDouble("", 1.1d) = 1.1d
* NumberUtils.toDouble("1.5", 0.0d) = 1.5d
* </pre>
*
* @param str the string to convert, may be <code>null</code>
* @param defaultValue the default value
* @return the double represented by the string, or defaultValue
* if conversion fails
* @since 2.1
*/
public static double toDouble(final String str, final double defaultValue) {
if (str == null) {
return defaultValue;
}
try {
return Double.parseDouble(str);
} catch (final NumberFormatException nfe) {
return defaultValue;
}
}
/**
* <p>Convert a <code>String</code> to an <code>int</code>, returning
* <code>zero</code> if the conversion fails.</p>
*
* <p>If the string is <code>null</code>, <code>zero</code> is returned.</p>
*
* <pre>
* NumberUtils.toInt(null) = 0
* NumberUtils.toInt("") = 0
* NumberUtils.toInt("1") = 1
* </pre>
*
* @param str the string to convert, may be null
* @return the int represented by the string, or <code>zero</code> if
* conversion fails
* @since 2.1
*/
public static int toInt(final String str) {
return toInt(str, 0);
}
/**
* <p>Convert a <code>String</code> to an <code>int</code>, returning a
* default value if the conversion fails.</p>
*
* <p>If the string is <code>null</code>, the default value is returned.</p>
*
* <pre>
* NumberUtils.toInt(null, 1) = 1
* NumberUtils.toInt("", 1) = 1
* NumberUtils.toInt("1", 0) = 1
* </pre>
*
* @param str the string to convert, may be null
* @param defaultValue the default value
* @return the int represented by the string, or the default if conversion fails
* @since 2.1
*/
public static int toInt(final String str, final int defaultValue) {
if(str == null) {
return defaultValue;
}
try {
return Integer.parseInt(str);
} catch (final NumberFormatException nfe) {
return defaultValue;
}
}
}
package com.virjar.superappium.util;
import lombok.extern.slf4j.Slf4j;
/**
* Created by virjar on 17/6/13.
*/
@Slf4j
public class ObjectFactory {
@SuppressWarnings("unchecked")
public static <T> T newInstance(String className) {
try {
Class<?> aClass = Class.forName(className);
return (T) aClass.newInstance();
} catch (Exception e) {
throw new RuntimeException("can not create instance for class " + className);
}
}
@SuppressWarnings("unchecked")
public static <T> T newInstance(Class<T> tClass) {
try {
return tClass.newInstance();
} catch (Exception e) {
throw new RuntimeException("can not create instance for class " + tClass);
}
}
@SuppressWarnings("unchecked")
public static <T> Class<T> classForName(String className) {
try {
return (Class<T>) Class.forName(className);
} catch (Exception e) {
throw new RuntimeException("can not load class: " + className);
}
}
}
package com.virjar.superappium.xpath.exception;
public class NoSuchFunctionException extends XpathSyntaxErrorException {
public NoSuchFunctionException(int errorPos, String msg) {
super(errorPos, msg);
}
}
package com.virjar.superappium.xpath.parser.expression;
import com.virjar.superappium.util.Lists;
import com.virjar.superappium.util.StringUtils;
import com.virjar.superappium.xpath.exception.NoSuchFunctionException;
import com.virjar.superappium.xpath.exception.XpathSyntaxErrorException;
import com.virjar.superappium.xpath.function.FunctionEnv;
import com.virjar.superappium.xpath.function.filter.FilterFunction;
import com.virjar.superappium.xpath.parser.TokenQueue;
import com.virjar.superappium.xpath.parser.expression.node.FunctionNode;
import com.virjar.superappium.xpath.parser.expression.token.Token;
import com.virjar.superappium.xpath.parser.expression.token.TokenAnalysisRegistry;
import com.virjar.superappium.xpath.parser.expression.token.TokenConsumer;
import java.util.List;
/**
* Created by virjar on 17/6/11. 对于函数,解析函数名字,参数列表,决定参数类型
*
* @author virjar
* @since 0.0.1
*/
public class FunctionParser {
private static final List<String> paramExcludeTypes = Lists.newArrayList(Token.OPERATOR);
private TokenQueue tokenQueue;
public FunctionParser(TokenQueue tokenQueue) {
this.tokenQueue = tokenQueue;
}
public FunctionNode parse() throws XpathSyntaxErrorException {
tokenQueue.consumeWhitespace();
String functionName = tokenQueue.consumeTo("(");
String params = tokenQueue.chompBalanced('(', ')');
FilterFunction filterFunction = FunctionEnv.getFilterFunction(functionName);
if (filterFunction == null) {
throw new NoSuchFunctionException(0, "not such function:" + functionName);
}
List<SyntaxNode> paramList = Lists.newLinkedList();
TokenQueue paramTokenQueue = new TokenQueue(StringUtils.trimToEmpty(params));
while ((paramTokenQueue.consumeWhitespace() && !paramTokenQueue.consumeWhitespace())
|| !paramTokenQueue.isEmpty()) {
boolean hint = false;
for (TokenConsumer tokenConsumer : TokenAnalysisRegistry.consumerIterable()) {
if (excludeForParam(tokenConsumer.tokenType())) {
continue;
}
String consume = tokenConsumer.consume(paramTokenQueue);
if (consume == null) {
continue;
}
hint = true;
paramList.add(TokenAnalysisRegistry.findHandler(tokenConsumer.tokenType()).parseToken(consume));
break;
}
if (!hint) {
throw new XpathSyntaxErrorException(paramTokenQueue.nowPosition(), "can not parse param list: "
+ paramTokenQueue.getQueue() + " ,for token " + paramTokenQueue.remainder());
}
paramTokenQueue.consumeWhitespace();
if (!paramTokenQueue.isEmpty()) {
if (paramTokenQueue.peek() != ',') {
throw new XpathSyntaxErrorException(paramTokenQueue.nowPosition(), "can not parse param list: \""
+ paramTokenQueue.getQueue() + "\" ,for token " + paramTokenQueue.remainder());
}
paramTokenQueue.advance();
}
}
return new FunctionNode(filterFunction, paramList);
}
private boolean excludeForParam(String tokenType) {
return paramExcludeTypes.contains(tokenType);
}
}
package com.virjar.superappium.xpath.parser.expression;
import com.virjar.superappium.util.Lists;
import com.virjar.superappium.util.ObjectFactory;
import com.virjar.superappium.util.StringUtils;
import com.virjar.superappium.xpath.parser.expression.node.AlgorithmUnit;
import com.virjar.superappium.xpath.parser.expression.operator.AddUnit;
import com.virjar.superappium.xpath.parser.expression.operator.And2Unit;
import com.virjar.superappium.xpath.parser.expression.operator.AndUnit;
import com.virjar.superappium.xpath.parser.expression.operator.ContainUnit;
import com.virjar.superappium.xpath.parser.expression.operator.DivideUnit;
import com.virjar.superappium.xpath.parser.expression.operator.EndWithUnit;
import com.virjar.superappium.xpath.parser.expression.operator.EqualUnit;
import com.virjar.superappium.xpath.parser.expression.operator.GreaterEqualUnit;
import com.virjar.superappium.xpath.parser.expression.operator.GreaterThanUnit;
import com.virjar.superappium.xpath.parser.expression.operator.LessEqualUnit;
import com.virjar.superappium.xpath.parser.expression.operator.LessThanUnit;
import com.virjar.superappium.xpath.parser.expression.operator.MinusUnit;
import com.virjar.superappium.xpath.parser.expression.operator.MultiUnit;
import com.virjar.superappium.xpath.parser.expression.operator.NotEqualUnit;
import com.virjar.superappium.xpath.parser.expression.operator.NotMatchUnit;
import com.virjar.superappium.xpath.parser.expression.operator.OpKey;
import com.virjar.superappium.xpath.parser.expression.operator.Or2Unit;
import com.virjar.superappium.xpath.parser.expression.operator.OrUnit;
import com.virjar.superappium.xpath.parser.expression.operator.RegexUnit;
import com.virjar.superappium.xpath.parser.expression.operator.RemainderUnit;
import com.virjar.superappium.xpath.parser.expression.operator.StartWithUnit;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import lombok.Getter;
......@@ -23,20 +43,39 @@ public class OperatorEnv {
@SuppressWarnings("unchecked")
private static void registerDefault() {
ClassScanner.AnnotationClassVisitor visitor = new ClassScanner.AnnotationClassVisitor(OpKey.class);
ClassScanner.scan(visitor, Lists.newArrayList("com.virjar.sipsoup.parse.expression.operator"));
Set<Class> classSet = visitor.getClassSet();
for (Class clazz : classSet) {
if (AlgorithmUnit.class.isAssignableFrom(clazz)) {
addOperator(clazz);
}
}
// ClassScanner.AnnotationClassVisitor visitor = new ClassScanner.AnnotationClassVisitor(OpKey.class);
// ClassScanner.scan(visitor, Lists.newArrayList("com.virjar.sipsoup.parse.expression.operator"));
// Set<Class> classSet = visitor.getClassSet();
// for (Class clazz : classSet) {
// if (AlgorithmUnit.class.isAssignableFrom(clazz)) {
// addOperator(clazz);
// }
// }
addOperator(AddUnit.class);
addOperator(AndUnit.class);
addOperator(And2Unit.class);
addOperator(ContainUnit.class);
addOperator(DivideUnit.class);
addOperator(EndWithUnit.class);
addOperator(EqualUnit.class);
addOperator(GreaterEqualUnit.class);
addOperator(GreaterThanUnit.class);
addOperator(LessEqualUnit.class);
addOperator(LessThanUnit.class);
addOperator(MinusUnit.class);
addOperator(MultiUnit.class);
addOperator(NotEqualUnit.class);
addOperator(NotMatchUnit.class);
addOperator(Or2Unit.class);
addOperator(OrUnit.class);
addOperator(RegexUnit.class);
addOperator(RemainderUnit.class);
addOperator(StartWithUnit.class);
}
public static void addOperator(String key, int priority, Class<? extends AlgorithmUnit> algorithmUnit) {
if ("#".equals(key)) {
throw new IllegalStateException("\"#\" can not to be register a operator,please contact author virjar@virjar.com"))
;
throw new IllegalStateException("\"#\" can not to be register a operator,please contact author virjar@virjar.com");
}
AlgorithmHolder holder = new AlgorithmHolder(algorithmUnit, key, priority);
......
package com.virjar.superappium.xpath.parser.expression.token.handler;
import com.virjar.superappium.util.BooleanUtils;
import com.virjar.superappium.util.Lists;
import com.virjar.superappium.xpath.exception.XpathSyntaxErrorException;
import com.virjar.superappium.xpath.function.FunctionEnv;
......@@ -12,7 +13,7 @@ public class BooleanHandler implements TokenHandler {
@Override
public SyntaxNode parseToken(final String tokenStr) throws XpathSyntaxErrorException {
return new FunctionNode(FunctionEnv.getFilterFunction(BooleanUtils.toBoolean(tokenStr) ? "true" : "false"),
Lists.<SyntaxNode> newLinkedList());
Lists.<SyntaxNode>newLinkedList());
}
@Override
......
......@@ -2,6 +2,7 @@ package com.virjar.superappium.xpath.parser.expression.token.handler;
import com.virjar.superappium.xpath.exception.XpathSyntaxErrorException;
import com.virjar.superappium.xpath.parser.TokenQueue;
import com.virjar.superappium.xpath.parser.expression.FunctionParser;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import com.virjar.superappium.xpath.parser.expression.token.Token;
import com.virjar.superappium.xpath.parser.expression.token.TokenHandler;
......
package com.virjar.superappium.xpath.parser.expression.token.handler;
import com.virjar.superappium.ViewImage;
import com.virjar.superappium.util.StringUtils;
import com.virjar.superappium.util.NumberUtils;
import com.virjar.superappium.xpath.parser.expression.SyntaxNode;
import com.virjar.superappium.xpath.parser.expression.token.Token;
import com.virjar.superappium.xpath.parser.expression.token.TokenHandler;
......@@ -12,13 +12,12 @@ public class NumberHandler implements TokenHandler {
return new SyntaxNode() {
@Override
public Object calc(ViewImage element) {
if (StringUtils.contains(tokenStr, ".")) {
if (tokenStr.contains(".")) {
return NumberUtils.toDouble(tokenStr);
} else {
return NumberUtils.toInt(tokenStr);
}
}
\
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment