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 */
017package org.apache.camel.management.mbean;
018
019/**
020 * Base implementation of {@link Statistic}
021 * <p/>
022 * The following modes is available:
023 * <ul>
024 * <li>VALUE - A statistic with this update mode is a simple value that is a straight forward representation of the
025 * updated value.</li>
026 * <li>DELTA - A statistic with this update mode is a value that represents the delta between the last two recorded
027 * values (or the initial value if two updates have not been recorded). This value can be negative if the delta goes up
028 * or down.</li>
029 * <li>COUNTER - A statistic with this update mode interprets updates as increments (positive values) or decrements
030 * (negative values) to the current value.</li>
031 * <li>MAXIMUM - A statistic with this update mode is a value that represents the maximum value amongst the update
032 * values applied to this statistic.</li>
033 * <li>MINIMUM - A statistic with this update mode is a value that represents the minimum value amongst the update
034 * values applied to this statistic.</li>
035 * <ul>
036 * The MAXIMUM and MINIMUM modes are not 100% thread-safe as there can be a lost-update problem. This is on purpose
037 * because the performance overhead to ensure atomic updates costs to much on CPU and memory footprint. The other modes
038 * are thread-safe.
039 */
040public abstract class Statistic {
041
042    public abstract void updateValue(long newValue);
043
044    public void increment() {
045        updateValue(1);
046    }
047
048    public void decrement() {
049        updateValue(-1);
050    }
051
052    public abstract long getValue();
053
054    /**
055     * Whether the statistic has been updated one or more times. Notice this is only working for value, maximum and
056     * minimum modes.
057     */
058    public abstract boolean isUpdated();
059
060    public abstract void reset();
061
062}