Java tutorial
/* **************************************************************************** * * * (c) Copyright 2006 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.finances.web; import java.util.Collection; import java.util.Iterator; import java.util.Map; import java.util.Set; import no.abmu.finances.domain.BoolPostData; import no.abmu.finances.domain.DoublePostData; import no.abmu.finances.domain.FloatPostData; import no.abmu.finances.domain.IntPostData; import no.abmu.finances.domain.PostData; import no.abmu.finances.domain.ReportData; import no.abmu.finances.domain.StringPostData; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * PostDataMap. * * @author Thomas Oldervoll * @author $Author: jens $ * @version $Rev: 14944 $ * @date $Date: 2010-02-16 15:58:12 +0100 (Tue, 16 Feb 2010) $ * @since 2006-02-28 (rev. 2566) * copyright ABM-Utvikling */ public class PostDataMap implements Map { private static final Log logger = (Log) LogFactory.getLog(PostDataMap.class); private ReportData reportData = new ReportData(null); public PostDataMap(ReportData reportData) { if (reportData == null) { logger.warn("reportData == null"); } this.reportData = reportData; } public ReportData getReportData() { return reportData; } private Map getPostData() { return reportData.getPostData(); } public int size() { return getPostData().size(); } public boolean isEmpty() { return reportData.getPostData().isEmpty(); } public boolean containsKey(Object key) { return reportData.getPostData().containsKey(key); } public boolean containsValue(Object value) { return reportData.getPostData().containsValue(value); } public Object get(Object key) { return reportData.createOrGetPostData((String) key); } public Object put(Object key, Object object) { // TODO: Refactor, make more general. Use PropertyEditor mechanism? String code = (String) key; String value = (String) object; PostData postData = reportData.createOrGetPostData(code); if (postData instanceof StringPostData) { ((StringPostData) postData).setValue(value); } if (postData instanceof IntPostData) { ((IntPostData) postData).setValue(Integer.getInteger(value)); } if (postData instanceof BoolPostData) { // TODO: this probably doesn't even work since // the booelan fields are checkboxes and might not send "true" or "false" ((BoolPostData) postData).setValue(Boolean.valueOf(value)); } if (postData instanceof FloatPostData) { ((FloatPostData) postData).setValue(Float.valueOf(value)); } if (postData instanceof DoublePostData) { ((DoublePostData) postData).setValue(Double.valueOf(value)); } reportData.addPostData(postData); return null; } public Object remove(Object key) { return reportData.getPostData().remove(key); } public void putAll(Map map) { reportData.getPostData().putAll(map); } public void clear() { reportData.getPostData().clear(); } public Set keySet() { return reportData.getPostData().keySet(); } public Collection values() { return reportData.getPostData().values(); } public Set entrySet() { return reportData.getPostData().entrySet(); } public boolean equals(Object other) { return reportData.equals(other); } public int hashCode() { return reportData.getPostData().hashCode(); } public String toString() { if (entrySet() == null) { return "null"; } if (entrySet().size() == 0) { return "empty"; } StringBuffer buf = new StringBuffer(); buf.append("\n"); for (Iterator i = entrySet().iterator(); i.hasNext();) { Map.Entry entry = (Map.Entry) i.next(); buf.append(entry.getKey() + "=" + entry.getValue() + System.getProperty("line.separator")); } return buf.toString(); } }