View Javadoc

1   /* This file is part of COPAL (COntext Provisioning for All).
2    *
3    * COPAL is a part of SM4All (Smart hoMes for All) project.
4    *
5    * COPAL is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU Lesser General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * COPAL is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with COPAL. If not, see <http://www.gnu.org/licenses/>.
17   */
18  package at.ac.tuwien.infosys.sm4all.copal.api;
19  
20  import java.text.MessageFormat;
21  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.CurrentActionName;
22  import at.ac.tuwien.infosys.sm4all.copal.api.event.xml.EventType;
23  
24  /**
25   * This class is used with the {@link ContextProcessor}s to create queries which
26   * listen on occurrence of events that need {@link ProcessorAction}.
27   * 
28   * @author sanjin
29   */
30  public class ActionQuery extends ContextQuery {
31  
32      private final ProcessorAction action;
33  
34      /**
35       * Create instance of the action query. The <code>action</code> defines the
36       * processor action for context events this action query should catch.
37       * 
38       * @param action the processor action.
39       * @throws NullPointerException if specified action is <code>null</code>.
40       */
41      public ActionQuery(final ProcessorAction action) {
42          super(MessageFormat.format("{0} on {1}", action.getName(),
43                  action.getEventType()), action.getEventType(),
44                  MessageFormat.format("`{0}` = \"{1}\" and `{2}` = \"{3}\"",
45                          EventType.INSTANCE.getQualifiedName(),
46                          action.getEventType(),
47                          CurrentActionName.INSTANCE.getQualifiedName(),
48                          action.getName()));
49  
50          this.action = action;
51      }
52  
53      /**
54       * @return the processor action.
55       */
56      public ProcessorAction getAction() {
57          return this.action;
58      }
59  
60      /**
61       * Returns hash code for this action query. The hash code for a
62       * <code>ActionQuery</code> object is hash code of its action.
63       * 
64       * @return a hash code value for this action query.
65       */
66      @Override
67      public int hashCode() {
68          return this.action.hashCode();
69      }
70  
71      /**
72       * Compares this action query to the specified object. The result is
73       * <code>true</code> if and only if the argument is not <code>null</code>
74       * and is a <code>ActionQuery</code> object that has same action this action
75       * query.
76       * 
77       * @param obj the object to compare this action query against.
78       * @return <code>true</code> if action queries are equal; <code>false</code>
79       *         otherwise.
80       */
81      @Override
82      public boolean equals(final Object obj) {
83          boolean result = false;
84  
85          if (obj != null)
86              if (this == obj)
87                  result = true;
88              else if (this.getClass() == obj.getClass()) {
89                  final ActionQuery other = (ActionQuery) obj;
90  
91                  result = this.action.equals(other.action);
92              }
93  
94          return result;
95      }
96  }