Skip to content
Snippets Groups Projects
Commit 34ba40e1 authored by felmer's avatar felmer
Browse files

LMS-1665 introduce Counters with tests.

SVN: 17487
parent f4f59dc4
No related branches found
No related tags found
No related merge requests found
/*
* Copyright 2010 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.common.utilities;
import java.util.HashMap;
import java.util.Map;
/**
* A set of counters identified by objects of type <code>T</code>.
*
* @author Franz-Josef Elmer
*/
public class Counters<T>
{
private static final class Counter
{
private int count;
public int increment()
{
return ++count;
}
}
private final Map<T, Counter> counters = new HashMap<T, Counter>();
/**
* Returns how often method {@link #count(Object)} has been invoked for the specified object.
*/
public int getCountOf(T object)
{
Counter counter = counters.get(object);
return counter == null ? 0 : counter.count;
}
/**
* Counts the specified object and return how often it has already been counted.
*/
public int count(T object)
{
Counter counter = counters.get(object);
if (counter == null)
{
counter = new Counter();
counters.put(object, counter);
}
return counter.increment();
}
}
/*
* Copyright 2010 ETH Zuerich, CISD
*
* 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 ch.systemsx.cisd.common.utilities;
import org.testng.AssertJUnit;
import org.testng.annotations.Test;
/**
*
*
* @author Franz-Josef Elmer
*/
public class CountersTest extends AssertJUnit
{
@Test
public void test()
{
Counters<String> counters = new Counters<String>();
assertEquals(0, counters.getCountOf("a"));
assertEquals(1, counters.count("a"));
assertEquals(1, counters.getCountOf("a"));
assertEquals(1, counters.count("b"));
assertEquals(1, counters.getCountOf("a"));
assertEquals(1, counters.getCountOf("b"));
assertEquals(2, counters.count("a"));
assertEquals(3, counters.count("a"));
assertEquals(3, counters.getCountOf("a"));
assertEquals(1, counters.getCountOf("b"));
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment