-
public class BidirectionalMap<T extends Object, R extends Object>
A bidirectional map. Each key-value-pair gets mapped in both directions. Keys and values must be unique in the sense that there must not be a duplicate key in the domain, nor a duplicate value in the coDomain, but the same element may appear once as key and once as value.
Example:
A -> B
A -> C
is invalid because A is contained twice in the Domain.<br>
A -> B
C -> B
is invalid because B is contained twice in the coDomain.<br>
A -> B
C -> A
is valid because A is only contained once in the domain and in the coDomain.
-
-
Constructor Summary
Constructors Constructor Description BidirectionalMap(Pair<T, R> elements)
Creates a map with the given set of elements mapping pair first -> pair.second.
-
Method Summary
Modifier and Type Method Description final Integer
getSize()
final Boolean
add(T entity, R value)
Adds a relation A -> B if domain does not contain A and coDomain does not contain B. final Boolean
add(Pair<T, R> element)
Adds a relation A -> B if domain does not contain A and coDomain does not contain B. final Boolean
addAll(Pair<T, R> items)
Adds all relations A -> B. final R
forward(T entity)
Forward lookup for entry. final R
forwardOrNull(T entity)
Forward lookup for entry. final T
backward(R value)
Backward lookup for entry. final T
backwardOrNull(R value)
Backward lookup for entry. final Boolean
remove(T entity, R value)
Removes relation A -> B if it exists. final Boolean
remove(Pair<T, R> element)
Removes relation A -> B if it exists. final Boolean
removeForward(T entity)
Removes by forward lookup. final Boolean
removeBackward(R value)
Removes by backward lookup. final Boolean
contains(T entity, R value)
Returns whether relation A -> B exists in this map. final Boolean
contains(Pair<T, R> pair)
Returns whether relation A -> B exists in this map. final Boolean
containsForward(T entity)
Returns whether a relation A -> * exists. final Boolean
containsBackward(R value)
Returns whether a relation * -> B exists. final Set<T>
getDomain()
Returns the domain of this map as a set. final Set<R>
getCoDomain()
Returns the coDomain of this map as a set. final Unit
clear()
Clears the map. final Boolean
isEmpty()
Returns whether this map contains no elements. final Boolean
isNotEmpty()
Returns whether this map contains elements. -
-
Method Detail
-
add
final Boolean add(T entity, R value)
Adds a relation A -> B if domain does not contain A and coDomain does not contain B. Returns
false
if the relation could not be added.
-
add
final Boolean add(Pair<T, R> element)
Adds a relation A -> B if domain does not contain A and coDomain does not contain B. Returns
false
if the relation could not be added.
-
addAll
final Boolean addAll(Pair<T, R> items)
Adds all relations A -> B. If any of the given items already exist, it gets ignored. If any item contains a key or value that already exists, the map remains unchanged.
Example: Map: (A->B), (C->D)
addAll(E->F),(G->H) results in (A->B), (C->D), (E->F),(G->H) : true
addAll(A->B),(E->F) results in (A->B), (C->D), (E->F), : true
addAll(A->C),(E->F) results in (A->B), (C->D) : false
-
forwardOrNull
final R forwardOrNull(T entity)
Forward lookup for entry.
- Parameters:
entity
- Relation key.
-
backwardOrNull
final T backwardOrNull(R value)
Backward lookup for entry.
- Parameters:
value
- Relation value.
-
remove
final Boolean remove(T entity, R value)
Removes relation A -> B if it exists.
- Parameters:
entity
- Relation key A.value
- Relation value B.
-
remove
final Boolean remove(Pair<T, R> element)
Removes relation A -> B if it exists.
- Parameters:
element
- Pair (Relation key A, Relation value B)
-
removeForward
final Boolean removeForward(T entity)
Removes by forward lookup. Removes relation A -> * if it exists.
- Parameters:
entity
- Relation key A.
-
removeBackward
final Boolean removeBackward(R value)
Removes by backward lookup. Removes relation * -> B.
- Parameters:
value
- Relation value B.
-
contains
final Boolean contains(T entity, R value)
Returns whether relation A -> B exists in this map.
- Parameters:
entity
- Relation key A.value
- Relation value B.
-
contains
final Boolean contains(Pair<T, R> pair)
Returns whether relation A -> B exists in this map.
- Parameters:
pair
- Relation pair A -> B.
-
containsForward
final Boolean containsForward(T entity)
Returns whether a relation A -> * exists.
- Parameters:
entity
- Relation key A.
-
containsBackward
final Boolean containsBackward(R value)
Returns whether a relation * -> B exists.
- Parameters:
value
- Relation value B.
-
getCoDomain
final Set<R> getCoDomain()
Returns the coDomain of this map as a set.
-
isNotEmpty
final Boolean isNotEmpty()
Returns whether this map contains elements.
-
-
-
-