com.simiacryptus.util.CountCollection Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-util Show documentation
Show all versions of java-util Show documentation
Miscellaneous utilities for Java 8
/*
* Copyright (c) 2019 by Andrew Charneski.
*
* The author 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 com.simiacryptus.util;
import com.google.common.collect.Maps;
import com.google.common.collect.Maps.EntryTransformer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;
/**
* The type Count collection.
*
* @param the type parameter
* @param the type parameter
*/
public class CountCollection> {
/**
* The Map.
*/
protected final C map;
/**
* Instantiates a new Count collection.
*
* @param collection the collection
*/
public CountCollection(final C collection) {
super();
this.map = collection;
}
/**
* Add int.
*
* @param bits the bits
* @return the int
*/
public int add(final T bits) {
return this.getCounter(bits).incrementAndGet();
}
/**
* Add int.
*
* @param bits the bits
* @param count the count
* @return the int
*/
public int add(final T bits, final int count) {
return this.getCounter(bits).addAndGet(count);
}
/**
* Count int.
*
* @param key the key
* @return the int
*/
protected int count(final T key) {
final AtomicInteger counter = this.map.get(key);
if (null == counter) {
return 0;
}
return counter.get();
}
private AtomicInteger getCounter(final T bits) {
AtomicInteger counter = this.map.get(bits);
if (null == counter) {
counter = new AtomicInteger();
this.map.put(bits, counter);
}
return counter;
}
/**
* Gets list.
*
* @return the list
*/
public List getList() {
final ArrayList list = new ArrayList();
for (final Entry e : this.map.entrySet()) {
for (int i = 0; i < e.getValue().get(); i++) {
list.add(e.getKey());
}
}
return list;
}
/**
* Gets map.
*
* @return the map
*/
public Map getMap() {
return Maps.transformEntries(this.map,
new EntryTransformer() {
@Override
public Integer transformEntry(final T key, final AtomicInteger value) {
return value.get();
}
});
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy