KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > tools > profiler > ProfilerPoint


1 /*
2  * Copyright (c) 1998-2005 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29
30 package com.caucho.tools.profiler;
31
32 import java.util.Collection JavaDoc;
33 import java.util.Collections JavaDoc;
34 import java.util.IdentityHashMap JavaDoc;
35 import java.util.Map JavaDoc;
36
37 /**
38  * Represents a unique point at which profiling is performed. Obtained from a
39  * {@link ProfilerManager}. Equality between two instances of ProfilerPoint is
40  * based on the name only.
41  */

42 public class ProfilerPoint
43   implements Comparable JavaDoc<ProfilerPoint>
44 {
45   private static final Profiler NOOP_PROFILER = new Profiler() {
46     public void finish()
47     {
48     }
49
50     public String JavaDoc toString()
51     {
52       return "NoopProfiler[]";
53     }
54   };
55
56   private final ProfilerManager _profilerManager;
57   private final String JavaDoc _name;
58
59   private long _longHashCode;
60
61   private Map JavaDoc<ProfilerNode, ProfilerNode> _childProfilerNodesMap;
62
63   ProfilerPoint(ProfilerManager profilerManager, String JavaDoc name)
64   {
65     assert profilerManager != null;
66     assert name != null;
67
68     _profilerManager = profilerManager;
69     _name = name;
70   }
71
72   protected ProfilerManager getProfilerManager()
73   {
74     return _profilerManager;
75   }
76
77   public String JavaDoc getName()
78   {
79     return _name;
80   }
81
82   public ProfilerPoint createProfilerPoint(String JavaDoc name)
83   {
84     return getProfilerManager().getProfilerPoint(name);
85   }
86
87   public Profiler start()
88   {
89     if (!_profilerManager.isEnabled())
90       return NOOP_PROFILER;
91
92     ThreadProfiler profiler = ThreadProfiler.current();
93
94     profiler.start(this);
95
96     return profiler;
97   }
98
99   protected Profiler start(ProfilerPoint parent)
100   {
101     if (!getProfilerManager().isEnabled())
102       return NOOP_PROFILER;
103
104     ThreadProfiler profiler = ThreadProfiler.current();
105
106     profiler.start(parent, this);
107
108     return profiler;
109   }
110
111   /**
112    * Caller must synchronize on this ProfilerPoint while it uses the returned
113    * map.
114    */

115   Collection JavaDoc<ProfilerNode> getProfilerNodes()
116   {
117     if (_childProfilerNodesMap == null)
118       return Collections.emptyList();
119     else
120       return _childProfilerNodesMap.values();
121   }
122
123   ProfilerNode getProfilerNode(ProfilerNode parentNode)
124   {
125     synchronized (this) {
126       ProfilerNode node;
127
128       if (_childProfilerNodesMap == null) {
129         _childProfilerNodesMap = new IdentityHashMap JavaDoc<ProfilerNode, ProfilerNode>();
130         node = null;
131       }
132       else
133         node = _childProfilerNodesMap.get(parentNode);
134
135       if (node == null) {
136         node = new ProfilerNode(parentNode, this);
137
138         _childProfilerNodesMap.put(parentNode, node);
139       }
140
141       return node;
142     }
143   }
144
145   /**
146    * Drop all of the ProfilerNode's.
147    */

148   void reset()
149   {
150     synchronized (this) {
151       if (_childProfilerNodesMap != null)
152         _childProfilerNodesMap.clear();
153     }
154   }
155
156   public boolean equals(Object JavaDoc o)
157   {
158     if (o == this)
159       return true;
160
161     if (o == null)
162       return false;
163
164     if (!(o instanceof ProfilerPoint))
165       return false;
166
167     final ProfilerPoint other = (ProfilerPoint) o;
168
169     if (longHashCode() != other.longHashCode())
170       return false;
171
172     return getName().equals(other.getName());
173   }
174
175   public int compareTo(ProfilerPoint other)
176   {
177     return getName().compareTo(other.getName());
178   }
179
180   public long longHashCode()
181   {
182     if (_longHashCode == 0) {
183       long longHashCode = 7;
184
185       final int len = _name.length();
186
187       for (int i = 0; i < len; i++)
188         longHashCode = longHashCode * 33 ^ _name.charAt(i);
189
190       _longHashCode = longHashCode;
191     }
192
193     return _longHashCode;
194   }
195
196   public int hashCode()
197   {
198     return (int) longHashCode();
199   }
200
201   public String JavaDoc toString()
202   {
203     return "ProfilerPoint[" + getName() + "]";
204   }
205 }
206
Popular Tags