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  package org.apache.maven.doxia.sink.impl;
20  
21  import java.util.Arrays;
22  import java.util.Objects;
23  
24  /**
25   * A single sink event, used for testing purposes in order to check
26   * the order and effect of some parser events.
27   *
28   * @author ltheussl
29   * @since 1.1
30   */
31  public class SinkEventElement {
32      /** The name of the sink event, ie the sink method name. */
33      private final String methodName;
34  
35      /** The array of arguments to the sink method. */
36      private final Object[] args;
37  
38      /** The line number of the source which emitted the event (-1 if unknown) */
39      private final int lineNumber;
40  
41      /**
42       * A SinkEventElement is characterized by the method name and associated array of arguments.
43       *
44       * @param name The name of the sink event, ie the sink method name.
45       * @param arguments The array of arguments to the sink method.
46       *      For a no-arg element this may be null or an empty array.
47       */
48      public SinkEventElement(String name, Object[] arguments, int lineNumber) {
49          Objects.requireNonNull(name, "name cannot be null");
50  
51          this.methodName = name;
52          this.args = arguments;
53          this.lineNumber = lineNumber;
54      }
55  
56      /**
57       * Return the name of the this event.
58       *
59       * @return The name of the sink event.
60       */
61      public String getName() {
62          return this.methodName;
63      }
64  
65      /**
66       * Return the array of arguments to the sink method.
67       *
68       * @return the array of arguments to the sink method.
69       */
70      public Object[] getArgs() {
71          return this.args;
72      }
73  
74      /**
75       * {@inheritDoc}
76       * @since 1.1.1
77       */
78      @Override
79      public String toString() {
80          StringBuilder builder = new StringBuilder();
81          builder.append(this.getClass().getSimpleName()).append('[');
82          builder.append("methodName: ").append(methodName).append(", ");
83          builder.append("args: ").append(Arrays.deepToString(args));
84          if (lineNumber != -1) {
85              builder.append(", ");
86              builder.append("lineNumber: ").append(lineNumber);
87          }
88          builder.append(']');
89          return builder.toString();
90      }
91  
92      @Override
93      public int hashCode() {
94          final int prime = 31;
95          int result = 1;
96          result = prime * result + Arrays.deepHashCode(args);
97          result = prime * result + Objects.hash(methodName);
98          return result;
99      }
100 
101     @Override
102     public boolean equals(Object obj) {
103         if (this == obj) {
104             return true;
105         }
106         if (obj == null) {
107             return false;
108         }
109         if (getClass() != obj.getClass()) {
110             return false;
111         }
112         SinkEventElement other = (SinkEventElement) obj;
113         return Arrays.deepEquals(args, other.args) && Objects.equals(methodName, other.methodName);
114     }
115 }