Find the lowest / highest key of a distributed map - java

I have a distributed map and I want to find the lowest or highest key (an object implementing compareable). What is the most efficient way to get those keys? I mean something like every node provides his lowest key and in the end the lowest key is the lowest of every node.
So I think:
MyObj max = Collections.max(map.keySet());
is not the most efficient way. And if I want to use
new DistributedTask<>(new Max(input), key);
I would need to now the key and therefore fetch all Keys over wire. I think in that case I could do Collections.max(map.keySet()); as well.
Hmm ... any ideas?

You could use EntryProcessor.executeOnEntries - with a stateful EntryProcessor - and then let the it do all the work for you; have each key map to a sentinel MIN and MAX enum if they are the min and max.
If you have some idea of the bounds, you could attach a filter Predicate as well to speed it up that way, too.

This map reduce solution seems to have a lot of overhead but it is the best way I could get the job done. Any better ideas are still welcome.
public static void main(String[] args) throws ExecutionException, InterruptedException {
IMap<String, Integer> map = instance.getMap("test");
JobTracker jobTracker = instance.getJobTracker( "default" );
KeyValueSource<String, Integer> source = KeyValueSource.fromMap( map );
Job<String, Integer> job = jobTracker.newJob(source);
JobCompletableFuture<Map<String, String>> future = job
.mapper(new MaxMapper())
.reducer(new MaxReducerFactory())
.submit();
System.out.println("mr max: " + future.get());
}
public static class MaxMapper implements Mapper<String, Integer, String, String> {
private volatile String max = null;
#Override
public void map(String s, Integer integer, Context<String, String> ctx) {
if (max == null || s.compareTo(max)>0) {
max = s;
ctx.emit("max", max);
}
}
}
public static class MaxReducerFactory implements ReducerFactory<String,String,String> {
#Override
public Reducer<String, String> newReducer(String s) {
return new MaxReducer();
}
private class MaxReducer extends Reducer<String, String> {
private volatile String max = null;
#Override
public void reduce(String s) {
if (max == null || s.compareTo(max)>0) max = s;
}
#Override
public String finalizeReduce() {
return max; // == null ? "" : max;
}
}
}

Mapper:
import com.hazelcast.mapreduce.Context;
import com.hazelcast.mapreduce.Mapper;
import stock.Stock;
public class MinMaxMapper implements Mapper<String, Stock, String, Double> {
static final String MIN = "min";
static final String MAX = "max";
#Override
public void map(String key, Stock value, Context<String, Double> context) {
context.emit(MIN, value.getPrice());
context.emit(MAX, value.getPrice());
}
}
Combiner:
import com.hazelcast.mapreduce.Combiner;
import com.hazelcast.mapreduce.CombinerFactory;
public class MinMaxCombinerFactory implements CombinerFactory<String, Double, Double> {
#Override
public Combiner<Double, Double> newCombiner(String key) {
return new MinMaxCombiner(MinMaxMapper.MAX.equals(key) ? true : false);
}
private static class MinMaxCombiner extends Combiner<Double, Double> {
private final boolean maxCombiner;
private double value;
private MinMaxCombiner(boolean maxCombiner) {
this.maxCombiner = maxCombiner;
this.value = maxCombiner ? -Double.MAX_VALUE : Double.MAX_VALUE;
}
#Override
public void combine(Double value) {
if (maxCombiner) {
this.value = Math.max(value, this.value);
} else {
this.value = Math.min(value, this.value);
}
}
#Override
public Double finalizeChunk() {
return value;
}
#Override
public void reset() {
this.value = maxCombiner ? -Double.MAX_VALUE : Double.MAX_VALUE;
}
}
}
Reducer:
import com.hazelcast.mapreduce.Reducer;
import com.hazelcast.mapreduce.ReducerFactory;
public class MinMaxReducerFactory implements ReducerFactory<String, Double, Double> {
#Override
public Reducer<Double, Double> newReducer(String key) {
return new MinMaxReducer(MinMaxMapper.MAX.equals(key) ? true : false);
}
private static class MinMaxReducer extends Reducer<Double, Double> {
private final boolean maxReducer;
private volatile double value;
private MinMaxReducer(boolean maxReducer) {
this.maxReducer = maxReducer;
this.value = maxReducer ? -Double.MAX_VALUE : Double.MAX_VALUE;
}
#Override
public void reduce(Double value) {
if (maxReducer) {
this.value = Math.max(value, this.value);
} else {
this.value = Math.min(value, this.value);
}
}
#Override
public Double finalizeReduce() {
return value;
}
}
}
Returns two elements map with min and max:
ICompletableFuture<Map<String, Double>> future =
job.mapper(new MinMaxMapper())
.combiner(new MinMaxCombinerFactory())
.reducer(new MinMaxReducerFactory())
.submit();
Map<String, Double> result = future.get();

Why don't you create an ordered index? Although I'm not quite sure if it currently is possible to find a maximum value using a predicate and once found, abort the evaluation of the predicate.

Related

A java map where the keys are known however the values should be computed later on as they are expensive

Does a java map implementation exist where the keys are known, however the values should only be computed on the first access as calculating the values is expensive.
The following demonstrates how I would like it to work.
someMap.keySet(); // Returns all keys but no values are computed.
someMap.get(key); // Returns the value for key computing it if needed.
The reason for this is I have something which holds a bunch of data and this Object returns the data as a Map<String, String> this is computationally heavy to compute because computing the values is expensive, the keys are however cheap to compute.
The Map must maintain its type so I can't return a Map<String, Supplier<String>>. The returned Map may be returned as read only.
The map itself could be created by passing in both a Set<String> defining the keys and a Function<String, String> which given a key returns its value.
One solution could be to have a Map that takes a Set of keys and a Function which given a key can compute the value.
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
/**
* Create a Map where we already know the keys but computing the values is expensive and so is delayed as
* much as possible.
*
*/
#AllArgsConstructor
public class MapWithValuesProvidedByFunction implements Map<String, String> {
/**
* All keys that are defined.
*/
private Set<String> keys;
/**
* A function which maps a key to its value.
*/
private Function<String, String> mappingFunction;
/**
* Holds all keys and values we have already computed.
*/
private final Map<String, String> computedValues = new HashMap<>();
#Override
public int size() {
return keys.size();
}
#Override
public boolean isEmpty() {
return keys.isEmpty();
}
#Override
public boolean containsKey(Object key) {
return keys.contains(key);
}
#Override
public boolean containsValue(Object value) {
if(computedValues.size() == keys.size()) return computedValues.containsValue(value);
for(String k : keys) {
String v = get(k);
if(v == value) return true;
if(v != null && v.equals(value)) return true;
}
return false;
}
#Override
public String get(Object key) {
if(keys.contains(key)) {
return computedValues.computeIfAbsent(key.toString(), mappingFunction);
}
return null;
}
#Override
public String put(String key, String value) {
throw new UnsupportedOperationException("Not modifiable");
}
#Override
public String remove(Object key) {
throw new UnsupportedOperationException("Not modifiable");
}
#Override
public void putAll(Map<? extends String, ? extends String> m) {
throw new UnsupportedOperationException("Not modifiable");
}
#Override
public void clear() {
throw new UnsupportedOperationException("Not modifiable");
}
#Override
public Set<String> keySet() {
return Collections.unmodifiableSet(keys);
}
#Override
public Collection<String> values() {
return keys.stream().map(this::get).collect(Collectors.toList());
}
#Override
public Set<java.util.Map.Entry<String, String>> entrySet() {
Set<Entry<String, String>> set = new HashSet<>();
for(String s : keys) {
set.add(new MyEntry(s, this::get));
}
return set;
}
#AllArgsConstructor
#EqualsAndHashCode
public class MyEntry implements Entry<String, String> {
private String key;
private Function<String, String> valueSupplier;
#Override
public String getKey() {
return key;
}
#Override
public String getValue() {
return valueSupplier.apply(key);
}
#Override
public String setValue(String value) {
throw new UnsupportedOperationException("Not modifiable");
}
}
}
An example of this being used might be:
Map<String, String> map = new MapWithValuesProvidedByFunction(
Set.of("a", "b", "c"), // The known keys
k -> "Slow to compute function"); // The function to make the values
Changing this to be generic should be easy enough.
I suspect a better solution exists, however this might be good enough for someone else.
You could do something like this. The map has a key and a Fnc class which holds a function and the argument to the function.
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
public class MapDemo {
public static Map<String, Object> mymap = new HashMap<>();
public static void main(String[] args) {
MapDemo thisClass = new MapDemo();
// populate the functions
mymap.put("v1", new Fnc<String>(String::toUpperCase));
mymap.put("10Fact", new Fnc<Integer>((Integer a) -> {
int f = 1;
int k = a;
while (k-- > 1) {
f *= k;
}
return f + "";
}));
mymap.put("expensive",
new Fnc<Integer>(thisClass::expensiveFunction));
// access them - first time does the computation
System.out.println(getValue("expensive", 1000));
System.out.println(getValue("10Fact", 10));
System.out.println(getValue("v1", "hello"));
// second time does not.
System.out.println(getValue("expensive"));
System.out.println(getValue("10Fact"));
System.out.println(getValue("v1"));
}
public String expensiveFunction(int q) {
return q * 100 + ""; // example
}
static class Fnc<T> {
Function<T, String> fnc;
public Fnc(Function<T,String> fnc) {
this.fnc = fnc;
}
}
public <T> void addFunction(String key,
Function<String, T> fnc) {
mymap.put(key, fnc);
}
public static String getValue(String key) {
Object ret = mymap.get(key);
if (ret instanceof Fnc) {
return null;
}
return (String)mymap.get(key);
}
public static <T> String getValue(String key, T arg) {
Object ret = mymap.get(key);
if (ret instanceof Fnc) {
System.out.println("Calculating...");
ret = ((Fnc)ret).fnc.apply(arg);
mymap.put(key, ret);
}
return (String) ret;
}
}
First time thru, the function is called and the value is computed, stored, and returned. Subsequent calls return the stored value.
Note that the value replaces the computing function.

Caffeine cache - many keys to single value

I have Caffeine cache with Key->Value mapping. There are multiple implementations of Key interface with different equals methods. In order to delete value from cache based on someOtherVal, I had to use code like cache.asMap().keySet().removeIf(comp::isSame) which is super slow.
Is there any other solution for this kind of many keys to single value mapping in cache? One thing that comes to my mind is to have 2 Cache instances, one with Cache<Key, String> and other with Cache<someOtherVal, Key>, and whenever I want to delete a value I locate Key using this other cache.
Then only question is how to keep this 2 caches in sync? Are there already solutions for this?
import java.time.Duration;
import java.util.Objects;
import java.util.UUID;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.google.common.base.Stopwatch;
public class Removal {
private static final int MAX = 1_000_000;
interface Key{
String getSomeOtherVal();
default boolean isSame(Key k){
return Objects.equals(k.getSomeOtherVal(),getSomeOtherVal());
}
}
static class KeyImpl implements Key{
int id;
String someOtherVal;
public KeyImpl(int id, String someOtherVal) {
this.id = id;
this.someOtherVal = someOtherVal;
}
public int getId() {
return id;
}
#Override
public String getSomeOtherVal() {
return someOtherVal;
}
#Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
KeyImpl key = (KeyImpl)o;
return id == key.id;
}
#Override
public int hashCode() {
return Objects.hash(id);
}
}
Cache<Key, String> cache = Caffeine.newBuilder().build();
public static void main(String[] args) {
Removal s = new Removal();
s.fill();
Duration sRem = s.slowRemovalFirst100();
Duration fRem = s.fastRemoval100To200();
System.out.println("Slow removal in " + sRem);
System.out.println("Fast removal in " + fRem);
}
private Duration slowRemovalFirst100(){
Stopwatch sw = Stopwatch.createStarted();
for(int i=0; i<100; i++){
Key comp = new KeyImpl(i, String.valueOf(i));
cache.asMap().keySet().removeIf(comp::isSame); //Finds a key by some other property and then removes it (SLOW)
//System.out.println("Removed " + i);
}
return sw.stop().elapsed();
}
private Duration fastRemoval100To200(){
Stopwatch sw = Stopwatch.createStarted();
for(int i=100; i<200; i++){
Key comp = new KeyImpl(i, String.valueOf(i));
cache.invalidate(comp); //Uses direct access to map by key (FAST)
//System.out.println("Removed " + i);
}
return sw.stop().elapsed();
}
private void fill(){
for(int i=0; i<MAX; i++){
cache.put(new KeyImpl(i, String.valueOf(i)), UUID.randomUUID().toString());
}
}
}
Result of running this code on my machine:
Slow removal in PT2.807105177S
Fast removal in PT0.000126183S
where you can see such a big difference...
Ok, I managed to solve this:
public class IndexedCache<K,V> implements Cache<K,V> {
#Delegate
private Cache<K, V> cache;
private Map<Class<?>, Map<Object, Set<K>>> indexes;
private IndexedCache(Builder<K, V> bldr){
this.indexes = bldr.indexes;
cache = bldr.caf.build();
}
public <R> void invalidateAllWithIndex(Class<R> clazz, R value) {
cache.invalidateAll(indexes.get(clazz).getOrDefault(value, new HashSet<>()));
}
public static class Builder<K, V>{
Map<Class<?>, Function<K, ?>> functions = new HashMap<>();
Map<Class<?>, Map<Object, Set<K>>> indexes = new ConcurrentHashMap<>();
Caffeine<K,V> caf;
public <R> Builder<K,V> withIndex(Class<R> clazz, Function<K, R> function){
functions.put(clazz, function);
indexes.put(clazz, new ConcurrentHashMap<>());
return this;
}
public IndexedCache<K, V> buildFromCaffeine(Caffeine<Object, Object> caffeine) {
caf = caffeine.writer(new CacheWriter<K, V>() {
#Override
public void write( K k, V v) {
for(Map.Entry<Class<?>, Map<Object, Set<K>>> indexesEntry : indexes.entrySet()){
indexesEntry.getValue().computeIfAbsent(functions.get(indexesEntry.getKey()).apply(k), (ky)-> new HashSet<>())
.add(k);
}
}
#Override
public void delete( K k, V v, RemovalCause removalCause) {
for(Map.Entry<Class<?>, Map<Object, Set<K>>> indexesEntry : indexes.entrySet()){
indexesEntry.getValue().remove(functions.get(indexesEntry.getKey()).apply(k));
}
}
});
return new IndexedCache<>(this);
}
}
}
and this is use-case:
#AllArgsConstructor
#Data
#EqualsAndHashCode(onlyExplicitlyIncluded = true)
static class CompositeKey{
#EqualsAndHashCode.Include
Integer k1;
String k2;
Long k3;
}
public static void main(String[] args) {
Caffeine<Object, Object> cfein = Caffeine.newBuilder().softValues().maximumSize(200_000);
IndexedCache<CompositeKey, String> cache = new IndexedCache.Builder<CompositeKey, String>()
.withIndex(Long.class, ck -> ck.getK3())
.withIndex(String.class, ck -> ck.getK2())
.buildFromCaffeine(cfein);
for(int i=0; i<100; i++){
cache.put(new CompositeKey(i, String.valueOf(i), Long.valueOf(i)), "sdfsdf");
}
for(int i=0; i<10; i++){
//use equals method of CompositeKey to do equals comp.
cache.invalidate(new CompositeKey(i, String.valueOf(i), Long.valueOf(i)));
}
for(int i=10; i<20; i++){
//use Long index
cache.invalidateAllWithIndex(Long.class, Long.valueOf(i));
}
for(int i=20; i<30; i++){
//use String index
cache.invalidateAllWithIndex(String.class, String.valueOf(i));
}
int y = 4;
}
here is link to discussion I had: https://github.com/ben-manes/caffeine/issues/279

Using range values as a key in a map

Let's say that I want my application to determine user's fitness level based on some criteria.
The criteria could be something like: age, currently taking medication?, 400m run
At first I though I could create a Map where the value is the fitness level and the key is an object that has all the criteria, but since the criteria are ranges this wouldn't work.
For example:
if age is between 18 and 22 and onMedication = false and run400m = [70, 80]
fitness level = GOOD
Now if only one of the parameters is in a different range the fitness level would be different. How could I achieve this?
You can use a TreeMap class for this. There are very useful methods to deal with ranges of key values. For example:
TreeMap<Integer, String> myTreeMap = new TreeMap<>();
myTreeMap.put(10, "A");
myTreeMap.put(20, "B");
myTreeMap.put(30, "C");
myTreeMap.put(40, "D");
System.out.println(myTreeMap.floorEntry(25));
Will be print the second option (20=B). I recommend that you check the TreeMap and all its methods for this case.
Maybe you could use OOP and do something like this:
public class FitnessApp {
public static void main(String[] args) {
Map<String, Object> params = new HashMap<>();
params.put("age", 17);
params.put("onMedication", false);
System.out.printf(new FitnessLevelCalculator().calculateFor(params).name());
}
}
class FitnessLevelCalculator {
private LinkedList<FitnessLevel> fitnessLevels = new LinkedList<>();
public FitnessLevelCalculator() {
fitnessLevels.add(new FitnessLevel(FitnessLevelEnum.ATHLETIC, Arrays.asList(new RangeCriteria("age", 18, 25), new BooleanCriteria("onMedication", false))));
fitnessLevels.add(new FitnessLevel(FitnessLevelEnum.GOOD, Arrays.asList(new RangeCriteria("age", 14, 17), new BooleanCriteria("onMedication", false))));
fitnessLevels.add(new FitnessLevel(FitnessLevelEnum.ILL, Arrays.asList(new RangeCriteria("age", 16, 17))));
}
public FitnessLevelEnum calculateFor(Map<String, Object> params) {
ListIterator<FitnessLevel> listIterator = fitnessLevels.listIterator();
while (listIterator.hasNext()) {
FitnessLevel fitnessLevel = listIterator.next();
if (fitnessLevel.accept(params)) {
return fitnessLevel.getLevel();
}
}
return FitnessLevelEnum.NOT_CLASSIFIED;
}
}
enum FitnessLevelEnum {
ILL, GOOD, ATHLETIC, NOT_CLASSIFIED
}
class FitnessLevel {
private List<Criteria> criteriaList = new ArrayList<>();
private FitnessLevelEnum level;
public FitnessLevel(FitnessLevelEnum level, List<Criteria> criteriaList) {
this.criteriaList = criteriaList;
this.level = level;
}
public boolean accept(Map<String, Object> params) {
for (Criteria criteria : criteriaList) {
if (!params.containsKey(criteria.getName())) {
return false;
}
if (!criteria.satisfies(params.get(criteria.getName()))) {
return false;
}
}
return true;
}
public FitnessLevelEnum getLevel() {
return level;
}
}
abstract class Criteria<T> {
private String name;
public Criteria(String name) {
this.name = name;
}
public abstract boolean satisfies(T param);
public String getName() {
return name;
}
}
class RangeCriteria extends Criteria<Integer> {
private int min;
private int max;
public RangeCriteria(String name, int min, int max) {
super(name);
this.min = min;
this.max = max;
}
#Override
public boolean satisfies(Integer param) {
return param >= min && param <= max;
}
}
class BooleanCriteria extends Criteria<Boolean> {
private Boolean expectedValue;
public BooleanCriteria(String name, Boolean expectedValue) {
super(name);
this.expectedValue = expectedValue;
}
#Override
public boolean satisfies(Boolean param) {
return param == expectedValue;
}
}
In your specific case, I don't think it's good to use as a key in Map. There maybe a way to put the Object with all conditions as your business but it's quite complex and not worth to do that. The Interpreter Pattern may help you on this.
Hope this help.

Java TreeMap get min and max values

I have this kind of a TreeMap. How can i get minimum and maximum values for town temperature after i make some entries in towns? I do not copy the code where i fill in some values in towns because it works fine.
Map<String, Town> towns = new TreeMap<>();
The Town.class is like this.
public class Town {
private int temperature;
private int rainfall;
private int windPower;
private Downwind downwind;
public Town(int temperature, int rainfall, int windPower, Downwind downwind) {
this.temperature = temperature;
this.rainfall = rainfall;
this.windPower = windPower;
this.downwind = downwind;
}
public int getTemperature() {
return temperature;
}
public void setTemperature(int temperature) {
this.temperature = temperature;
}
public int getRainfall() {
return rainfall;
}
public void setRainfall(int rainfall) {
this.rainfall = rainfall;
}
public int getWindPower() {
return windPower;
}
public void setWindPower(int windPower) {
this.windPower = windPower;
}
public Downwind getDownwind() {
return downwind;
}
public void setDownwind(Downwind downwind) {
this.downwind = downwind;
}
The simplest:
Optional<Town> maybeMinTown = towns.values().stream()
.min(Comparator.comparing(Town::getTemperature));
Town minTown = maybeMinTown.get(); // throws NoSuchElementException when towns map is empty
the same for max - you only need to use max instead of min.
UPDATE
To get just temperature you can map Town to temperature and then call min/max:
final OptionalInt min = towns.values().stream()
.mapToInt(Town::getTemperature)
.min();
min.getAsInt(); // or you can call min.orElseGet(some_lowest_value)
OptionalInt is the same for int what Optional<Town> is for Town.
If you are not in Java 8, you have to order the Map by value to achieve that, maybe something like this
public static Map<String, Town> sortByValues(final Map<String, Town> map) {
Comparator<String> valueComparator = new Comparator<String>() {
public int compare(String k1, String k2) {
if (map.get(k1).getTemperature() < map.get(k2).getTemperature()) {
return 1;
} else if (map.get(k1).getTemperature() == map.get(k2).getTemperature()) {
return 0;
} else {
return -1;
}
}
};
Map<String, Town> sortedByValues = new TreeMap<String, Town>(valueComparator);
sortedByValues.putAll(map);
return sortedByValues;
}
When you create your TreeMap, pass an instance of this comparator class
towns = sortByValues(towns);
After that, you will have the maximun value at position 0 and the minimun in the last element.
UPDATE
Change the code in order to compile.

Real time sorted by value, auto-discarding, bounded collection ?

I spent some time to try to make a collection that:
1) is sorted by value (not by key)
2) is sorted each time an element is added or modified
3) is fixed size and discard automatically smallest/biggest element depending of the sort way
4) is safe thread
So 3) and 4) I think it is quite ok. For 1) and 2) it was a bit more tricky. I spent quite a long time on this thread, experimenting the different sample, but one big issue is that the collection are sorted only once when object are inserted.
Anyway, I try to implement my own collection, which is working (shouldn't be used for huge data as it is sorted quite often) but I'm not so happy with the design. Especially in the fact that my value objects are constrained to be Observable (which is good) but not comparable so I had to use a dirty instanceof + exception for this.
Any sugestion to improve this ?
Here is the code:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
public class SortedDiscardingSyncArray<K, V extends Observable> implements Observer {
// Comparison way (ascendent or descendant)
public static enum ComparisonWay
{
DESC,
ASC;
}
// this is backed by a List (and ArrayList impl)
private List<ArrayElement> array;
// Capacity, configurable, over this limit, an item will be discarded
private int MAX_CAPACITY = 200;
// default is descending comparison
private ComparisonWay compareWay = ComparisonWay.DESC;
public SortedDiscardingSyncArray(ComparisonWay compareWay, int mAX_CAPACITY) {
super();
this.compareWay = compareWay;
MAX_CAPACITY = mAX_CAPACITY;
array = new ArrayList <ArrayElement>(MAX_CAPACITY);
}
public SortedDiscardingSyncArray(int mAX_CAPACITY) {
super();
MAX_CAPACITY = mAX_CAPACITY;
array = new ArrayList<ArrayElement>(MAX_CAPACITY);
}
public SortedDiscardingSyncArray() {
super();
array = new ArrayList <ArrayElement>(MAX_CAPACITY);
}
public boolean put(K key, V value)
{
try {
return put (new ArrayElement(key, value, this));
} catch (Exception e) {
e.printStackTrace();
return false;
}
finally
{
sortArray();
}
}
private synchronized boolean put(ArrayElement ae)
{
if (array.size() < MAX_CAPACITY)
{
return array.add(ae);
}
// check if last one is greater/smaller than current value to insert
else if (ae.compareTo(array.get(MAX_CAPACITY-1)) < 0)
{
array.remove(MAX_CAPACITY - 1);
return array.add(ae);
}
// else we don't insert
return false;
}
public V getValue (int index)
{
return array.get(index).getValue();
}
public V getValue (K key)
{
for (ArrayElement ae : array)
{
if (ae.getKey().equals(key)) return ae.getValue();
}
return null;
}
public K getKey (int index)
{
return array.get(index).getKey();
}
private void sortArray()
{
Collections.sort(array);
}
public synchronized void setValue(K key, V newValue) {
for (ArrayElement ae : array)
{
if (ae.getKey().equals(key))
{
ae.setValue(newValue);
return;
}
}
}
public int size() {
return array.size();
}
#Override
public void update(java.util.Observable arg0, Object arg1) {
sortArray();
}
public static void main(String[] args) {
// some test on the class
SortedDiscardingSyncArray<String, ObservableSample> myData = new SortedDiscardingSyncArray<String, ObservableSample>(ComparisonWay.DESC, 20);
String Ka = "Ka";
String Kb = "Kb";
String Kc = "Kc";
String Kd = "Kd";
myData.put(Ka, new ObservableSample(0));
myData.put(Kb, new ObservableSample(3));
myData.put(Kc, new ObservableSample(1));
myData.put(Kd, new ObservableSample(2));
for (int i=0; i < myData.size(); i++)
{
System.out.println(myData.getKey(i).toString() + " - " + myData.getValue(i).toString());
}
System.out.println("Modifying data...");
myData.getValue(Kb).setValue(12);
myData.getValue(Ka).setValue(34);
myData.getValue(Kd).setValue(9);
myData.getValue(Kc).setValue(19);
for (int i=0; i < myData.size(); i++)
{
System.out.println(myData.getKey(i).toString() + " - " + myData.getValue(i).toString());
}
}
private class ArrayElement implements Comparable <ArrayElement> {
public ArrayElement(K key, V value, Observer obs) throws Exception {
super();
// don't know how to handle that case
// maybe multiple inheritance would have helped here ?
if (! (value instanceof Comparable)) throw new Exception("Object must be 'Comparable'");
this.key = key;
this.value = value;
value.addObserver(obs);
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append(key);
sb.append(" - ");
sb.append(value);
return sb.toString();
}
private K key;
private V value;
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public synchronized void setValue(V value) {
this.value = value;
}
#SuppressWarnings("unchecked")
#Override
public int compareTo(ArrayElement o) {
int c;
if (compareWay == ComparisonWay.DESC) c = ((Comparable<V>) o.getValue()).compareTo(this.getValue());
else c = ((Comparable<V>) this.getValue()).compareTo(o.getValue());
if (c != 0) {
return c;
}
Integer hashCode1 = o.getValue().hashCode();
Integer hashCode2 = this.getValue().hashCode();
// we don't check the compare way for hash code (useless ?)
return hashCode1.compareTo(hashCode2);
}
}
}
And the other class for testing purpose:
import java.util.Observable;
public class ObservableSample extends Observable implements Comparable <ObservableSample>
{
private Integer value = 0;
public ObservableSample(int value) {
this.value = value;
setChanged();
notifyObservers();
}
public String toString()
{
return String.valueOf(this.value);
}
public void setValue(Integer value) {
this.value = value;
setChanged();
notifyObservers();
}
public Integer getValue() {
return value;
}
#Override
public int compareTo(ObservableSample o) {
int c;
c = (this.getValue()).compareTo(o.getValue());
if (c != 0) {
return c;
}
Integer hashCode1 = o.getValue().hashCode();
Integer hashCode2 = this.getValue().hashCode();
// we don't check the compare way for hash code (useless ?)
return hashCode1.compareTo(hashCode2);
}
}
Collections are difficult to write, maybe you should look for an existing implementation.
Try checking out ImmutableSortedSet from Guava.
You can have a marker interface
public interface ComparableObservable extends Observable, Comparable {
}
and then change
SortedDiscardingSyncArray<K, V extends Observable>
to
SortedDiscardingSyncArray<K, V extends ComparableObservable>
to avoid the explicit cast.
Other than that the code is quite verbose and I didn't follow it completely. I would also suggest having a look at guava or (apache) commons-collections library to explore if you can find something reusable.
You can write generic wildcards with multiple bounds. So change your declaration of <K, V extends Observable> to <K, V extends Observable & Comparable<V>> and then you can treat V as if it implements both interfaces, without an otherwise empty and useless interface.
Another few things: Pick a naming convention, and stick with it. The one I use is that a name such as MAX_CAPACITY would be used for a static final field (i.e. a constant, such as a default) and that the equivalent instance field would be maxCapacity Names such as mAX_CAPACITY would be right out of the question.
See: Oracle's naming conventions for Java
Instead of using a ComparisonWay enum, I would take a custom Comparator. Much more flexible, and doesn't replicate something that already exists.
See: the Comparator API docs
Your code, as written, is not thread safe. In particular an observed element calling the unsynchronized update method may thus invoke sortArray without obtaining the proper lock. FindBugs is a great tool that catches a lot of problems like this.
Your ObservableSample does not really follow good practices with regards to how it implements Comparable, in that it does not really compare data values but instead the hashCode. The hashCode is essentially arbitrary and collisions are quite possible. Additionally, the Comparable interface requests that usually you should be "consistent with Equals", for which you also might want to take a look at the documentation for the Object class's equals method
Yes, it sounds like a lot of work, but if you go through it and do it right you will save yourself astounding amounts of debugging effort down the road. If you do not do these properly and to the spec, you will find that when you place it in Sets or Maps your keys or values strangely disappear, reappear, or get clobbered. And it will depend on which version of Java you run, potentially!
Here is a version updated. Still not completly sure it is safe thread but findbugs tool didn't give so usefull tips. Also for the comparisonWay, I don't want to constraint the user to develop its own comparator, I want to keep the things simple.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
public class SortedDiscardingSyncArray<K, V extends Observable & Comparable<V>> implements Observer {
// Comparison way (ascendent or descendant)
public static enum ComparisonWay { DESC, ASC; }
// this is backed by a List (and ArrayList)
private List<ArrayElement> array;
// Capacity, configurable, over this limit, an item will be discarded
private int maxCapacity = 200;
// default is descending comparison
private ComparisonWay compareWay = ComparisonWay.DESC;
public SortedDiscardingSyncArray(ComparisonWay compareWay, int maxCapacity) {
super();
this.compareWay = compareWay;
this.maxCapacity = maxCapacity;
array = new ArrayList <ArrayElement>(maxCapacity);
}
public SortedDiscardingSyncArray(int maxCapacity) {
super();
this.maxCapacity = maxCapacity;
array = new ArrayList<ArrayElement>(maxCapacity);
}
public SortedDiscardingSyncArray() {
super();
array = new ArrayList <ArrayElement>(maxCapacity);
}
// not synchronized, but calling internal sync put command
public boolean put(K key, V value)
{
try {
return put (new ArrayElement(key, value, this));
} catch (Exception e) {
e.printStackTrace();
return false;
}
finally
{
sortArray();
}
}
private synchronized boolean put(ArrayElement ae)
{
if (array.size() < maxCapacity) return array.add(ae);
// check if last one is greater/smaller than current value to insert
else if (ae.compareTo(array.get(maxCapacity-1)) < 0)
{
array.remove(maxCapacity - 1);
return array.add(ae);
}
// else we don't insert and return false
return false;
}
public V getValue (int index)
{
return array.get(index).getValue();
}
public V getValue (K key)
{
for (ArrayElement ae : array)
{
if (ae.getKey().equals(key)) return ae.getValue();
}
return null;
}
public K getKey (int index)
{
return array.get(index).getKey();
}
private synchronized void sortArray()
{
Collections.sort(array);
}
public synchronized void setValue(K key, V newValue) {
for (ArrayElement ae : array)
{
if (ae.getKey().equals(key))
{
ae.setValue(newValue);
return;
}
}
}
public int size() {
return array.size();
}
#Override
public void update(java.util.Observable arg0, Object arg1) {
sortArray();
}
public static void main(String[] args) {
// some test on the class
SortedDiscardingSyncArray<String, ObservableSample> myData = new SortedDiscardingSyncArray<String, ObservableSample>(ComparisonWay.DESC, 20);
String Ka = "Ka";
String Kb = "Kb";
String Kc = "Kc";
String Kd = "Kd";
myData.put(Ka, new ObservableSample(0));
myData.put(Kb, new ObservableSample(3));
myData.put(Kc, new ObservableSample(1));
myData.put(Kd, new ObservableSample(2));
for (int i=0; i < myData.size(); i++)
{
System.out.println(myData.getKey(i).toString() + " - " + myData.getValue(i).toString());
}
System.out.println("Modifying data...");
myData.getValue(Kb).setValue(12);
myData.getValue(Ka).setValue(34);
myData.getValue(Kd).setValue(9);
myData.getValue(Kc).setValue(19);
for (int i=0; i < myData.size(); i++)
{
System.out.println(myData.getKey(i).toString() + " - " + myData.getValue(i).toString());
}
}
private class ArrayElement implements Comparable <ArrayElement> {
public ArrayElement(K key, V value, Observer obs) throws Exception {
super();
this.key = key;
this.value = value;
value.addObserver(obs);
}
public String toString()
{
StringBuffer sb = new StringBuffer();
sb.append(key);
sb.append(" - ");
sb.append(value);
return sb.toString();
}
private K key;
private V value;
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public synchronized void setValue(V value) {
this.value = value;
}
#Override
public int compareTo(ArrayElement o) {
int c;
if (compareWay == ComparisonWay.DESC) c = o.getValue().compareTo(this.getValue());
else c = this.getValue().compareTo(o.getValue());
if (c != 0) {
return c;
}
Integer hashCode1 = o.getValue().hashCode();
Integer hashCode2 = this.getValue().hashCode();
// we don't check the compare way for hash code (useless ?)
return hashCode1.compareTo(hashCode2);
}
}
}

Categories

Resources