File: ActiveCachedArrayList.java

package info (click to toggle)
libjide-oss-java 3.7.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 13,444 kB
  • sloc: java: 91,177; xml: 661; makefile: 35
file content (139 lines) | stat: -rw-r--r-- 3,686 bytes parent folder | download | duplicates (5)
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
package com.jidesoft.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;


public class ActiveCachedArrayList<E> extends ArrayList<E> {
    private HashMap<Object, Integer> _indexCache;
    private boolean lazyCache = false;

    public ActiveCachedArrayList() {
        this(false);
    }

    public ActiveCachedArrayList(boolean inStatus) {
        this.lazyCache = inStatus;
        this.recache();
    }

    public ActiveCachedArrayList(Collection<? extends E> c) {
        super(c);
        this.recache();
    }

    public ActiveCachedArrayList(int initialCapacity) {
        super(initialCapacity);
        this.recache();
    }

    @Override
    public int indexOf(Object elem) {
        if (_indexCache == null) {
            _indexCache = new HashMap();
        }
        Integer o = _indexCache.get(elem);
        if (o != null) {
            return o;
        }
        else {
            int i = super.indexOf(elem);
            _indexCache.put(elem, i);
            return i;
        }
    }

    @Override
    public boolean add(E o) {
        boolean returnB = super.add(o);
        if (lazyCache)
            this.invalidateCache();
        else
            this.addCache(o);
        return returnB;
    }

    @Override
    public void add(int index, E element) {
        super.add(index, element);
        if (lazyCache)
            this.invalidateCache();
        else
            this.recache();
    }

    @Override
    public E remove(int index) {
        E returnE = super.remove(index);
        if (lazyCache) this.invalidateCache();
        else this.recache();
        return returnE;
    }

    @Override
    public boolean remove(Object o) {
        boolean returnB = super.remove(o);
        if (lazyCache) this.invalidateCache();
        else this.recache();
        return returnB;
    }

    @Override
    public boolean addAll(Collection<? extends E> c) {
        boolean returnB = super.addAll(c);
        if (lazyCache) this.invalidateCache();
        else this.recache();
        return returnB;
    }

    @Override
    public boolean addAll(int index, Collection<? extends E> c) {
        boolean returnB = super.addAll(index, c);
        if (lazyCache) this.invalidateCache();
        else this.recache();
        return returnB;
    }

    @Override
    public E set(int index, E element) {
        E returnE = super.set(index, element);
        if (lazyCache) this.invalidateCache();
        else this.recache();
        return returnE;
    }

    @Override
    public void removeRange(int frontIndex, int toIndex) {
        super.removeRange(frontIndex, toIndex);
        if (lazyCache) this.invalidateCache();
        else this.recache();
    }

    public void invalidateCache() {
        _indexCache = null;
    }

    public void recache() {
        _indexCache = new HashMap();
        Integer i = 0;
        for (Object elem : this) {
            _indexCache.put(elem, i);
            i++;
        }
    }

    // if value was appended to the end of the cache
    // add to _indexCache if it is first occurence
    // if _indexCache already has a value, that means there is an earlier occurence
    // since ArrayList.indexOf() returns the first occurence, it should return the earlier one
    public void addCache(E o) {
        if (_indexCache.get(o) != null)
            _indexCache.put(o, _indexCache.size());
    }

    public void setLazy(boolean inStatus) {
        this.lazyCache = inStatus;
        if (inStatus == false) this.recache();
    }
}