View Javadoc

1   /*
2    ~ Copyright 2006-2007 Nicolas De Loof.
3    ~
4    ~ Licensed under the Apache License, Version 2.0 (the "License");
5    ~ you may not use this file except in compliance with the License.
6    ~ You may obtain a copy of the License at
7    ~
8    ~      http://www.apache.org/licenses/LICENSE-2.0
9    ~
10   ~ Unless required by applicable law or agreed to in writing, software
11   ~ distributed under the License is distributed on an "AS IS" BASIS,
12   ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   ~ See the License for the specific language governing permissions and
14   ~ limitations under the License.
15   */
16  package org.jmonit.features;
17  
18  import java.util.ArrayList;
19  import java.util.LinkedList;
20  import java.util.List;
21  
22  import org.jmonit.events.DataMonitoredEvent;
23  import org.jmonit.events.MonitoringEvent;
24  
25  /**
26   * Maintains a list a last N values. Can be used to build a short term detailled
27   * history of the monitor.
28   * 
29   * @author <a href="mailto:nicolas.deloof@gmail.com">Nicolas De Loof</a>
30   */
31  public class LastValues
32      extends AbstractFeature<LastValues>
33  {
34  
35      private static final int DEFAULT_SIZE = 10;
36  
37      /** List of last values added to the monitor */
38      private LinkedList values = new LinkedList();
39  
40      /** max size of values history */
41      private int size = DEFAULT_SIZE;
42  
43      /**
44       * {@inheritDoc}
45       * 
46       * @see org.jmonit.events.MonitoringEventListener#onMonitoringEvent(org.jmonit.events.MonitoringEvent)
47       */
48      public void onMonitoringEvent( MonitoringEvent event )
49      {
50          if ( event instanceof DataMonitoredEvent )
51          {
52              DataMonitoredEvent data = (DataMonitoredEvent) event;
53              added( data.getData() );
54          }
55      }
56  
57      private synchronized void added( long value )
58      {
59          values.add( new Double( value ) );
60          truncate();
61      }
62  
63      public synchronized void clear()
64      {
65          values.clear();
66      }
67  
68      /**
69       * @return the size
70       */
71      public int getSize()
72      {
73          return size;
74      }
75  
76      /**
77       * @return the values
78       */
79      public synchronized List getValues()
80      {
81          return new ArrayList( values );
82      }
83  
84      /**
85       * @param size the size to set
86       */
87      public void setSize( int size )
88      {
89          this.size = size;
90          truncate();
91      }
92  
93      /**
94       * {@inheritDoc}
95       * 
96       * @see java.lang.Object#toString()
97       */
98      public synchronized String toString()
99      {
100         StringBuffer stb = new StringBuffer( "LastValues " );
101         stb.append( values.toString() );
102         return stb.toString();
103     }
104 
105     /**
106      * Remove the last values of the list until max size
107      */
108     private void truncate()
109     {
110         while ( values.size() > size )
111         {
112             values.removeFirst();
113         }
114     }
115 
116     /**
117      * {@inheritDoc}
118      * 
119      * @see org.jmonit.spi.Plugin#getFeature()
120      */
121     public LastValues getFeature()
122     {
123         return this;
124     }
125 }