01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.kernel.exps;
20:
21: import org.apache.openjpa.kernel.StoreContext;
22: import serp.util.Numbers;
23:
24: /**
25: * Find the index of one string within another.
26: *
27: * @author Abe White
28: */
29: class IndexOf extends Val {
30:
31: private final Val _val;
32: private final Val _args;
33:
34: /**
35: * Constructor. Provide target string and the arguments to the
36: * indexOf method.
37: */
38: public IndexOf(Val val, Val args) {
39: _val = val;
40: _args = args;
41: }
42:
43: public Class getType() {
44: return int.class;
45: }
46:
47: public void setImplicitType(Class type) {
48: }
49:
50: protected Object eval(Object candidate, Object orig,
51: StoreContext ctx, Object[] params) {
52: Object str = _val.eval(candidate, orig, ctx, params);
53: Object arg = _args.eval(candidate, orig, ctx, params);
54: int idx;
55: if (arg instanceof Object[]) {
56: Object[] args = (Object[]) arg;
57: idx = str.toString().indexOf(args[0].toString(),
58: ((Number) args[1]).intValue());
59: } else
60: idx = str.toString().indexOf(arg.toString());
61: return Numbers.valueOf(idx);
62: }
63:
64: public void acceptVisit(ExpressionVisitor visitor) {
65: visitor.enter(this);
66: _val.acceptVisit(visitor);
67: _args.acceptVisit(visitor);
68: visitor.exit(this);
69: }
70: }
|