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. import javax.mail.internet.InternetAddress;
  9. /**
  10. * This abstract class implements string comparisons for Message
  11. * addresses. <p>
  12. *
  13. * Note that this class differs from the <code>AddressTerm</code> class
  14. * in that this class does comparisons on address strings rather than
  15. * Address objects.
  16. *
  17. * @since JavaMail 1.1
  18. */
  19. public abstract class AddressStringTerm extends StringTerm {
  20. /**
  21. * Constructor.
  22. *
  23. * @param pattern the address pattern to be compared.
  24. */
  25. protected AddressStringTerm(String pattern) {
  26. super(pattern, true); // we need case-insensitive comparison.
  27. }
  28. /**
  29. * Check whether the address pattern specified in the constructor is
  30. * a substring of the string representation of the given Address
  31. * object. <p>
  32. *
  33. * Note that if the string representation of the given Address object
  34. * contains charset or transfer encodings, the encodings must be
  35. * accounted for, during the match process. <p>
  36. *
  37. * @param a The comparison is applied to this Address object.
  38. * @return true if the match succeeds, otherwise false.
  39. */
  40. protected boolean match(Address a) {
  41. if (a instanceof InternetAddress) {
  42. InternetAddress ia = (InternetAddress)a;
  43. // We dont use toString() to get "a"'s String representation,
  44. // because InternetAddress.toString() returns a RFC 2047
  45. // encoded string, which isn't what we need here.
  46. return super.match(ia.toUnicodeString());
  47. } else
  48. return super.match(a.toString());
  49. }
  50. /**
  51. * Equality comparison.
  52. */
  53. public boolean equals(Object obj) {
  54. if (!(obj instanceof AddressStringTerm))
  55. return false;
  56. return super.equals(obj);
  57. }
  58. }