EMMA Coverage Report (generated Tue Apr 17 08:51:20 BST 2007)
[all classes][org.jtoolkit.essence.utils.impl]

COVERAGE SUMMARY FOR SOURCE FILE [MapArray.java]

nameclass, %method, %block, %line, %
MapArray.java100% (1/1)85%  (11/13)78%  (268/343)84%  (49.5/59)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MapArray100% (1/1)85%  (11/13)78%  (268/343)84%  (49.5/59)
hashCode (): int 0%   (0/1)0%   (0/4)0%   (0/1)
put (Object, Object): Object 0%   (0/1)0%   (0/44)0%   (0/6)
equals (Object): boolean 100% (1/1)59%  (20/34)75%  (4.5/6)
toMap (Object []): Map 100% (1/1)75%  (40/53)88%  (7/8)
MapArray (Map, Object []): void 100% (1/1)100% (13/13)100% (5/5)
asArray (Map): Object [] 100% (1/1)100% (36/36)100% (7/7)
asKeys (Object []): Map 100% (1/1)100% (25/25)100% (4/4)
asMap (): Map 100% (1/1)100% (39/39)100% (5/5)
create (Map, Object []): Map 100% (1/1)100% (6/6)100% (1/1)
entrySet (): Set 100% (1/1)100% (4/4)100% (1/1)
get (Object): Object 100% (1/1)100% (22/22)100% (4/4)
size (): int 100% (1/1)100% (3/3)100% (1/1)
toString (): String 100% (1/1)100% (60/60)100% (10/10)

1package org.jtoolkit.essence.utils.impl;
2 
3import org.jetbrains.annotations.NotNull;
4import org.jetbrains.annotations.Nullable;
5import org.jtoolkit.essence.concurrency.NotThreadSafe;
6import org.jtoolkit.essence.utils.ImmutableUtils;
7 
8import java.util.*;
9 
10/*
11   Copyright 2006 Peter Lawrey
12 
13   Licensed under the Apache License, Version 2.0 (the "License");
14   you may not use this file except in compliance with the License.
15   You may obtain a copy of the License at
16 
17       http://www.apache.org/licenses/LICENSE-2.0
18 
19   Unless required by applicable law or agreed to in writing, software
20   distributed under the License is distributed on an "AS IS" BASIS,
21   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22   See the License for the specific language governing permissions and
23   limitations under the License.
24*/
25 
26/**
27 * A map build from a key/index and an array.  More useful where there are many of these e.g. when reading a table.
28 *
29 * @author Peter Lawrey
30 */
31@NotThreadSafe
32public class MapArray<K, V> extends AbstractMap<K, V> {
33    //    private static final Log LOG = LogFactory.getLog(MapArray.class);
34    private final int size;
35    private final Map<K, Integer> keys;
36    private final V[] values;
37 
38    public MapArray(@NotNull Map<K, Integer> keys, @NotNull V[] values) {
39        size = keys.size();
40        this.keys = keys;
41        this.values = values;
42    }
43 
44    public int size() {
45        return size;
46    }
47 
48    @Nullable public V get(@NotNull Object key) {
49        Integer idx = keys.get(key);
50        if (idx == null || idx >= values.length)
51            return null;
52        return values[idx];
53    }
54 
55    @Nullable public V put(@NotNull K key, @NotNull V value) {
56        Integer idx = keys.get(key);
57        if (idx == null || idx >= values.length)
58            throw new IllegalArgumentException("Unable to set " + key + " to " + value);
59        V prev = values[idx];
60        values[idx] = value;
61        return prev;
62    }
63 
64    @NotNull public Set<Entry<K, V>> entrySet() {
65        return asMap().entrySet();
66    }
67 
68    @NotNull public Map<K, V> asMap() {
69        Map<K, V> ret = new LinkedHashMap<K, V>();
70        for (Map.Entry<K, Integer> entry : keys.entrySet())
71            if (entry.getValue() < values.length)
72                ret.put(entry.getKey(), values[entry.getValue()]);
73        return ret;
74    }
75 
76    public boolean equals(Object obj) {
77        if (obj == null) return false;
78        if (obj instanceof MapArray) {
79            MapArray ma = (MapArray) obj;
80            if (keys.equals(ma.keys))
81                return Arrays.equals(values, ma.values);
82        }
83        return obj instanceof Map && asMap().equals(obj);
84    }
85 
86    public int hashCode() {
87        return asMap().hashCode();
88    }
89 
90    public String toString() {
91        StringBuilder sb = new StringBuilder("{");
92        for (Map.Entry<K, Integer> entry : keys.entrySet())
93            if (entry.getValue() < values.length) {
94                if (sb.length() > 1)
95                    sb.append(", ");
96                sb.append(entry.getKey());
97                sb.append('=');
98                sb.append(values[entry.getValue()]);
99            }
100        sb.append('}');
101        return sb.toString();
102    }
103 
104    @NotNull public static <K> Map<K, Integer> asKeys(@NotNull K[] keys) {
105        Map<K, Integer> ret = new LinkedHashMap<K, Integer>(keys.length, 1.0f);
106        for (int i = 0; i < keys.length; i++)
107            ret.put(keys[i], i);
108        return ret;
109    }
110 
111    @NotNull public static Object[] asArray(@NotNull Map<?, ?> map) {
112        // keys cannot be null, but values can be.
113        Set<? extends Entry<?, ?>> entries = map.entrySet();
114        Object[] objs = new Object[entries.size() * 2];
115        int count = 0;
116        for (Map.Entry entry : entries) {
117            objs[count++] = entry.getKey();
118            objs[count++] = entry.getValue();
119        }
120        return objs;
121    }
122 
123    /**
124     * @noinspection unchecked
125     */
126    @NotNull public static <K, V> Map<K, V> toMap(@NotNull Object[] objs) {
127        if (objs.length == 0) return Collections.emptyMap();
128        Map<K, V> map = new LinkedHashMap<K, V>(objs.length / 2, 1.0f);
129        for (int j = 0; j < objs.length; j += 2) {
130            K key = (K) objs[j];
131            if (key == null)
132                throw new IllegalArgumentException(Thread.currentThread().getName() + ": Unexpected null key in map.");
133            map.put(key, (V) objs[j + 1]);
134        }
135        return ImmutableUtils.immutableCopyMap(map);
136    }
137 
138    @NotNull public static <K, V> Map<K, V> create(@NotNull Map<K, Integer> heading, @NotNull V[] row) {
139        return new MapArray<K, V>(heading, row);
140    }
141}

[all classes][org.jtoolkit.essence.utils.impl]
EMMA 2.0.5312 (C) Vladimir Roubtsov