View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
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  }