Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

343
LINES

< > BotCompany Repo | #1031043 // CompactAbstractMap - two fields less than AbstractMap [i.e. NO fields at all]

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (4221L/24K).

1  
asclass CompactAbstractMap<K, V> implements Map<K, V> {
2  
  public int size() {
3  
      return entrySet().size();
4  
  }
5  
6  
  public boolean isEmpty() {
7  
      return size() == 0;
8  
  }
9  
10  
  public boolean containsValue(Object value) {
11  
      Iterator<Entry<K, V>> i = entrySet().iterator();
12  
      if (value == null) {
13  
          while (i.hasNext()) {
14  
              Entry<K, V> e = i.next();
15  
              if (e.getValue() == null)
16  
                  return true;
17  
          }
18  
      } else {
19  
          while (i.hasNext()) {
20  
              Entry<K, V> e = i.next();
21  
              if (value.equals(e.getValue()))
22  
                  return true;
23  
          }
24  
      }
25  
      return false;
26  
  }
27  
28  
  public boolean containsKey(Object key) {
29  
      Iterator<Entry<K, V>> i = entrySet().iterator();
30  
      if (key == null) {
31  
          while (i.hasNext()) {
32  
              Entry<K, V> e = i.next();
33  
              if (e.getKey() == null)
34  
                  return true;
35  
          }
36  
      } else {
37  
          while (i.hasNext()) {
38  
              Entry<K, V> e = i.next();
39  
              if (key.equals(e.getKey()))
40  
                  return true;
41  
          }
42  
      }
43  
      return false;
44  
  }
45  
46  
  public V get(Object key) {
47  
      Iterator<Entry<K, V>> i = entrySet().iterator();
48  
      if (key == null) {
49  
          while (i.hasNext()) {
50  
              Entry<K, V> e = i.next();
51  
              if (e.getKey() == null)
52  
                  return e.getValue();
53  
          }
54  
      } else {
55  
          while (i.hasNext()) {
56  
              Entry<K, V> e = i.next();
57  
              if (key.equals(e.getKey()))
58  
                  return e.getValue();
59  
          }
60  
      }
61  
      return null;
62  
  }
63  
64  
  public V put(K key, V value) {
65  
      throw new UnsupportedOperationException();
66  
  }
67  
68  
  public V remove(Object key) {
69  
      Iterator<Entry<K, V>> i = entrySet().iterator();
70  
      Entry<K, V> correctEntry = null;
71  
      if (key == null) {
72  
          while (correctEntry == null && i.hasNext()) {
73  
              Entry<K, V> e = i.next();
74  
              if (e.getKey() == null)
75  
                  correctEntry = e;
76  
          }
77  
      } else {
78  
          while (correctEntry == null && i.hasNext()) {
79  
              Entry<K, V> e = i.next();
80  
              if (key.equals(e.getKey()))
81  
                  correctEntry = e;
82  
          }
83  
      }
84  
85  
      V oldValue = null;
86  
      if (correctEntry != null) {
87  
          oldValue = correctEntry.getValue();
88  
          i.remove();
89  
      }
90  
      return oldValue;
91  
  }
92  
93  
  public void putAll(Map<? extends K, ? extends V> m) {
94  
      for (Entry<? extends K, ? extends V> e : m.entrySet())
95  
          put(e.getKey(), e.getValue());
96  
  }
97  
98  
  public void clear() {
99  
      entrySet().clear();
100  
  }
101  
102  
  public Set<K> keySet() {
103  
      return new AbstractSet<K>() {
104  
          public Iterator<K> iterator() {
105  
              return new Iterator<K>() {
106  
                  private Iterator<Entry<K, V>> i = entrySet().iterator();
107  
108  
                  public boolean hasNext() {
109  
                      return i.hasNext();
110  
                  }
111  
112  
                  public K next() {
113  
                      return i.next().getKey();
114  
                  }
115  
116  
                  public void remove() {
117  
                      i.remove();
118  
                  }
119  
              };
120  
          }
121  
122  
          public int size() {
123  
              return CompactAbstractMap.this.size();
124  
          }
125  
126  
          public boolean isEmpty() {
127  
              return CompactAbstractMap.this.isEmpty();
128  
          }
129  
130  
          public void clear() {
131  
              CompactAbstractMap.this.clear();
132  
          }
133  
134  
          public boolean contains(Object k) {
135  
              return CompactAbstractMap.this.containsKey(k);
136  
          }
137  
      };
138  
  }
139  
140  
  public Collection<V> values() {
141  
      return new AbstractCollection<V>() {
142  
          public Iterator<V> iterator() {
143  
              return new Iterator<V>() {
144  
                  private Iterator<Entry<K, V>> i = entrySet().iterator();
145  
146  
                  public boolean hasNext() {
147  
                      return i.hasNext();
148  
                  }
149  
150  
                  public V next() {
151  
                      return i.next().getValue();
152  
                  }
153  
154  
                  public void remove() {
155  
                      i.remove();
156  
                  }
157  
              };
158  
          }
159  
160  
          public int size() {
161  
              return CompactAbstractMap.this.size();
162  
          }
163  
164  
          public boolean isEmpty() {
165  
              return CompactAbstractMap.this.isEmpty();
166  
          }
167  
168  
          public void clear() {
169  
              CompactAbstractMap.this.clear();
170  
          }
171  
172  
          public boolean contains(Object v) {
173  
              return CompactAbstractMap.this.containsValue(v);
174  
          }
175  
      };
176  
  }
177  
178  
  public abstract Set<Entry<K, V>> entrySet();
179  
180  
  public boolean equals(Object o) {
181  
      if (o == this)
182  
          return true;
183  
184  
      if (!(o instanceof Map))
185  
          return false;
186  
      Map<?, ?> m = (Map<?, ?>) o;
187  
      if (m.size() != size())
188  
          return false;
189  
190  
      try {
191  
          for (Entry<K, V> e : entrySet()) {
192  
              K key = e.getKey();
193  
              V value = e.getValue();
194  
              if (value == null) {
195  
                  if (!(m.get(key) == null && m.containsKey(key)))
196  
                      return false;
197  
              } else {
198  
                  if (!value.equals(m.get(key)))
199  
                      return false;
200  
              }
201  
          }
202  
      } catch (ClassCastException unused) {
203  
          return false;
204  
      } catch (NullPointerException unused) {
205  
          return false;
206  
      }
207  
208  
      return true;
209  
  }
210  
211  
  public int hashCode() {
212  
      int h = 0;
213  
      for (Entry<K, V> entry : entrySet())
214  
          h += entry.hashCode();
215  
      return h;
216  
  }
217  
218  
  public String toString() {
219  
      Iterator<Entry<K, V>> i = entrySet().iterator();
220  
      if (!i.hasNext())
221  
          return "{}";
222  
223  
      StringBuilder sb = new StringBuilder();
224  
      sb.append('{');
225  
      for (; ; ) {
226  
          Entry<K, V> e = i.next();
227  
          K key = e.getKey();
228  
          V value = e.getValue();
229  
          sb.append(key == this ? "(this Map)" : key);
230  
          sb.append('=');
231  
          sb.append(value == this ? "(this Map)" : value);
232  
          if (!i.hasNext())
233  
              return sb.append('}').toString();
234  
          sb.append(',').append(' ');
235  
      }
236  
  }
237  
238  
  protected Object clone() throws CloneNotSupportedException {
239  
      CompactAbstractMap<?, ?> result = (CompactAbstractMap<?, ?>) super.clone();
240  
      return result;
241  
  }
242  
243  
  public static class SimpleEntry<K, V>
244  
          implements Entry<K, V>, java.io.Serializable {
245  
      @java.io.Serial
246  
      private static final long serialVersionUID = -8499721149061103585L;
247  
248  
      @SuppressWarnings("serial")
249  
      private final K key;
250  
      @SuppressWarnings("serial")
251  
      private V value;
252  
253  
      public SimpleEntry(K key, V value) {
254  
          this.key = key;
255  
          this.value = value;
256  
      }
257  
258  
      public SimpleEntry(Entry<? extends K, ? extends V> entry) {
259  
          this.key = entry.getKey();
260  
          this.value = entry.getValue();
261  
      }
262  
263  
      public K getKey() {
264  
          return key;
265  
      }
266  
267  
      public V getValue() {
268  
          return value;
269  
      }
270  
271  
      public V setValue(V value) {
272  
          V oldValue = this.value;
273  
          this.value = value;
274  
          return oldValue;
275  
      }
276  
277  
      public boolean equals(Object o) {
278  
          if (!(o instanceof Map.Entry))
279  
              return false;
280  
          Entry<?, ?> e = (Entry<?, ?>) o;
281  
          return eq(key, e.getKey()) && eq(value, e.getValue());
282  
      }
283  
284  
      public int hashCode() {
285  
          return (key == null ? 0 : key.hashCode()) ^
286  
                  (value == null ? 0 : value.hashCode());
287  
      }
288  
289  
      public String toString() {
290  
          return key + "=" + value;
291  
      }
292  
293  
  }
294  
295  
  public static class SimpleImmutableEntry<K, V>
296  
          implements Entry<K, V>, java.io.Serializable {
297  
      @java.io.Serial
298  
      private static final long serialVersionUID = 7138329143949025153L;
299  
300  
      @SuppressWarnings("serial")
301  
      private final K key;
302  
      @SuppressWarnings("serial")
303  
      private final V value;
304  
305  
      public SimpleImmutableEntry(K key, V value) {
306  
          this.key = key;
307  
          this.value = value;
308  
      }
309  
310  
      public SimpleImmutableEntry(Entry<? extends K, ? extends V> entry) {
311  
          this.key = entry.getKey();
312  
          this.value = entry.getValue();
313  
      }
314  
315  
      public K getKey() {
316  
          return key;
317  
      }
318  
319  
      public V getValue() {
320  
          return value;
321  
      }
322  
323  
      public V setValue(V value) {
324  
          throw new UnsupportedOperationException();
325  
      }
326  
327  
      public boolean equals(Object o) {
328  
          if (!(o instanceof Map.Entry))
329  
              return false;
330  
          Entry<?, ?> e = (Entry<?, ?>) o;
331  
          return eq(key, e.getKey()) && eq(value, e.getValue());
332  
      }
333  
334  
      public int hashCode() {
335  
          return (key == null ? 0 : key.hashCode()) ^
336  
                  (value == null ? 0 : value.hashCode());
337  
      }
338  
339  
      public String toString() {
340  
          return key + "=" + value;
341  
      }
342  
  }
343  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 10 computer(s): bhatertpkbcr, ekrmjmnbrukm, elmgxqgtpvxh, gjtlkbvenryc, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, vouqrxazstgt, wnsclhtenguj

No comments. add comment

Snippet ID: #1031043
Snippet name: CompactAbstractMap - two fields less than AbstractMap [i.e. NO fields at all]
Eternal ID of this version: #1031043/4
Text MD5: 89711cf6ebff563a8c410bec75e8577a
Transpilation MD5: fd07164415e6f0d2ff851df52abf40a2
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-07 14:26:56
Source code size: 9289 bytes / 343 lines
Pitched / IR pitched: No / No
Views / Downloads: 155 / 3505
Version history: 3 change(s)
Referenced in: [show references]