Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2009-2010 Alexander Kerner. All rights reserved. Licensed 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 net.sf.kerner.utils.counter;
 
 import java.util.List;

A Counter can be used to monitor program behavior or to keep track of some progress, e.g. how many loop cycles have already been performed.

A Counter can be initialized with a java.lang.Runnable, which will be executed with given interval.
Per default the interval is 1, which means given java.lang.Runnable will be executed (synchronously) with every count.

Per default, a Counter will execute nothing with an interval of 1.

This class is fully threadsave.

Example:

 TODO example
 

Author(s):
Alexander Kerner
Version:
2010-10-06
 
 public class Counter {
 
     // Field //
 
     private final int initCnt;
     private volatile int cnt = 0;
     private volatile int interval = 1;
     private volatile int intervalHelper = 0;
     private volatile boolean performed = false;
     private volatile List<Runnablerunner = new ArrayList<Runnable>();
 
     // Constructor //
 
    
Create a new Counter that has initially performed zero counts and will do nothing on call of perform().
 
     public Counter() {
         // all zero
          = 0;
     }

    
Create a new Counter that has initially performed zero counts and will execute [@code runner} on call of perform().
 
     public Counter(Runnable runner) {
         // all zero
          = 0;
         this..add(runner);
     }

    

Construct a new Counter with has initially performed count counts and will do nothing on call of perform(). getCount() will return count.

Parameters:
count Initial counting for this Counter.
 
     public Counter(int count) {
         synchronized (Counter.class) {
             this. = count;
             this. = count;
         }
     }

    

Construct a new Counter with has initially performed count counts and will execute [@code runner} on call of perform(). getCount() will return count.

Parameters:
count Initial counting for this Counter.
    public Counter(int countRunnable runner) {
        synchronized (Counter.class) {
            this. = count;
            this. = count;
            this..add(runner);
        }
    }
    // Private //
    private void checkPerform() {
        // System.err.println("intervalHelper=" + intervalHelper);
        // System.err.println("interval=" + interval);
        // System.err.println("count=" + cnt);
        if ( == ) {
            perform();
             = true;
             = 0;
        } else {
             = false;
        }
    }
    // Protected //
    // Default //
    // Public //

    

Increase counter by 1.

    public synchronized void count() {
        ++;
        ++;
        checkPerform();
    }

    

Resets this Counter and calls perform() if there has been any counts after last call of perform().

    public synchronized void finish() {
        if () {
        } else
            perform();
         = 0;
         = ;
         = 1;
    }

    

Sets interval for this Counter.

Parameters:
interval number of counts count() has to be called before perform() is called.
Throws:
java.lang.NumberFormatException if interval < 1
    public synchronized void setInterval(int interval) {
        if (interval < 1)
            throw new NumberFormatException("interval must be >= 1");
        this. = interval;
    }

    

Returns:
current interval for this Counter.
    public int getInterval() {
        return ;
    }

    

Returns the number of count() has been called.

Returns:
current counts
    public int getCount() {
        return ;
    }

    

Returns the init value of cnt.

Returns:
init count
    public int getInitCount() {
        return ;
    }

    

Set count.

Parameters:
count count to set
    public synchronized void setCount(int count) {
        this. = count;
        this. = 0;
    }
    // Override //
    @Override
    public String toString() {
        return Integer.toString(getCount());
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ;
        result = prime * result + ;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof Counter))
            return false;
        Counter other = (Counterobj;
        if ( != other.cnt)
            return false;
        if ( != other.interval)
            return false;
        return true;
    }

    
Set this Counter java.lang.Runnable.

Parameters:
runner java.lang.Runnable that is run every interval
    public synchronized void addRunnable(Runnable runner) {
        this..add(runner);
    }
    public synchronized void clearRunnables() {
        this..clear();
    }

    

Performs action.

    public synchronized void perform() {
        for (Runnable r : ) {
            r.run();
        }
    }
New to GrepCode? Check out our FAQ X