View Javadoc
1 /* 2 QueryJ 3 4 Copyright (C) 2002 Jose San Leandro Armend?riz 5 jsanleandro@yahoo.es 6 chousz@yahoo.com 7 8 This library is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public 10 License as published by the Free Software Foundation; either 11 version 2.1 of the License, or (at your option) any later version. 12 13 This library is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 General Public License for more details. 17 18 You should have received a copy of the GNU General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 22 Thanks to ACM S.L. for distributing this library under the GPL license. 23 Contact info: jsanleandro@yahoo.es 24 Postal Address: c/Playa de Lagoa, 1 25 Urb. Valdecaba?as 26 Boadilla del monte 27 28660 Madrid 28 Spain 29 30 ****************************************************************************** 31 * 32 * Filename: $RCSfile: ConditionFactory.java,v $ 33 * 34 * Author: Jose San Leandro Armend?riz 35 * 36 * Description: Has the responsiblity of knowing how to create conditions. 37 * 38 * Last modified by: $Author: chous $ at $Date: 2003/08/29 08:14:58 $ 39 * 40 * File version: $Revision: 1.1 $ 41 * 42 * Project version: $Name: $ 43 * 44 * $Id: ConditionFactory.java,v 1.1 2003/08/29 08:14:58 chous Exp $ 45 * 46 */ 47 package org.acmsl.queryj; 48 49 /* 50 * Importing ACM-SL classes. 51 */ 52 import org.acmsl.queryj.Query; 53 import org.acmsl.queryj.SelectQuery; 54 55 /* 56 * Importing some JDK classes. 57 */ 58 import java.lang.ref.WeakReference; 59 60 /*** 61 * Has the responsiblity of knowing how to create conditions. 62 * @author <a href="mailto:jsanleandro@yahoo.es">Jose San Leandro</a> 63 * @version $Revision: 1.1 $ 64 */ 65 public class ConditionFactory 66 { 67 /*** 68 * Singleton implemented as a weak reference. 69 */ 70 private static WeakReference singleton; 71 72 /*** 73 * Protected constructor to avoid accidental instantiation. 74 */ 75 protected ConditionFactory() {}; 76 77 /*** 78 * Specifies a new weak reference. 79 * @param factory the factory instance to use. 80 */ 81 protected static void setReference(ConditionFactory factory) 82 { 83 singleton = new WeakReference(factory); 84 } 85 86 /*** 87 * Retrieves the weak reference. 88 * @return such reference. 89 */ 90 protected static WeakReference getReference() 91 { 92 return singleton; 93 } 94 95 /*** 96 * Retrieves a ConditionFactory instance. 97 * @return such instance. 98 */ 99 public static ConditionFactory getInstance() 100 { 101 ConditionFactory result = null; 102 103 WeakReference reference = getReference(); 104 105 if (reference != null) 106 { 107 result = (ConditionFactory) reference.get(); 108 } 109 110 if (result == null) 111 { 112 result = new ConditionFactory() {}; 113 114 setReference(result); 115 } 116 117 return result; 118 } 119 120 /*** 121 * Creates a condition 122 * @param leftSideField the left-side field. 123 * @param operator the operator. 124 * @param rightSideField the right-side field. 125 * @return such type of instance. 126 */ 127 public Condition createCondition( 128 Field leftSideField, 129 ConditionOperator operator, 130 Field rightSideField) 131 { 132 Condition result = null; 133 134 if ( (leftSideField != null) 135 && (operator != null)) 136 // && (rightSideField != null)) // Removed due to isNull() 137 { 138 result = 139 new AtomicCondition( 140 leftSideField, operator, rightSideField) {}; 141 } 142 143 return result; 144 } 145 146 /*** 147 * Creates a condition 148 * @param leftSideField the left-side field. 149 * @param operator the operator. 150 * @param value the fixed value. 151 * @return such type of instance. 152 */ 153 public Condition createCondition( 154 Field leftSideField, 155 ConditionOperator operator, 156 int value) 157 { 158 Condition result = null; 159 160 if ( (leftSideField != null) 161 && (operator != null)) 162 { 163 result = new AtomicCondition(leftSideField, operator, value) {}; 164 } 165 166 return result; 167 } 168 169 /*** 170 * Creates a variable condition. 171 * @param field the field. 172 * @param operator the operator. 173 * @return such type of instance. 174 */ 175 public VariableCondition createVariableCondition( 176 Field field, 177 ConditionOperator operator) 178 { 179 VariableCondition result = null; 180 181 if ( (field != null) 182 && (operator != null)) 183 { 184 result = new VariableCondition(field, operator) {}; 185 } 186 187 return result; 188 } 189 190 /*** 191 * Creates a wrapper condition. 192 * @param condition the condition to wrap. 193 * @param prefix the prefix. 194 * @param suffix the suffix. 195 * @return the wrapped condition. 196 */ 197 public Condition wrap(Condition condition, String prefix, String suffix) 198 { 199 Condition result = condition; 200 201 if (result != null) 202 { 203 result = new _ConditionWrapper(condition, prefix, suffix) {}; 204 } 205 206 return result; 207 } 208 209 /*** 210 * Creates a wrapper condition. 211 * @param condition the condition to wrap. 212 * @param prefix the prefix. 213 * @param suffix the suffix. 214 * @return the wrapped condition. 215 */ 216 public Condition wrap( 217 AtomicCondition condition, String prefix, String suffix) 218 { 219 Condition result = condition; 220 221 if (result != null) 222 { 223 result = new _AtomicConditionWrapper(condition, prefix, suffix) {}; 224 } 225 226 return result; 227 } 228 229 /*** 230 * Creates a wrapper condition. 231 * @param condition the condition to wrap. 232 * @param prefix the prefix. 233 * @param suffix the suffix. 234 * @return the wrapped condition. 235 */ 236 public VariableCondition wrap( 237 VariableCondition condition, String prefix, String suffix) 238 { 239 VariableCondition result = condition; 240 241 if (result != null) 242 { 243 result = 244 new _VariableConditionWrapper(condition, prefix, suffix) {}; 245 } 246 247 return result; 248 } 249 250 /*** 251 * Envelopes a condition surrounding it with appropiate prefix and suffix. 252 * @author <a href="mailto:jsanleandro@yahoo.es">Jose San Leandro</a> 253 * @version $Revision: 1.1 $ 254 */ 255 protected static class _ConditionWrapper 256 extends Condition 257 { 258 /*** 259 * The condition to wrap. 260 */ 261 private Condition m__Condition; 262 263 /*** 264 * The prefix. 265 */ 266 private String m__strPrefix; 267 268 /*** 269 * The suffix. 270 */ 271 private String m__strSuffix; 272 273 /*** 274 * Creates a condition wrapper with given information. 275 * @param condition the condition to wrap. 276 * @param prefix the prefix. 277 * @param suffix the suffix. 278 */ 279 public _ConditionWrapper( 280 Condition condition, String prefix, String suffix) 281 { 282 super(); 283 284 unmodifiableSetCondition(condition); 285 unmodifiableSetPrefix(prefix); 286 unmodifiableSetSuffix(suffix); 287 } 288 /*** 289 * Specifies the condition to wrap. 290 * @param condition such condition. 291 */ 292 private void unmodifiableSetCondition(Condition condition) 293 { 294 m__Condition = condition; 295 } 296 297 /*** 298 * Specifies the condition to wrap. 299 * @param condition such condition. 300 */ 301 protected void setCondition(Condition condition) 302 { 303 unmodifiableSetCondition(condition); 304 } 305 306 /*** 307 * Retrieves the wrapped condition. 308 * @return such condition. 309 */ 310 public Condition getCondition() 311 { 312 return m__Condition; 313 } 314 315 /*** 316 * Specifies the prefix. 317 * @param prefix such prefix. 318 */ 319 private void unmodifiableSetPrefix(String prefix) 320 { 321 m__strPrefix = prefix; 322 } 323 324 /*** 325 * Specifies the prefix. 326 * @param prefix such prefix. 327 */ 328 protected void setPrefix(String prefix) 329 { 330 unmodifiableSetPrefix(prefix); 331 } 332 333 /*** 334 * Retrieves the prefix. 335 * @return such prefix. 336 */ 337 public String getPrefix() 338 { 339 return m__strPrefix; 340 } 341 342 /*** 343 * Specifies the suffix. 344 * @param suffix such suffix. 345 */ 346 private void unmodifiableSetSuffix(String suffix) 347 { 348 m__strSuffix = suffix; 349 } 350 351 /*** 352 * Specifies the suffix. 353 * @param suffix such suffix. 354 */ 355 protected void setSuffix(String suffix) 356 { 357 unmodifiableSetSuffix(suffix); 358 } 359 360 /*** 361 * Retrieves the suffix. 362 * @return such suffix. 363 */ 364 public String getSuffix() 365 { 366 return m__strSuffix; 367 } 368 369 // Delegate methods // 370 371 /*** 372 * Requests OR evaluation with given condition. 373 * @param condition the condition to evaluate. 374 * @return the resulting condition. 375 */ 376 public Condition or(Condition condition) 377 { 378 Condition result = null; 379 380 Condition t_Condition = getCondition(); 381 382 if (t_Condition != null) 383 { 384 result = t_Condition.or(condition); 385 } 386 387 return result; 388 } 389 390 /*** 391 * Requests AND evaluation with given condition. 392 * @param condition the condition to evaluate. 393 * @return the resulting condition. 394 */ 395 public Condition and(Condition condition) 396 { 397 Condition result = null; 398 399 Condition t_Condition = getCondition(); 400 401 if (t_Condition != null) 402 { 403 result = t_Condition.and(condition); 404 } 405 406 return result; 407 } 408 409 /*** 410 * Outputs a text version of the condition. 411 * @return such text. 412 */ 413 public String toString() 414 { 415 return getPrefix() + getCondition() + getSuffix(); 416 } 417 } 418 419 /*** 420 * Envelopes a condition surrounding it with appropiate prefix and suffix. 421 * @author <a href="mailto:jsanleandro@yahoo.es">Jose San Leandro</a> 422 * @version $Revision: 1.1 $ 423 */ 424 protected static class _AtomicConditionWrapper 425 extends AtomicCondition 426 { 427 /*** 428 * The condition to wrap. 429 */ 430 private AtomicCondition m__Condition; 431 432 /*** 433 * The prefix. 434 */ 435 private String m__strPrefix; 436 437 /*** 438 * The suffix. 439 */ 440 private String m__strSuffix; 441 442 /*** 443 * Creates a condition wrapper with given information. 444 * @param condition the condition to wrap. 445 * @param prefix the prefix. 446 * @param suffix the suffix. 447 */ 448 public _AtomicConditionWrapper( 449 AtomicCondition condition, String prefix, String suffix) 450 { 451 super( 452 condition.getLeftSideField(), 453 condition.getOperator(), 454 condition.getRightSideField()); 455 456 unmodifiableSetCondition(condition); 457 unmodifiableSetPrefix(prefix); 458 unmodifiableSetSuffix(suffix); 459 } 460 461 /*** 462 * Specifies the condition to wrap. 463 * @param condition such condition. 464 */ 465 private void unmodifiableSetCondition(AtomicCondition condition) 466 { 467 m__Condition = condition; 468 } 469 470 /*** 471 * Specifies the condition to wrap. 472 * @param condition such condition. 473 */ 474 protected void setCondition(AtomicCondition condition) 475 { 476 unmodifiableSetCondition(condition); 477 } 478 479 /*** 480 * Retrieves the wrapped condition. 481 * @return such condition. 482 */ 483 public AtomicCondition getCondition() 484 { 485 return m__Condition; 486 } 487 488 /*** 489 * Specifies the prefix. 490 * @param prefix such prefix. 491 */ 492 private void unmodifiableSetPrefix(String prefix) 493 { 494 m__strPrefix = prefix; 495 } 496 497 /*** 498 * Specifies the prefix. 499 * @param prefix such prefix. 500 */ 501 protected void setPrefix(String prefix) 502 { 503 unmodifiableSetPrefix(prefix); 504 } 505 506 /*** 507 * Retrieves the prefix. 508 * @return such prefix. 509 */ 510 public String getPrefix() 511 { 512 return m__strPrefix; 513 } 514 515 /*** 516 * Specifies the suffix. 517 * @param suffix such suffix. 518 */ 519 private void unmodifiableSetSuffix(String suffix) 520 { 521 m__strSuffix = suffix; 522 } 523 524 /*** 525 * Specifies the suffix. 526 * @param suffix such suffix. 527 */ 528 protected void setSuffix(String suffix) 529 { 530 unmodifiableSetSuffix(suffix); 531 } 532 533 /*** 534 * Retrieves the suffix. 535 * @return such suffix. 536 */ 537 public String getSuffix() 538 { 539 return m__strSuffix; 540 } 541 542 // Delegated methods // 543 544 /*** 545 * Retrieves the left-side field. 546 * @return such reference. 547 */ 548 public Field getLeftSideField() 549 { 550 Field result = null; 551 552 AtomicCondition t_Condition = getCondition(); 553 554 if (t_Condition != null) 555 { 556 result = t_Condition.getLeftSideField(); 557 } 558 559 return result; 560 } 561 562 /*** 563 * Retrieves the condition operator. 564 * @return such reference. 565 */ 566 public ConditionOperator getOperator() 567 { 568 ConditionOperator result = null; 569 570 AtomicCondition t_Condition = getCondition(); 571 572 if (t_Condition != null) 573 { 574 result = t_Condition.getOperator(); 575 } 576 577 return result; 578 } 579 580 /*** 581 * Retrieves the right-side field. 582 * @return such reference. 583 */ 584 public Field getRightSideField() 585 { 586 Field result = null; 587 588 AtomicCondition t_Condition = getCondition(); 589 590 if (t_Condition != null) 591 { 592 result = t_Condition.getRightSideField(); 593 } 594 595 return result; 596 } 597 598 /*** 599 * Retrieves the right-side value. 600 * @return such reference. 601 */ 602 public String getRightSideValue() 603 { 604 String result = null; 605 606 AtomicCondition t_Condition = getCondition(); 607 608 if (t_Condition != null) 609 { 610 result = t_Condition.getRightSideValue(); 611 } 612 613 return result; 614 } 615 616 /*** 617 * Outputs a text version of the condition. 618 * @return such text. 619 */ 620 public String toString() 621 { 622 return getPrefix() + getCondition() + getSuffix(); 623 } 624 } 625 626 627 /*** 628 * Envelopes a variable condition surrounding it with appropiate 629 * prefix and suffix. 630 * @author <a href="mailto:jsanleandro@yahoo.es">Jose San Leandro</a> 631 * @version $Revision: 1.1 $ 632 */ 633 protected static class _VariableConditionWrapper 634 extends VariableCondition 635 { 636 /*** 637 * The condition to wrap. 638 */ 639 private VariableCondition m__Condition; 640 641 /*** 642 * The prefix. 643 */ 644 private String m__strPrefix; 645 646 /*** 647 * The suffix. 648 */ 649 private String m__strSuffix; 650 651 /*** 652 * Creates a condition wrapper with given information. 653 * @param condition the condition to wrap. 654 * @param prefix the prefix. 655 * @param suffix the suffix. 656 */ 657 public _VariableConditionWrapper( 658 VariableCondition condition, String prefix, String suffix) 659 { 660 super( 661 condition.getLeftSideField(), 662 condition.getOperator()); 663 664 unmodifiableSetCondition(condition); 665 unmodifiableSetPrefix(prefix); 666 unmodifiableSetSuffix(suffix); 667 } 668 669 /*** 670 * Specifies the condition to wrap. 671 * @param condition such condition. 672 */ 673 private void unmodifiableSetCondition(VariableCondition condition) 674 { 675 m__Condition = condition; 676 } 677 678 /*** 679 * Specifies the condition to wrap. 680 * @param condition such condition. 681 */ 682 protected void setCondition(VariableCondition condition) 683 { 684 unmodifiableSetCondition(condition); 685 } 686 687 /*** 688 * Retrieves the wrapped condition. 689 * @return such condition. 690 */ 691 public VariableCondition getCondition() 692 { 693 return m__Condition; 694 } 695 696 /*** 697 * Specifies the prefix. 698 * @param prefix such prefix. 699 */ 700 private void unmodifiableSetPrefix(String prefix) 701 { 702 m__strPrefix = prefix; 703 } 704 705 /*** 706 * Specifies the prefix. 707 * @param prefix such prefix. 708 */ 709 protected void setPrefix(String prefix) 710 { 711 unmodifiableSetPrefix(prefix); 712 } 713 714 /*** 715 * Retrieves the prefix. 716 * @return such prefix. 717 */ 718 public String getPrefix() 719 { 720 return m__strPrefix; 721 } 722 723 /*** 724 * Specifies the suffix. 725 * @param suffix such suffix. 726 */ 727 private void unmodifiableSetSuffix(String suffix) 728 { 729 m__strSuffix = suffix; 730 } 731 732 /*** 733 * Specifies the suffix. 734 * @param suffix such suffix. 735 */ 736 protected void setSuffix(String suffix) 737 { 738 unmodifiableSetSuffix(suffix); 739 } 740 741 /*** 742 * Retrieves the suffix. 743 * @return such suffix. 744 */ 745 public String getSuffix() 746 { 747 return m__strSuffix; 748 } 749 750 751 // Delegated methods // 752 753 /*** 754 * Retrieves the left-side field. 755 * @return such reference. 756 */ 757 public Field getLeftSideField() 758 { 759 Field result = null; 760 761 VariableCondition t_Condition = getCondition(); 762 763 if (t_Condition != null) 764 { 765 result = t_Condition.getLeftSideField(); 766 } 767 768 return result; 769 } 770 771 /*** 772 * Retrieves the condition operator. 773 * @return such reference. 774 */ 775 public ConditionOperator getOperator() 776 { 777 ConditionOperator result = null; 778 779 VariableCondition t_Condition = getCondition(); 780 781 if (t_Condition != null) 782 { 783 result = t_Condition.getOperator(); 784 } 785 786 return result; 787 } 788 789 /*** 790 * Retrieves the right-side field. 791 * @return such reference. 792 */ 793 public Field getRightSideField() 794 { 795 Field result = null; 796 797 VariableCondition t_Condition = getCondition(); 798 799 if (t_Condition != null) 800 { 801 result = t_Condition.getRightSideField(); 802 } 803 804 return result; 805 } 806 807 /*** 808 * Retrieves the right-side value. 809 * @return such reference. 810 */ 811 public String getRightSideValue() 812 { 813 String result = null; 814 815 VariableCondition t_Condition = getCondition(); 816 817 if (t_Condition != null) 818 { 819 result = t_Condition.getRightSideValue(); 820 } 821 822 return result; 823 } 824 825 /*** 826 * Outputs a text version of the condition. 827 * @return such text. 828 */ 829 public String toString() 830 { 831 return getPrefix() + getCondition() + getSuffix(); 832 } 833 } 834 }

This page was automatically generated by Maven