Java tutorial
/* **************************************************************************** * * * (c) Copyright 2009 ABM-utvikling * * * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * This program is distributed in the hope that it will be useful, but * * WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General * * Public License for more details. http://www.gnu.org/licenses/gpl.html * * * **************************************************************************** */ package no.abmu.questionnaire.web; import java.io.Serializable; import java.util.Collection; import java.util.Iterator; import java.util.Map; import java.util.Set; import no.abmu.questionnaire.domain.data.FieldData; import no.abmu.questionnaire.domain.data.SchemaData; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * FieldDataMap. This class dispatches calls to fieldData in the private variable fieldData. * * @author Aase Mestad, aase@jpro.no * @author $Author: jens $ * @version $Rev: 14827 $ * @date $Date: 2010-02-10 07:53:40 +0100 (Wed, 10 Feb 2010) $ * @since 2006-02-28 (rev. 2566) * @copyright ABM-Utvikling */ @SuppressWarnings("serial") public class FieldDataMap implements Map<String, FieldData>, Serializable { private static final Log logger = (Log) LogFactory.getLog(FieldDataMap.class); private SchemaData schemaData; public FieldDataMap(SchemaData schemaData) { if (schemaData == null) { throw new IllegalArgumentException("schemaData is null"); } this.schemaData = schemaData; } public SchemaData getSchemaData() { return schemaData; } private Map<String, FieldData> getFieldData() { return schemaData.getFieldData(); } public int size() { return getFieldData().size(); } public boolean isEmpty() { return schemaData.getFieldData().isEmpty(); } public boolean containsKey(Object key) { return schemaData.getFieldData().containsKey(key); } public boolean containsValue(Object value) { return schemaData.getFieldData().containsValue(value); } public FieldData get(String key) { if (logger.isDebugEnabled()) { logger.debug("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); logger.debug("SchemaData for schema: " + schemaData.getSchema().toString()); logger.debug("SchemaData has FieldData :" + schemaData.getFieldData().size()); logger.debug("Schema has Fields: " + schemaData.getSchema().getFields().size()); logger.debug("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"); } FieldData fieldDate = schemaData.createOrGetFieldData(key); if (logger.isDebugEnabled()) { logger.debug( "[FieldDataMap] Get object for " + key.toString() + " of type " + fieldDate.getClass().getName() + " and has value [" + (fieldDate == null ? "null" : fieldDate.toString()) + "]."); } return fieldDate; } /** * TODO: Is this used? * @param key * @param object * @return */ /* public void put(String key, FieldData object) { logger.info("[FieldDataMap] Put on key " + key.toString() + " with value " + object.toString()); // TODO: Refactor, make more general. Use PropertyEditor mechanism? String code = (String) key; String value = (String) object; FieldData fieldData = schemaData.createOrGetFieldData(code); if (fieldData instanceof StringFieldData) { ((StringFieldData) fieldData).setValue(value); } if (fieldData instanceof LongFieldData) { ((LongFieldData) fieldData).setValue(Long.getLong(value)); } if (fieldData instanceof BooleanFieldData) { // TODO: this probably doesn't even work since // the boolean fields are checkboxes and might not send "true" or "false" ((BooleanFieldData) fieldData).setValue(Boolean.valueOf(value)); } if (fieldData instanceof BigDecimalFieldData) { ((BigDecimalFieldData) fieldData).setValue(new BigDecimal(value)); } schemaData.addFieldData(fieldData); } */ public FieldData remove(Object key) { return schemaData.getFieldData().remove(key); } public FieldData put(String key, FieldData fieldData) { schemaData.getFieldData().put(key, fieldData); return fieldData; } public void put(String key, Object value) { if (value instanceof FieldData) { FieldData fieldData = (FieldData) value; put(key, fieldData); } else { FieldData fieldData = schemaData.getFieldData().get(key); if (fieldData != null) { fieldData.setValue(value); } } } public FieldData get(Object key) { return schemaData.getFieldData().get(key); } public void putAll(Map<? extends String, ? extends FieldData> map) { schemaData.getFieldData().putAll(map); } public void clear() { schemaData.getFieldData().clear(); } public Set<String> keySet() { return schemaData.getFieldData().keySet(); } public Collection<FieldData> values() { return schemaData.getFieldData().values(); } public Set<Entry<String, FieldData>> entrySet() { return schemaData.getFieldData().entrySet(); } public boolean equals(Object other) { return schemaData.equals(other); } public int hashCode() { return schemaData.getFieldData().hashCode(); } public String toString() { if (entrySet() == null) { return "null"; } if (entrySet().size() == 0) { return "empty"; } StringBuffer buf = new StringBuffer(); buf.append("\n"); for (Iterator<Entry<String, FieldData>> i = entrySet().iterator(); i.hasNext();) { Map.Entry<String, FieldData> entry = i.next(); buf.append(entry.getKey() + "=" + entry.getValue() + System.getProperty("line.separator")); } return buf.toString(); } }