1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.expreval.expr.function;
22
23 import org.apache.expreval.client.NullColumnValueException;
24 import org.apache.expreval.client.ResultMissingColumnException;
25 import org.apache.expreval.expr.node.GenericValue;
26 import org.apache.expreval.expr.node.StringValue;
27 import org.apache.hadoop.hbase.hbql.client.HBqlException;
28 import org.apache.hadoop.hbase.hbql.client.Util;
29 import org.apache.hadoop.hbase.hbql.impl.HConnectionImpl;
30
31 import java.util.List;
32
33 public class StringFunction extends GenericFunction implements StringValue {
34
35 public StringFunction(final FunctionType functionType, final List<GenericValue> exprs) {
36 super(functionType, exprs);
37 }
38
39 public String getValue(final HConnectionImpl conn, final Object object) throws HBqlException,
40 ResultMissingColumnException,
41 NullColumnValueException {
42 switch (this.getFunctionType()) {
43
44 case TRIM: {
45 final String val = (String)this.getExprArg(0).getValue(conn, object);
46 return val.trim();
47 }
48
49 case LOWER: {
50 final String val = (String)this.getExprArg(0).getValue(conn, object);
51 return val.toLowerCase();
52 }
53
54 case UPPER: {
55 final String val = (String)this.getExprArg(0).getValue(conn, object);
56 return val.toUpperCase();
57 }
58
59 case CONCAT: {
60 final String v1 = (String)this.getExprArg(0).getValue(conn, object);
61 final String v2 = (String)this.getExprArg(1).getValue(conn, object);
62 return v1 + v2;
63 }
64
65 case REPLACE: {
66 final String v1 = (String)this.getExprArg(0).getValue(conn, object);
67 final String v2 = (String)this.getExprArg(1).getValue(conn, object);
68 final String v3 = (String)this.getExprArg(2).getValue(conn, object);
69 return v1.replace(v2, v3);
70 }
71
72 case SUBSTRING: {
73 final String val = (String)this.getExprArg(0).getValue(conn, object);
74 final int begin = ((Number)this.getExprArg(1).getValue(conn, object)).intValue();
75 final int length = ((Number)this.getExprArg(2).getValue(conn, object)).intValue();
76 return val.substring(begin, begin + length);
77 }
78
79 case ZEROPAD: {
80 final int num = ((Number)this.getExprArg(0).getValue(conn, object)).intValue();
81 final int width = ((Number)this.getExprArg(1).getValue(conn, object)).intValue();
82 return Util.getZeroPaddedNonNegativeNumber(num, width);
83 }
84
85 case REPEAT: {
86 final String val = (String)this.getExprArg(0).getValue(conn, object);
87 final int cnt = ((Number)this.getExprArg(1).getValue(conn, object)).intValue();
88 final StringBuilder sbuf = new StringBuilder();
89 for (int i = 0; i < cnt; i++)
90 sbuf.append(val);
91 return sbuf.toString();
92 }
93
94 default:
95 throw new HBqlException("Invalid function: " + this.getFunctionType());
96 }
97 }
98 }