List of usage examples for org.apache.lucene.util BytesRef utf8ToString
public String utf8ToString()
From source file:io.crate.operation.scalar.ConcatFunctionTest.java
License:Apache License
private void assertEval(String expected, Object... args) { List<DataType> argumentTypes = new ArrayList<>(args.length); Input[] inputs = new Input[args.length]; for (int i = 0; i < args.length; i++) { inputs[i] = new ObjectInput(args[i]); argumentTypes.add(DataTypes.guessType(args[i])); }// w w w .j av a2s . co m Scalar scalar = ((Scalar) functions.get(new FunctionIdent(ConcatFunction.NAME, argumentTypes))); @SuppressWarnings("unchecked") BytesRef evaluate = (BytesRef) scalar.evaluate(inputs); assertThat(evaluate.utf8ToString(), is(expected)); }
From source file:io.crate.operation.scalar.DateTruncFunction.java
License:Apache License
protected DateTimeUnit intervalAsUnit(BytesRef interval) { if (interval == null) { throw new IllegalArgumentException( String.format(Locale.ENGLISH, "invalid interval NULL for scalar '%s'", NAME)); }//from ww w . j av a 2s .com DateTimeUnit intervalAsUnit = DATE_FIELD_PARSERS.get(interval); if (intervalAsUnit == null) { throw new IllegalArgumentException(String.format(Locale.ENGLISH, "invalid interval '%s' for scalar '%s'", interval.utf8ToString(), NAME)); } return intervalAsUnit; }
From source file:io.crate.operation.scalar.DateTruncTimeZoneAwareFunction.java
License:Apache License
private DateTimeZone parseZone(BytesRef zone) throws IllegalArgumentException { String text = zone.utf8ToString(); int index = text.indexOf(':'); if (index != -1) { int beginIndex = text.charAt(0) == '+' ? 1 : 0; // format like -02:30 return DateTimeZone.forOffsetHoursMinutes(Integer.parseInt(text.substring(beginIndex, index)), Integer.parseInt(text.substring(index + 1))); } else {//from w ww. ja v a 2s .c om // id, listed here: http://joda-time.sourceforge.net/timezones.html // or here: http://www.joda.org/joda-time/timezones.html return DateTimeZone.forID(text); } }
From source file:io.crate.operation.scalar.FormatFunctionTest.java
License:Apache License
@Test @SuppressWarnings("unchecked") public void testEvaluate() throws Exception { final Literal<BytesRef> formatString = Literal.newLiteral("%s bla %s"); List<Symbol> args = Arrays.<Symbol>asList(formatString, createReference("name", DataTypes.STRING), createReference("age", DataTypes.LONG)); Function function = createFunction(FormatFunction.NAME, DataTypes.STRING, args); Scalar<BytesRef, Object> format = (Scalar<BytesRef, Object>) functions.get(function.info().ident()); Input<Object> arg1 = new Input<Object>() { @Override/*from w w w. j a v a 2 s . c o m*/ public Object value() { return formatString.value(); } }; Input<Object> arg2 = new Input<Object>() { @Override public Object value() { return "Arthur"; } }; Input<Object> arg3 = new Input<Object>() { @Override public Object value() { return 38L; } }; BytesRef result = format.evaluate(arg1, arg2, arg3); assertThat(result.utf8ToString(), is("Arthur bla 38")); arg1 = new Input<Object>() { @Override public Object value() { return formatString.value(); } }; arg2 = new Input<Object>() { @Override public Object value() { return new BytesRef("Arthur"); } }; arg3 = new Input<Object>() { @Override public Object value() { return 42L; } }; result = format.evaluate(arg1, arg2, arg3); assertThat(result.utf8ToString(), is("Arthur bla 42")); }
From source file:io.crate.operation.scalar.regex.RegexMatcher.java
License:Apache License
public BytesRef replace(BytesRef term, BytesRef replacement) { UTF8toUTF16(term, utf16);/*from w ww . j a v a 2 s . com*/ if (globalFlag) { return new BytesRef(matcher.replaceAll(replacement.utf8ToString())); } else { return new BytesRef(matcher.replaceFirst(replacement.utf8ToString())); } }
From source file:io.crate.operation.scalar.regex.RegexMatcher.java
License:Apache License
public static int parseFlags(@Nullable BytesRef flagsString) { int flags = 0; if (flagsString == null) { return flags; }//from w ww . jav a 2 s . co m for (char flag : flagsString.utf8ToString().toCharArray()) { switch (flag) { case 'i': flags = flags | Pattern.CASE_INSENSITIVE; break; case 'u': flags = flags | Pattern.UNICODE_CASE; break; case 'U': flags = flags | Pattern.UNICODE_CHARACTER_CLASS; break; case 's': flags = flags | Pattern.DOTALL; break; case 'm': flags = flags | Pattern.MULTILINE; break; case 'x': flags = flags | Pattern.COMMENTS; break; case 'd': flags = flags | Pattern.UNIX_LINES; break; default: break; } } return flags; }
From source file:io.crate.operation.scalar.regex.RegexMatcher.java
License:Apache License
public static boolean isGlobal(@Nullable BytesRef flags) { if (flags == null) { return false; }//from www. j a v a 2 s. co m return flags.utf8ToString().indexOf('g') != -1; }
From source file:io.crate.operation.scalar.regex.ReplaceFunctionTest.java
License:Apache License
@Test public void testNormalizeSymbol() throws Exception { final BytesRef expected = new BytesRef("fooCraterbequebaz bar"); List<Symbol> arguments = Arrays.<Symbol>asList(Literal.newLiteral("foobarbequebaz bar"), Literal.newLiteral("(ba)"), Literal.newLiteral("Crate")); Function function = createFunction(ReplaceFunction.NAME, DataTypes.STRING, arguments); ReplaceFunction regexpImpl = (ReplaceFunction) functions.get(function.info().ident()); Symbol result = regexpImpl.normalizeSymbol(function); assertThat(result, isLiteral(expected.utf8ToString())); arguments = Arrays.<Symbol>asList(createReference("text", DataTypes.STRING), Literal.newLiteral("(ba)"), Literal.newLiteral("Crate")); function = createFunction(ReplaceFunction.NAME, DataTypes.STRING, arguments); regexpImpl = (ReplaceFunction) functions.get(function.info().ident()); result = regexpImpl.normalizeSymbol(function); assertThat(result, instanceOf(Function.class)); assertThat((Function) result, is(function)); }
From source file:io.crate.operation.scalar.regex.ReplaceFunctionTest.java
License:Apache License
@Test public void testNormalizeSymbolWithFlags() throws Exception { final BytesRef expected = new BytesRef("fooCraterbequebaz bar"); List<Symbol> arguments = Arrays.<Symbol>asList(Literal.newLiteral("foobarbequebaz bar"), Literal.newLiteral("(ba)"), Literal.newLiteral("Crate"), Literal.newLiteral("us n")); Function function = createFunction(ReplaceFunction.NAME, DataTypes.STRING, arguments); ReplaceFunction regexpImpl = (ReplaceFunction) functions.get(function.info().ident()); Symbol result = regexpImpl.normalizeSymbol(function); assertThat(result, isLiteral(expected.utf8ToString())); }
From source file:io.crate.operation.scalar.SubscriptFunctionTest.java
License:Apache License
@Test public void testNormalizeSymbol() throws Exception { final Literal<Object[]> term = Literal.newLiteral( new BytesRef[] { new BytesRef("Youri"), new BytesRef("Ruben") }, new ArrayType(DataTypes.STRING)); final Literal<Integer> termIndex = Literal.newLiteral(1); final BytesRef expected = new BytesRef("Youri"); List<Symbol> arguments = Arrays.<Symbol>asList(term, termIndex); Function function = createFunction(SubscriptFunction.NAME, DataTypes.STRING, arguments); SubscriptFunction subscriptFunction = (SubscriptFunction) functions.get(function.info().ident()); Symbol result = subscriptFunction.normalizeSymbol(function); assertThat(result, isLiteral(expected.utf8ToString())); arguments = Arrays.<Symbol>asList(createReference("text", term.valueType()), termIndex); function = createFunction(SubscriptFunction.NAME, DataTypes.STRING, arguments); subscriptFunction = (SubscriptFunction) functions.get(function.info().ident()); result = subscriptFunction.normalizeSymbol(function); assertThat(result, instanceOf(Function.class)); assertThat((Function) result, is(function)); }