KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > orbutil > DenseIntMapImpl


1 /*
2  * @(#)DenseIntMapImpl.java 1.4 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.orbutil ;
9
10 import java.util.ArrayList JavaDoc ;
11
12 /** Utility for managing mappings from densely allocated integer
13  * keys to arbitrary objects. This should only be used for
14  * keys in the range 0..max such that "most" of the key space is actually
15  * used.
16  */

17 public class DenseIntMapImpl
18 {
19     private ArrayList JavaDoc list = new ArrayList JavaDoc() ;
20
21     private void checkKey( int key )
22     {
23     if (key < 0)
24         throw new IllegalArgumentException JavaDoc( "Key must be >= 0." ) ;
25     }
26
27     /** If key >= 0, return the value bound to key, or null if none.
28      * Throws IllegalArgumentException if key <0.
29      */

30     public Object JavaDoc get( int key )
31     {
32     checkKey( key ) ;
33
34     Object JavaDoc result = null ;
35     if (key < list.size())
36         result = list.get( key ) ;
37
38     return result ;
39     }
40
41     /** If key >= 0, bind value to the key.
42      * Throws IllegalArgumentException if key <0.
43      */

44     public void set( int key, Object JavaDoc value )
45     {
46     checkKey( key ) ;
47     extend( key ) ;
48     list.set( key, value ) ;
49     }
50
51     private void extend( int index )
52     {
53     if (index >= list.size()) {
54         list.ensureCapacity( index + 1 ) ;
55         int max = list.size() ;
56         while (max++ <= index)
57         list.add( null ) ;
58     }
59     }
60 }
61
Popular Tags