001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: VeryLazyList.java,v $
031: * Revision 1.5 2005/10/14 14:03:22 colinmacleod
032: * Changed to use PrototypeFactory rather than IntantiateFactory.
033: *
034: * Revision 1.4 2005/10/11 18:55:29 colinmacleod
035: * Fixed some checkstyle and javadoc issues.
036: *
037: * Revision 1.3 2005/10/03 10:17:24 colinmacleod
038: * Fixed some style and javadoc issues.
039: *
040: * Revision 1.2 2005/10/02 14:06:32 colinmacleod
041: * Added/improved log4j logging.
042: *
043: * Revision 1.1 2005/10/02 10:49:38 colinmacleod
044: * First version. Simplifies handling of primitive types in Struts forms.
045: *
046: */
047: package com.ivata.mask.util;
048:
049: import java.util.List;
050:
051: import org.apache.commons.collections.functors.PrototypeFactory;
052: import org.apache.commons.collections.list.LazyList;
053: import org.apache.log4j.Logger;
054:
055: import com.ivata.mask.field.FieldValueConvertor;
056:
057: /**
058: * <p>
059: * This factory class makes it simple to create a list of a primitive type (or
060: * any type for which there is an <strong>ivata masks</strong> field value
061: * convertor.
062: * </p>
063: * <p>
064: * This list overrides <code>set</code> so it can be used to populate lists in
065: * <em>Struts</em> forms.
066: * </p>
067: *
068: * @since ivata masks 1.0 (29-Sep-2005)
069: * @author Colin MacLeod
070: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
071: * @version $Revision: 1.5 $
072: */
073: public class VeryLazyList extends LazyList {
074: /**
075: * Logger for this class.
076: */
077: private static final Logger logger = Logger
078: .getLogger(VeryLazyList.class);
079:
080: /**
081: * Serialization version (for <code>Serializable</code> interface)..
082: */
083: private static final long serialVersionUID = 1L;
084:
085: /**
086: * Create a vary lazy list.
087: *
088: * @param list
089: * <copyDoc>Refer to {@link List#set}.</copyDoc>
090: * @param targetClass The class of object you want to add to the list.
091: * The class you provide must be a primitive type,
092: * or must have a constructor which accepts a string.
093: * @return a new very lazy list.
094: */
095: public static List decorate(final List list, final Class targetClass) {
096: if (logger.isDebugEnabled()) {
097: logger.debug("decorate(List list = " + list
098: + ", Class targetClass = " + targetClass
099: + ") - start");
100: }
101:
102: List returnList = new VeryLazyList(list, targetClass,
103: new FieldValueConvertor());
104: if (logger.isDebugEnabled()) {
105: logger
106: .debug("decorate(List, Class) - end - return value = "
107: + returnList);
108: }
109: return returnList;
110: }
111:
112: /**
113: * Create a vary lazy list.
114: *
115: * @param list
116: * <copyDoc>Refer to {@link List#set}.</copyDoc>
117: * @param targetClass The class of object you want to add to the list.
118: * @param convertor Used to create objects of the chosen class.
119: * @return a new very lazy list.
120: */
121: public static List decorate(final List list,
122: final Class targetClass, final FieldValueConvertor convertor) {
123: if (logger.isDebugEnabled()) {
124: logger.debug("decorate(List list = " + list
125: + ", Class targetClass = " + targetClass
126: + ", FieldValueConvertor convertor = " + convertor
127: + ") - start");
128: }
129:
130: List returnList = new VeryLazyList(list, targetClass, convertor);
131: if (logger.isDebugEnabled()) {
132: logger.debug("decorate - end - return value = "
133: + returnList);
134: }
135: return returnList;
136: }
137:
138: /**
139: * <copyDoc>Refer to {@link #decorate(List, Class, FieldValueConvertor)}.
140: * </copyDoc>
141: */
142: private FieldValueConvertor convertor;
143: /**
144: * <copyDoc>Refer to {@link #decorate(List, Class, FieldValueConvertor)}.
145: * </copyDoc>
146: */
147: private Class targetClass;
148:
149: /**
150: * Calls super constructor directly.
151: *
152: * @param listParam
153: * <copyDoc>Refer to {@link List#set}.</copyDoc>
154: * @param targetClassParam
155: * <copyDoc>Refer to {@link #decorate(List, Class, FieldValueConvertor)}.
156: * </copyDoc>
157: * @param convertorParam
158: * <copyDoc>Refer to {@link #decorate(List, Class, FieldValueConvertor)}.
159: * </copyDoc>
160: */
161: protected VeryLazyList(final List listParam,
162: final Class targetClassParam,
163: final FieldValueConvertor convertorParam) {
164: super (listParam, PrototypeFactory.getInstance(convertorParam
165: .convertFromString(targetClassParam, "")));
166: this .convertor = convertorParam;
167: this .targetClass = targetClassParam;
168: }
169:
170: /**
171: * Overridden to read the object's string value and convert it before adding
172: * to the list.
173: *
174: * @param indexParam
175: * <copyDoc>Refer to {@link List#set}.</copyDoc>
176: * @param elementParam
177: * <copyDoc>Refer to {@link List#set}.</copyDoc>
178: * @return
179: * <copyDoc>Refer to {@link List#set}.</copyDoc>
180: */
181: public synchronized Object set(final int indexParam,
182: final Object elementParam) {
183: if (logger.isDebugEnabled()) {
184: logger.debug("set(int indexParam = " + indexParam
185: + ", Object elementParam = " + elementParam
186: + ") - start");
187: }
188:
189: while (indexParam >= this .size()) {
190: super .add(factory.create());
191: }
192: String convertFrom;
193: if (elementParam instanceof List) {
194: convertFrom = ((List) elementParam).get(0).toString();
195: } else {
196: convertFrom = elementParam.toString();
197: }
198: Object object = convertor.convertFromString(targetClass,
199: convertFrom);
200: Object returnObject = super .set(indexParam, object);
201: if (logger.isDebugEnabled()) {
202: logger.debug("set(int, Object) - end - return value = "
203: + returnObject);
204: }
205: return returnObject;
206: }
207:
208: }
|