Clover Coverage Report
Coverage timestamp: Sa Aug 2 2008 13:56:27 CEST
../../../../../img/srcFileCovDistChart6.png 69% of files have more coverage
34   155   19   4,86
16   72   0,56   7
7     2,71  
1    
 
  SimpleAttributes       Line # 42 34 19 54,4% 0.54385966
 
  (128)
 
1    /* $Id: SimpleAttributes.java,v 1.1 2008/07/26 08:00:50 m31 Exp $
2    *
3    * This file is part of the project "Hilbert II" - http://www.qedeq.org
4    *
5    * Copyright 2000-2008, 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.1 $
40    * @author Michael Meyling
41    */
 
42    public 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  35404 toggle public final void add(final String key, final String value) {
54  35404 if (map.containsKey(key)) {
55  0 throw new IllegalArgumentException(
56    "Key " + key + " already known with value: " + map.get(key));
57    }
58  35404 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  19266 toggle public final String getString(final String key) {
68  19266 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  4566 toggle public final Integer getInteger(final String key) {
80  4566 String value = (String) map.get(key);
81  4566 if (value != null) {
82  3930 value = value.trim();
83    }
84  4566 if (value == null || value.length() == 0) {
85  636 return null;
86    }
87  3930 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  706 toggle public final Boolean getBoolean(final String key) {
99  706 String value = ((String) map.get(key));
100  706 if (value != null) {
101  79 value = value.trim();
102    }
103  706 if (value == null || value.length() == 0) {
104  627 return null;
105    }
106  79 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 toggle 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  19820 toggle public final String[] getKeySortedStringValues() {
141  19820 SortedMap sorted = new TreeMap(map);
142  19820 return (String[]) sorted.values().toArray(new String[0]);
143    }
144   
 
145  0 toggle public String toString() {
146  0 final StringBuffer buffer = new StringBuffer();
147  0 final Iterator iterator = map.entrySet().iterator();
148  0 while (iterator.hasNext()) {
149  0 Map.Entry entry = (Map.Entry) iterator.next();
150  0 buffer.append(entry.getKey() + "=\"" + entry.getValue() + "\" ");
151    }
152  0 return buffer.toString();
153    }
154   
155    }