View Javadoc

1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
20   */
21  package net.sf.jour.statistic;
22  
23  //import org.apache.log4j.Logger;
24  
25  /***
26   * TODO Add docs
27   *
28   * Created on 13.12.2004
29   * Contributing Author(s):
30   *
31   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
32   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
33   *
34   * @author vlads
35   * @version $Revision: 1.6 $ ($Author: vlads $)  $Date: 2004/12/17 04:17:21 $
36   */
37  public class TimeBaseAccumulationList extends AccumulationItmesList {
38  
39      public static final String ATTRIBUTE_TIME_START = "TimeBaseAccumulationList.TIME_START"; 
40      public static final String ATTRIBUTE_NAME = "TimeBaseAccumulationList.NAME";
41      /*
42       * millis, site of the intervals.
43       */
44      private long timeFrame = 1000 * 60 * 1 / 4; // 1 minute(s)
45      
46      public String getName() {
47          return (String)getAttribute(ATTRIBUTE_NAME);
48      }
49      
50      public void setName(String name) {
51          setAttribute(ATTRIBUTE_NAME, name);
52      }
53      
54      /*
55       * round down the time interval to the frame start.
56       */
57  	public long roundTimeStamp(double millis) {
58  		double f = (millis) / (this.timeFrame);
59  		return this.timeFrame * new Double(Math.floor(f)).longValue();
60  	}
61  	
62  	public Object timeStampKey(double timeMillis) {
63  	    return new Long(roundTimeStamp(timeMillis));   
64  	}
65  	
66      public void onNewAccumulation(Object key, Accumulation accumulation) {
67          super.onNewAccumulation(key, accumulation);
68          accumulation.setAttribute(ATTRIBUTE_TIME_START , key);
69      }
70  
71      public void add(double timeMillis, Object num) {
72          super.add(timeStampKey(timeMillis), num);
73      }
74      
75      public MillisecondsPeriod getPeriod(Object key) {
76          return new MillisecondsPeriod( ((Long)key).longValue(), this.timeFrame);
77      }
78      
79      public double getCallPerMinute(Object key) {
80          Accumulation acc = getItem(key);
81          return ((double) acc.getCount()  * 1000.0 * 60) / (this.timeFrame);
82      }
83      /***
84       * @return Returns the timeFrame, millis.
85       */
86      public long getTimeFrame() {
87          return this.timeFrame;
88      }
89      /***
90       * @param timeFrame The timeFrame to set, millis.
91       */
92      public void setTimeFrame(long timeFrame) {
93          if (timeFrame != 0) {
94              this.timeFrame = timeFrame;
95          }
96      }
97  }