/* DefaultHeaderMap822.java * * Copyright (c) 2002, Benja Fallenstein * * You may use and distribute under the terms of either the GNU Lesser * General Public License, either version 2 of the license or, * at your choice, any later version. Alternatively, you may use and * distribute under the terms of the XPL. * * See the LICENSE.lgpl and LICENSE.xpl files for the specific terms of * the licenses. * * This software 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 README * file for more details. * */ /* * Written by Benja Fallenstein */ package gzz.util.headers; import java.util.*; public class DefaultHeaderMap822 implements HeaderMap822 { protected Map fields; protected DefaultHeaderMap822(Map fields, HeaderMap822 initializeWith) { this.fields = fields; if(initializeWith != null) { for(Iterator i=initializeWith.keySet().iterator(); i.hasNext();) { String fieldName = (String)i.next(); fieldName = fieldName.toLowerCase(); Collection c = createCollection(); c.addAll(initializeWith.getAll(fieldName)); fields.put(fieldName, c); } } } public DefaultHeaderMap822() { this(new HashMap(), null); } public DefaultHeaderMap822(HeaderMap822 initializeWith) { this(new HashMap(), initializeWith); } protected Collection createCollection() { return new ArrayList(); } protected final Collection getOrCreateCollection(String fieldName) { fieldName = fieldName.toLowerCase(); Collection c = (Collection)fields.get(fieldName); if(c == null) { c = createCollection(); fields.put(fieldName, c); } return c; } public String get(String fieldName) throws NoSuchElementException, MoreThanOneElementException { Collection c = getOrCreateCollection(fieldName); if(c.isEmpty()) throw new NoSuchElementException(fieldName); if(c.size() > 1) throw new MoreThanOneElementException(fieldName); return (String)c.iterator().next(); } public Collection getAll(String fieldName) { Collection c = getOrCreateCollection(fieldName); return Collections.unmodifiableCollection(c); } public Set keySet() { return fields.keySet(); } public void add(String fieldName, String fieldValue) { getOrCreateCollection(fieldName).add(fieldValue); } }