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 comparisons for the Recipient Address headers.
  10. *
  11. * @author Bill Shannon
  12. * @author John Mani
  13. */
  14. public final class RecipientTerm extends AddressTerm {
  15. /**
  16. * The recipient type.
  17. *
  18. * @serial
  19. */
  20. protected Message.RecipientType type;
  21. /**
  22. * Constructor.
  23. *
  24. * @param type the recipient type
  25. * @param address the address to match for
  26. */
  27. public RecipientTerm(Message.RecipientType type, Address address) {
  28. super(address);
  29. this.type = type;
  30. }
  31. /**
  32. * Return the type of recipient to match with.
  33. */
  34. public Message.RecipientType getRecipientType() {
  35. return type;
  36. }
  37. /**
  38. * The match method.
  39. *
  40. * @param msg The address match is applied to this Message's recepient
  41. * address
  42. * @return true if the match succeeds, otherwise false
  43. */
  44. public boolean match(Message msg) {
  45. Address[] recipients;
  46. try {
  47. recipients = msg.getRecipients(type);
  48. } catch (Exception e) {
  49. return false;
  50. }
  51. if (recipients == null)
  52. return false;
  53. for (int i=0; i < recipients.length; i++)
  54. if (super.match(recipients[i]))
  55. return true;
  56. return false;
  57. }
  58. /**
  59. * Equality comparison.
  60. */
  61. public boolean equals(Object obj) {
  62. if (!(obj instanceof RecipientTerm))
  63. return false;
  64. RecipientTerm rt = (RecipientTerm)obj;
  65. return rt.type.equals(this.type) && super.equals(obj);
  66. }
  67. /**
  68. * Compute a hashCode for this object.
  69. */
  70. public int hashCode() {
  71. return type.hashCode() + super.hashCode();
  72. }
  73. }