1. /*
  2. * @(#)RepositoryIdCache_1_3_1.java 1.3 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.orbutil;
  8. import java.util.Stack;
  9. import java.util.Hashtable;
  10. import java.util.EmptyStackException;
  11. import java.util.Enumeration;
  12. // Really limited pool - in this case just creating several at a time...
  13. class RepositoryIdPool_1_3_1 extends Stack {
  14. private static int MAX_CACHE_SIZE = 4;
  15. private RepositoryIdCache_1_3_1 cache;
  16. public final synchronized RepositoryId_1_3_1 popId() {
  17. try {
  18. return (RepositoryId_1_3_1)super.pop();
  19. }
  20. catch(EmptyStackException e) {
  21. increasePool(5);
  22. return (RepositoryId_1_3_1)super.pop();
  23. }
  24. }
  25. // Pool management
  26. final void increasePool(int size) {
  27. //if (cache.size() <= MAX_CACHE_SIZE)
  28. for (int i = size; i > 0; i--)
  29. push(new RepositoryId_1_3_1());
  30. /*
  31. // _REVISIT_ This will not work w/out either thread tracing or weak references. I am
  32. // betting that thread tracing almost completely negates benefit of reuse. Until either
  33. // 1.2 only inclusion or proof to the contrary, I'll leave it this way...
  34. else {
  35. int numToReclaim = cache.size() / 2;
  36. Enumeration keys = cache.keys();
  37. Enumeration elements = cache.elements();
  38. for (int i = numToReclaim; i > 0; i--) {
  39. Object key = keys.nextElement();
  40. Object element = elements.nextElement();
  41. push(element);
  42. cache.remove(key);
  43. }
  44. }
  45. */
  46. }
  47. final void setCaches(RepositoryIdCache_1_3_1 cache) {
  48. this.cache = cache;
  49. }
  50. }
  51. public class RepositoryIdCache_1_3_1 extends Hashtable {
  52. private RepositoryIdPool_1_3_1 pool = new RepositoryIdPool_1_3_1();
  53. public RepositoryIdCache_1_3_1() {
  54. pool.setCaches(this);
  55. }
  56. public final synchronized RepositoryId_1_3_1 getId(String key) {
  57. RepositoryId_1_3_1 repId = (RepositoryId_1_3_1)super.get(key);
  58. if (repId != null)
  59. return repId;
  60. else {
  61. //repId = pool.popId().init(key);
  62. repId = new RepositoryId_1_3_1(key);
  63. put(key, repId);
  64. return repId;
  65. }
  66. }
  67. }