KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > ccm > runtime > SlotFactory


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.ccm.runtime;
31
32 import org.objectweb.corba.runtime.*;
33
34 /**
35  ** <p>Default implementation of the <tt>SlotFactory</tt> interface.</p>
36  **/

37 public class SlotFactory
38 {
39     //
40
private java.util.Hashtable JavaDoc _values; // (thread id, hashtable of values)
41
private SlotImpl[] _slots; // index = ECA::SlotId, value = SlotImpl)
42
private int _default_length;
43     private int _default_increment;
44     private int _slot_number;
45
46     // default constructor
47
public
48     SlotFactory()
49     {
50         _values = new java.util.Hashtable JavaDoc();
51
52         _default_length = 10;
53         _default_increment = 10;
54         _slot_number = 0;
55         _slots = new SlotImpl[_default_length];
56     }
57
58     // constructor with length and increment for the slot table
59
public
60     SlotFactory(int dl, int di)
61     {
62         _values = new java.util.Hashtable JavaDoc();
63
64         _default_length = dl;
65         _default_increment = di;
66         _slot_number = 0;
67         _slots = new SlotImpl[_default_length];
68     }
69
70     //
71
// internal operations
72
//
73

74     private void
75     reallocateSlotTable()
76     {
77         SlotImpl[] ntable = new SlotImpl[_slots.length+_default_increment];
78
79         // copy
80
System.arraycopy(_slots, 0, ntable, 0, _slots.length);
81         _slots = ntable;
82     }
83
84     private void
85     setSlot(int sid, SlotImpl slot)
86     {
87         // check if size if ok
88
if (sid>=_slots.length) {
89             reallocateSlotTable();
90         }
91
92         _slots[sid] = slot;
93     }
94
95     //
96
// public operations
97
//
98

99     final public int
100     allocateSlotId()
101     {
102         // set a null slot
103
setSlot(_slot_number, null);
104
105         //
106
return _slot_number;
107     }
108
109     final public SlotImpl
110     getSlot(int sid)
111     {
112         // check if the slot is already created
113
if (_slots[sid]!=null) {
114             return _slots[sid];
115         }
116
117         // create and store
118
SlotImpl slot = new SlotImpl(_values, sid);
119         _slots[sid] = slot;
120
121         return slot;
122     }
123 }
124
Popular Tags