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 org.apache.jsieve;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.apache.jsieve.exception.SieveException;
26  import org.apache.jsieve.mail.MailAdapter;
27  
28  /**
29   * <p>
30   * A parsed representation of an RFC3028 testlist argument...
31   * </p>
32   * 
33   * <code>test-list = "(" test *("," test) ")"</code>
34   */
35  public class TestList implements Executable {
36      /**
37       * List of Tests
38       */
39      private List<Test> fieldTests;
40  
41      /**
42       * Constructor for TestList.
43       */
44      private TestList() {
45          super();
46      }
47  
48      /**
49       * Constructor for TestList.
50       * 
51       * @param children -
52       *            A List of Tests
53       */
54      public TestList(List<Test> children) {
55          this();
56          setTests(children);
57      }
58  
59      /**
60       * Constructor for TestList.
61       * 
62       * @param child -
63       *            A Test
64       */
65      public TestList(Test child) {
66          this();
67          List<Test> children = new ArrayList<Test>();
68          children.add(child);
69          setTests(children);
70      }
71  
72      /**
73       * @see org.apache.jsieve.Executable#execute(MailAdapter, SieveContext)
74       */
75      public Object execute(MailAdapter mail, SieveContext context)
76              throws SieveException {
77          return new Boolean(allTestsPass(mail, context));
78      }
79  
80      /**
81       * Do all tests pass for the given mail?
82       * 
83       * @param mail not null
84       * @param context not null
85       * @return false when any test in the list fails when run against the given mail,
86       * true when no tests fail
87       * @throws SieveException
88       */
89      public boolean allTestsPass(MailAdapter mail, SieveContext context) throws SieveException {
90          boolean result = true;
91          for (Test test:getTests()) {
92              result = test.isTestPassed(mail, context);
93              if (!result) {
94                  break;
95              }
96          }
97          return result;
98      }
99  
100     /**
101      * Do any tests pass for the given mail?
102      * 
103      * @param mail not null
104      * @param context not null
105      * @return true when any test in this list passes,
106      * false otherwise
107      * @throws SieveException
108      */
109     public boolean anyTestsPass(MailAdapter mail, SieveContext context) throws SieveException {
110         boolean result = false;
111         for (Test test:getTests()) {
112             result = test.isTestPassed(mail, context);
113             if (result) {
114                 break;
115             }
116         }
117         return result;
118     }
119     
120     /**
121      * Returns the children.
122      * 
123      * @return List
124      */
125     public List<Test> getTests() {
126         return fieldTests;
127     }
128 
129     /**
130      * Sets the children.
131      * 
132      * @param children
133      *            The children to set
134      */
135     protected void setTests(List<Test> children) {
136         fieldTests = children;
137     }
138 
139     public String toString() {
140         return "TEST LIST: " + fieldTests;
141     }
142 
143     /**
144      * Is this test list empty?
145      * @return true when empty,
146      * false when tests exist
147      */
148     public boolean isEmpty() {
149         return fieldTests.isEmpty();
150     }
151 
152 }