25 26 package com.sun.corba.se.impl.orbutil ; 27 28 import java.util.ArrayList ; 29 30 /** Utility for managing mappings from densely allocated integer 31 * keys to arbitrary objects. This should only be used for 32 * keys in the range 0..max such that "most" of the key space is actually 33 * used. 34 */ 35 public class DenseIntMapImpl 36 { 37 private ArrayList list = new ArrayList() ; 38 39 private void checkKey( int key ) 40 { 41 if (key < 0) 42 throw new IllegalArgumentException( "Key must be >= 0." ) ; 43 } 44 45 /** If key >= 0, return the value bound to key, or null if none. 46 * Throws IllegalArgumentException if key <0. 47 */ 48 public Object get( int key ) 49 { 50 checkKey( key ) ; 51 52 Object result = null ; 53 if (key < list.size()) 54 result = list.get( key ) ; 55 56 return result ; 57 } 58 59 /** If key >= 0, bind value to the key. 60 * Throws IllegalArgumentException if key <0. 61 */ 62 public void set( int key, Object value ) 63 { 64 checkKey( key ) ; 65 extend( key ) ; 66 list.set( key, value ) ; 67 } 68 69 private void extend( int index ) 70 { 71 if (index >= list.size()) { 72 list.ensureCapacity( index + 1 ) ; 73 int max = list.size() ; 74 while (max++ <= index) 75 list.add( null ) ; 76 } 77 } 78 } | 25 26 package com.sun.corba.se.impl.orbutil ; 27 28 import java.util.ArrayList ; 29 30 /** Utility for managing mappings from densely allocated integer 31 * keys to arbitrary objects. This should only be used for 32 * keys in the range 0..max such that "most" of the key space is actually 33 * used. 34 */ 35 public class DenseIntMapImpl 36 { 37 private ArrayList list = new ArrayList() ; 38 39 private void checkKey( int key ) 40 { 41 if (key < 0) 42 throw new IllegalArgumentException( "Key must be >= 0." ) ; 43 } 44 45 /** 46 * If {@code key >= 0}, return the value bound to key, or null if none. 47 * Throws IllegalArgumentException if {@code key < 0}. 48 */ 49 public Object get( int key ) 50 { 51 checkKey( key ) ; 52 53 Object result = null ; 54 if (key < list.size()) 55 result = list.get( key ) ; 56 57 return result ; 58 } 59 60 /** 61 * If {@code key >= 0}, bind value to the key. 62 * Throws IllegalArgumentException if {@code key < 0}. 63 */ 64 public void set( int key, Object value ) 65 { 66 checkKey( key ) ; 67 extend( key ) ; 68 list.set( key, value ) ; 69 } 70 71 private void extend( int index ) 72 { 73 if (index >= list.size()) { 74 list.ensureCapacity( index + 1 ) ; 75 int max = list.size() ; 76 while (max++ <= index) 77 list.add( null ) ; 78 } 79 } 80 } |