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