View Javadoc

1   package org.e2etrace.trace;
2   
3   /*
4    * Copyright 2006 Gunther Popp
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * 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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.Serializable;
20  
21  /**
22   * Base class for trace step ids.
23   * <p>
24   *
25   * This class ensures that all trace step id use the same <code>equals</code>
26   * and <code>hashCode</code> implementation based on the string representation
27   * of the trace step id.
28   * <p>
29   *
30   * @author Gunther Popp
31   *
32   */
33  public class AbstractTraceStepId implements ITraceStepId, Serializable {
34  
35    private static final long serialVersionUID = 1L;
36    private String id;
37  
38    /**
39     * Constructor.
40     * <p>
41     *
42     * @param idAsString string representation of the trace step id.
43     *          <p>
44     */
45    protected AbstractTraceStepId(String idAsString) {
46      this.id = idAsString;
47    }
48  
49    /** {@inheritDoc} */
50    public int hashCode() {
51      final int PRIME = 31;
52      int result = 1;
53      result = PRIME * result + ((id == null) ? 0 : id.hashCode());
54      return result;
55    }
56  
57    /** {@inheritDoc} */
58    public boolean equals(Object obj) {
59      if (this == obj)
60        return true;
61      if (obj == null)
62        return false;
63      final AbstractTraceStepId other = (AbstractTraceStepId) obj;
64      if (id == null) {
65        if (other.id != null)
66          return false;
67      } else if (!id.equals(other.id))
68        return false;
69      return true;
70    }
71  
72    /** {@inheritDoc} */
73    public String asString() {
74      return this.id;
75    }
76  
77  }