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