- /*
- * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
- * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
- */
-
- package javax.mail.search;
-
- import javax.mail.Message;
-
- /**
- * This class implements comparisons for Message headers.
- * The comparison is case-insensitive.
- *
- * @author Bill Shannon
- * @author John Mani
- */
- public final class HeaderTerm extends StringTerm {
- /**
- * The name of the header.
- *
- * @serial
- */
- protected String headerName;
-
- /**
- * Constructor.
- *
- * @param headerName The name of the header
- * @param pattern The pattern to search for
- */
- public HeaderTerm(String headerName, String pattern) {
- super(pattern);
- this.headerName = headerName;
- }
-
- /**
- * Return the name of the header to compare with.
- */
- public String getHeaderName() {
- return headerName;
- }
-
- /**
- * The header match method.
- *
- * @param msg The match is applied to this Message's header
- * @return true if the match succeeds, otherwise false
- */
- public boolean match(Message msg) {
- String[] headers;
-
- try {
- headers = msg.getHeader(headerName);
- } catch (Exception e) {
- return false;
- }
-
- if (headers == null)
- return false;
-
- for (int i=0; i < headers.length; i++)
- if (super.match(headers[i]))
- return true;
- return false;
- }
-
- /**
- * Equality comparison.
- */
- public boolean equals(Object obj) {
- if (!(obj instanceof HeaderTerm))
- return false;
- HeaderTerm ht = (HeaderTerm)obj;
- // XXX - depends on header comparisons being case independent
- return ht.headerName.equalsIgnoreCase(headerName) && super.equals(ht);
- }
-
- /**
- * Compute a hashCode for this object.
- */
- public int hashCode() {
- // XXX - depends on header comparisons being case independent
- return headerName.toLowerCase().hashCode() + super.hashCode();
- }
- }