de.unkrig.commons.lang.protocol
Class Mappings

java.lang.Object
  extended by de.unkrig.commons.lang.protocol.Mappings

public final class Mappings
extends java.lang.Object

Utility methods related to Mapping.


Method Summary
static
<K,V> Mapping<K,V>
augment(Mapping<K,V> in, java.lang.Object... keysAndValues)
          Equivalent with union(Mapping, Mapping), where the first argument is in and the second is constructed from the given keyValuePairs.
static
<K,V> Mapping<K,V>
fromMap(java.util.Map<K,V> map)
          Returns a proxy Mapping for a Map.
static
<K,T> T
get(Mapping<? extends K,?> mapping, java.lang.Object key, java.lang.Class<T> targetType)
          Invokes Mapping.get(Object) on the mapping and, if the result is not null, converts it to the given targetType.
static
<K,T> T
getNonNull(Mapping<K,?> mapping, java.lang.String key, java.lang.Class<T> targetType)
           
static
<K,V> Mapping<K,V>
mapping(java.lang.Object... keysAndValues)
           
static
<K,V> Mapping<K,V>
none()
           
static
<K,V> Mapping<K,V>
override(Mapping<K,V> in, java.lang.Object... keysAndValues)
          Equivalent with union(Mapping, Mapping), where the first argument is constructed from the given keyValuePairs and the second argument is in.
static Mapping<java.lang.String,java.lang.Object> propertiesOf(java.lang.Object subject)
          Returns a mapping of property names to property values for the given subject object.
static
<K,V> Mapping<K,V>
union(Mapping<K,V> op1, Mapping<K,V> op2)
          Returns a mapping that implements the "union" of two mappings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

fromMap

public static <K,V> Mapping<K,V> fromMap(java.util.Map<K,V> map)
Returns a proxy Mapping for a Map. This is very straightforward because the Map declares all methods that the Mapping requires. (Actually the Map should extend Mapping, but doesn't.)


mapping

public static <K,V> Mapping<K,V> mapping(java.lang.Object... keysAndValues)
Parameters:
keysAndValues - An alternating sequence of keys and values; even elements must have type K, odd elements must have type V
Returns:
A mapping of the keys and values

none

public static <K,V> Mapping<K,V> none()
Returns:
A Mapping with no mappings

propertiesOf

public static Mapping<java.lang.String,java.lang.Object> propertiesOf(java.lang.Object subject)
Returns a mapping of property names to property values for the given subject object.

The existence of a property "propertyName" is determined by the existence of one of the following, in this order:

Returns:
A mapping of property name to property value

get

@Nullable
public static <K,T> T get(Mapping<? extends K,?> mapping,
                                   java.lang.Object key,
                                   java.lang.Class<T> targetType)
Invokes Mapping.get(Object) on the mapping and, if the result is not null, converts it to the given targetType.

Throws:
java.lang.IllegalArgumentException - The type of the value of the property is not assignable to T

getNonNull

public static <K,T> T getNonNull(Mapping<K,?> mapping,
                                 java.lang.String key,
                                 java.lang.Class<T> targetType)
Returns:
The value to which the mapping maps the key
Throws:
java.lang.IllegalArgumentException - The mapping does not contain the given key
java.lang.IllegalArgumentException - The mapping contains the key, but the mapped value is null
java.lang.IllegalArgumentException - The mapping contains the key, but the mapped value is not assignable to T

union

public static <K,V> Mapping<K,V> union(Mapping<K,V> op1,
                                       Mapping<K,V> op2)
Returns a mapping that implements the "union" of two mappings.

For the returned Mapping, the following conditions apply:


augment

public static <K,V> Mapping<K,V> augment(Mapping<K,V> in,
                                         java.lang.Object... keysAndValues)
Equivalent with union(Mapping, Mapping), where the first argument is in and the second is constructed from the given keyValuePairs.

Parameters:
keysAndValues - An alternating sequence of keys and values; even elements must have type K, odd elements must have type V

override

public static <K,V> Mapping<K,V> override(Mapping<K,V> in,
                                          java.lang.Object... keysAndValues)
Equivalent with union(Mapping, Mapping), where the first argument is constructed from the given keyValuePairs and the second argument is in.

Parameters:
keysAndValues - An alternating sequence of keys and values; even elements must have type K, odd elements must have type V