summaryrefslogtreecommitdiff
path: root/java/src/Freeze/SubMap.java
blob: f7b9f94ea6c885db3d8b1b657cef8b605067981d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// **********************************************************************
//
// Copyright (c) 2003-2009 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

package Freeze;

//
// Sub-map of a Freeze Map or of another submap
//
//
// When it's based of an index, the key is the index key and the value
// is a Set of Map.Entry.
//

class SubMap extends java.util.AbstractMap implements java.util.SortedMap
{
    private class IndexValue extends java.util.AbstractSet
    {
        public java.util.Iterator
        iterator()
        {
            return _index.untypedFind(_myKey, true);
        }
               
        public int
        size()
        {
            return _index.untypedCount(_myKey);
        }

        public boolean equals(Object o)
        {
            if(o instanceof IndexValue)
            {
                IndexValue indexValue = (IndexValue)o;
                return indexValue._myKey.equals(_myKey);
            }
            else
            {
                return false;
            }
        }

        public int hashCode()
        {
            return _myKey.hashCode();
        }
        
        private IndexValue(Object key)
        {
            _myKey = key;
        }

        private Object
        getKey()
        {
            return _myKey;
        }

        private Object _myKey;
    }

    private class IndexEntry implements java.util.Map.Entry
    {
        public Object getKey()
        {
            return _value.getKey();
        }

        public Object getValue()
        {
            return _value;
        }
     
        public Object setValue(Object value)
        {
            throw new UnsupportedOperationException();
        } 

        public boolean equals(Object o)
        {
            if(o instanceof IndexEntry)
            {
                IndexEntry indexEntry = (IndexEntry)o;
                return indexEntry._value.equals(_value);
            }
            else
            {
                return false;
            }
        }

        public int hashCode()
        {
            return _value.hashCode();
        }
        
        SubMap parent()
        {
            return SubMap.this;
        }

        private IndexEntry(Object key)
        {
            _value = new IndexValue(key);
        }

        private IndexValue _value;
    }

    private class IndexIterator implements Map.EntryIterator
    {
        public boolean hasNext()
        {
            return _iterator.hasNext();
        }
       
        public Object next()
        {
            Map.Entry entry = (Map.Entry)_iterator.next();
            return new IndexEntry(_index.decodeKey(entry.getIndexBytes(),
                                                   _map.connection().communicator()));
        }
        
        public void remove()
        {
            _iterator.remove();
        }
        
        public void close()
        {
            _iterator.close();
        }

        public void destroy()
        {
            close();
        }
        
        private IndexIterator()
        {
            assert _index != null;
            _iterator = _map.createIterator(_index, _fromKey, _toKey);
        }

        Map.EntryIterator _iterator;
    }

    SubMap(Map map, Object fromKey, Object toKey)
    {
        _fromKey = fromKey;
        _toKey = toKey;
        _map = map;
        _index = null;

        if(fromKey != null && toKey != null)
        {
            if(map.comparator().compare(fromKey, toKey) >= 0)
            {
                throw new IllegalArgumentException();
            }
        }
    }
    
    SubMap(Map.Index index, Object fromKey, Object toKey)
    {
        _fromKey = fromKey;
        _toKey = toKey;
        _map = index.parent();
        _index = index;

        if(fromKey != null && toKey != null)
        {
            if(index.comparator().compare(fromKey, toKey) >= 0)
            {
                throw new IllegalArgumentException();
            }
        }
    }
    
    private SubMap(SubMap subMap, Object fromKey, Object toKey)
    {
        _fromKey = fromKey;
        _toKey = toKey;
        _map = subMap._map;
        _index = subMap._index;

        if(fromKey != null && toKey != null)
        {
            if(comparator().compare(fromKey, toKey) >= 0)
            {
                throw new IllegalArgumentException();
            }
        }
    }

    //
    // SortedMap methods
    //
    public java.util.Comparator comparator()
    {
        if(_index != null)
        {
            return _index.comparator();
        }
        else
        {
            return _map.comparator();
        }
    }

    public Object firstKey()
    {
        return _index != null ?
            _index.firstKey(_fromKey, _toKey) :
            _map.firstKey(_fromKey, _toKey);
    }

    public Object lastKey()
    {
        return _index != null ?
            _index.lastKey(_fromKey, _toKey) :
            _map.lastKey(_fromKey, _toKey);
    }

    public java.util.SortedMap headMap(Object toKey)
    {
        if(toKey == null)
        {
            throw new NullPointerException();
        }
        return new SubMap(this, _fromKey, toKey);
        
    }
    
    public java.util.SortedMap tailMap(Object fromKey)
    {
        if(fromKey == null)
        {
            throw new NullPointerException();
        }
        return new SubMap(this, fromKey, _toKey);
    } 
   
    public java.util.SortedMap subMap(Object fromKey, Object toKey)
    {
        if(fromKey == null || toKey == null )
        {
            throw new NullPointerException();
        }
        return new SubMap(this, fromKey, toKey);
    }

    //
    // java.util.Map methods
    //
    public java.util.Set
    entrySet()
    {
        if(_entrySet == null)
        {
            _entrySet = new java.util.AbstractSet()
            {
                public java.util.Iterator
                iterator()
                {
                    if(_index == null)
                    {
                        return _map.createIterator(_index, _fromKey, _toKey);
                    }
                    else
                    {
                        return new IndexIterator();
                    }
                }
                
                public boolean
                contains(Object o)
                {
                    if(_index == null)
                    {
                        //
                        // If the main map contains this object, check it's within [fromKey, toKey[
                        //
                        if(_map.entrySet().contains(o))
                        {
                            Map.Entry entry = (Map.Entry)o;
                            return inRange(entry.getKey());
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        if(o instanceof IndexEntry)
                        {
                            IndexEntry indexEntry = (IndexEntry)o;
                            return indexEntry.parent() == SubMap.this && 
                                _index.containsKey(indexEntry.getKey());
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                        
                public boolean
                remove(Object o)
                {
                    if(_index == null)
                    {
                        if(o instanceof Map.Entry)
                        {
                            Map.Entry entry = (Map.Entry)o;
                            return inRange(entry.getKey()) && _map.entrySet().remove(o);
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        //
                        // Not yet implemented, should remove all objects that
                        // match this index-key
                        //
                        throw new UnsupportedOperationException();
                    }
                }
                
                public int
                size()
                {
                    throw new UnsupportedOperationException(); 
                }
                
                public boolean
                isEmpty()
                {
                    try
                    {
                        firstKey();
                        return false;
                    }
                    catch(NoSuchElementException e)
                    {
                        return true;
                    }
                }
                };
        }
        return _entrySet;
    }
    
    //
    // Put is not implemented (you have to put in the main map view)
    //


    public boolean constainsKey(Object key)
    {
        if(!inRange(key))
        {
            return false;
        }

        //
        // Then check if it's in the map
        //
        if(_index == null)
        {
            return _map.containsKey(key);
        }
        else
        {
            return _index.containsKey(key);
        }
    }
    
    
    public Object
    get(Object key)
    {
        if(!inRange(key))
        {
            return null;
        }
        
        if(_index == null)
        {
            return _map.get(key);
        }
        else
        {
            if(_index.containsKey(key))
            {
                return new IndexValue(key);
            }
            else
            {
                return null;
            }
        }
    }
    
    public Object
    remove(Object key)
    {
        if(!inRange(key))
        {
            return null;
        }
        
        if(_index == null)
        {
            return _map.remove(key);
        }
        else
        {
            //
            // Not yet implemented
            //
            throw new UnsupportedOperationException();
        }
    }

    public boolean
    fastRemove(Object key)
    {
        if(!inRange(key))
        {
            return false;
        }
        
        if(_index == null)
        {
            return _map.fastRemove(key);
        }
        else
        {
            //
            // Not yet implemented
            //
            throw new UnsupportedOperationException();
        }
    }

    
    private boolean inRange(Object key)
    {
        if(_fromKey != null && comparator().compare(_fromKey, key) > 0)
        {
            return false;
        }
        if(_toKey != null && comparator().compare(key, _toKey) >= 0)
        {
            return false;
        }
        return true;
    }

    private final Object _fromKey;
    private final Object _toKey;
    private final Map _map;
    private final Map.Index _index;
    private java.util.Set _entrySet;
}