OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
180 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils;
15
 
16
import java.util.ArrayList;
17
import java.util.Iterator;
18
import java.util.Map;
19
import java.util.Set;
20
 
21
/**
22
 * Map optimisée pour les petites tailles (<50).
23
 *
24
 * Empreinte mémoire divisée 4 par rapport à HashMap.
25
 *
26
 * N'implemente pas Map pour ne pas exposer des méthodes sous-optimales
27
 *
28
 */
29
public class QuickOrderedMap<K, V> {
30
 
31
    private final ArrayList<Object> keysAndValues;
32
 
33
    public QuickOrderedMap() {
34
        this(10);
35
    }
36
 
37
    public QuickOrderedMap(int initialCapacity) {
38
        this.keysAndValues = new ArrayList<>(initialCapacity * 2);
39
    }
40
 
41
    public int size() {
42
        return this.keysAndValues.size() / 2;
43
    }
44
 
45
    public boolean isEmpty() {
46
        return this.keysAndValues.isEmpty();
47
    }
48
 
49
    public boolean containsKey(K key) {
50
        final int size = this.keysAndValues.size();
51
        for (int i = 0; i < size; i += 2) {
52
            if (this.keysAndValues.get(i).equals(key)) {
53
                return true;
54
            }
55
        }
56
        return false;
57
    }
58
 
59
    public boolean containsValue(V value) {
60
        final int size = this.keysAndValues.size();
61
        for (int i = 1; i < size; i += 2) {
62
            if (this.keysAndValues.get(i).equals(value)) {
63
                return true;
64
            }
65
        }
66
        return false;
67
    }
68
 
69
    @SuppressWarnings("unchecked")
70
    public K getKey(int index) {
71
        return (K) this.keysAndValues.get(index * 2);
72
    }
73
 
74
    @SuppressWarnings("unchecked")
75
    public V getValue(int index) {
76
        return (V) this.keysAndValues.get(1 + index * 2);
77
    }
78
 
79
    @SuppressWarnings("unchecked")
80
    public V get(K key) {
81
        final int size = this.keysAndValues.size();
82
        for (int i = 0; i < size; i += 2) {
83
            if (this.keysAndValues.get(i).equals(key)) {
84
 
85
                return (V) this.keysAndValues.get(i + 1);
86
            }
87
        }
88
        return null;
89
    }
90
 
91
    @SuppressWarnings("unchecked")
92
    public V put(K key, V value) {
93
        final int size = this.keysAndValues.size();
94
        for (int i = 0; i < size; i += 2) {
95
            if (this.keysAndValues.get(i).equals(key)) {
96
                final Object old = this.keysAndValues.get(i + 1);
97
                this.keysAndValues.set(i + 1, value);
98
                return (V) old;
99
            }
100
        }
101
        this.keysAndValues.add(key);
102
        this.keysAndValues.add(value);
103
        return null;
104
    }
105
 
106
    @SuppressWarnings("unchecked")
107
    public V remove(K key) {
108
        final int size = this.keysAndValues.size();
109
        for (int i = 0; i < size; i += 2) {
110
            if (this.keysAndValues.get(i).equals(key)) {
111
                this.keysAndValues.remove(i);
112
                return (V) this.keysAndValues.remove(i);
113
            }
114
        }
115
        return null;
116
    }
117
 
118
    public void putAll(Map<? extends K, ? extends V> m) {
119
        final Set<? extends K> keySet = m.keySet();
120
        if (!isEmpty()) {
121
            for (Iterator<? extends K> iterator = keySet.iterator(); iterator.hasNext();) {
122
                K key = iterator.next();
123
                put(key, m.get(key));
124
            }
125
        } else {
126
            for (Iterator<? extends K> iterator = keySet.iterator(); iterator.hasNext();) {
127
                K key = iterator.next();
128
                this.keysAndValues.add(key);
129
                this.keysAndValues.add(m.get(key));
130
            }
131
        }
132
    }
133
 
134
    public void clear() {
135
        this.keysAndValues.clear();
136
    }
137
 
138
}