File Source: DoubleMap.java
/*
P/P * Method: com.dmdirc.util.DoubleMap__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22 package com.dmdirc.util;
23
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Set;
28
29 /**
30 * An object that maps keys to values, and values back to keys. Currently
31 * does no checking for duplicates. Does not allow null values.
32 *
33 * @param <A> The first type of data to be mapped
34 * @param <B> The second type of data to be mapped
35 * @author chris
36 */
/*
P/P * Method: void com.dmdirc.util.DoubleMap()
*
* Postconditions:
* this.keys == &new ArrayList(DoubleMap#1)
* this.values == &new ArrayList(DoubleMap#2)
* new ArrayList(DoubleMap#1) num objects == 1
* new ArrayList(DoubleMap#2) num objects == 1
*/
37 public class DoubleMap<A,B> {
38
39 /** The keys in this map. */
40 protected final List<A> keys = new ArrayList<A>();
41 /** The values in this map. */
42 protected final List<B> values = new ArrayList<B>();
43
44 /**
45 * Adds the specified pair to this map.
46 *
47 * @param key The key for the map
48 * @param value The value for the map
49 */
50 public void put(final A key, final B value) {
/*
P/P * Method: void put(Object, Object)
*
* Preconditions:
* key != null
* this.keys != null
* this.values != null
* value != null
*/
51 if (key == null || value == null) {
52 throw new NullPointerException();
53 }
54
55 keys.add(key);
56 values.add(value);
57 }
58
59 /**
60 * Retrieves the value associated with the specified key.
61 *
62 * @param key The key to search for
63 * @return The value of the specified key
64 */
65 public B getValue(final A key) {
/*
P/P * Method: Object getValue(Object)
*
* Preconditions:
* this.keys != null
* this.values != null
*
* Postconditions:
* init'ed(return_value)
*/
66 return values.get(keys.indexOf(key));
67 }
68
69 /**
70 * Retrieves the key associated with the specified value.
71 *
72 * @param value The value to search for
73 * @return The key of the specified value
74 */
75 public A getKey(final B value) {
/*
P/P * Method: Object getKey(Object)
*
* Preconditions:
* this.keys != null
* this.values != null
*
* Postconditions:
* init'ed(return_value)
*/
76 return keys.get(values.indexOf(value));
77 }
78
79 /**
80 * Retrieves the set of keys in this double map.
81 *
82 * @return This map's key set
83 */
84 public Set<A> keySet() {
/*
P/P * Method: Set keySet()
*
* Postconditions:
* return_value == &new HashSet(keySet#1)
* new HashSet(keySet#1) num objects == 1
*/
85 return new HashSet<A>(keys);
86 }
87
88 /**
89 * Retrieves the set of values in this double map.
90 *
91 * @return This map's value set
92 */
93 public Set<B> valueSet() {
/*
P/P * Method: Set valueSet()
*
* Postconditions:
* return_value == &new HashSet(valueSet#1)
* new HashSet(valueSet#1) num objects == 1
*/
94 return new HashSet<B>(values);
95 }
96
97 }
SofCheck Inspector Build Version : 2.17854
| DoubleMap.java |
2009-Jun-25 01:54:24 |
| DoubleMap.class |
2009-Sep-02 17:04:17 |