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. import javax.mail.Address;
  8. /**
  9. * This class implements string comparisons for the Recipient Address
  10. * headers. <p>
  11. *
  12. * Note that this class differs from the <code>RecipientTerm</code> class
  13. * in that this class does comparisons on address strings rather than Address
  14. * objects. The string comparisons are case-insensitive.
  15. *
  16. * @since JavaMail 1.1
  17. */
  18. public final class RecipientStringTerm extends AddressStringTerm {
  19. /**
  20. * The recipient type.
  21. *
  22. * @serial
  23. */
  24. private Message.RecipientType type;
  25. /**
  26. * Constructor.
  27. *
  28. * @param type the recipient type
  29. * @param address the address pattern to be compared.
  30. */
  31. public RecipientStringTerm(Message.RecipientType type, String pattern) {
  32. super(pattern);
  33. this.type = type;
  34. }
  35. /**
  36. * Return the type of recipient to match with.
  37. */
  38. public Message.RecipientType getRecipientType() {
  39. return type;
  40. }
  41. /**
  42. * Check whether the address specified in the constructor is
  43. * a substring of the recipient address of this Message.
  44. *
  45. * @param msg The comparison is applied to this Message's recepient
  46. * address.
  47. * @return true if the match succeeds, otherwise false.
  48. */
  49. public boolean match(Message msg) {
  50. Address[] recipients;
  51. try {
  52. recipients = msg.getRecipients(type);
  53. } catch (Exception e) {
  54. return false;
  55. }
  56. if (recipients == null)
  57. return false;
  58. for (int i=0; i < recipients.length; i++)
  59. if (super.match(recipients[i]))
  60. return true;
  61. return false;
  62. }
  63. /**
  64. * Equality comparison.
  65. */
  66. public boolean equals(Object obj) {
  67. if (!(obj instanceof RecipientStringTerm))
  68. return false;
  69. RecipientStringTerm rst = (RecipientStringTerm)obj;
  70. return rst.type.equals(this.type) && super.equals(obj);
  71. }
  72. /**
  73. * Compute a hashCode for this object.
  74. */
  75. public int hashCode() {
  76. return type.hashCode() + super.hashCode();
  77. }
  78. }