001: package net.sf.saxon.instruct;
002:
003: import net.sf.saxon.om.ValueRepresentation;
004:
005: /**
006: * A ParameterSet is a set of parameters supplied when calling a template.
007: * It is a collection of name-value pairs, the names being represented by numeric references
008: * to the NamePool
009: */
010:
011: public class ParameterSet {
012: private int[] keys;
013: private ValueRepresentation[] values;
014: private int used = 0;
015:
016: public static ParameterSet EMPTY_PARAMETER_SET = new ParameterSet(0);
017:
018: /**
019: * Create an empty parameter set
020: */
021:
022: public ParameterSet() {
023: this (10);
024: }
025:
026: /**
027: * Create a parameter set specifying the initial capacity
028: */
029:
030: public ParameterSet(int capacity) {
031: keys = new int[capacity];
032: values = new ValueRepresentation[capacity];
033: }
034:
035: /**
036: * Create a parameter set as a copy of an existing parameter set
037: */
038:
039: public ParameterSet(ParameterSet existing, int extra) {
040: this (existing.used + extra);
041: for (int i = 0; i < existing.used; i++) {
042: put(existing.keys[i], existing.values[i]);
043: }
044: }
045:
046: /**
047: * Add a parameter to the ParameterSet
048: *
049: * @param fingerprint The fingerprint of the parameter name.
050: * @param value The value of the parameter
051: */
052:
053: public void put(int fingerprint, ValueRepresentation value) {
054: for (int i = 0; i < used; i++) {
055: if (keys[i] == fingerprint) {
056: values[i] = value;
057: return;
058: }
059: }
060: if (used + 1 > keys.length) {
061: int newlength = (used <= 5 ? 10 : used * 2);
062: int[] newkeys = new int[newlength];
063: ValueRepresentation[] newvalues = new ValueRepresentation[newlength];
064: System.arraycopy(values, 0, newvalues, 0, used);
065: System.arraycopy(keys, 0, newkeys, 0, used);
066: values = newvalues;
067: keys = newkeys;
068: }
069: keys[used] = fingerprint;
070: values[used++] = value;
071: }
072:
073: /**
074: * Get a parameter
075: *
076: * @param fingerprint The fingerprint of the name.
077: * @return The value of the parameter, or null if not defined
078: */
079:
080: public ValueRepresentation get(int fingerprint) {
081: for (int i = 0; i < used; i++) {
082: if (keys[i] == fingerprint) {
083: return values[i];
084: }
085: }
086: return null;
087: }
088:
089: /**
090: * Clear all values
091: */
092:
093: public void clear() {
094: used = 0;
095: }
096:
097: }
098: //
099: // 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: //
|