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  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  /**
25   * Abstract base class for trace steps.
26   * <p>
27   *
28   * All custom trace step implementation must inherit from this class.
29   * <p>
30   *
31   * @author Gunther Popp
32   *
33   */
34  public abstract class AbstractTraceStep implements ITraceStep, Serializable {
35  
36    private static final long serialVersionUID = 1L;
37    private ITraceStepId id;
38    protected List children;
39    private ITraceStep parent;
40  
41    /**
42     * Constructor.
43     *
44     * @param id Id of the new trace step
45     */
46    public AbstractTraceStep(ITraceStepId id) {
47      this.id = id;
48    }
49  
50    /** {@inheritDoc} */
51    public ITraceStepId getId() {
52      return this.id;
53    }
54  
55    /** {@inheritDoc} */
56    public void addChild(ITraceStep child) {
57      if (child == null) {
58        return;
59      }
60  
61      // A "late" intialization is used for the children list to reduce the
62      // overhead
63      // for TraceSteps.
64      if (this.children == null) {
65        this.children = new ArrayList();
66      }
67  
68      this.children.add(child);
69  
70      child.setParent(this);
71  
72    }
73  
74    /** {@inheritDoc} */
75    public ITraceStep[] getChildren() {
76      ITraceStep[] steps = new ITraceStep[0];
77  
78      if (this.children != null) {
79        steps = new ITraceStep[this.children.size()];
80  
81        for (int i = 0; i < steps.length; i++) {
82          steps[i] = (ITraceStep) this.children.get(i);
83        }
84  
85      }
86  
87      return steps;
88    }
89  
90    /** {@inheritDoc} */
91    public ITraceStep getParent() {
92      return this.parent;
93    }
94  
95    /** {@inheritDoc} */
96    public void setParent(ITraceStep parent) {
97      this.parent = parent;
98    }
99  
100   /** {@inheritDoc} */
101   public int hashCode() {
102     int hashCode = 1;
103 
104     hashCode = 31 * hashCode + (id == null ? 0 : id.hashCode());
105 
106     return hashCode;
107   }
108 
109   /** {@inheritDoc} */
110   public boolean equals(Object o) {
111     if (this == o) {
112       return true;
113     }
114     if (o == null) {
115       return false;
116     }
117     if (o.getClass() != getClass()) {
118       return false;
119     }
120     AbstractTraceStep castedObj = (AbstractTraceStep) o;
121 
122     return ((this.id == null ? castedObj.id == null : this.id.equals(castedObj.id)));
123   }
124 
125   /** {@inheritDoc} */
126   public abstract void enter() throws IllegalStateException;
127 
128   /** {@inheritDoc} */
129   public abstract long getDuration();
130 
131   /** {@inheritDoc} */
132   public abstract long getIsolatedDuration();
133 
134   /** {@inheritDoc} */
135   public abstract boolean isActive();
136 
137   /** {@inheritDoc} */
138   public abstract void leave() throws IllegalStateException;
139 
140   /**
141    * Invokes <code>leave()</code> on all children of this trace step.
142    * <p>
143    */
144   protected void leaveAllChildren() {
145 
146     if (this.children != null) {
147       for (Iterator iter = this.children.iterator(); iter.hasNext();) {
148         ITraceStep element = (ITraceStep) iter.next();
149 
150         if (element.isActive()) {
151           element.leave();
152         }
153       }
154     }
155   }
156 
157 }