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. package com.sun.corba.se.impl.orbutil ;
  8. import java.util.ArrayList ;
  9. /** Utility for managing mappings from densely allocated integer
  10. * keys to arbitrary objects. This should only be used for
  11. * keys in the range 0..max such that "most" of the key space is actually
  12. * used.
  13. */
  14. public class DenseIntMapImpl
  15. {
  16. private ArrayList list = new ArrayList() ;
  17. private void checkKey( int key )
  18. {
  19. if (key < 0)
  20. throw new IllegalArgumentException( "Key must be >= 0." ) ;
  21. }
  22. /** If key >= 0, return the value bound to key, or null if none.
  23. * Throws IllegalArgumentException if key <0.
  24. */
  25. public Object get( int key )
  26. {
  27. checkKey( key ) ;
  28. Object result = null ;
  29. if (key < list.size())
  30. result = list.get( key ) ;
  31. return result ;
  32. }
  33. /** If key >= 0, bind value to the key.
  34. * Throws IllegalArgumentException if key <0.
  35. */
  36. public void set( int key, Object value )
  37. {
  38. checkKey( key ) ;
  39. extend( key ) ;
  40. list.set( key, value ) ;
  41. }
  42. private void extend( int index )
  43. {
  44. if (index >= list.size()) {
  45. list.ensureCapacity( index + 1 ) ;
  46. int max = list.size() ;
  47. while (max++ <= index)
  48. list.add( null ) ;
  49. }
  50. }
  51. }