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.commons.monitoring.stopwatches;
018    
019    import org.apache.commons.monitoring.counters.Counter;
020    
021    import java.util.concurrent.atomic.AtomicInteger;
022    
023    import static org.apache.commons.monitoring.counters.Unit.Time.NANOSECOND;
024    
025    public class CounterStopWatch implements StopWatch {
026        protected final Counter counter;
027        protected final long startedAt;
028        protected final AtomicInteger concurrencyCounter;
029        protected long stopedAt;
030        protected boolean stoped;
031    
032        public CounterStopWatch(final Counter counter) {
033            this.counter = counter;
034            startedAt = nanotime();
035    
036            concurrencyCounter = counter.currentConcurrency();
037            final int concurrency = concurrencyCounter.incrementAndGet();
038            counter.updateConcurrency(concurrency);
039        }
040    
041        protected long nanotime() {
042            return System.nanoTime();
043        }
044    
045        @Override
046        public long getElapsedTime() {
047            if (!stoped) {
048                return nanotime() - startedAt;
049            }
050            return stopedAt - startedAt;
051        }
052    
053        @Override
054        public StopWatch stop() {
055            if (!stoped) {
056                stopedAt = nanotime();
057                stoped = true;
058                doStop();
059            }
060            return this;
061        }
062    
063        protected void doStop() {
064            counter.add(getElapsedTime(), NANOSECOND);
065            concurrencyCounter.decrementAndGet();
066        }
067    
068        @Override
069        public String toString() {
070            final StringBuilder stb = new StringBuilder();
071            if (counter != null) {
072                stb.append("Execution for ").append(counter.getKey().toString()).append(" ");
073            }
074            if (stoped) {
075                stb.append("stoped after ").append(getElapsedTime()).append("ns");
076            } else {
077                stb.append("running for ").append(getElapsedTime()).append("ns");
078            }
079            return stb.toString();
080    
081        }
082    }