View Javadoc
1   package de.fhdw.wtf.context.model.collections;
2   
3   import de.fhdw.wtf.context.model.Anything;
4   import de.fhdw.wtf.persistence.meta.UserObject;
5   
6   /**
7    * This class represents Maps. It contains 3 valued associations with one value K as key and another value V as value.
8    * Values can be accessed via the key.
9    * 
10   * @param <K>
11   *            Type of key elements.
12   * @param <V>
13   *            Type of value elements.
14   */
15  public class PersistentMap<K extends Anything, V extends Anything> extends MutableMap<K, V> {
16  	
17  	/**
18  	 * Manages the links of this persistent map.
19  	 */
20  	private transient PersistentMapWithKeyValueLinks<Anything, Anything> links;
21  	/**
22  	 * Name of the underlying database association representing the 0..* relation.
23  	 */
24  	public static final String linksAssociationName = "links";
25  	
26  	/**
27  	 * Creates an empty PersistentMap.
28  	 */
29  	public PersistentMap() {
30  		this.initLinks();
31  	}
32  	
33  	/**
34  	 * Loads object from database.
35  	 * 
36  	 * @param userObject
37  	 *            The underlying user object.
38  	 */
39  	public PersistentMap(final UserObject userObject) {
40  		super(userObject);
41  		this.initLinks();
42  	}
43  	
44  	/**
45  	 * Initializes the links. As this field is transient, this has to be done by both constructors.
46  	 */
47  	private void initLinks() {
48  		final String assocName = PersistentMap.class.getName() + "." + linksAssociationName;
49  		this.links = new PersistentMapWithKeyValueLinks<>(this.getObject(), assocName);
50  	}
51  	
52  	@Override
53  	public void put(final K key, final V value) {
54  		this.links.put(key, value);
55  	}
56  	
57  	@SuppressWarnings("unchecked")
58  	@Override
59  	public V get(final K key) {
60  		return (V) this.links.get(key);
61  	}
62  	
63  	@SuppressWarnings("unchecked")
64  	@Override
65  	public V remove(final K key) {
66  		return (V) this.links.remove(key);
67  	}
68  }