001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common.web.template;
009:
010: //base classes
011: import java.util.ArrayList;
012:
013: //project specific classes
014: import org.jfolder.common.UnexpectedSystemException;
015: import org.jfolder.common.web.template.ConsoleParameterContext;
016:
017: //other classes
018:
019: public class ConsoleParameterHolder implements ConsoleParameterContext {
020:
021: //
022: private ArrayList parameters = null;
023:
024: //
025: private ConsoleParameterHolder() {
026: //
027: this .parameters = new ArrayList();
028: }
029:
030: public final static ConsoleParameterHolder newInstance() {
031: //
032: return new ConsoleParameterHolder();
033: }
034:
035: //
036: //
037: //
038: public int hashCode() {
039:
040: int outValue = 0;
041:
042: outValue += this .parameters.hashCode();
043:
044: return outValue;
045: }
046:
047: public boolean equals(Object inObj) {
048:
049: boolean outValue = true;
050:
051: if (inObj instanceof ConsoleParameterHolder) {
052: ConsoleParameterHolder nextCph = ((ConsoleParameterHolder) inObj);
053: outValue &= (this .parameters.equals(nextCph.parameters));
054: } else {
055: outValue &= false;
056: }
057:
058: return outValue;
059: }
060:
061: public final static ArrayList getIndexes() {
062:
063: ArrayList outValue = new ArrayList();
064:
065: outValue.add(FIRST_INPUT);
066: outValue.add(SECOND_INPUT);
067: outValue.add(THIRD_INPUT);
068: outValue.add(FOURTH_INPUT);
069: outValue.add(FIFTH_INPUT);
070: outValue.add(SIXTH_INPUT);
071: outValue.add(SEVENTH_INPUT);
072: outValue.add(EIGHTH_INPUT);
073: outValue.add(NINTH_INPUT);
074: outValue.add(TENTH_INPUT);
075:
076: return outValue;
077: }
078:
079: private boolean isIndexPresent(Integer inIndex) {
080:
081: boolean outValue = false;
082:
083: ArrayList indexes = getIndexes();
084: int index = indexes.indexOf(inIndex);
085: if (index != -1) {
086: Object value = indexes.get(index);
087: outValue = (inIndex == value);
088: }
089:
090: return outValue;
091: }
092:
093: public final static Integer getFirstIndex() {
094: return FIRST_INPUT;
095: }
096:
097: public Integer getNextIndex(Integer inIndex) {
098:
099: Integer outValue = null;
100:
101: ArrayList indexes = getIndexes();
102:
103: int index = inIndex.intValue();
104: if (index < (this .parameters.size() - 1)) {
105: outValue = ((Integer) indexes.get(index + 1));
106: }
107:
108: return outValue;
109: }
110:
111: public int getIndexCount() {
112: return this .parameters.size();
113: }
114:
115: //private void initializeGenericAll(Object inValue) {
116: // this.parameters.clear();
117: // ArrayList indexes = getIndexes();
118: // for (int i = 0; i < indexes.size(); i++) {
119: // Integer nextIndex = ((Integer)indexes.get(i));
120: // addGenericInput(nextIndex, inValue);
121: // }
122: //}
123:
124: //
125: public Object getInput(Integer inIndex) {
126:
127: Object outValue = null;
128:
129: if (inIndex.intValue() >= MAX_CREATION_PARAMS) {
130: throw new UnexpectedSystemException(
131: "There can be no more than '" + MAX_CREATION_PARAMS
132: + "' parameters");
133: } else if (inIndex.intValue() < 0) {
134: throw new UnexpectedSystemException("Index '"
135: + (inIndex.intValue() + 1) + "' must be at least 1");
136: } else if (!isIndexPresent(inIndex)) {
137: throw new UnexpectedSystemException(
138: "Only indexes found in ConsoleParameterContext can be used");
139: } else if (inIndex.intValue() < this .parameters.size()) {
140: outValue = this .parameters.get(inIndex.intValue());
141: } else {
142: outValue = null;
143: //throw new UnexpectedSystemException(
144: // "Trying to get value at index '" + (inIndex.intValue() + 1)
145: // + "' but the last index of the array is '"
146: // + (this.parameters.size()) + "'");
147: }
148:
149: return outValue;
150: }
151:
152: //
153: public void addInput(Integer inIndex, String inValue) {
154: addGenericInput(inIndex, inValue);
155: }
156:
157: public void addInput(Integer inIndex,
158: CreationParameterContextInput inValue) {
159: //
160: addGenericInput(inIndex, inValue);
161: }
162:
163: //public void addInput(Integer inIndex, String inValue[]) {
164: // addGenericInput(inIndex, inValue);
165: //}
166: private void addGenericInput(Integer inIndex, Object inValue) {
167: if (inIndex.intValue() >= MAX_CREATION_PARAMS) {
168: throw new UnexpectedSystemException(
169: "There can be no more than '" + MAX_CREATION_PARAMS
170: + "' parameters");
171: } else if (inIndex.intValue() < 0) {
172: throw new UnexpectedSystemException("Index '"
173: + (inIndex.intValue() + 1) + "' must be at least 1");
174: } else if (!isIndexPresent(inIndex)) {
175: throw new UnexpectedSystemException(
176: "Only indexes found in ConsoleParameterContext can be used");
177: } else if (inIndex.intValue() == this .parameters.size()) {
178: this .parameters.add(inValue);
179: } else {
180: throw new UnexpectedSystemException(
181: "Trying to add at index '"
182: + (inIndex.intValue() + 1)
183: + "' but the last index of the array is '"
184: + (this .parameters.size()) + "'");
185: }
186: }
187:
188: }
|