KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > masterfs > filebasedfs > Statistics


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.masterfs.filebasedfs;
21 import java.util.Iterator JavaDoc;
22 import org.netbeans.modules.masterfs.filebasedfs.naming.NamingFactory;
23
24 /**
25  *
26  * @author Radek Matous
27  */

28 public final class Statistics {
29     /** Creates a new instance of Statistics */
30
31     public static final Statistics.TimeConsumer REFRESH_FOLDER =
32             new Statistics.TimeConsumer("Folder refresh");//NOI18N
33
public static final Statistics.TimeConsumer REFRESH_FILE =
34             new Statistics.TimeConsumer("File refresh");//NOI18N
35
public static final Statistics.TimeConsumer REFRESH_FS =
36             new Statistics.TimeConsumer("FileSystem refresh");//NOI18N
37
public static final Statistics.TimeConsumer LISTENERS_CALLS =
38             new Statistics.TimeConsumer("Invocation of FileChangeListeners");//NOI18N
39

40     
41     private Statistics() {}
42     
43     
44     public static Statistics.StopWatch getStopWatch(Statistics.TimeConsumer consumer) {
45         return new Statistics.StopWatch(consumer);
46     }
47     
48     public static int fileSystems() {
49         return FileBasedFileSystem.getSize();
50     }
51     
52     public static int fileNamings() {
53         return NamingFactory.getSize();
54     }
55     
56     public static int fileObjects() {
57         int retVal = 0;
58         Iterator JavaDoc it = FileBasedFileSystem.getInstances().iterator();
59         for (int i = 0; it.hasNext(); i++) {
60             FileBasedFileSystem fbs = (FileBasedFileSystem)it.next();
61             retVal += fileObjectsPerFileSystem(fbs);
62         }
63         
64         return retVal;
65     }
66     
67     public static int fileObjectsPerFileSystem(FileBasedFileSystem fbs) {
68         return fbs.getFactory().getSize();
69     }
70
71             
72     public static final class TimeConsumer {
73         private int elapsedTime;
74         private int numberOfCalls;
75         private final String JavaDoc description;
76         
77         private TimeConsumer(final String JavaDoc description) {
78             this.description = description;
79         }
80         
81         public int getConsumedTime() {
82             return elapsedTime;
83         }
84         
85         public int getNumberOfCalls() {
86             return numberOfCalls;
87         }
88         
89         public void reset() {
90             elapsedTime = 0;
91             numberOfCalls = 0;
92         }
93
94         public String JavaDoc toString() {
95             return description + ": " + numberOfCalls + " calls in " + elapsedTime + "ms";
96         }
97         
98         private void incrementNumerOfCalls() {
99             numberOfCalls++;
100             
101         }
102     }
103     
104     public static final class StopWatch {
105         private long startTime = 0;
106         private final Statistics.TimeConsumer activity;
107         
108         
109         /** Creates a new instance of ElapsedTime */
110         private StopWatch(Statistics.TimeConsumer activity) {
111             this.activity = activity;
112         }
113         
114         
115         public void start() {
116             startTime = System.currentTimeMillis();
117         }
118         
119         public void stop() {
120             assert startTime != 0;
121             activity.elapsedTime += (System.currentTimeMillis() - startTime);
122             activity.incrementNumerOfCalls();
123             startTime = 0;
124         }
125     }
126 }
127
128
Popular Tags