KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > data > expression > FunctionExpression


1 package prefuse.data.expression;
2
3 import java.text.DecimalFormat JavaDoc;
4 import java.text.NumberFormat JavaDoc;
5
6 import prefuse.data.Edge;
7 import prefuse.data.Node;
8 import prefuse.data.Schema;
9 import prefuse.data.Tuple;
10 import prefuse.util.ColorLib;
11 import prefuse.util.MathLib;
12 import prefuse.util.StringLib;
13 import prefuse.util.collections.CopyOnWriteArrayList;
14
15 /**
16  * Abstract base class for FunctionExpression implementations. Provides
17  * parameter handling support.
18  *
19  * @author <a HREF="http://jheer.org">jeffrey heer</a>
20  */

21 public abstract class FunctionExpression extends AbstractExpression
22     implements Function
23 {
24     protected CopyOnWriteArrayList m_params;
25     protected final int m_pcount;
26     
27     /**
28      * Protected constructor.
29      * @param parameterCount the max parameter count
30      */

31     protected FunctionExpression(int parameterCount) {
32         m_pcount = parameterCount;
33     }
34     
35     /**
36      * @see prefuse.data.expression.Function#getName()
37      */

38     public abstract String JavaDoc getName();
39     
40     /**
41      * @see prefuse.data.expression.Function#addParameter(prefuse.data.expression.Expression)
42      */

43     public void addParameter(Expression e) {
44         int pc = getParameterCount();
45         if ( pc!=VARARGS && paramCount()+1 > pc ) {
46             throw new IllegalStateException JavaDoc(
47                 "This function takes only "+pc+" parameters.");
48         }
49         if ( m_params == null )
50             m_params = new CopyOnWriteArrayList();
51         m_params.add(e);
52     }
53     
54     /**
55      * An internal-only method that returns the current number of
56      * parameters collected.
57      */

58     protected int paramCount() {
59         return m_params==null ? 0 : m_params.size();
60     }
61     
62     /**
63      * Return the parameter expression at the given index.
64      * @param idx the parameter index
65      * @return the parameter value Expression at the given index
66      */

67     protected final Expression param(int idx) {
68         return (Expression)m_params.get(idx);
69     }
70     
71     /**
72      * @see prefuse.data.expression.Function#getParameterCount()
73      */

74     public int getParameterCount() {
75         return m_pcount;
76     }
77     
78     /**
79      * Throw an exception when needed parameters are missing.
80      */

81     protected void missingParams() {
82         throw new IllegalStateException JavaDoc(
83             "Function is missing parameters: " + getName());
84     }
85     
86     // ------------------------------------------------------------------------
87

88     /**
89      * @see prefuse.data.expression.Expression#visit(prefuse.data.expression.ExpressionVisitor)
90      */

91     public void visit(ExpressionVisitor v) {
92         v.visitExpression(this);
93         if ( paramCount() > 0 ) {
94             Object JavaDoc[] params = m_params.getArray();
95             for ( int i=0; i<params.length; ++i ) {
96                 v.down();
97                 ((Expression)params[i]).visit(v);
98                 v.up();
99             }
100         }
101     }
102     
103     /**
104      * @see prefuse.data.expression.AbstractExpression#addChildListeners()
105      */

106     protected void addChildListeners() {
107         if ( paramCount() > 0 ) {
108             Object JavaDoc[] params = m_params.getArray();
109             for ( int i=0; i<params.length; ++i )
110                 ((Expression)params[i]).addExpressionListener(this);
111         }
112     }
113     
114     /**
115      * @see prefuse.data.expression.AbstractExpression#removeChildListeners()
116      */

117     protected void removeChildListeners() {
118         if ( paramCount() > 0 ) {
119             Object JavaDoc[] params = m_params.getArray();
120             for ( int i=0; i<params.length; ++i )
121                 ((Expression)params[i]).removeExpressionListener(this);
122         }
123     }
124     
125     /**
126      * @see java.lang.Object#toString()
127      */

128     public String JavaDoc toString() {
129         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
130         sbuf.append(getName()).append('(');
131         for ( int i=0; i<paramCount(); ++i ) {
132             if ( i > 0 ) sbuf.append(", ");
133             sbuf.append(param(i).toString());
134         }
135         sbuf.append(')');
136         return sbuf.toString();
137     }
138
139 } // end of class FunctionExpression
140

141 /**
142  * Default Function Implementations
143  */

144
145 // ----------------------------------------------------------------------------
146
// Mathematical Functions
147

148 abstract class DoubleFunction extends FunctionExpression {
149     protected DoubleFunction(int parameterCount) {
150         super(parameterCount);
151     }
152     public Class JavaDoc getType(Schema s) {
153         return double.class;
154     }
155     public Object JavaDoc get(Tuple t) {
156         return new Double JavaDoc(getDouble(t));
157     }
158     public int getInt(Tuple t) {
159         return (int)getDouble(t);
160     }
161     public long getLong(Tuple t) {
162         return (long)getDouble(t);
163     }
164     public float getFloat(Tuple t) {
165         return (float)getDouble(t);
166     }
167 }
168 abstract class IntFunction extends FunctionExpression {
169     protected IntFunction(int parameterCount) {
170         super(parameterCount);
171     }
172     public Class JavaDoc getType(Schema s) {
173         return int.class;
174     }
175     public Object JavaDoc get(Tuple t) {
176         return new Integer JavaDoc(getInt(t));
177     }
178     public long getLong(Tuple t) {
179         return (long)getInt(t);
180     }
181     public float getFloat(Tuple t) {
182         return (float)getFloat(t);
183     }
184     public double getDouble(Tuple t) {
185         return (double)getInt(t);
186     }
187 }
188 abstract class BooleanFunction extends FunctionExpression
189     implements Predicate
190 {
191     protected BooleanFunction(int parameterCount) {
192         super(parameterCount);
193     }
194     public Class JavaDoc getType(Schema s) {
195         return boolean.class;
196     }
197     public Object JavaDoc get(Tuple t) {
198         return getBoolean(t) ? Boolean.TRUE : Boolean.FALSE;
199     }
200 }
201
202 //ROW()
203
class RowFunction extends IntFunction {
204     public RowFunction() { super(0); }
205     public String JavaDoc getName() { return "ROW"; }
206     public int getInt(Tuple t) {
207         return t.getRow();
208     }
209 }
210 //ISNODE()
211
class IsNodeFunction extends BooleanFunction {
212     public IsNodeFunction() { super(0); }
213     public String JavaDoc getName() { return "ISNODE"; }
214     public boolean getBoolean(Tuple t) {
215         return (t instanceof Node);
216     }
217 }
218 //ISEDGE()
219
class IsEdgeFunction extends BooleanFunction {
220     public IsEdgeFunction() { super(0); }
221     public String JavaDoc getName() { return "ISEDGE"; }
222     public boolean getBoolean(Tuple t) {
223         return (t instanceof Edge);
224     }
225 }
226 //DEGREE()
227
class DegreeFunction extends IntFunction {
228     public DegreeFunction() { super(0); }
229     public String JavaDoc getName() { return "DEGREE"; }
230     public int getInt(Tuple t) {
231         return (t instanceof Node ? ((Node)t).getDegree() : 0 );
232     }
233 }
234 //INDEGREE()
235
class InDegreeFunction extends IntFunction {
236     public InDegreeFunction() { super(0); }
237     public String JavaDoc getName() { return "INDEGREE"; }
238     public int getInt(Tuple t) {
239         return (t instanceof Node ? ((Node)t).getInDegree() : 0 );
240     }
241 }
242 //OUTDEGREE()
243
class OutDegreeFunction extends IntFunction {
244     public OutDegreeFunction() { super(0); }
245     public String JavaDoc getName() { return "OUTDEGREE"; }
246     public int getInt(Tuple t) {
247         return (t instanceof Node ? ((Node)t).getOutDegree() : 0 );
248     }
249 }
250 //CHILDCOUNT()
251
class ChildCountFunction extends IntFunction {
252     public ChildCountFunction() { super(0); }
253     public String JavaDoc getName() { return "CHILDCOUNT"; }
254     public int getInt(Tuple t) {
255         return (t instanceof Node ? ((Node)t).getChildCount() : 0 );
256     }
257 }
258 //TREEDEPTH()
259
class TreeDepthFunction extends IntFunction {
260     public TreeDepthFunction() { super(0); }
261     public String JavaDoc getName() { return "TREEDEPTH"; }
262     public int getInt(Tuple t) {
263         return (t instanceof Node ? ((Node)t).getDepth() : 0 );
264     }
265 }
266
267 //ABS(X)
268
class AbsFunction extends DoubleFunction {
269     public AbsFunction() { super(1); }
270     public String JavaDoc getName() { return "ABS"; }
271     public double getDouble(Tuple t) {
272         if ( paramCount() == 1 ) {
273             return Math.abs(param(0).getDouble(t));
274         } else {
275             missingParams(); return Double.NaN;
276         }
277     }
278 }
279
280 //ACOS(X)
281
class AcosFunction extends DoubleFunction {
282     public AcosFunction() { super(1); }
283     public String JavaDoc getName() { return "ACOS"; }
284     public double getDouble(Tuple t) {
285         if ( paramCount() == 1 ) {
286             return Math.acos(param(0).getDouble(t));
287         } else {
288             missingParams(); return Double.NaN;
289         }
290     }
291 }
292
293 //ASIN(X)
294
class AsinFunction extends DoubleFunction {
295     public AsinFunction() { super(1); }
296     public String JavaDoc getName() { return "ASIN"; }
297     public double getDouble(Tuple t) {
298         if ( paramCount() == 1 ) {
299             return Math.asin(param(0).getDouble(t));
300         } else {
301             missingParams(); return Double.NaN;
302         }
303     }
304 }
305
306 //ATAN(X)
307
class AtanFunction extends DoubleFunction {
308     public AtanFunction() { super(1); }
309     public String JavaDoc getName() { return "ATAN"; }
310     public double getDouble(Tuple t) {
311         if ( paramCount() == 1 ) {
312             return Math.atan(param(0).getDouble(t));
313         } else {
314             missingParams(); return Double.NaN;
315         }
316     }
317 }
318
319 //ATAN2(Y,X)
320
class Atan2Function extends DoubleFunction {
321     public Atan2Function() { super(2); }
322     public String JavaDoc getName() { return "ATAN2"; }
323     public double getDouble(Tuple t) {
324         if ( paramCount() == 2 ) {
325             return Math.atan2(param(0).getDouble(t), param(1).getDouble(t));
326         } else {
327             missingParams(); return Double.NaN;
328         }
329     }
330 }
331 //CEILING(X), CEIL(X)
332
class CeilFunction extends DoubleFunction {
333     public CeilFunction() { super(1); }
334     public String JavaDoc getName() { return "CEIL"; }
335     public double getDouble(Tuple t) {
336         if ( paramCount() == 1 ) {
337             return Math.ceil(param(0).getDouble(t));
338         } else {
339             missingParams(); return Double.NaN;
340         }
341     }
342     public int getInt(Tuple t) {
343         return (int)getDouble(t);
344     }
345 }
346 //COS(X)
347
class CosFunction extends DoubleFunction {
348     public CosFunction() { super(1); }
349     public String JavaDoc getName() { return "COS"; }
350     public double getDouble(Tuple t) {
351         if ( paramCount() == 1 ) {
352             return Math.cos(param(0).getDouble(t));
353         } else {
354             missingParams(); return Double.NaN;
355         }
356     }
357 }
358 //COT(X)
359
class CotFunction extends DoubleFunction {
360     public CotFunction() { super(1); }
361     public String JavaDoc getName() { return "COT"; }
362     public double getDouble(Tuple t) {
363         if ( paramCount() == 1 ) {
364             return 1/Math.tan(param(0).getDouble(t));
365         } else {
366             missingParams(); return Double.NaN;
367         }
368     }
369 }
370 //DEGREES(X)
371
class DegreesFunction extends DoubleFunction {
372     public DegreesFunction() { super(1); }
373     public String JavaDoc getName() { return "DEGREES"; }
374     public double getDouble(Tuple t) {
375         if ( paramCount() == 1 ) {
376             return Math.toDegrees(param(0).getDouble(t));
377         } else {
378             missingParams(); return Double.NaN;
379         }
380     }
381 }
382 //E()
383
class EFunction extends DoubleFunction {
384     public EFunction() { super(0); }
385     public String JavaDoc getName() { return "E"; }
386     public double getDouble(Tuple t) {
387         return Math.E;
388     }
389 }
390 //EXP(X)
391
class ExpFunction extends DoubleFunction {
392     public ExpFunction() { super(1); }
393     public String JavaDoc getName() { return "EXP"; }
394     public double getDouble(Tuple t) {
395         if ( paramCount() == 1 ) {
396             return Math.exp(param(0).getDouble(t));
397         } else {
398             missingParams(); return Double.NaN;
399         }
400     }
401 }
402 //FLOOR(X)
403
class FloorFunction extends DoubleFunction {
404     public FloorFunction() { super(1); }
405     public String JavaDoc getName() { return "FLOOR"; }
406     public double getDouble(Tuple t) {
407         if ( paramCount() == 1 ) {
408             return Math.floor(param(0).getDouble(t));
409         } else {
410             missingParams(); return Double.NaN;
411         }
412     }
413     public int getInt(Tuple t) {
414         return (int)getDouble(t);
415     }
416 }
417 //LOG(X), LOG(B,X)
418
class LogFunction extends DoubleFunction {
419     public LogFunction() { super(2); }
420     public String JavaDoc getName() { return "LOG"; }
421     public double getDouble(Tuple t) {
422         int pc = paramCount();
423         if ( pc == 2 ) {
424             double b = param(0).getDouble(t);
425             double x = param(1).getDouble(t);
426             return Math.log(x)/Math.log(b);
427         } else if ( pc == 1 ) {
428             return Math.log(param(0).getDouble(t));
429         } else {
430             missingParams();
431             return Double.NaN;
432         }
433     }
434 }
435 //LOG2(X)
436
class Log2Function extends DoubleFunction {
437     public Log2Function() { super(1); }
438     public String JavaDoc getName() { return "LOG2"; }
439     public double getDouble(Tuple t) {
440         if ( paramCount() == 1 ) {
441             return MathLib.log2(param(0).getDouble(t));
442         } else {
443             missingParams(); return Double.NaN;
444         }
445     }
446 }
447 //LOG10(X)
448
class Log10Function extends DoubleFunction {
449     public Log10Function() { super(1); }
450     public String JavaDoc getName() { return "LOG10"; }
451     public double getDouble(Tuple t) {
452         if ( paramCount() == 1 ) {
453             return MathLib.log10(param(0).getDouble(t));
454         } else {
455             missingParams(); return Double.NaN;
456         }
457     }
458 }
459 //SAFELOG10(X)
460
class SafeLog10Function extends DoubleFunction {
461     public SafeLog10Function() { super(2); }
462     public String JavaDoc getName() { return "SAFELOG10"; }
463     public double getDouble(Tuple t) {
464         int pc = paramCount();
465         if ( pc == 1 ) {
466             double x = param(0).getDouble(t);
467             return MathLib.safeLog10(x);
468         } else {
469             missingParams();
470             return Double.NaN;
471         }
472     }
473 }
474 //MAX(X1,X2,...)
475
class MaxFunction extends DoubleFunction {
476     public MaxFunction() { super(Function.VARARGS); }
477     public String JavaDoc getName() { return "MAX"; }
478     public double getDouble(Tuple t) {
479         double x, v = param(0).getDouble(t);
480         for ( int i=1; i<paramCount(); ++i ) {
481             x = param(i).getDouble(t);
482             if ( x > v ) v = x;
483         }
484         return v;
485     }
486 }
487 //MIN(X1,X2,...)
488
class MinFunction extends DoubleFunction {
489     public MinFunction() { super(Function.VARARGS); }
490     public String JavaDoc getName() { return "MIN"; }
491     public double getDouble(Tuple t) {
492         double x, v = param(0).getDouble(t);
493         for ( int i=1; i<paramCount(); ++i ) {
494             x = param(i).getDouble(t);
495             if ( x < v ) v = x;
496         }
497         return v;
498     }
499 }
500 //MOD(N,M)
501
class ModFunction extends DoubleFunction {
502     public ModFunction() { super(2); }
503     public String JavaDoc getName() { return "MOD"; }
504     public double getDouble(Tuple t) {
505         if ( paramCount() == 2 ) {
506             double x = param(0).getDouble(t);
507             double y = param(1).getDouble(t);
508             return x % y;
509         } else {
510             missingParams(); return Double.NaN;
511         }
512     }
513 }
514 //PI()
515
class PiFunction extends DoubleFunction {
516     public PiFunction() { super(0); }
517     public String JavaDoc getName() { return "PI"; }
518     public double getDouble(Tuple t) {
519         return Math.PI;
520     }
521 }
522 //POW(X,Y), POWER(X,Y)
523
class PowFunction extends DoubleFunction {
524     public PowFunction() { super(1); }
525     public String JavaDoc getName() { return "POW"; }
526     public double getDouble(Tuple t) {
527         if ( paramCount() == 2 ) {
528             return Math.pow(param(0).getDouble(t), param(1).getDouble(t));
529         } else {
530             missingParams(); return Double.NaN;
531         }
532     }
533 }
534 //RADIANS(X)
535
class RadiansFunction extends DoubleFunction {
536     public RadiansFunction() { super(1); }
537     public String JavaDoc getName() { return "RADIANS"; }
538     public double getDouble(Tuple t) {
539         if ( paramCount() == 1 ) {
540             return Math.toRadians(param(0).getDouble(t));
541         } else {
542             missingParams(); return Double.NaN;
543         }
544     }
545 }
546 //RAND(), RAND(N)
547
class RandFunction extends DoubleFunction {
548     public RandFunction() { super(0); }
549     public String JavaDoc getName() { return "RAND"; }
550     public double getDouble(Tuple t) {
551         return Math.random();
552     }
553 }
554 //ROUND(X) // --> ROUND(X,D) TODO
555
class RoundFunction extends DoubleFunction {
556     public RoundFunction() { super(1); }
557     public String JavaDoc getName() { return "ROUND"; }
558     public double getDouble(Tuple t) {
559         if ( paramCount() == 1 ) {
560             return Math.round(param(0).getDouble(t));
561         } else {
562             missingParams(); return Double.NaN;
563         }
564     }
565     public int getInt(Tuple t) {
566         return (int)getDouble(t);
567     }
568 }
569 //SIGN(X)
570
class SignFunction extends DoubleFunction {
571     public SignFunction() { super(1); }
572     public String JavaDoc getName() { return "SIGN"; }
573     public Class JavaDoc getType(Schema s) {
574         return int.class;
575     }
576     public double getDouble(Tuple t) {
577         return getInt(t);
578     }
579     public int getInt(Tuple t) {
580         if ( paramCount() == 1 ) {
581             double d = param(0).getDouble(t);
582             return d<0 ? -1 : d==0 ? 0 : 1;
583         } else {
584             missingParams(); return Integer.MIN_VALUE;
585         }
586     }
587 }
588 //SIN(X)
589
class SinFunction extends DoubleFunction {
590     public SinFunction() { super(1); }
591     public String JavaDoc getName() { return "SIN"; }
592     public double getDouble(Tuple t) {
593         if ( paramCount() == 1 ) {
594             return Math.sin(param(0).getDouble(t));
595         } else {
596             missingParams(); return Double.NaN;
597         }
598     }
599 }
600 //SQRT(X)
601
class SqrtFunction extends DoubleFunction {
602     public SqrtFunction() { super(1); }
603     public String JavaDoc getName() { return "SQRT"; }
604     public double getDouble(Tuple t) {
605         if ( paramCount() == 1 ) {
606             return Math.sqrt(param(0).getDouble(t));
607         } else {
608             missingParams(); return Double.NaN;
609         }
610     }
611 }
612 //SUM(X1,X2,...)
613
class SumFunction extends DoubleFunction {
614     public SumFunction() { super(Function.VARARGS); }
615     public String JavaDoc getName() { return "SUM"; }
616     public double getDouble(Tuple t) {
617         double v = param(0).getDouble(t);
618         for ( int i=1; i<paramCount(); ++i )
619             v += param(i).getDouble(t);
620         return v;
621     }
622 }
623 //SAFESQRT(X)
624
class SafeSqrtFunction extends DoubleFunction {
625     public SafeSqrtFunction() { super(2); }
626     public String JavaDoc getName() { return "SAFESQRT"; }
627     public double getDouble(Tuple t) {
628         int pc = paramCount();
629         if ( pc == 1 ) {
630             double x = param(0).getDouble(t);
631             return MathLib.safeSqrt(x);
632         } else {
633             missingParams();
634             return Double.NaN;
635         }
636     }
637 }
638 //TAN(X)
639
class TanFunction extends DoubleFunction {
640     public TanFunction() { super(1); }
641     public String JavaDoc getName() { return "TAN"; }
642     public double getDouble(Tuple t) {
643         if ( paramCount() == 1 ) {
644             return Math.tan(param(0).getDouble(t));
645         } else {
646             missingParams(); return Double.NaN;
647         }
648     }
649 }
650 //TRUNCATE(X,D)
651
// TODO
652

653 //----------------------------------------------------------------------------
654
// String Functions
655

656 abstract class StringFunction extends FunctionExpression {
657     protected StringFunction(int parameterCount) {
658         super(parameterCount);
659     }
660     public Class JavaDoc getType(Schema s) {
661         return String JavaDoc.class;
662     }
663     protected StringBuffer JavaDoc getBuffer() {
664         return new StringBuffer JavaDoc();
665     }
666 }
667 //CAP(str1)
668
class CapFunction extends StringFunction {
669     public CapFunction() { super(1); }
670     public String JavaDoc getName() { return "CAP"; }
671     public Object JavaDoc get(Tuple t) {
672         String JavaDoc str = param(0).get(t).toString();
673         return StringLib.capitalizeFirstOnly(str);
674     }
675 }
676 //CONCAT(str1,str2,...)
677
class ConcatFunction extends StringFunction {
678     public ConcatFunction() { super(Function.VARARGS); }
679     public String JavaDoc getName() { return "CONCAT"; }
680     public Object JavaDoc get(Tuple t) {
681         StringBuffer JavaDoc sbuf = getBuffer();
682         for ( int i=0; i<paramCount(); ++i ) {
683             sbuf.append(param(i).get(t).toString());
684         }
685         return sbuf.toString();
686     }
687 }
688 //CONCAT_WS(separator,str1,str2,...)
689
class ConcatWsFunction extends StringFunction {
690     public ConcatWsFunction() { super(Function.VARARGS); }
691     public String JavaDoc getName() { return "CONCAT_WS"; }
692     public Object JavaDoc get(Tuple t) {
693         StringBuffer JavaDoc sbuf = getBuffer();
694         String JavaDoc sep = param(0).get(t).toString();
695         for ( int i=1; i<paramCount(); ++i ) {
696             sbuf.append(param(i).get(t).toString());
697             sbuf.append(sep);
698         }
699         return sbuf.toString();
700     }
701 }
702 //FORMAT(X,D)
703
class FormatFunction extends StringFunction {
704     public FormatFunction() { super(2); }
705     public String JavaDoc getName() { return "FORMAT"; }
706     public Object JavaDoc get(Tuple t) {
707         double x = param(0).getDouble(t);
708         int d = param(1).getInt(t);
709         DecimalFormat JavaDoc df = (DecimalFormat JavaDoc)NumberFormat.getInstance();
710         df.setMinimumFractionDigits(d);
711         df.setMaximumFractionDigits(d);
712         return df.format(x);
713     }
714 }
715 //INSERT(str,pos,len,newstr)
716
class InsertFunction extends StringFunction {
717     public InsertFunction() { super(4); }
718     public String JavaDoc getName() { return "INSERT"; }
719     public Object JavaDoc get(Tuple t) {
720         String JavaDoc str = param(0).get(t).toString();
721         int strlen = str.length();
722         int pos = param(1).getInt(t);
723         int len = pos+param(2).getInt(t);
724         String JavaDoc newstr = param(1).get(t).toString();
725         if ( pos < 0 || pos > strlen )
726             return str;
727         if ( len < 0 || len > strlen )
728             return str.substring(0,pos)+newstr;
729         else
730             return str.substring(0,pos)+newstr+str.substring(len);
731     }
732 }
733 //INSTR(str,substr)
734
//LEFT(str,len)
735
class LeftFunction extends StringFunction {
736     public LeftFunction() { super(2); }
737     public String JavaDoc getName() { return "LEFT"; }
738     public Object JavaDoc get(Tuple t) {
739         String JavaDoc src = param(0).get(t).toString();
740         int len = param(1).getInt(t);
741         return src.substring(0, len);
742     }
743     
744 }
745 //LENGTH(str)
746
class LengthFunction extends IntFunction {
747     public LengthFunction() { super(1); }
748     public String JavaDoc getName() { return "LENGTH"; }
749     public int getInt(Tuple t) {
750         return param(0).get(t).toString().length();
751     }
752 }
753 //LOCATE(substr,str), LOCATE(substr,str,pos)
754
//LOWER(str) | LCASE(str)
755
class LowerFunction extends StringFunction {
756     public LowerFunction() { super(1); }
757     public String JavaDoc getName() { return "LOWER"; }
758     public Object JavaDoc get(Tuple t) {
759         return param(0).get(t).toString().toLowerCase();
760     }
761 }
762 //LPAD(str,len,padstr)
763
class LPadFunction extends StringFunction {
764     public LPadFunction() { super(3); }
765     public String JavaDoc getName() { return "LPAD"; }
766     public Object JavaDoc get(Tuple t) {
767         String JavaDoc str = param(0).get(t).toString();
768         int len = param(1).getInt(t);
769         String JavaDoc pad = param(2).get(t).toString();
770         int strlen = str.length();
771         if ( strlen > len ) {
772             return str.substring(0,len);
773         } else if ( strlen == len ) {
774             return str;
775         } else {
776             StringBuffer JavaDoc sbuf = getBuffer();
777             int padlen = pad.length();
778             int diff = len-strlen;
779             for ( int i=0; i<diff; i+=padlen)
780                 sbuf.append(pad);
781             if ( sbuf.length() > diff )
782                 sbuf.delete(diff, sbuf.length());
783             sbuf.append(str);
784             return sbuf.toString();
785         }
786     }
787 }
788 //LTRIM(str)
789
//MID(str,pos,len) -- same as substring
790
//POSITION(substr, str)
791
class PositionFunction extends IntFunction {
792     public PositionFunction() { super(2); }
793     public String JavaDoc getName() { return "POSITION"; }
794     public int getInt(Tuple t) {
795         String JavaDoc substr = param(0).get(t).toString();
796         String JavaDoc src = param(1).get(t).toString();
797         return src.indexOf(substr);
798     }
799 }
800 //QUOTE(str)
801
//REPEAT(str,count)
802
class RepeatFunction extends StringFunction {
803     public RepeatFunction() { super(2); }
804     public String JavaDoc getName() { return "REPEAT"; }
805     public Object JavaDoc get(Tuple t) {
806         String JavaDoc src = param(0).get(t).toString();
807         int count = param(1).getInt(t);
808         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
809         for ( int i=0; i<count; ++i ) {
810             sbuf.append(src);
811         }
812         return sbuf.toString();
813     }
814 }
815 //REPLACE(str,from_str,to_str)
816
class ReplaceFunction extends StringFunction {
817     public ReplaceFunction() { super(3); }
818     public String JavaDoc getName() { return "REPLACE"; }
819     public Object JavaDoc get(Tuple t) {
820         String JavaDoc src = param(0).get(t).toString();
821         String JavaDoc from = param(1).get(t).toString();
822         String JavaDoc to = param(2).get(t).toString();
823         return src.replaceAll(from, to);
824     }
825     
826 }
827 //REVERSE(str)
828
class ReverseFunction extends StringFunction {
829     public ReverseFunction() { super(1); }
830     public String JavaDoc getName() { return "REVERSE"; }
831     public Object JavaDoc get(Tuple t) {
832         String JavaDoc str = param(0).get(t).toString();
833         StringBuffer JavaDoc sbuf = getBuffer();
834         for ( int i=str.length()-1; --i>=0; ) {
835             sbuf.append(str.charAt(i));
836         }
837         return sbuf.toString();
838     }
839 }
840 //RIGHT(str,len)
841
class RightFunction extends StringFunction {
842     public RightFunction() { super(2); }
843     public String JavaDoc getName() { return "RIGHT"; }
844     public Object JavaDoc get(Tuple t) {
845         String JavaDoc src = param(0).get(t).toString();
846         int len = param(1).getInt(t);
847         return src.substring(src.length()-len);
848     }
849     
850 }
851 //RPAD(str,len,padstr)
852
class RPadFunction extends StringFunction {
853     public RPadFunction() { super(3); }
854     public String JavaDoc getName() { return "RPAD"; }
855     public Object JavaDoc get(Tuple t) {
856         String JavaDoc str = param(0).get(t).toString();
857         int len = param(1).getInt(t);
858         String JavaDoc pad = param(2).get(t).toString();
859         int strlen = str.length();
860         if ( strlen > len ) {
861             return str.substring(0,len);
862         } else if ( strlen == len ) {
863             return str;
864         } else {
865             StringBuffer JavaDoc sbuf = getBuffer();
866             sbuf.append(str);
867             int padlen = pad.length();
868             int diff = len-strlen;
869             for ( int i=0; i<diff; i+=padlen)
870                 sbuf.append(pad);
871             if ( sbuf.length() > len )
872                 sbuf.delete(len, sbuf.length());
873             return sbuf.toString();
874         }
875     }
876 }
877 //RTRIM(str)
878
//// SOUNDEX(str)
879
//SPACE(N)
880
class SpaceFunction extends StringFunction {
881     public SpaceFunction() { super(1); }
882     public String JavaDoc getName() { return "SPACE"; }
883     public Object JavaDoc get(Tuple t) {
884         int n = param(0).getInt(t);
885         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
886         for ( int i=0; i<n; ++i )
887             sbuf.append(' ');
888         return sbuf.toString();
889     }
890     
891 }
892 //SUBSTRING(str,pos), SUBSTRING(str,pos,len)
893
class SubstringFunction extends StringFunction {
894     public SubstringFunction() { super(3); }
895     public String JavaDoc getName() { return "SUBSTRING"; }
896     public Object JavaDoc get(Tuple t) {
897         String JavaDoc src = param(0).get(t).toString();
898         int pos = param(1).getInt(t);
899         if ( paramCount() == 3 ) {
900             int len = param(2).getInt(t);
901             return src.substring(pos, pos+len);
902         } else {
903             return src.substring(pos);
904         }
905     }
906 }
907 //SUBSTRING_INDEX(str,delim,count)
908
//TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM(remstr FROM] str)
909
//UPPER(str) | UCASE(str)
910
class UpperFunction extends StringFunction {
911     public UpperFunction() { super(1); }
912     public String JavaDoc getName() { return "UPPER"; }
913     public Object JavaDoc get(Tuple t) {
914         return param(0).get(t).toString().toUpperCase();
915     }
916 }
917
918 // ----------------------------------------------------------------------------
919

920 //RGB(r,g,b)
921
class RGBFunction extends IntFunction {
922     public RGBFunction() { super(3); }
923     public String JavaDoc getName() { return "RGB"; }
924     public int getInt(Tuple t) {
925         int r = param(0).getInt(t);
926         int g = param(1).getInt(t);
927         int b = param(2).getInt(t);
928         return ColorLib.rgb(r,g,b);
929     }
930 }
931 //HEX(hex)
932
class HexFunction extends IntFunction {
933     public HexFunction() { super(1); }
934     public String JavaDoc getName() { return "RGB"; }
935     public int getInt(Tuple t) {
936         String JavaDoc hex = (String JavaDoc)param(0).get(t);
937         return ColorLib.hex(hex);
938     }
939 }
940 //RGBA(r,g,b,a)
941
class RGBAFunction extends IntFunction {
942     public RGBAFunction() { super(4); }
943     public String JavaDoc getName() { return "RGBA"; }
944     public int getInt(Tuple t) {
945         int r = param(0).getInt(t);
946         int g = param(1).getInt(t);
947         int b = param(2).getInt(t);
948         int a = param(3).getInt(t);
949         return ColorLib.rgba(r,g,b,a);
950     }
951 }
952 //GRAY(v) | GRAY(v, a)
953
class GrayFunction extends IntFunction {
954     public GrayFunction() { super(2); }
955     public String JavaDoc getName() { return "GRAY"; }
956     public int getInt(Tuple t) {
957         int g = param(0).getInt(t);
958         if ( paramCount() == 2 ) {
959             int a = param(1).getInt(t);
960             return ColorLib.gray(g, a);
961         } else {
962             return ColorLib.gray(g);
963         }
964     }
965 }
966 //HSB(h,s,b)
967
class HSBFunction extends IntFunction {
968     public HSBFunction() { super(3); }
969     public String JavaDoc getName() { return "HSB"; }
970     public int getInt(Tuple t) {
971         float h = param(0).getFloat(t);
972         float s = param(1).getFloat(t);
973         float b = param(2).getFloat(t);
974         return ColorLib.hsb(h,s,b);
975     }
976 }
977 //HSBA(h,s,b,a)
978
class HSBAFunction extends IntFunction {
979     public HSBAFunction() { super(4); }
980     public String JavaDoc getName() { return "HSBA"; }
981     public int getInt(Tuple t) {
982         float h = param(0).getFloat(t);
983         float s = param(1).getFloat(t);
984         float b = param(2).getFloat(t);
985         float a = param(3).getFloat(t);
986         return ColorLib.hsba(h,s,b,a);
987     }
988 }
989 //COLORINTERP(c1, c2, f)
990
class ColorInterpFunction extends IntFunction {
991     public ColorInterpFunction() { super(3); }
992     public String JavaDoc getName() { return "COLORINTERP"; }
993     public int getInt(Tuple t) {
994         int c1 = param(0).getInt(t);
995         int c2 = param(1).getInt(t);
996         double f = param(2).getDouble(t);
997         return ColorLib.interp(c1, c2, f);
998     }
999 }
1000
Popular Tags