KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > sequencing > PreallocationHandler


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2005, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.sequencing;
23
24 import java.util.Vector JavaDoc;
25 import java.util.Hashtable JavaDoc;
26
27 class PreallocationHandler implements SequencingLogInOut {
28     protected Hashtable JavaDoc preallocatedSequences;
29
30     public PreallocationHandler() {
31         super();
32     }
33
34     /**
35     * PROTECTED:
36     * return a vector from the global sequences based on
37     * seqName. If there is not one, put it there.
38     */

39     protected Vector JavaDoc getPreallocatedSequences(String JavaDoc seqName) {
40         Vector JavaDoc sequencesForName;
41         synchronized (preallocatedSequences) {
42             sequencesForName = (Vector JavaDoc)preallocatedSequences.get(seqName);
43             if (sequencesForName == null) {
44                 sequencesForName = new Vector JavaDoc();
45                 preallocatedSequences.put(seqName, sequencesForName);
46             }
47         }
48         return sequencesForName;
49     }
50
51     // SequencingLogInOut
52
public void onConnect() {
53         initializePreallocated();
54     }
55
56     public void onDisconnect() {
57         preallocatedSequences = null;
58     }
59
60     public boolean isConnected() {
61         return preallocatedSequences != null;
62     }
63
64     // removes all preallocated objects.
65
// a dangerous method to use in multithreaded environment method,
66
// but so handy for testing
67
public void initializePreallocated() {
68         preallocatedSequences = new Hashtable JavaDoc(20);
69     }
70
71     // removes all preallocated objects for the specified seqName.
72
// a dangerous method to use in multithreaded environment method,
73
// but so handy for testing
74
public void initializePreallocated(String JavaDoc seqName) {
75         preallocatedSequences.remove(seqName);
76     }
77
78     public Vector JavaDoc getPreallocated(String JavaDoc seqName) {
79         return getPreallocatedSequences(seqName);
80     }
81
82     public void setPreallocated(String JavaDoc seqName, Vector JavaDoc sequences) {
83         getPreallocatedSequences(seqName).addAll(sequences);
84     }
85 }
86
Popular Tags