Author: nicolas Date: Fri Sep 4 15:39:04 2009 New Revision: 811448 URL: http://svn.apache.org/viewvc?rev=811448&view=rev Log: avoid method override in design, set method final where possible
Added: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/AbstractNoOpMetric.java Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/Counter.java commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/Gauge.java commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/AbstractMetric.java commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/NoOpCounter.java commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/NoOpGauge.java commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObservableMetric.java commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverCounter.java commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverGauge.java commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverMetric.java commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/SynchronizedCounter.java commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/SynchronizedGauge.java commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ThreadSafeCounter.java commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ThreadSafeGauge.java Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/Counter.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/Counter.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/Counter.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/Counter.java Fri Sep 4 15:39:04 2009 @@ -32,4 +32,6 @@ { } + + public void add( double delta ); } Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/Gauge.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/Gauge.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/Gauge.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/Gauge.java Fri Sep 4 15:39:04 2009 @@ -30,11 +30,15 @@ */ double getValue(); + void increment(); + void increment( Unit unit ); void set( double value, Unit unit ); void decrement( Unit unit ); + + void decrement(); public interface Observable extends Metric.Observable, Gauge Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/AbstractMetric.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/AbstractMetric.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/AbstractMetric.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/AbstractMetric.java Fri Sep 4 15:39:04 2009 @@ -74,7 +74,7 @@ add( delta ); } - protected double normalize( double value, Unit unit ) + protected final double normalize( double value, Unit unit ) { if ( !this.unit.isCompatible( unit ) ) { @@ -83,17 +83,17 @@ return value * unit.getScale() / this.unit.getScale(); } - public Monitor getMonitor() + public final Monitor getMonitor() { return monitor; } - public Role getRole() + public final Role getRole() { return role; } - public void setMonitor( Monitor monitor ) + public final void setMonitor( Monitor monitor ) { if ( this.monitor != null && this.monitor != monitor ) { @@ -102,43 +102,16 @@ this.monitor = monitor; } - public Unit getUnit() + public final Unit getUnit() { return unit; } /** * @return - * @see org.apache.commons.math.stat.descriptive.StatisticalSummary#getMax() - */ - public double getMax() - { - return getSummary().getMax(); - } - - /** - * @return - * @see org.apache.commons.math.stat.descriptive.StatisticalSummary#getMin() - */ - public double getMin() - { - return getSummary().getMin(); - } - - /** - * @return - * @see org.apache.commons.math.stat.descriptive.StatisticalSummary#getN() - */ - public long getHits() - { - return getSummary().getN(); - } - - /** - * @return * @see org.apache.commons.math.stat.descriptive.StatisticalSummary#getStandardDeviation() */ - public double getStandardDeviation() + public final double getStandardDeviation() { return getSummary().getStandardDeviation(); } @@ -147,7 +120,7 @@ * @return * @see org.apache.commons.math.stat.descriptive.StatisticalSummary#getSum() */ - public double getSum() + public final double getSum() { return getSummary().getSum(); } @@ -156,25 +129,16 @@ * @return * @see org.apache.commons.math.stat.descriptive.StatisticalSummary#getVariance() */ - public double getVariance() + public final double getVariance() { return getSummary().getVariance(); } /** * @return - * @see org.apache.commons.math.stat.descriptive.SummaryStatistics#getMean() - */ - public double getMean() - { - return getSummary().getMean(); - } - - /** - * @return * @see org.apache.commons.math.stat.descriptive.SummaryStatistics#getGeometricMean() */ - public double getGeometricMean() + public final double getGeometricMean() { return getSummary().getGeometricMean(); } @@ -183,7 +147,7 @@ * @return * @see org.apache.commons.math.stat.descriptive.SummaryStatistics#getSumOfLogs() */ - public double getSumOfLogs() + public final double getSumOfLogs() { return getSummary().getSumOfLogs(); } @@ -192,12 +156,12 @@ * @return * @see org.apache.commons.math.stat.descriptive.SummaryStatistics#getSumsq() */ - public double getSumOfSquares() + public final double getSumOfSquares() { return getSummary().getSumsq(); } - public void accept( Visitor visitor ) + public final void accept( Visitor visitor ) { switch ( getType() ) { Added: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/AbstractNoOpMetric.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/AbstractNoOpMetric.java?rev=811448&view=auto ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/AbstractNoOpMetric.java (added) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/AbstractNoOpMetric.java Fri Sep 4 15:39:04 2009 @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.commons.monitoring.metrics; + +import org.apache.commons.monitoring.Role; + +/** + * @author <a href="mailto:nico...@apache.org">Nicolas De Loof</a> + */ +public abstract class AbstractNoOpMetric + extends AbstractMetric +{ + + /** + * @param role + */ + public AbstractNoOpMetric( Role role ) + { + super( role ); + } + + public final void reset() + { + // NoOp + } + + public final long getHits() + { + return 0; + } + + public final double getMin() + { + return 0; + } + + public final double getMax() + { + return 0; + } + + public final double getMean() + { + return 0; + } + + public final void add( double delta ) + { + // NoOp + } +} \ No newline at end of file Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/NoOpCounter.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/NoOpCounter.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/NoOpCounter.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/NoOpCounter.java Fri Sep 4 15:39:04 2009 @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.commons.monitoring.metrics; import org.apache.commons.monitoring.Counter; @@ -5,7 +22,7 @@ import org.apache.commons.monitoring.Role; public class NoOpCounter - extends AbstractMetric + extends AbstractNoOpMetric implements Counter { public NoOpCounter( Role role ) @@ -17,14 +34,4 @@ { return Metric.Type.COUNTER; } - - public void reset() - { - // NoOp - } - - public void add( double delta ) - { - // NoOp - } } Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/NoOpGauge.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/NoOpGauge.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/NoOpGauge.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/NoOpGauge.java Fri Sep 4 15:39:04 2009 @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.commons.monitoring.metrics; import org.apache.commons.monitoring.Gauge; @@ -6,7 +23,7 @@ import org.apache.commons.monitoring.Unit; public class NoOpGauge - extends AbstractMetric + extends AbstractNoOpMetric implements Gauge { public NoOpGauge( Role role ) @@ -19,32 +36,32 @@ return Metric.Type.GAUGE; } - public void add( double delta ) + public final void decrement() { // NoOp } - public void reset() + public final void decrement( Unit unit ) { // NoOp } - public void decrement( Unit unit ) + public final double getValue() { - // NoOp + return 0; } - public double getValue() + public final void increment() { - return 0; + // NoOp } - public void increment( Unit unit ) + public final void increment( Unit unit ) { // NoOp } - public void set( double value, Unit unit ) + public final void set( double value, Unit unit ) { // NoOp } Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObservableMetric.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObservableMetric.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObservableMetric.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObservableMetric.java Fri Sep 4 15:39:04 2009 @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.commons.monitoring.metrics; import java.util.List; Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverCounter.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverCounter.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverCounter.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverCounter.java Fri Sep 4 15:39:04 2009 @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.commons.monitoring.metrics; import org.apache.commons.math.stat.descriptive.SummaryStatistics; @@ -10,8 +27,8 @@ * @author <a href="mailto:nico...@apache.org">Nicolas De Loof</a> */ public class ObserverCounter - extends ObserverMetric -implements Counter + extends ObserverMetric<Counter> + implements Counter { private Counter.Observable observable; @@ -25,34 +42,39 @@ observable.addListener( this ); } + protected Counter getDelegate() + { + return delegate; + } + @Override - protected SummaryStatistics getSummary() + protected final SummaryStatistics getSummary() { return delegate.getSummary(); } @Override - protected Metric.Observable getObservable() + protected final Metric.Observable getObservable() { return observable; } - public void onValueChanged( Metric.Observable metric, double value ) + public final void onValueChanged( Metric.Observable metric, double value ) { delegate.threadSafeAdd( value ); } - public void add( double delta ) + public final void add( double delta ) { throw new UnsupportedOperationException( "Observer cannot be updated directly" ); } - public Metric.Type getType() + public final Metric.Type getType() { return Metric.Type.COUNTER; } - public void reset() + public final void reset() { throw new UnsupportedOperationException( "Observer cannot be updated directly" ); } Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverGauge.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverGauge.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverGauge.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverGauge.java Fri Sep 4 15:39:04 2009 @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.commons.monitoring.metrics; import org.apache.commons.math.stat.descriptive.SummaryStatistics; @@ -11,7 +28,7 @@ * @author <a href="mailto:nico...@apache.org">Nicolas De Loof</a> */ public class ObserverGauge - extends ObserverMetric + extends ObserverMetric<Gauge> implements Gauge { private Gauge.Observable observable; @@ -27,6 +44,11 @@ observable.addListener( this ); } + protected Gauge getDelegate() + { + return delegate; + } + @Override protected SummaryStatistics getSummary() { @@ -39,32 +61,42 @@ return observable; } - public void onValueChanged( Metric.Observable metric, double value ) + public final void onValueChanged( Metric.Observable metric, double value ) { delegate.threadSafeSet( value ); } - public double getValue() + public final double getValue() { return delegate.getValue(); } - public void add( double delta ) + public final void add( double delta ) + { + throw new UnsupportedOperationException( "Observer cannot be updated directly" ); + } + + public final void increment() + { + throw new UnsupportedOperationException( "Observer cannot be updated directly" ); + } + + public final void increment( Unit unit ) { throw new UnsupportedOperationException( "Observer cannot be updated directly" ); } - public void increment( Unit unit ) + public final void set( double value, Unit unit ) { throw new UnsupportedOperationException( "Observer cannot be updated directly" ); } - public void set( double value, Unit unit ) + public final void decrement() { throw new UnsupportedOperationException( "Observer cannot be updated directly" ); } - public void decrement( Unit unit ) + public final void decrement( Unit unit ) { throw new UnsupportedOperationException( "Observer cannot be updated directly" ); } Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverMetric.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverMetric.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverMetric.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ObserverMetric.java Fri Sep 4 15:39:04 2009 @@ -1,10 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.commons.monitoring.metrics; import org.apache.commons.monitoring.Detachable; import org.apache.commons.monitoring.Metric; import org.apache.commons.monitoring.Role; -public abstract class ObserverMetric +public abstract class ObserverMetric<T extends Metric> extends AbstractMetric implements Detachable, Metric.Listener { @@ -22,10 +39,32 @@ attachedAt = System.currentTimeMillis(); } + protected abstract T getDelegate(); + + public final long getHits() + { + return getDelegate().getHits(); + } + + public final double getMin() + { + return getDelegate().getMin(); + } + + public final double getMax() + { + return getDelegate().getMax(); + } + + public final double getMean() + { + return getDelegate().getMean(); + } + /** * @see org.apache.commons.monitoring.Detachable#detach() */ - public void detach() + public final void detach() { getObservable().removeListener( this ); detached = true; @@ -35,7 +74,7 @@ /** * @see org.apache.commons.monitoring.Detachable#getAttachedAt() */ - public long getAttachedAt() + public final long getAttachedAt() { return attachedAt; } @@ -43,7 +82,7 @@ /** * @see org.apache.commons.monitoring.Detachable#getDetachedAt() */ - public long getDetachedAt() + public final long getDetachedAt() { return detachedAt; } @@ -51,7 +90,7 @@ /** * @see org.apache.commons.monitoring.Detachable#isDetached() */ - public boolean isDetached() + public final boolean isDetached() { return detached; } Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/SynchronizedCounter.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/SynchronizedCounter.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/SynchronizedCounter.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/SynchronizedCounter.java Fri Sep 4 15:39:04 2009 @@ -54,5 +54,4 @@ doReset(); } - } Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/SynchronizedGauge.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/SynchronizedGauge.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/SynchronizedGauge.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/SynchronizedGauge.java Fri Sep 4 15:39:04 2009 @@ -38,16 +38,6 @@ super( role ); } - /** - * {...@inheritdoc} - * - * @see org.apache.commons.monitoring.Metric#getType() - */ - public Type getType() - { - return Type.GAUGE; - } - public synchronized void reset() { doReset(); Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ThreadSafeCounter.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ThreadSafeCounter.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ThreadSafeCounter.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ThreadSafeCounter.java Fri Sep 4 15:39:04 2009 @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.commons.monitoring.metrics; import org.apache.commons.monitoring.Counter; @@ -12,17 +29,32 @@ super( role ); } - /** - * {...@inheritdoc} - * - * @see org.apache.commons.monitoring.Metric#getType() - */ - public Type getType() + public final Type getType() { return Type.COUNTER; } - public void add( double delta ) + public final long getHits() + { + return getSummary().getN(); + } + + public final double getMax() + { + return getSummary().getMax(); + } + + public final double getMin() + { + return getSummary().getMin(); + } + + public final double getMean() + { + return getSummary().getMean(); + } + + public final void add( double delta ) { threadSafeAdd( delta ); fireValueChanged( delta ); @@ -35,12 +67,12 @@ */ protected abstract void threadSafeAdd( double delta ); - protected void doThreadSafeAdd( double delta ) + protected final void doThreadSafeAdd( double delta ) { getSummary().addValue( delta ); } - protected void doReset() + protected final void doReset() { getSummary().clear(); } Modified: commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ThreadSafeGauge.java URL: http://svn.apache.org/viewvc/commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ThreadSafeGauge.java?rev=811448&r1=811447&r2=811448&view=diff ============================================================================== --- commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ThreadSafeGauge.java (original) +++ commons/sandbox/monitoring/trunk/core/src/main/java/org/apache/commons/monitoring/metrics/ThreadSafeGauge.java Fri Sep 4 15:39:04 2009 @@ -1,3 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.apache.commons.monitoring.metrics; import org.apache.commons.math.stat.descriptive.rank.Max; @@ -27,45 +44,74 @@ protected Max max = new Max(); - @Override - public double getMax() + protected long hits; + + public ThreadSafeGauge( Role role ) + { + super( role ); + } + + public final Type getType() + { + return Type.GAUGE; + } + + public final long getHits() + { + return hits; + } + + public final double getMax() { return max.getResult(); } - @Override - public double getMin() + public final double getMin() { return min.getResult(); - } - - public ThreadSafeGauge( Role role ) + } + + public final double getMean() { - super( role ); + if ( Double.isNaN( lastUse ) || Double.isNaN( firstUse ) ) + { + return Double.NaN; + } + return getSummary().getMean() / ( lastUse - firstUse ); } - public double getValue() + public final double getValue() { return value; } - public void increment( Unit unit ) + public final void increment() + { + add( 1 ); + } + + public final void increment( Unit unit ) { add( 1, unit ); } - public void decrement( Unit unit ) + public final void decrement( Unit unit ) { add( -1, unit ); } - public void add( double delta ) + public final void decrement() + { + add( -1 ); + } + + public final void add( double delta ) { double d = threadSafeAdd( delta ); fireValueChanged( d ); } - protected double threadSafeAdd( double delta ) + protected final double threadSafeAdd( double delta ) { threadSafeSet( value + delta ); return value; @@ -76,12 +122,12 @@ return System.nanoTime(); } - public double get() + public final double get() { return value; } - public void set( double d, Unit unit ) + public final void set( double d, Unit unit ) { d = normalize( d, unit ); threadSafeSet( d ); @@ -95,7 +141,7 @@ */ protected abstract void threadSafeSet( double d ); - protected void doReset() + protected final void doReset() { // Don't reset value ! getSummary().clear(); @@ -103,7 +149,7 @@ firstUse = Double.NaN; } - protected void doThreadSafeSet( double d ) + protected final void doThreadSafeSet( double d ) { value = d; long now = nanotime(); @@ -118,18 +164,9 @@ getSummary().addValue( s ); } lastUse = now; + hits++; min.increment( value ); max.increment( value ); } - @Override - public double getMean() - { - if ( Double.isNaN( lastUse ) || Double.isNaN( firstUse ) ) - { - return Double.NaN; - } - return super.getMean() / ( lastUse - firstUse ); - } - } \ No newline at end of file