Skip to content

Commit

Permalink
Change hierarchy of map that uses optional values
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Mendez committed Jun 10, 2017
1 parent 8cb1403 commit e29b38e
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 65 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package de.tudresden.inf.lat.tabula.common;

import java.util.Collection;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;

/**
* An object implementing this interface wraps a map with some conditions:
Expand All @@ -17,11 +20,25 @@
* @param <V>
* type of mapped values
*/
public interface OptMap<K, V> extends Map<K, V> {
public interface OptMap<K, V> {

/**
* Returns the size of this map.
*
* @return the size of this map
*/
int size();

/**
* Returns <code>true</code> if and only if this map is empty.
*
* @return <code>true</code> if and only if this map is empty
*/
boolean isEmpty();

/**
* Returns <code>true</code> if and only if this map contains a mapping for
* the given key. This method replaces {@link #containsKey(Object)}.
* the given key. This method replaces {@link Map#containsKey(Object)}.
*
* @param key
* key
Expand All @@ -30,18 +47,18 @@ public interface OptMap<K, V> extends Map<K, V> {
* @throws NullPointerException
* if a <code>null</code> value is given
*/
public boolean isKeyContained(K key);
boolean isKeyContained(K key);

/**
* @deprecated Replaced by {@link #isKeyContained isKeyContained(K)}
*/
@Deprecated
public boolean containsKey(Object key);
// /**
// * @deprecated Replaced by {@link #isKeyContained isKeyContained(K)}
// */
// @Deprecated
// public boolean containsKey(Object key);

/**
* Returns <code>true</code> if and only if this map associates one or more
* keys to the given value. This method replaces
* {@link #containsValue(Object)}.
* {@link Map#containsValue(Object)}.
*
* @param value
* value
Expand All @@ -50,35 +67,35 @@ public interface OptMap<K, V> extends Map<K, V> {
* @throws NullPointerException
* if a <code>null</code> value is given
*/
public boolean isValueContained(V value);
boolean isValueContained(V value);

/**
* @deprecated Replaced by {@link #isValueContained isValueContained(V)}
*/
@Deprecated
public boolean containsValue(Object value);
// /**
// * @deprecated Replaced by {@link #isValueContained isValueContained(V)}
// */
// @Deprecated
// boolean containsValue(Object value);

/**
* Returns an optional containing the value associated to the given key, is
* this association exists, or an empty optional otherwise. This method
* replaces {@link #get(Object)}.
* replaces {@link Map#get(Object)}.
*
* @param key
* key
* @return an optional containing the value associated to the given key, is
* this association exists, or an empty optional otherwise
*/
public Optional<V> getOpt(K key);
Optional<V> getOpt(K key);

/**
* @deprecated Replaced by {@link #getOpt getOpt(K)}
*/
@Deprecated
public V get(Object key);
// /**
// * @deprecated Replaced by {@link #getOpt getOpt(K)}
// */
// @Deprecated
// V get(Object key);

/**
* Associates the given value with the given key. This method replaces
* {@link #put put(K, V)}.
* {@link Map#put put(K, V)}.
*
* @param key
* key
Expand All @@ -90,17 +107,17 @@ public interface OptMap<K, V> extends Map<K, V> {
* @throws NullPointerException
* if a <code>null</code> value is given
*/
public Optional<V> putOpt(K key, V value);
Optional<V> putOpt(K key, V value);

/**
* @deprecated Replaced by {@link #putOpt putOpt(K, V)}
*/
@Deprecated
public V put(K key, V value);
// /**
// * @deprecated Replaced by {@link #putOpt putOpt(K, V)}
// */
// @Deprecated
// V put(K key, V value);

/**
* Removes the mapping for the given key. This method replaces
* {@link #remove(Object)}.
* {@link Map#remove(Object)}.
*
* @param key
* key
Expand All @@ -112,10 +129,51 @@ public interface OptMap<K, V> extends Map<K, V> {
*/
public Optional<V> removeOpt(K key);

// /**
// * @deprecated Replaced by {@link #removeOpt removeOpt(K)}
// */
// @Deprecated
// V remove(Object key);

/**
* @deprecated Replaced by {@link #removeOpt removeOpt(K)}
* Clears this map.
*/
void clear();

/**
* Adds all the associations given in the specified map.
*
* @param m
* map containing associations
*/
void putAll(Map<? extends K, ? extends V> m);

/**
* Returns the set of keys.
*
* @return the set of keys
*/
Set<K> keySet();

/**
* Returns the collection of values.
*
* @return the collection of values
*/
Collection<V> values();

/**
* Returns a set of associations.
*
* @return a set of associations
*/
Set<Entry<K, V>> entrySet();

/**
* Returns this as a {@link Map}.
*
* @return this as a <code>Map</code>
*/
@Deprecated
public V remove(Object key);
Map<K, V> asMap();

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Set;

/**
* This is the default implementation of {{@link OptMap}.
* This is the default implementation of {@link OptMap}.
*
* @author Julian Mendez
*
Expand All @@ -21,15 +21,36 @@ public class OptMapImpl<K, V> implements OptMap<K, V> {

private final Map<K, V> map;

/**
* Constructs a new map. The default implementation structure is a
* {@link HashMap}.
*/
public OptMapImpl() {
this.map = new HashMap<K, V>();
}

/**
* Constructs a new map using a specified {@link Map}.
*
* @param map
* map
*/
public OptMapImpl(Map<K, V> map) {
Objects.requireNonNull(map);
this.map = map;
}

/**
* Constructs a new map using another specified {@link OptMap}.
*
* @param map
* map
*/
public OptMapImpl(OptMap<K, V> map) {
Objects.requireNonNull(map);
this.map = map.asMap();
}

@Override
public int size() {
return this.map.size();
Expand All @@ -46,38 +67,38 @@ public boolean isKeyContained(K key) {
return this.map.containsKey(key);
}

@Override
@Deprecated
public boolean containsKey(Object key) {
Objects.requireNonNull(key);
return this.map.containsKey(key);
}
// @Override
// @Deprecated
// public boolean containsKey(Object key) {
// Objects.requireNonNull(key);
// return this.map.containsKey(key);
// }

@Override
public boolean isValueContained(V value) {
Objects.requireNonNull(value);
return this.map.containsValue(value);
}

@Override
@Deprecated
public boolean containsValue(Object value) {
Objects.requireNonNull(value);
return this.map.containsValue(value);
}
// @Override
// @Deprecated
// public boolean containsValue(Object value) {
// Objects.requireNonNull(value);
// return this.map.containsValue(value);
// }

@Override
public Optional<V> getOpt(K key) {
Objects.requireNonNull(key);
return Optional.ofNullable(this.map.get(key));
}

@Override
@Deprecated
public V get(Object key) {
Objects.requireNonNull(key);
return this.map.get(key);
}
// @Override
// @Deprecated
// public V get(Object key) {
// Objects.requireNonNull(key);
// return this.map.get(key);
// }

@Override
public Optional<V> putOpt(K key, V value) {
Expand All @@ -86,26 +107,26 @@ public Optional<V> putOpt(K key, V value) {
return Optional.ofNullable(this.map.put(key, value));
}

@Override
@Deprecated
public V put(K key, V value) {
Objects.requireNonNull(key);
Objects.requireNonNull(value);
return this.map.put(key, value);
}
// @Override
// @Deprecated
// public V put(K key, V value) {
// Objects.requireNonNull(key);
// Objects.requireNonNull(value);
// return this.map.put(key, value);
// }

@Override
public Optional<V> removeOpt(K key) {
Objects.requireNonNull(key);
return Optional.ofNullable(this.map.remove(key));
}

@Override
@Deprecated
public V remove(Object key) {
Objects.requireNonNull(key);
return this.map.remove(key);
}
// @Override
// @Deprecated
// public V remove(Object key) {
// Objects.requireNonNull(key);
// return this.map.remove(key);
// }

@Override
public void putAll(Map<? extends K, ? extends V> m) {
Expand Down Expand Up @@ -148,4 +169,9 @@ public String toString() {
return this.map.toString();
}

@Override
public Map<K, V> asMap() {
return this.map;
}

}

0 comments on commit e29b38e

Please sign in to comment.