File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\PackedMap.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2010, CloudBees, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import com.thoughtworks.xstream.converters.UnmarshallingContext;
27 import com.thoughtworks.xstream.converters.collections.MapConverter;
28 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
29 import com.thoughtworks.xstream.mapper.Mapper;
30
31 import java.util.AbstractList;
32 import java.util.AbstractMap;
33 import java.util.AbstractSet;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.LinkedHashMap;
38 import java.util.Map;
39 import java.util.Set;
40 import java.util.TreeMap;
41
42 /**
43 * Read-only map implementation that uses less memory than {@link HashMap}/{@link TreeMap}.
44 *
45 * <p>
46 * The implementation is backed by a single exact-length array, so this implementation has the following
47 * performance characteristics.
48 *
49 * <ul>
50 * <li>iteration is fast (but creates a lot of short-lived objects.)
51 * <li>lookup is O(N)
52 * <li>memory consumption is low
53 * </ul>
54 *
55 * @author Kohsuke Kawaguchi
56 */
57 @SuppressWarnings({"unchecked"})
58 public final class PackedMap<K,V> extends AbstractMap<K,V> {
59 private Object[] kvpairs;
60
61 /**
62 *
63 * @param src
64 * Map to copy contents from. Iteration order is preserved.
65 */
66 public static <K,V> PackedMap<K,V> of(Map<? extends K,? extends V> src) {
67 return new PackedMap<K, V>(src);
68 }
69
70 private PackedMap(Map<? extends K,? extends V> src) {
71 kvpairs = new Object[src.size()*2];
72 int i=0;
73 for (Entry<? extends K, ? extends V> e : src.entrySet()) {
74 kvpairs[i++] = e.getKey();
75 kvpairs[i++] = e.getValue();
76 }
77 }
78
79 private final Set<Entry<K,V>> entrySet = new AbstractSet<Entry<K, V>>() {
80 @Override
81 public Iterator<Entry<K, V>> iterator() {
82 return new Iterator<Entry<K, V>>() {
83 int index=0;
84 public boolean hasNext() {
85 return index<kvpairs.length;
86 }
87
88 @SuppressWarnings({"unchecked"})
89 public Entry<K, V> next() {
90 final K k = (K)kvpairs[index++];
91 final V v = (V)kvpairs[index++];
92 return new Entry<K, V>() {
93 public K getKey() {
94 return k;
95 }
96
97 public V getValue() {
98 return v;
99 }
100
101 public V setValue(V value) {
102 throw new UnsupportedOperationException();
103 }
104 };
105 }
106
107 public void remove() {
108 throw new UnsupportedOperationException();
109 }
110 };
111 }
112
113 @Override
114 public int size() {
115 return kvpairs.length/2;
116 }
117 };
118
119 @Override
120 public Set<Entry<K, V>> entrySet() {
121 return entrySet;
122 }
123
124 @Override
125 public boolean containsKey(Object key) {
126 for (int i=0; i<kvpairs.length; i+=2)
127 if (key.equals(kvpairs[i]))
128 return true;
129 return false;
130 }
131
132 @Override
133 public V get(Object key) {
134 for (int i=0; i<kvpairs.length; i+=2)
135 if (key.equals(kvpairs[i]))
136 return (V)kvpairs[i+1];
137 return null;
138 }
139
140 @Override
141 public Collection<V> values() {
142 return new AbstractList<V>() {
143 @Override
144 public V get(int index) {
145 return (V)kvpairs[index*2];
146 }
147
148 @Override
149 public int size() {
150 return PackedMap.this.size();
151 }
152 };
153 }
154
155 /**
156 * Should persist like a regular map.
157 */
158 public static class ConverterImpl extends MapConverter {
159 public ConverterImpl(Mapper mapper) {
160 super(mapper);
161 }
162
163 @Override
164 public boolean canConvert(Class type) {
165 return type==PackedMap.class;
166 }
167
168 @Override
169 protected Object createCollection(Class type) {
170 return new LinkedHashMap<String,String>();
171 }
172
173 @Override
174 public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
175 return PackedMap.of((Map)super.unmarshal(reader, context));
176 }
177 }
178 }
179