Clover coverage report - QedeqKernelSe Coverage Report
Coverage timestamp: Do Jan 11 2007 09:03:50 CET
file stats: LOC: 155   Methods: 7
NCLOC: 72   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SimpleAttributes.java 68,8% 67,6% 85,7% 70,2%
coverage coverage
 1    /* $Id: SimpleAttributes.java,v 1.13 2006/10/20 20:23:04 m31 Exp $
 2    *
 3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
 4    *
 5    * Copyright 2000-2006, Michael Meyling <mime@qedeq.org>.
 6    *
 7    * "Hilbert II" is free software; you can redistribute
 8    * it and/or modify it under the terms of the GNU General Public
 9    * License as published by the Free Software Foundation; either
 10    * version 2 of the License, or (at your option) any later version.
 11    *
 12    * This program is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15    * GNU General Public License for more details.
 16    */
 17   
 18    package org.qedeq.kernel.xml.parser;
 19   
 20    import java.text.DateFormat;
 21    import java.text.ParseException;
 22    import java.text.SimpleDateFormat;
 23    import java.util.Date;
 24    import java.util.HashMap;
 25    import java.util.Iterator;
 26    import java.util.Map;
 27    import java.util.SortedMap;
 28    import java.util.TreeMap;
 29   
 30   
 31    /**
 32    * Value object that contains unsorted key value pairs. Both arguments are string type, but there
 33    * are access methods for different data types. If the type conversion in not possible an
 34    * appropriate {@link RuntimeException} is thrown.
 35    * <p>
 36    * With {@link #add} another key value pair is added. An {@link IllegalArgumentException} is thrown,
 37    * if the key is already known.
 38    *
 39    * @version $Revision: 1.13 $
 40    * @author Michael Meyling
 41    */
 42    public final class SimpleAttributes {
 43   
 44    /** Key value storage. */
 45    private Map map = new HashMap();
 46   
 47    /**
 48    * Adds a key value pair. This key must be still unknown.
 49    *
 50    * @param key Key.
 51    * @param value Value, maybe <code>null</code>.
 52    */
 53  14493 public final void add(final String key, final String value) {
 54  14493 if (map.containsKey(key)) {
 55  0 throw new IllegalArgumentException(
 56    "Key " + key + " already known with value: " + map.get(key));
 57    }
 58  14493 map.put(key, value);
 59    }
 60   
 61    /**
 62    * Returns the value for a key. If the key dosn't exist <code>null</code> is returned.
 63    *
 64    * @param key Key.
 65    * @return Associated value.
 66    */
 67  8095 public final String getString(final String key) {
 68  8095 return (String) map.get(key);
 69    }
 70   
 71    /**
 72    * Returns the value for a key as an Integer. If the key dosn't exist
 73    * <code>null</code> is returned.
 74    * If the value must be transformable into an Integer value.
 75    *
 76    * @param key Key.
 77    * @return Associated value converted into an Integer.
 78    */
 79  3708 public final Integer getInteger(final String key) {
 80  3708 String value = (String) map.get(key);
 81  3708 if (value != null) {
 82  3171 value = value.trim();
 83    }
 84  3708 if (value == null || value.length() == 0) {
 85  537 return null;
 86    }
 87  3171 return new Integer(value);
 88    }
 89   
 90    /**
 91    * Returns the value for a key as an Boolean. If the key dosn't exist
 92    * <code>null</code> is returned.
 93    * If the value must be transformable into an Boolean value.
 94    *
 95    * @param key Key.
 96    * @return Associated value converted into an Boolean.
 97    */
 98  220 public final Boolean getBoolean(final String key) {
 99  220 String value = ((String) map.get(key));
 100  220 if (value != null) {
 101  29 value = value.trim();
 102    }
 103  220 if (value == null || value.length() == 0) {
 104  191 return null;
 105    }
 106  29 return Boolean.valueOf(value);
 107    }
 108   
 109    /**
 110    * Returns the value for a key as an Date. If the key dosn't exist
 111    * <code>null</code> is returned.
 112    * If the value must be transformable into an Date value.
 113    * The expected date format is "yyyy-MM-dd'T'HH:mm:ss".
 114    *
 115    * @param key Key.
 116    * @return Associated value converted into an Date.
 117    */
 118  0 public final Date getDate(final String key) {
 119  0 String value = (String) map.get(key);
 120  0 if (value != null) {
 121  0 value = value.trim();
 122    }
 123  0 if (value == null || value.length() == 0) {
 124  0 return null;
 125    }
 126  0 try {
 127  0 DateFormat formater = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
 128  0 Date date = formater.parse(value);
 129  0 return date;
 130    } catch (ParseException e) {
 131  0 throw new IllegalArgumentException(e.toString());
 132    }
 133    }
 134   
 135    /**
 136    * Get the attribute values, sorted by their keys.
 137    *
 138    * @return Key sorted string values.
 139    */
 140  5846 public final String[] getKeySortedStringValues() {
 141  5846 SortedMap sorted = new TreeMap(map);
 142  5846 return (String[]) sorted.values().toArray(new String[0]);
 143    }
 144   
 145  13456 public String toString() {
 146  13456 final StringBuffer buffer = new StringBuffer();
 147  13456 final Iterator iterator = map.entrySet().iterator();
 148  13456 while (iterator.hasNext()) {
 149  14566 Map.Entry entry = (Map.Entry) iterator.next();
 150  14566 buffer.append(entry.getKey() + "=\"" + entry.getValue() + "\" ");
 151    }
 152  13456 return buffer.toString();
 153    }
 154   
 155    }