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
|
/*******************************************************************************
* Copyright (c) 2010, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.osgi.internal.baseadaptor;
import java.util.*;
import org.eclipse.osgi.service.resolver.extras.Sortable;
/**
* Simple map when dealing with small amounts of entries.
* This class also provides a Collections view of the keys
*
* @param <K> The key type
* @param <V> the value type
*/
public class ArrayMap<K, V> implements Collection<K>, Sortable<K> {
final List<K> keys;
final List<V> values;
public ArrayMap(int initialCapacity) {
keys = new ArrayList<K>(initialCapacity);
values = new ArrayList<V>(initialCapacity);
}
/**
* Note that the keys and values are not copied. Changes to this ArrayMap
* will change the values of the keys and values Lists.
* @param keys
* @param values
*/
public ArrayMap(List<K> keys, List<V> values) {
if (keys.size() != values.size())
throw new IllegalArgumentException("Keys and values size must be equal."); //$NON-NLS-1$
this.keys = keys;
this.values = values;
}
public V get(K key) {
int index = keys.indexOf(key);
if (index < 0)
return null;
return values.get(index);
}
public void put(K key, V value) {
int index = keys.indexOf(key);
if (index > 0) {
values.set(index, value);
} else {
keys.add(key);
values.add(value);
}
}
public boolean remove(Object key) {
int index = keys.indexOf(key);
if (index < 0)
return false;
keys.remove(index);
values.remove(index);
return true;
}
public void clear() {
keys.clear();
values.clear();
}
public List<K> getKeys() {
return new ArrayList<K>(keys);
}
public List<V> getValues() {
return new ArrayList<V>(values);
}
public int size() {
return keys.size();
}
public boolean isEmpty() {
return keys.isEmpty();
}
public boolean contains(Object o) {
return keys.contains(o);
}
public Iterator<K> iterator() {
final Iterator<K> keyIter = keys.iterator();
final Iterator<V> valueIter = values.iterator();
return new Iterator<K>() {
public boolean hasNext() {
return keyIter.hasNext();
}
public K next() {
valueIter.next();
return keyIter.next();
}
public void remove() {
valueIter.remove();
keyIter.remove();
}
};
}
public Object[] toArray() {
return keys.toArray();
}
public <T> T[] toArray(T[] a) {
return keys.toArray(a);
}
public boolean add(K o) {
throw new UnsupportedOperationException();
}
public boolean containsAll(Collection<?> c) {
throw new UnsupportedOperationException();
}
public boolean addAll(Collection<? extends K> c) {
throw new UnsupportedOperationException();
}
public boolean removeAll(Collection<?> c) {
boolean result = false;
for (Object key : c)
result |= remove(key);
return result;
}
public boolean retainAll(Collection<?> c) {
boolean result = false;
Object[] keyArray = keys.toArray();
for (Object key : keyArray) {
if (!c.contains(key))
result |= remove(key);
}
return result;
}
public K getKey(int index) {
return keys.get(index);
}
public V getValue(int index) {
return values.get(index);
}
public void sort(Comparator<K> comparator) {
List<K> sortedKeys = new ArrayList<K>(keys);
Collections.sort(sortedKeys, comparator);
List<V> sortedValues = new ArrayList<V>(sortedKeys.size());
for (K key : sortedKeys) {
sortedValues.add(getByIdentity(key));
}
clear();
for (int i = 0; i < sortedKeys.size(); i++) {
put(sortedKeys.get(i), sortedValues.get(i));
}
}
private V getByIdentity(K key) {
int index = 0;
for (K existing : keys) {
if (existing == key)
return getValue(index);
index++;
}
return null;
}
}
|