Skip to content

Method: get(Object)

1: /**
2: * Copyright (C) 2016 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.adapters;
16:
17: import java.lang.reflect.Field;
18: import java.util.*;
19:
20: import cz.cvut.kbss.jopa.sessions.UnitOfWorkImpl;
21:
22: public class IndirectMap<K, V> extends IndirectCollection<Map<K, V>> implements Map<K, V> {
23:
24: private final Map<K, V> internalMap;
25:
26: /**
27: * No-arg constructor to support clone building
28: */
29: IndirectMap() {
30: this.internalMap = new HashMap<>();
31: }
32:
33: public IndirectMap(Object owner, Field f, UnitOfWorkImpl persistenceContext, Map<K, V> referencedMap) {
34: super(owner, f, persistenceContext);
35: this.internalMap = Objects.requireNonNull(referencedMap);
36: }
37:
38: @Override
39: public Map<K, V> getReferencedCollection() {
40: return internalMap;
41: }
42:
43: @Override
44: public int size() {
45: return internalMap.size();
46: }
47:
48: @Override
49: public boolean isEmpty() {
50: return internalMap.isEmpty();
51: }
52:
53: @Override
54: public boolean containsKey(Object key) {
55: return internalMap.containsKey(key);
56: }
57:
58: @Override
59: public boolean containsValue(Object value) {
60: return internalMap.containsValue(value);
61: }
62:
63: @Override
64: public V get(Object key) {
65: return internalMap.get(key);
66: }
67:
68: @Override
69: public V put(K key, V value) {
70: V val = internalMap.put(key, value);
71: persistChange();
72: return val;
73: }
74:
75: @Override
76: public V remove(Object key) {
77: V val = internalMap.remove(key);
78: if (val != null) {
79: persistChange();
80: }
81: return val;
82: }
83:
84: @Override
85: public void putAll(Map<? extends K, ? extends V> m) {
86: internalMap.putAll(m);
87: if (!m.isEmpty()) {
88: persistChange();
89: }
90: }
91:
92: @Override
93: public void clear() {
94: if (!isEmpty()) {
95: internalMap.clear();
96: persistChange();
97: }
98: }
99:
100: @Override
101: public Set<K> keySet() {
102: return internalMap.keySet();
103: }
104:
105: @Override
106: public Collection<V> values() {
107: return internalMap.values();
108: }
109:
110: @Override
111: public Set<java.util.Map.Entry<K, V>> entrySet() {
112: return internalMap.entrySet();
113: }
114:
115: @Override
116: public boolean equals(Object o) {
117: if (o instanceof Map) {
118: if (o instanceof IndirectMap) {
119: return internalMap.equals(((IndirectMap) o).internalMap);
120: }
121: return internalMap.equals(o);
122: }
123: return false;
124: }
125:
126: @Override
127: public int hashCode() {
128: return internalMap.hashCode();
129: }
130:
131: @Override
132: public String toString() {
133: return internalMap.toString();
134: }
135: }