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.event;
19  
20  import java.text.MessageFormat;
21  import java.util.Arrays;
22  import org.apache.log4j.Logger;
23  
24  /**
25   * Class which defines an already processed action that was executed on an
26   * event. This class has one-to-many association with the {@link BaseEvent}.
27   * This class is not thread safe.
28   * 
29   * @author sanjin
30   */
31  public class ProcessedAction implements ContextEventAction {
32  
33      private static final Logger LOGGER = Logger.getLogger(ProcessedAction.class);
34  
35      private final String[] processedBy;
36      private final String name;
37  
38      /**
39       * Create instance of {@link ProcessedAction} with specified name and names
40       * of processors.
41       * 
42       * @param name the name of the {@link ProcessedAction}.
43       * @param processedBy the names of processors that processed this
44       *        {@link ProcessedAction}.
45       * @throws NullPointerException if specified name is <code>null</code>.
46       * @throws IllegalArgumentException if specified name is an empty or blank
47       *         string.
48       */
49      public ProcessedAction(final String name, final String... processedBy) {
50          super();
51  
52          if (null == name) {
53              throw new NullPointerException("Name cannot be null.");
54          }
55          if (name.trim().isEmpty()) {
56              throw new IllegalArgumentException(
57                      "Name cannot be an empty or blank string.");
58          }
59          if (null == processedBy) {
60              throw new NullPointerException("Processors cannot be null.");
61          }
62          for (final String processor : processedBy) {
63              if (null == processor) {
64                  throw new NullPointerException("Processor cannot be null.");
65              }
66              if (processor.trim().isEmpty()) {
67                  throw new IllegalArgumentException(
68                          "Processor cannot be an empty or blank string.");
69              }
70          }
71  
72          this.name = name;
73          this.processedBy = Arrays.copyOf(processedBy, processedBy.length);
74  
75          if (LOGGER.isDebugEnabled()) {
76              LOGGER.debug(MessageFormat.format(
77                      "Created processed action ''{0}''.", this.name));
78          }
79      }
80  
81      /**
82       * Create instance of {@link ProcessedAction} from specified
83       * {@link CurrentAction}. Name and processedBy properties of the
84       * {@link CurrentAction} are copied.
85       * 
86       * @param action the {@link CurrentAction}.
87       * @throws NullPointerException if specified {@link CurrentAction} is
88       *         <code>null</code>.
89       */
90      public ProcessedAction(final CurrentAction action) {
91          this(getName(action), action.getProcessedBy());
92      }
93  
94      @Override
95      public String getName() {
96          return this.name;
97      }
98  
99      /**
100      * @return name of processors that processed this {@link ProcessedAction}.
101      */
102     public String[] getProcessedBy() {
103         return Arrays.copyOf(this.processedBy, this.processedBy.length);
104     }
105 
106     /**
107      * Returns hash code for this {@link ProcessedAction}. The hash code for a
108      * {@link ProcessedAction} object is hash code of its name.
109      * 
110      * @return a hash code value for this processed {@link ProcessedAction}.
111      */
112     @Override
113     public int hashCode() {
114         return this.name.hashCode();
115     }
116 
117     /**
118      * Compares this {@link ProcessedAction} to the specified {@link Object}.
119      * The result is <code>true</code> if and only if the argument is not
120      * <code>null</code> and is {@link ProcessedAction} object that has same
121      * name and is processed by same processor as this {@link ProcessedAction}.
122      * 
123      * @param obj the {@link Object} to compare this {@link ProcessedAction}
124      *        against.
125      * @return <code>true</code> if {@link ProcessedAction}s are equal;
126      *         <code>false</code> otherwise.
127      */
128     @Override
129     public boolean equals(final Object obj) {
130         boolean result = false;
131 
132         if (null != obj) {
133             if (this == obj) {
134                 result = true;
135             } else if (obj instanceof ProcessedAction) {
136                 final ProcessedAction other = (ProcessedAction) obj;
137 
138                 result = this.name.equals(other.getName())
139                         && Arrays.equals(this.processedBy, other.processedBy);
140             }
141         }
142 
143         return result;
144     }
145 
146     private static String getName(final CurrentAction action) {
147         if (null == action) {
148             throw new NullPointerException("Current action cannot be null.");
149         }
150 
151         return action.getName();
152     }
153 }