001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency;
010:
011: import java.util.ArrayList;
012: import java.util.List;
013: import java.util.Map;
014:
015: /**
016: * Ancestor of all parameter collection types
017: *
018: * @author Gennady Krizhevsky
019: */
020: public abstract class AbstractParameters implements Listable {
021: protected ArrayList parameters;
022:
023: protected AbstractParameters() {
024: this .parameters = new ArrayList();
025: }
026:
027: /**
028: * Constructs an empty parameters collection with the specified initial capacity.
029: *
030: * @param capacity
031: */
032: protected AbstractParameters(int capacity) {
033: this .parameters = new ArrayList(capacity);
034: }
035:
036: /**
037: * <p> Copy constructor
038: *
039: * @param parameters input parameters tobe populated into this collection
040: */
041: public AbstractParameters(AbstractParameters parameters) {
042: if (parameters == null) {
043: this .parameters = new ArrayList();
044: } else {
045: this .parameters = new ArrayList(parameters.size());
046: addAll(parameters);
047: }
048: }
049:
050: /**
051: * Constructs parameters collection from Parameter []
052: *
053: * @param parameters Parameter []
054: */
055: public AbstractParameters(Parameter[] parameters) {
056: if (parameters == null) {
057: this .parameters = new ArrayList();
058: } else {
059: this .parameters = new ArrayList(parameters.length);
060: for (int i = 0; i < parameters.length; i++) {
061: Parameter parameter = parameters[i];
062: validateParameterType(parameter);
063: this .parameters.add(parameter);
064: }
065: }
066: }
067:
068: /**
069: * Validates parameter type
070: *
071: * @param parameter parameter
072: */
073: protected void validateParameterType(Parameter parameter) {
074: }
075:
076: /**
077: * Ensures that passed value is not null
078: *
079: * @param value value to test
080: * @throws OdalRuntimePersistencyException if value is null
081: */
082: protected void assertValueNotParameter(Object value) {
083: if (value instanceof Parameter) {
084: throw new OdalRuntimePersistencyException(
085: "Cannot create Parameter with value of "
086: + Parameter.class + " class");
087: }
088: }
089:
090: /**
091: * Returns value of parameter at index
092: *
093: * @param index parameter index
094: * @return value of parameter at index
095: */
096: public Object getValue(int index) {
097: Parameter parameter = (Parameter) parameters.get(index);
098: return parameter.getValue();
099: }
100:
101: /**
102: * Returns type of parameter at index
103: *
104: * @see com.completex.objective.components.persistency.Parameter#getType()
105: * @param index parameter index
106: * @return type of parameter at index
107: */
108: public Object getType(int index) {
109: Parameter parameter = (Parameter) parameters.get(index);
110: return parameter.getType();
111: }
112:
113: /**
114: * Returns array of all parameter values
115: *
116: * @return array of all parameter values
117: */
118: public Object[] getValues() {
119: return parameters == null ? null : parameters
120: .toArray(new Object[parameters.size()]);
121: }
122:
123: /**
124: * Returns the number of parameters in this collection.
125: *
126: * @return the number of parameters in this collection.
127: */
128: public int size() {
129: return parameters == null ? 0 : parameters.size();
130: }
131:
132: /**
133: * Returns true if at least one of the parameters of this collection has value as null
134: *
135: * @return true if at least one of the parameters of this collection has value as null
136: */
137: public boolean hasNulls() {
138: if (parameters == null) {
139: return true;
140: }
141:
142: for (int i = 0; i < parameters.size(); i++) {
143: Parameter parameter = (Parameter) parameters.get(i);
144: if (parameter == null || parameter.getValue() == null) {
145: return true;
146: }
147: }
148: return false;
149: }
150:
151: /**
152: * Appends all of the parameters in the specified AbstractParameters to the end of
153: * this collection
154: *
155: * @param argParameters the parameters to be inserted into this list.
156: */
157: protected void addAll(AbstractParameters argParameters) {
158: parameters.addAll(argParameters.parameters);
159: }
160:
161: /**
162: * Returns the parameter at the specified position in this collection.
163: *
164: * @param index index of parameter to return.
165: * @return the parameter at the specified position in this collection.
166: * @throws IndexOutOfBoundsException if index is out of range <tt>(index
167: * < 0 || index >= size())</tt>.
168: */
169: public Parameter get(int index) {
170: return (Parameter) parameters.get(index);
171: }
172:
173: public String toString() {
174: return new StringBuffer().append(super .toString()).append(
175: "; parameters = ").append(parameters.toString())
176: .toString();
177: }
178:
179: public List toList() {
180: ArrayList list = null;
181: if (parameters != null) {
182: list = new ArrayList(parameters.size());
183: for (int i = 0; i < parameters.size(); i++) {
184: Parameter parameter = (Parameter) parameters.get(i);
185: list.add(parameter.toMap());
186: }
187: }
188: return list;
189: }
190:
191: public void fromList(List list) {
192: if (list != null) {
193: this .parameters = new ArrayList(list.size());
194: for (int i = 0; i < list.size(); i++) {
195: Map map = (Map) list.get(i);
196: Parameter parameter = new Parameter(map);
197: this.parameters.add(parameter);
198: }
199: }
200: }
201:
202: }
|