001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.management.mbean;
018
019 /**
020 * Default implementation of {@link Statistic}
021 */
022 public class Statistic {
023
024 /**
025 * Statistics mode
026 * <ul>
027 * <li>VALUE - A statistic with this update mode is a simple value that is a straight forward
028 * representation of the updated value.</li>
029 * <li>DIFFERENCE - A statistic with this update mode is a value that represents the difference
030 * between the last two recorded values (or the initial value if two updates have
031 * not been recorded).</li>
032 * <li>COUNTER - A statistic with this update mode interprets updates as increments (positive values)
033 * or decrements (negative values) to the current value.</li>
034 * <li>MAXIMUM - A statistic with this update mode is a value that represents the maximum value
035 * amongst the update values applied to this statistic.</li>
036 * <li>MINIMUM - A statistic with this update mode is a value that represents the minimum value
037 * amongst the update values applied to this statistic.</li>
038 * <ul>
039 */
040 public enum UpdateMode {
041 VALUE, DIFFERENCE, COUNTER, MAXIMUM, MINIMUM
042 }
043
044 private final UpdateMode updateMode;
045 private long value;
046 private long updateCount;
047
048 /**
049 * Instantiates a new statistic.
050 *
051 * @param name name of statistic
052 * @param owner owner
053 * @param updateMode The statistic update mode.
054 */
055 public Statistic(String name, Object owner, UpdateMode updateMode) {
056 this.updateMode = updateMode;
057 }
058
059 public synchronized void updateValue(long newValue) {
060 switch (this.updateMode) {
061 case COUNTER:
062 this.value += newValue;
063 break;
064 case VALUE:
065 this.value = newValue;
066 break;
067 case DIFFERENCE:
068 this.value -= newValue;
069 if (this.value < 0) {
070 this.value = -this.value;
071 }
072 break;
073 case MAXIMUM:
074 // initialize value at first time
075 if (this.updateCount == 0 || this.value < newValue) {
076 this.value = newValue;
077 }
078 break;
079 case MINIMUM:
080 // initialize value at first time
081 if (this.updateCount == 0 || this.value > newValue) {
082 this.value = newValue;
083 }
084 break;
085 default:
086 }
087 this.updateCount++;
088 }
089
090 public synchronized void increment() {
091 updateValue(1);
092 }
093
094 public synchronized long getValue() {
095 return this.value;
096 }
097
098 public synchronized long getUpdateCount() {
099 return this.updateCount;
100 }
101
102 public synchronized void reset() {
103 this.value = 0;
104 this.updateCount = 0;
105 }
106
107 public String toString() {
108 return "" + value;
109 }
110
111 }