MATSIM
Counter.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Counter.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2008 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.utils.misc;
22 
23 import java.util.concurrent.atomic.AtomicLong;
24 
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 
34 public final class Counter {
35  private final String prefix;
36  private final String suffix;
37  private final int multiplier;
38  private final AtomicLong counter = new AtomicLong(0);
39  private final AtomicLong nextCounter = new AtomicLong(1);
40  private static final Logger log = LogManager.getLogger(Counter.class);
41 
45  public Counter(final String prefix) {
46  this( prefix , "" );
47  }
48 
53  public Counter(final String prefix, final String suffix ) {
54  this( prefix, suffix, 2 ) ;
55  }
60  public Counter(final String prefix, final String suffix, int multiplier) {
61  this.prefix = prefix;
62  this.suffix = suffix;
63  this.multiplier = multiplier;
64  }
65 
66  public void incCounter() {
67  long i = this.counter.incrementAndGet();
68  long n = this.nextCounter.get();
69  if ((i >= n) && (this.nextCounter.compareAndSet(n, n*multiplier))) {
70  log.info(this.prefix + n + this.suffix);
71  }
72  }
73 
74  public void printCounter() {
75  log.info(this.prefix + this.counter.get() + this.suffix);
76  }
77 
78  public long getCounter() {
79  return this.counter.get();
80  }
81 
82  public void reset() {
83  this.counter.set(0);
84  this.nextCounter.set(1);
85  }
86 }
Counter(final String prefix, final String suffix)
Definition: Counter.java:53
Counter(final String prefix, final String suffix, int multiplier)
Definition: Counter.java:60
Counter(final String prefix)
Definition: Counter.java:45