1 package prefuse.data.expression; 2 3 import java.text.DecimalFormat ; 4 import java.text.NumberFormat ; 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 21 public abstract class FunctionExpression extends AbstractExpression 22 implements Function 23 { 24 protected CopyOnWriteArrayList m_params; 25 protected final int m_pcount; 26 27 31 protected FunctionExpression(int parameterCount) { 32 m_pcount = parameterCount; 33 } 34 35 38 public abstract String getName(); 39 40 43 public void addParameter(Expression e) { 44 int pc = getParameterCount(); 45 if ( pc!=VARARGS && paramCount()+1 > pc ) { 46 throw new IllegalStateException ( 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 58 protected int paramCount() { 59 return m_params==null ? 0 : m_params.size(); 60 } 61 62 67 protected final Expression param(int idx) { 68 return (Expression)m_params.get(idx); 69 } 70 71 74 public int getParameterCount() { 75 return m_pcount; 76 } 77 78 81 protected void missingParams() { 82 throw new IllegalStateException ( 83 "Function is missing parameters: " + getName()); 84 } 85 86 88 91 public void visit(ExpressionVisitor v) { 92 v.visitExpression(this); 93 if ( paramCount() > 0 ) { 94 Object [] 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 106 protected void addChildListeners() { 107 if ( paramCount() > 0 ) { 108 Object [] params = m_params.getArray(); 109 for ( int i=0; i<params.length; ++i ) 110 ((Expression)params[i]).addExpressionListener(this); 111 } 112 } 113 114 117 protected void removeChildListeners() { 118 if ( paramCount() > 0 ) { 119 Object [] params = m_params.getArray(); 120 for ( int i=0; i<params.length; ++i ) 121 ((Expression)params[i]).removeExpressionListener(this); 122 } 123 } 124 125 128 public String toString() { 129 StringBuffer sbuf = new StringBuffer (); 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 } 141 144 145 148 abstract class DoubleFunction extends FunctionExpression { 149 protected DoubleFunction(int parameterCount) { 150 super(parameterCount); 151 } 152 public Class getType(Schema s) { 153 return double.class; 154 } 155 public Object get(Tuple t) { 156 return new Double (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 getType(Schema s) { 173 return int.class; 174 } 175 public Object get(Tuple t) { 176 return new Integer (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 getType(Schema s) { 195 return boolean.class; 196 } 197 public Object get(Tuple t) { 198 return getBoolean(t) ? Boolean.TRUE : Boolean.FALSE; 199 } 200 } 201 202 class RowFunction extends IntFunction { 204 public RowFunction() { super(0); } 205 public String getName() { return "ROW"; } 206 public int getInt(Tuple t) { 207 return t.getRow(); 208 } 209 } 210 class IsNodeFunction extends BooleanFunction { 212 public IsNodeFunction() { super(0); } 213 public String getName() { return "ISNODE"; } 214 public boolean getBoolean(Tuple t) { 215 return (t instanceof Node); 216 } 217 } 218 class IsEdgeFunction extends BooleanFunction { 220 public IsEdgeFunction() { super(0); } 221 public String getName() { return "ISEDGE"; } 222 public boolean getBoolean(Tuple t) { 223 return (t instanceof Edge); 224 } 225 } 226 class DegreeFunction extends IntFunction { 228 public DegreeFunction() { super(0); } 229 public String getName() { return "DEGREE"; } 230 public int getInt(Tuple t) { 231 return (t instanceof Node ? ((Node)t).getDegree() : 0 ); 232 } 233 } 234 class InDegreeFunction extends IntFunction { 236 public InDegreeFunction() { super(0); } 237 public String getName() { return "INDEGREE"; } 238 public int getInt(Tuple t) { 239 return (t instanceof Node ? ((Node)t).getInDegree() : 0 ); 240 } 241 } 242 class OutDegreeFunction extends IntFunction { 244 public OutDegreeFunction() { super(0); } 245 public String getName() { return "OUTDEGREE"; } 246 public int getInt(Tuple t) { 247 return (t instanceof Node ? ((Node)t).getOutDegree() : 0 ); 248 } 249 } 250 class ChildCountFunction extends IntFunction { 252 public ChildCountFunction() { super(0); } 253 public String getName() { return "CHILDCOUNT"; } 254 public int getInt(Tuple t) { 255 return (t instanceof Node ? ((Node)t).getChildCount() : 0 ); 256 } 257 } 258 class TreeDepthFunction extends IntFunction { 260 public TreeDepthFunction() { super(0); } 261 public String getName() { return "TREEDEPTH"; } 262 public int getInt(Tuple t) { 263 return (t instanceof Node ? ((Node)t).getDepth() : 0 ); 264 } 265 } 266 267 class AbsFunction extends DoubleFunction { 269 public AbsFunction() { super(1); } 270 public String 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 class AcosFunction extends DoubleFunction { 282 public AcosFunction() { super(1); } 283 public String 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 class AsinFunction extends DoubleFunction { 295 public AsinFunction() { super(1); } 296 public String 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 class AtanFunction extends DoubleFunction { 308 public AtanFunction() { super(1); } 309 public String 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 class Atan2Function extends DoubleFunction { 321 public Atan2Function() { super(2); } 322 public String 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 class CeilFunction extends DoubleFunction { 333 public CeilFunction() { super(1); } 334 public String 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 class CosFunction extends DoubleFunction { 348 public CosFunction() { super(1); } 349 public String 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 class CotFunction extends DoubleFunction { 360 public CotFunction() { super(1); } 361 public String 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 class DegreesFunction extends DoubleFunction { 372 public DegreesFunction() { super(1); } 373 public String 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 class EFunction extends DoubleFunction { 384 public EFunction() { super(0); } 385 public String getName() { return "E"; } 386 public double getDouble(Tuple t) { 387 return Math.E; 388 } 389 } 390 class ExpFunction extends DoubleFunction { 392 public ExpFunction() { super(1); } 393 public String 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 class FloorFunction extends DoubleFunction { 404 public FloorFunction() { super(1); } 405 public String 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 class LogFunction extends DoubleFunction { 419 public LogFunction() { super(2); } 420 public String 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 class Log2Function extends DoubleFunction { 437 public Log2Function() { super(1); } 438 public String 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 class Log10Function extends DoubleFunction { 449 public Log10Function() { super(1); } 450 public String 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 class SafeLog10Function extends DoubleFunction { 461 public SafeLog10Function() { super(2); } 462 public String 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 class MaxFunction extends DoubleFunction { 476 public MaxFunction() { super(Function.VARARGS); } 477 public String 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 class MinFunction extends DoubleFunction { 489 public MinFunction() { super(Function.VARARGS); } 490 public String 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 class ModFunction extends DoubleFunction { 502 public ModFunction() { super(2); } 503 public String 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 class PiFunction extends DoubleFunction { 516 public PiFunction() { super(0); } 517 public String getName() { return "PI"; } 518 public double getDouble(Tuple t) { 519 return Math.PI; 520 } 521 } 522 class PowFunction extends DoubleFunction { 524 public PowFunction() { super(1); } 525 public String 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 class RadiansFunction extends DoubleFunction { 536 public RadiansFunction() { super(1); } 537 public String 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 class RandFunction extends DoubleFunction { 548 public RandFunction() { super(0); } 549 public String getName() { return "RAND"; } 550 public double getDouble(Tuple t) { 551 return Math.random(); 552 } 553 } 554 class RoundFunction extends DoubleFunction { 556 public RoundFunction() { super(1); } 557 public String 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 class SignFunction extends DoubleFunction { 571 public SignFunction() { super(1); } 572 public String getName() { return "SIGN"; } 573 public Class 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 class SinFunction extends DoubleFunction { 590 public SinFunction() { super(1); } 591 public String 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 class SqrtFunction extends DoubleFunction { 602 public SqrtFunction() { super(1); } 603 public String 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 class SumFunction extends DoubleFunction { 614 public SumFunction() { super(Function.VARARGS); } 615 public String 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 class SafeSqrtFunction extends DoubleFunction { 625 public SafeSqrtFunction() { super(2); } 626 public String 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 class TanFunction extends DoubleFunction { 640 public TanFunction() { super(1); } 641 public String 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 653 656 abstract class StringFunction extends FunctionExpression { 657 protected StringFunction(int parameterCount) { 658 super(parameterCount); 659 } 660 public Class getType(Schema s) { 661 return String .class; 662 } 663 protected StringBuffer getBuffer() { 664 return new StringBuffer (); 665 } 666 } 667 class CapFunction extends StringFunction { 669 public CapFunction() { super(1); } 670 public String getName() { return "CAP"; } 671 public Object get(Tuple t) { 672 String str = param(0).get(t).toString(); 673 return StringLib.capitalizeFirstOnly(str); 674 } 675 } 676 class ConcatFunction extends StringFunction { 678 public ConcatFunction() { super(Function.VARARGS); } 679 public String getName() { return "CONCAT"; } 680 public Object get(Tuple t) { 681 StringBuffer 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 class ConcatWsFunction extends StringFunction { 690 public ConcatWsFunction() { super(Function.VARARGS); } 691 public String getName() { return "CONCAT_WS"; } 692 public Object get(Tuple t) { 693 StringBuffer sbuf = getBuffer(); 694 String 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 class FormatFunction extends StringFunction { 704 public FormatFunction() { super(2); } 705 public String getName() { return "FORMAT"; } 706 public Object get(Tuple t) { 707 double x = param(0).getDouble(t); 708 int d = param(1).getInt(t); 709 DecimalFormat df = (DecimalFormat )NumberFormat.getInstance(); 710 df.setMinimumFractionDigits(d); 711 df.setMaximumFractionDigits(d); 712 return df.format(x); 713 } 714 } 715 class InsertFunction extends StringFunction { 717 public InsertFunction() { super(4); } 718 public String getName() { return "INSERT"; } 719 public Object get(Tuple t) { 720 String 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 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 class LeftFunction extends StringFunction { 736 public LeftFunction() { super(2); } 737 public String getName() { return "LEFT"; } 738 public Object get(Tuple t) { 739 String src = param(0).get(t).toString(); 740 int len = param(1).getInt(t); 741 return src.substring(0, len); 742 } 743 744 } 745 class LengthFunction extends IntFunction { 747 public LengthFunction() { super(1); } 748 public String getName() { return "LENGTH"; } 749 public int getInt(Tuple t) { 750 return param(0).get(t).toString().length(); 751 } 752 } 753 class LowerFunction extends StringFunction { 756 public LowerFunction() { super(1); } 757 public String getName() { return "LOWER"; } 758 public Object get(Tuple t) { 759 return param(0).get(t).toString().toLowerCase(); 760 } 761 } 762 class LPadFunction extends StringFunction { 764 public LPadFunction() { super(3); } 765 public String getName() { return "LPAD"; } 766 public Object get(Tuple t) { 767 String str = param(0).get(t).toString(); 768 int len = param(1).getInt(t); 769 String 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 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 class PositionFunction extends IntFunction { 792 public PositionFunction() { super(2); } 793 public String getName() { return "POSITION"; } 794 public int getInt(Tuple t) { 795 String substr = param(0).get(t).toString(); 796 String src = param(1).get(t).toString(); 797 return src.indexOf(substr); 798 } 799 } 800 class RepeatFunction extends StringFunction { 803 public RepeatFunction() { super(2); } 804 public String getName() { return "REPEAT"; } 805 public Object get(Tuple t) { 806 String src = param(0).get(t).toString(); 807 int count = param(1).getInt(t); 808 StringBuffer sbuf = new StringBuffer (); 809 for ( int i=0; i<count; ++i ) { 810 sbuf.append(src); 811 } 812 return sbuf.toString(); 813 } 814 } 815 class ReplaceFunction extends StringFunction { 817 public ReplaceFunction() { super(3); } 818 public String getName() { return "REPLACE"; } 819 public Object get(Tuple t) { 820 String src = param(0).get(t).toString(); 821 String from = param(1).get(t).toString(); 822 String to = param(2).get(t).toString(); 823 return src.replaceAll(from, to); 824 } 825 826 } 827 class ReverseFunction extends StringFunction { 829 public ReverseFunction() { super(1); } 830 public String getName() { return "REVERSE"; } 831 public Object get(Tuple t) { 832 String str = param(0).get(t).toString(); 833 StringBuffer 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 class RightFunction extends StringFunction { 842 public RightFunction() { super(2); } 843 public String getName() { return "RIGHT"; } 844 public Object get(Tuple t) { 845 String src = param(0).get(t).toString(); 846 int len = param(1).getInt(t); 847 return src.substring(src.length()-len); 848 } 849 850 } 851 class RPadFunction extends StringFunction { 853 public RPadFunction() { super(3); } 854 public String getName() { return "RPAD"; } 855 public Object get(Tuple t) { 856 String str = param(0).get(t).toString(); 857 int len = param(1).getInt(t); 858 String 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 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 class SpaceFunction extends StringFunction { 881 public SpaceFunction() { super(1); } 882 public String getName() { return "SPACE"; } 883 public Object get(Tuple t) { 884 int n = param(0).getInt(t); 885 StringBuffer sbuf = new StringBuffer (); 886 for ( int i=0; i<n; ++i ) 887 sbuf.append(' '); 888 return sbuf.toString(); 889 } 890 891 } 892 class SubstringFunction extends StringFunction { 894 public SubstringFunction() { super(3); } 895 public String getName() { return "SUBSTRING"; } 896 public Object get(Tuple t) { 897 String 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 class UpperFunction extends StringFunction { 911 public UpperFunction() { super(1); } 912 public String getName() { return "UPPER"; } 913 public Object get(Tuple t) { 914 return param(0).get(t).toString().toUpperCase(); 915 } 916 } 917 918 920 class RGBFunction extends IntFunction { 922 public RGBFunction() { super(3); } 923 public String 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 class HexFunction extends IntFunction { 933 public HexFunction() { super(1); } 934 public String getName() { return "RGB"; } 935 public int getInt(Tuple t) { 936 String hex = (String )param(0).get(t); 937 return ColorLib.hex(hex); 938 } 939 } 940 class RGBAFunction extends IntFunction { 942 public RGBAFunction() { super(4); } 943 public String 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 class GrayFunction extends IntFunction { 954 public GrayFunction() { super(2); } 955 public String 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 class HSBFunction extends IntFunction { 968 public HSBFunction() { super(3); } 969 public String 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 class HSBAFunction extends IntFunction { 979 public HSBAFunction() { super(4); } 980 public String 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 class ColorInterpFunction extends IntFunction { 991 public ColorInterpFunction() { super(3); } 992 public String 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 |