KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > instruct > ParameterSet


1 package net.sf.saxon.instruct;
2 import net.sf.saxon.om.ValueRepresentation;
3
4 /**
5  * A ParameterSet is a set of parameters supplied when calling a template.
6  * It is a collection of name-value pairs, the names being represented by numeric references
7  * to the NamePool
8  */

9
10 public class ParameterSet
11 {
12     private int[] keys;
13     private ValueRepresentation[] values;
14     private int used = 0;
15
16     public static ParameterSet EMPTY_PARAMETER_SET = new ParameterSet(0);
17
18     /**
19      * Create an empty parameter set
20      */

21
22     public ParameterSet() {
23         this(10);
24     }
25
26     /**
27      * Create a parameter set specifying the initial capacity
28      */

29
30     public ParameterSet(int capacity) {
31         keys = new int[capacity];
32         values = new ValueRepresentation[capacity];
33     }
34
35     /**
36      * Create a parameter set as a copy of an existing parameter set
37      */

38
39     public ParameterSet(ParameterSet existing, int extra) {
40         this(existing.used + extra);
41         for (int i=0; i<existing.used; i++) {
42             put(existing.keys[i], existing.values[i]);
43         }
44     }
45
46     /**
47      * Add a parameter to the ParameterSet
48      *
49      * @param fingerprint The fingerprint of the parameter name.
50      * @param value The value of the parameter
51      */

52
53     public void put (int fingerprint, ValueRepresentation value) {
54         for (int i=0; i<used; i++) {
55             if (keys[i]==fingerprint) {
56                 values[i]=value;
57                 return;
58             }
59         }
60         if (used+1 > keys.length) {
61             int newlength = (used<=5 ? 10 : used*2);
62             int[] newkeys = new int[newlength];
63             ValueRepresentation[] newvalues = new ValueRepresentation[newlength];
64             System.arraycopy(values, 0, newvalues, 0, used);
65             System.arraycopy(keys, 0, newkeys, 0, used);
66             values = newvalues;
67             keys = newkeys;
68         }
69         keys[used] = fingerprint;
70         values[used++] = value;
71     }
72
73     /**
74      * Get a parameter
75      *
76      * @param fingerprint The fingerprint of the name.
77      * @return The value of the parameter, or null if not defined
78      */

79
80     public ValueRepresentation get (int fingerprint) {
81         for (int i=0; i<used; i++) {
82             if (keys[i]==fingerprint) {
83                 return values[i];
84             }
85         }
86         return null;
87     }
88
89     /**
90      * Clear all values
91      */

92
93     public void clear() {
94         used = 0;
95     }
96
97 }
98 //
99
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
100
// you may not use this file except in compliance with the License. You may obtain a copy of the
101
// License at http://www.mozilla.org/MPL/
102
//
103
// Software distributed under the License is distributed on an "AS IS" basis,
104
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
105
// See the License for the specific language governing rights and limitations under the License.
106
//
107
// The Original Code is: all this file.
108
//
109
// The Initial Developer of the Original Code is Michael H. Kay.
110
//
111
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
112
//
113
// Contributor(s): none.
114
//
Popular Tags