KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > viewers > deferred > IntHashMap


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.viewers.deferred;
12
13 import java.util.HashMap JavaDoc;
14
15 /**
16  * Represents a map of objects onto ints. This is intended for future optimization:
17  * using int primitives would allow for an implementation that doesn't require
18  * additional object allocations for Integers. However, the current implementation
19  * simply delegates to the Java HashMap class.
20  *
21  * @since 3.1
22  */

23 /* package */ class IntHashMap {
24     private HashMap JavaDoc map;
25     
26     /**
27      * @param size
28      * @param loadFactor
29      */

30     public IntHashMap(int size, float loadFactor) {
31         map = new HashMap JavaDoc(size, loadFactor);
32     }
33     
34     /**
35      *
36      */

37     public IntHashMap() {
38         map = new HashMap JavaDoc();
39     }
40     
41     /**
42      * @param key
43      */

44     public void remove(Object JavaDoc key) {
45         map.remove(key);
46     }
47     
48     /**
49      * @param key
50      * @param value
51      */

52     public void put(Object JavaDoc key, int value) {
53         map.put(key, new Integer JavaDoc(value));
54     }
55     
56     /**
57      * @param key
58      * @return the int value at the given key
59      */

60     public int get(Object JavaDoc key) {
61         return get(key, 0);
62     }
63     
64     /**
65      * @param key
66      * @param defaultValue
67      * @return the int value at the given key, or the default value if this map does not contain the given key
68      */

69     public int get(Object JavaDoc key, int defaultValue) {
70         Integer JavaDoc result = (Integer JavaDoc)map.get(key);
71         
72         if (result != null) {
73             return result.intValue();
74         }
75         
76         return defaultValue;
77     }
78     
79     /**
80      * @param key
81      * @return <code>true</code> if this map contains the given key, <code>false</code> otherwise
82      */

83     public boolean containsKey(Object JavaDoc key) {
84         return map.containsKey(key);
85     }
86     
87     /**
88      * @return the number of key/value pairs
89      */

90     public int size() {
91         return map.size();
92     }
93 }
94
Popular Tags