IKVM11  11
Java SE 11 Virtual Machine for .NET
Loading...
Searching...
No Matches
MapWrapper.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Linq;
5
6using java.lang;
7
8namespace java.util
9{
10
11 class MapWrapper<TKey, TValue> : IDictionary<TKey, TValue>
12 {
13
14 readonly Map _map;
15
21 public MapWrapper(Map map)
22 {
23 _map = map ?? throw new ArgumentNullException(nameof(map));
24 }
25
27 public TValue this[TKey key]
28 {
29 get => (TValue)_map.get(key);
30 set => _map.put(key, value);
31 }
32
34 public ICollection<TKey> Keys => _map.keySet().AsCollection<TKey>();
35
37 public ICollection<TValue> Values => _map.values().AsCollection<TValue>();
38
40 public int Count => _map.size();
41
43 public bool IsReadOnly => false;
44
46 public void Add(TKey key, TValue value) => _map.put(key, value);
47
49 public void Add(KeyValuePair<TKey, TValue> item) => _map.put(item.Key, item.Value);
50
52 public void Clear() => _map.clear();
53
55 public bool Contains(KeyValuePair<TKey, TValue> item)
56 {
57 return _map.containsKey(item.Key) && _map.get(item.Key).Equals(item.Value);
58 }
59
61 public bool ContainsKey(TKey key)
62 {
63 return _map.containsKey(key);
64 }
65
67 public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
68 {
69 foreach (var entry in this)
70 array[arrayIndex++] = entry;
71 }
72
74 public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
75 {
76 return _map.entrySet().AsEnumerable<Map.Entry>().Select(i => new KeyValuePair<TKey, TValue>((TKey)i.getKey(), (TValue)i.getValue())).GetEnumerator();
77 }
78
80 public bool Remove(TKey key)
81 {
82 if (_map.containsKey(key))
83 {
84 _map.remove(key);
85 return true;
86 }
87
88 return false;
89 }
90
92 public bool Remove(KeyValuePair<TKey, TValue> item)
93 {
94 return _map.remove(item.Key, item.Value);
95 }
96
98 public bool TryGetValue(TKey key, out TValue value)
99 {
100 if (_map.containsKey(key))
101 {
102 value = (TValue) _map.get(key);
103 return true;
104 }
105
106 value = default!;
107 return false;
108 }
109
111 IEnumerator IEnumerable.GetEnumerator()
112 {
113 return GetEnumerator();
114 }
115
116 }
117
118}
void CopyTo(KeyValuePair< TKey, TValue >[] array, int arrayIndex)
Definition MapWrapper.cs:67
MapWrapper(Map map)
Initializes a new instance.
Definition MapWrapper.cs:21
bool TryGetValue(TKey key, out TValue value)
Definition MapWrapper.cs:98
bool Remove(KeyValuePair< TKey, TValue > item)
Definition MapWrapper.cs:92
bool Contains(KeyValuePair< TKey, TValue > item)
Definition MapWrapper.cs:55
void Add(TKey key, TValue value)
ICollection< TValue > Values
Definition MapWrapper.cs:37
ICollection< TKey > Keys
Definition MapWrapper.cs:34
bool Remove(TKey key)
Definition MapWrapper.cs:80
bool ContainsKey(TKey key)
Definition MapWrapper.cs:61
void Add(KeyValuePair< TKey, TValue > item)
IEnumerator< KeyValuePair< TKey, TValue > > GetEnumerator()
Definition MapWrapper.cs:74