KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)RepositoryIdCache_1_3_1.java 1.5 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.Stack JavaDoc;
11 import java.util.Hashtable JavaDoc;
12 import java.util.EmptyStackException JavaDoc;
13 import java.util.Enumeration JavaDoc;
14
15 // Really limited pool - in this case just creating several at a time...
16
class RepositoryIdPool_1_3_1 extends Stack JavaDoc {
17     
18     private static int MAX_CACHE_SIZE = 4;
19     private RepositoryIdCache_1_3_1 cache;
20     
21     public final synchronized RepositoryId_1_3_1 popId() {
22
23         try {
24             return (RepositoryId_1_3_1)super.pop();
25         }
26         catch(EmptyStackException JavaDoc e) {
27             increasePool(5);
28             return (RepositoryId_1_3_1)super.pop();
29         }
30
31     }
32
33     // Pool management
34
final void increasePool(int size) {
35         //if (cache.size() <= MAX_CACHE_SIZE)
36
for (int i = size; i > 0; i--)
37         push(new RepositoryId_1_3_1());
38         /*
39       // _REVISIT_ This will not work w/out either thread tracing or weak references. I am
40       // betting that thread tracing almost completely negates benefit of reuse. Until either
41       // 1.2 only inclusion or proof to the contrary, I'll leave it this way...
42       else {
43       int numToReclaim = cache.size() / 2;
44       Enumeration keys = cache.keys();
45       Enumeration elements = cache.elements();
46       for (int i = numToReclaim; i > 0; i--) {
47       Object key = keys.nextElement();
48       Object element = elements.nextElement();
49                 
50       push(element);
51       cache.remove(key);
52       }
53       }
54         */

55     }
56     
57     final void setCaches(RepositoryIdCache_1_3_1 cache) {
58         this.cache = cache;
59     }
60
61 }
62
63 public class RepositoryIdCache_1_3_1 extends Hashtable JavaDoc {
64
65     private RepositoryIdPool_1_3_1 pool = new RepositoryIdPool_1_3_1();
66     
67     public RepositoryIdCache_1_3_1() {
68         pool.setCaches(this);
69     }
70     
71     public final synchronized RepositoryId_1_3_1 getId(String JavaDoc key) {
72         RepositoryId_1_3_1 repId = (RepositoryId_1_3_1)super.get(key);
73
74         if (repId != null)
75             return repId;
76         else {
77             //repId = pool.popId().init(key);
78
repId = new RepositoryId_1_3_1(key);
79             put(key, repId);
80             return repId;
81         }
82
83     }
84 }
85
86
87
Popular Tags