Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
* An {@link HeterogeneousMap} is a container which allows to map keys to values
* with customized types. It is opposed in this sense to the standard
* {@link Map} which associates keys to values having a common type. Thus, while
* a standard {@link Map} returns values of a given type, each key of an
* {@link HeterogeneousMap} stores the type information of the value it is
* mapped to, which allows to retrieve this value with the corresponding type.
* Such a container is suited for storing key-value pairs where values are
* particularly heterogeneous (thus the naming), a situation which would lead a
* standard {@link Map} to store values as non-informative {@link Object}
* instances, thus enforcing casting.
*
*
*
* For practical purpose, an {@link HeterogeneousMap} is a {@link Map}-like
* container, in the sense that it reproduces the API of a classical {@link Map}
* with only the required differences. Despite the high similarity, it remains
* unable to extend the {@link Map} interface, a limitation which is compensated
* by using the {@link Map} provided by {@link #toMap()}.
*
*
* PAY ATTENTION
*
* While the parameterized {@link Key} allows to ensure that correct values are
* mapped at compile-time, the checks which can only be done at run-time are
* achieved through the information stored in the {@link Key}. If the
* {@link Key} is instantiated without this information, type safety is reduced.
* Moreover, instances of parameterized types which cannot be checked at
* compile-time will not be (fully) checked at run-time because of Java's type
* erasure.
*
*
* @author Matthieu Vergne
*
*/
public class HeterogeneousMap implements Iterable, Object>> {
/**
* A {@link Key} is a unique descriptor identifying a specific item in an
* {@link HeterogeneousMap}. It stores the relevant information about the type
* of the values it can be mapped to.
*
* @author Matthieu Vergne
*
* @param
*/
public static class Key {
private final Class valueClass;
/**
* Instantiate a {@link Key} for a specific type of values. Such instance allows
* to cover both compile-time and run-time type safety. However, it does not
* allow to cover parameterized types.
*/
public Key(Class valueClass) {
this.valueClass = valueClass;
}
/**
* Instantiate a {@link Key} without storing {@link Class} information. Such
* instance covers only compile-time type safety. However, it also manages
* parameterized types.
*/
public Key() {
this(null);
}
@SuppressWarnings("unchecked")
private T cast(Object value) {
if (valueClass == null) {
return (T) value;
} else {
return valueClass.cast(value);
}
}
private boolean canBeMappedTo(Object value) {
return value == null || valueClass == null || valueClass.isInstance(value);
}
@Override
public String toString() {
return "Key(" + valueClass + ")";
}
}
private final Map, Object> innerMap;
private HeterogeneousMap(Supplier