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 From Address
  10. * header. <p>
  11. *
  12. * Note that this class differs from the <code>FromTerm</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 FromStringTerm extends AddressStringTerm {
  19. /**
  20. * Constructor.
  21. *
  22. * @param address the address pattern to be compared.
  23. */
  24. public FromStringTerm(String pattern) {
  25. super(pattern);
  26. }
  27. /**
  28. * Check whether the address string specified in the constructor is
  29. * a substring of the From address of this Message.
  30. *
  31. * @param msg The comparison is applied to this Message's From
  32. * address.
  33. * @return true if the match succeeds, otherwise false.
  34. */
  35. public boolean match(Message msg) {
  36. Address[] from;
  37. try {
  38. from = msg.getFrom();
  39. } catch (Exception e) {
  40. return false;
  41. }
  42. if (from == null)
  43. return false;
  44. for (int i=0; i < from.length; i++)
  45. if (super.match(from[i]))
  46. return true;
  47. return false;
  48. }
  49. /**
  50. * Equality comparison.
  51. */
  52. public boolean equals(Object obj) {
  53. if (!(obj instanceof FromStringTerm))
  54. return false;
  55. return super.equals(obj);
  56. }
  57. }