View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package javax.mail.search;
21  
22  import javax.mail.Message;
23  import javax.mail.MessagingException;
24  
25  /**
26   * @version $Rev: 593593 $ $Date: 2007-11-09 12:04:20 -0500 (Fri, 09 Nov 2007) $
27   */
28  public final class HeaderTerm extends StringTerm {
29      protected String headerName;
30  
31      public HeaderTerm(String header, String pattern) {
32          super(pattern);
33          this.headerName = header;
34      }
35  
36      public String getHeaderName() {
37          return headerName;
38      }
39  
40      public boolean match(Message message) {
41          try {
42              String values[] = message.getHeader(headerName);
43              if (values != null) {
44                  for (int i = 0; i < values.length; i++) {
45                      String value = values[i];
46                      if (match(value)) {
47                          return true;
48                      }
49                  }
50              }
51              return false;
52          } catch (MessagingException e) {
53              return false;
54          }
55      }
56  
57      public boolean equals(Object other) {
58          if (other == this) return true;
59          if (other instanceof HeaderTerm == false) return false;
60          // we need to compare with more than just the header name. 
61          return headerName.equalsIgnoreCase(((HeaderTerm) other).headerName) && super.equals(other);
62      }
63  
64      public int hashCode() {
65          return headerName.toLowerCase().hashCode() + super.hashCode();
66      }
67  }