1. /*
  2. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  3. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. */
  5. package javax.mail.search;
  6. import javax.mail.Message;
  7. /**
  8. * This class implements the logical OR operator on individual SearchTerms.
  9. *
  10. * @author Bill Shannon
  11. * @author John Mani
  12. */
  13. public final class OrTerm extends SearchTerm {
  14. /**
  15. * The array of terms on which the OR operator should
  16. * be applied.
  17. *
  18. * @serial
  19. */
  20. protected SearchTerm[] terms;
  21. /**
  22. * Constructor that takes two operands.
  23. *
  24. * @param t1 first term
  25. * @param t2 second term
  26. */
  27. public OrTerm(SearchTerm t1, SearchTerm t2) {
  28. terms = new SearchTerm[2];
  29. terms[0] = t1;
  30. terms[1] = t2;
  31. }
  32. /**
  33. * Constructor that takes an array of SearchTerms.
  34. *
  35. * @param t1 first term
  36. * @param t2 second term
  37. */
  38. public OrTerm(SearchTerm[] t) {
  39. terms = new SearchTerm[t.length];
  40. for (int i = 0; i < t.length; i++)
  41. terms[i] = t[i];
  42. }
  43. /**
  44. * Return the search terms.
  45. */
  46. public SearchTerm[] getTerms() {
  47. return (SearchTerm[])terms.clone();
  48. }
  49. /**
  50. * The OR operation. <p>
  51. *
  52. * The terms specified in the constructor are applied to
  53. * the given object and the OR operator is applied to their results.
  54. *
  55. * @param msg The specified SearchTerms are applied to this Message
  56. * and the OR operator is applied to their results.
  57. * @return true if the OR succeds, otherwise false
  58. */
  59. public boolean match(Message msg) {
  60. for (int i=0; i < terms.length; i++)
  61. if (terms[i].match(msg))
  62. return true;
  63. return false;
  64. }
  65. /**
  66. * Equality comparison.
  67. */
  68. public boolean equals(Object obj) {
  69. if (!(obj instanceof OrTerm))
  70. return false;
  71. OrTerm ot = (OrTerm)obj;
  72. if (ot.terms.length != terms.length)
  73. return false;
  74. for (int i=0; i < terms.length; i++)
  75. if (!terms[i].equals(ot.terms[i]))
  76. return false;
  77. return true;
  78. }
  79. /**
  80. * Compute a hashCode for this object.
  81. */
  82. public int hashCode() {
  83. int hash = 0;
  84. for (int i=0; i < terms.length; i++)
  85. hash += terms[i].hashCode();
  86. return hash;
  87. }
  88. }