5773 |
29 Sep 11 |
nicklas |
1 |
/** |
5773 |
29 Sep 11 |
nicklas |
$Id $ |
5773 |
29 Sep 11 |
nicklas |
3 |
|
5773 |
29 Sep 11 |
nicklas |
Copyright (C) 2011 Nicklas Nordborg |
5773 |
29 Sep 11 |
nicklas |
5 |
|
5773 |
29 Sep 11 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
5773 |
29 Sep 11 |
nicklas |
Available at http://base.thep.lu.se/ |
5773 |
29 Sep 11 |
nicklas |
8 |
|
5773 |
29 Sep 11 |
nicklas |
BASE is free software; you can redistribute it and/or |
5773 |
29 Sep 11 |
nicklas |
modify it under the terms of the GNU General Public License |
5773 |
29 Sep 11 |
nicklas |
as published by the Free Software Foundation; either version 3 |
5773 |
29 Sep 11 |
nicklas |
of the License, or (at your option) any later version. |
5773 |
29 Sep 11 |
nicklas |
13 |
|
5773 |
29 Sep 11 |
nicklas |
BASE is distributed in the hope that it will be useful, |
5773 |
29 Sep 11 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
5773 |
29 Sep 11 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
5773 |
29 Sep 11 |
nicklas |
GNU General Public License for more details. |
5773 |
29 Sep 11 |
nicklas |
18 |
|
5773 |
29 Sep 11 |
nicklas |
You should have received a copy of the GNU General Public License |
5773 |
29 Sep 11 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
5773 |
29 Sep 11 |
nicklas |
21 |
*/ |
5773 |
29 Sep 11 |
nicklas |
22 |
package net.sf.basedb.util.jep; |
5773 |
29 Sep 11 |
nicklas |
23 |
|
5773 |
29 Sep 11 |
nicklas |
24 |
import java.util.Stack; |
5773 |
29 Sep 11 |
nicklas |
25 |
|
5773 |
29 Sep 11 |
nicklas |
26 |
import org.nfunk.jep.ParseException; |
5773 |
29 Sep 11 |
nicklas |
27 |
|
5773 |
29 Sep 11 |
nicklas |
28 |
|
5773 |
29 Sep 11 |
nicklas |
29 |
/** |
5773 |
29 Sep 11 |
nicklas |
Adds a <code>left(string, index|string)</code> function to Jep. It is used to |
5773 |
29 Sep 11 |
nicklas |
cut out the leftmost characters of a string. If the second argument is a |
5773 |
29 Sep 11 |
nicklas |
number, the string is cut with {@link String#substring(int, int)} with 0 as |
5773 |
29 Sep 11 |
nicklas |
the first argument. If the second argument is a string, the position |
5773 |
29 Sep 11 |
nicklas |
of that string is looked up in the first string and is used as an index. |
5773 |
29 Sep 11 |
nicklas |
Example: |
5773 |
29 Sep 11 |
nicklas |
<pre class="code"> |
5773 |
29 Sep 11 |
nicklas |
left('foo:bar', 3) == 'foo' |
5773 |
29 Sep 11 |
nicklas |
left('foo:bar', ':') == 'foo') |
5773 |
29 Sep 11 |
nicklas |
</pre> |
5773 |
29 Sep 11 |
nicklas |
40 |
|
5773 |
29 Sep 11 |
nicklas |
If the second variant is used and the second string is not found within the first, |
5773 |
29 Sep 11 |
nicklas |
the first string is returned unmodified. |
5773 |
29 Sep 11 |
nicklas |
43 |
|
5773 |
29 Sep 11 |
nicklas |
@author nicklas |
5773 |
29 Sep 11 |
nicklas |
@since 3.0 |
5773 |
29 Sep 11 |
nicklas |
@base.modified $Date $ |
5773 |
29 Sep 11 |
nicklas |
47 |
*/ |
5773 |
29 Sep 11 |
nicklas |
48 |
public class LeftFunction |
5773 |
29 Sep 11 |
nicklas |
49 |
implements JepFunction |
5773 |
29 Sep 11 |
nicklas |
50 |
{ |
5773 |
29 Sep 11 |
nicklas |
51 |
|
5773 |
29 Sep 11 |
nicklas |
52 |
public LeftFunction() |
5773 |
29 Sep 11 |
nicklas |
53 |
{} |
5773 |
29 Sep 11 |
nicklas |
54 |
|
5773 |
29 Sep 11 |
nicklas |
55 |
/* |
5773 |
29 Sep 11 |
nicklas |
From the JepFunction interface |
5773 |
29 Sep 11 |
nicklas |
57 |
------------------------------------------- |
5773 |
29 Sep 11 |
nicklas |
58 |
*/ |
5773 |
29 Sep 11 |
nicklas |
59 |
/** |
5773 |
29 Sep 11 |
nicklas |
@return The string "left" |
5773 |
29 Sep 11 |
nicklas |
61 |
*/ |
6127 |
14 Sep 12 |
nicklas |
62 |
@Override |
5773 |
29 Sep 11 |
nicklas |
63 |
public String getFunctionName() |
5773 |
29 Sep 11 |
nicklas |
64 |
{ |
5773 |
29 Sep 11 |
nicklas |
65 |
return "left"; |
5773 |
29 Sep 11 |
nicklas |
66 |
} |
5773 |
29 Sep 11 |
nicklas |
67 |
// ------------------------------------------- |
5773 |
29 Sep 11 |
nicklas |
68 |
/* |
5773 |
29 Sep 11 |
nicklas |
From the PostfixMathCommandI interface |
5773 |
29 Sep 11 |
nicklas |
70 |
------------------------------------------- |
5773 |
29 Sep 11 |
nicklas |
71 |
*/ |
5773 |
29 Sep 11 |
nicklas |
72 |
/** |
5773 |
29 Sep 11 |
nicklas |
@return Always 2 |
5773 |
29 Sep 11 |
nicklas |
74 |
*/ |
6127 |
14 Sep 12 |
nicklas |
75 |
@Override |
5773 |
29 Sep 11 |
nicklas |
76 |
public int getNumberOfParameters() |
5773 |
29 Sep 11 |
nicklas |
77 |
{ |
5773 |
29 Sep 11 |
nicklas |
78 |
return 2; |
5773 |
29 Sep 11 |
nicklas |
79 |
} |
6127 |
14 Sep 12 |
nicklas |
80 |
@Override |
5773 |
29 Sep 11 |
nicklas |
81 |
public void setCurNumberOfParameters(int n) |
5773 |
29 Sep 11 |
nicklas |
82 |
{} |
6127 |
14 Sep 12 |
nicklas |
83 |
@Override |
5773 |
29 Sep 11 |
nicklas |
84 |
public boolean checkNumberOfParameters(int n) |
5773 |
29 Sep 11 |
nicklas |
85 |
{ |
5773 |
29 Sep 11 |
nicklas |
86 |
return n == 2; |
5773 |
29 Sep 11 |
nicklas |
87 |
} |
6127 |
14 Sep 12 |
nicklas |
88 |
@Override |
6875 |
20 Apr 15 |
nicklas |
89 |
@SuppressWarnings({"unchecked", "rawtypes"}) |
5773 |
29 Sep 11 |
nicklas |
90 |
public void run(Stack stack) |
5773 |
29 Sep 11 |
nicklas |
91 |
throws ParseException |
5773 |
29 Sep 11 |
nicklas |
92 |
{ |
5773 |
29 Sep 11 |
nicklas |
93 |
if (stack == null || stack.empty()) |
5773 |
29 Sep 11 |
nicklas |
94 |
{ |
5773 |
29 Sep 11 |
nicklas |
95 |
throw new ParseException("Stack is empty"); |
5773 |
29 Sep 11 |
nicklas |
96 |
} |
5773 |
29 Sep 11 |
nicklas |
97 |
Object arg2 = stack.pop(); |
5773 |
29 Sep 11 |
nicklas |
98 |
Object arg1 = stack.pop(); |
5773 |
29 Sep 11 |
nicklas |
99 |
|
5773 |
29 Sep 11 |
nicklas |
100 |
String s1 = arg1.toString(); |
5773 |
29 Sep 11 |
nicklas |
101 |
int index = 0; |
5773 |
29 Sep 11 |
nicklas |
102 |
if (arg2 instanceof Number) |
5773 |
29 Sep 11 |
nicklas |
103 |
{ |
5773 |
29 Sep 11 |
nicklas |
104 |
index = ((Number)arg2).intValue(); |
5773 |
29 Sep 11 |
nicklas |
105 |
} |
5773 |
29 Sep 11 |
nicklas |
106 |
else |
5773 |
29 Sep 11 |
nicklas |
107 |
{ |
5773 |
29 Sep 11 |
nicklas |
108 |
index = s1.indexOf(arg2.toString()); |
5773 |
29 Sep 11 |
nicklas |
109 |
} |
5773 |
29 Sep 11 |
nicklas |
110 |
stack.push(index < 0 ? s1 : s1.substring(0, index)); |
5773 |
29 Sep 11 |
nicklas |
111 |
} |
5773 |
29 Sep 11 |
nicklas |
112 |
// ------------------------------------------- |
5773 |
29 Sep 11 |
nicklas |
113 |
|
5773 |
29 Sep 11 |
nicklas |
114 |
|
5773 |
29 Sep 11 |
nicklas |
115 |
} |