Skip to content

Commit

Permalink
Added getOne() method and fixed equals() method
Browse files Browse the repository at this point in the history
  • Loading branch information
sangupta committed Dec 19, 2016
1 parent 6087a82 commit 120ad8a
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/main/java/com/sangupta/jerry/ds/SimpleMultiMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import net.jcip.annotations.ThreadSafe;

/**
* A very simple implementation of a Multi-Map for use that does not implement
* the {@link Map} interface. It is useful for situations where a very
Expand All @@ -43,6 +45,7 @@
* @author sangupta
*
*/
@ThreadSafe
public class SimpleMultiMap<K, V> {

/**
Expand Down Expand Up @@ -97,6 +100,27 @@ public List<V> getValues(K key) {
return this.map.get(key);
}

/**
* Return the very first element from all the values stored against the key
*
* @param key
* the key being looked for
*
* @return the very first value, <code>null</code> otherwise
*/
public V getOne(K key) {
List<V> values = this.getValues(key);
if(values == null) {
return null;
}

if(values.isEmpty()) {
return null;
}

return values.get(0);
}

/**
* Return the number of values stored against the given key.
*
Expand Down Expand Up @@ -146,6 +170,10 @@ public void put(K key, V value) {
values.add(value);
}

public void forceReplace(K key, V value) {

}

/**
* Remove and return all values associated with the given key.
*
Expand Down Expand Up @@ -182,7 +210,20 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
return this.map.equals(obj);
if(obj == null) {
return false;
}

if(this == obj) {
return true;
}

if(!(map instanceof SimpleMultiMap)) {
return false;
}

SimpleMultiMap<?, ?> map = (SimpleMultiMap<?, ?>) obj;
return this.map.equals(map.map);
}

}

0 comments on commit 120ad8a

Please sign in to comment.