EMMA Coverage Report (generated Tue May 06 07:29:23 PDT 2008)
[all classes][org.springframework.batch.core]

COVERAGE SUMMARY FOR SOURCE FILE [JobParameters.java]

nameclass, %method, %block, %line, %
JobParameters.java100% (1/1)100% (17/17)100% (310/310)100% (55/55)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class JobParameters100% (1/1)100% (17/17)100% (310/310)100% (55/55)
JobParameters (): void 100% (1/1)100% (23/23)100% (6/6)
JobParameters (Map, Map, Map, Map): void 100% (1/1)100% (70/70)100% (10/10)
copyDateMap (Map): Map 100% (1/1)100% (32/32)100% (7/7)
equals (Object): boolean 100% (1/1)100% (23/23)100% (8/8)
getDate (String): Date 100% (1/1)100% (6/6)100% (1/1)
getDateParameters (): Map 100% (1/1)100% (4/4)100% (1/1)
getDouble (String): Double 100% (1/1)100% (6/6)100% (1/1)
getDoubleParameters (): Map 100% (1/1)100% (4/4)100% (1/1)
getLong (String): Long 100% (1/1)100% (6/6)100% (1/1)
getLongParameters (): Map 100% (1/1)100% (4/4)100% (1/1)
getParameters (): Map 100% (1/1)100% (21/21)100% (5/5)
getString (String): String 100% (1/1)100% (6/6)100% (1/1)
getStringParameters (): Map 100% (1/1)100% (4/4)100% (1/1)
hashCode (): int 100% (1/1)100% (19/19)100% (1/1)
isEmpty (): boolean 100% (1/1)100% (20/20)100% (1/1)
toString (): String 100% (1/1)100% (21/21)100% (1/1)
validateMap (Map, Class): void 100% (1/1)100% (41/41)100% (8/8)

1/**
2 * 
3 */
4package org.springframework.batch.core;
5 
6import java.util.Collections;
7import java.util.Date;
8import java.util.Iterator;
9import java.util.LinkedHashMap;
10import java.util.Map;
11import java.util.Map.Entry;
12 
13import org.apache.commons.lang.builder.HashCodeBuilder;
14 
15/**
16 * Value object representing runtime parameters to a batch job. Because the
17 * parameters have no individual meaning outside of the JobParameters they are
18 * contained within, it is a value object rather than an entity. It is also
19 * extremely important that a parameters object can be reliably compared to
20 * another for equality, in order to determine if one JobParameters object
21 * equals another. Furthermore, because these parameters will need to be
22 * persisted, it is vital that the types added are restricted.
23 * 
24 * @author Lucas Ward
25 * @since 1.0
26 */
27public class JobParameters {
28 
29        private final Map stringMap;
30 
31        private final Map longMap;
32 
33        private final Map doubleMap;
34 
35        private final Map dateMap;
36 
37        /**
38         * Default constructor. Creates a new empty JobRuntimeParameters. It should
39         * be noted that this constructor should only be used if an empty parameters
40         * is needed, since JobRuntimeParameters is immutable.
41         */
42        public JobParameters() {
43                this.stringMap = new LinkedHashMap();
44                this.longMap = new LinkedHashMap();
45                this.doubleMap = new LinkedHashMap();
46                this.dateMap = new LinkedHashMap();
47        }
48 
49        /**
50         * Create a new parameters object based upon the maps for each of the
51         * supported data types. See {@link JobParametersBuilder} for an easier way
52         * to create parameters.
53         */
54        public JobParameters(Map stringMap, Map longMap, Map doubleMap, Map dateMap) {
55                super();
56 
57                validateMap(stringMap, String.class);
58                validateMap(longMap, Long.class);
59                validateMap(doubleMap, Double.class);
60                validateMap(dateMap, Date.class);
61                this.stringMap = new LinkedHashMap(stringMap);
62                this.longMap = new LinkedHashMap(longMap);
63                this.doubleMap = new LinkedHashMap(doubleMap);
64                this.dateMap = copyDateMap(dateMap);
65        }
66 
67        /**
68         * Typesafe Getter for the String represented by the provided key.
69         * 
70         * @param key The key to get a value for
71         * @return The <code>String</code> value
72         */
73        public String getString(String key) {
74                return (String) stringMap.get(key);
75        }
76 
77        /**
78         * Typesafe Getter for the Long represented by the provided key.
79         * 
80         * @param key The key to get a value for
81         * @return The <code>Long</code> value
82         */
83        public Long getLong(String key) {
84                return (Long) longMap.get(key);
85        }
86 
87        /**
88         * Typesafe Getter for the Long represented by the provided key.
89         * 
90         * @param key The key to get a value for
91         * @return The <code>Double</code> value
92         */
93        public Double getDouble(String key) {
94                return (Double) doubleMap.get(key);
95        }
96 
97        /**
98         * Typesafe Getter for the Date represented by the provided key.
99         * 
100         * @param key The key to get a value for
101         * @return The <code>java.util.Date</code> value
102         */
103        public Date getDate(String key) {
104                return (Date) dateMap.get(key);
105        }
106 
107        /**
108         * Get a map of all parameters, including string, long, and date. It should
109         * be noted that a Collections$UnmodifiableMap is returned, ensuring
110         * immutability.
111         * 
112         * @return an unmodifiable map containing all parameters.
113         */
114        public Map getParameters() {
115                Map tempMap = new LinkedHashMap(stringMap);
116                tempMap.putAll(longMap);
117                tempMap.putAll(doubleMap);
118                tempMap.putAll(dateMap);
119                return Collections.unmodifiableMap(tempMap);
120        }
121 
122        /**
123         * Get a map of only string parameters.
124         * 
125         * @return String parameters.
126         */
127        public Map getStringParameters() {
128                return Collections.unmodifiableMap(stringMap);
129        }
130 
131        /**
132         * Get a map of only Long parameters
133         * 
134         * @return long parameters.
135         */
136        public Map getLongParameters() {
137                return Collections.unmodifiableMap(longMap);
138        }
139 
140        /**
141         * Get a map of only Double parameters
142         * 
143         * @return long parameters.
144         */
145        public Map getDoubleParameters() {
146                return Collections.unmodifiableMap(doubleMap);
147        }
148 
149        /**
150         * Get a map of only Date parameters
151         * 
152         * @return date parameters.
153         */
154        public Map getDateParameters() {
155                return Collections.unmodifiableMap(dateMap);
156        }
157 
158        /**
159         * @return true if the prameters is empty, false otherwise.
160         */
161        public boolean isEmpty() {
162                return (dateMap.isEmpty() && longMap.isEmpty() && doubleMap.isEmpty() && stringMap.isEmpty());
163        }
164 
165        /*
166         * Convenience method for validating that a the provided map only contains a
167         * particular type as a value, with only a String as a key.
168         */
169        private void validateMap(Map map, Class type) {
170 
171                for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
172 
173                        Entry entry = (Entry) it.next();
174                        if (entry.getKey() instanceof String == false) {
175                                throw new IllegalArgumentException("All parameter keys must be strings.");
176                        }
177                        if (entry.getValue().getClass() != type) {
178                                throw new IllegalArgumentException("The values in this map must be of type:[" + type + "].");
179                        }
180                }
181        }
182 
183        /*
184         * Convenience method for copying Date values to ensure immutability.
185         */
186        private Map copyDateMap(Map dateMap) {
187                Map tempMap = new LinkedHashMap();
188 
189                for (Iterator it = dateMap.entrySet().iterator(); it.hasNext();) {
190                        Entry entry = (Entry) it.next();
191                        Date date = (Date) entry.getValue();
192                        tempMap.put(entry.getKey(), new Date(date.getTime()));
193                }
194 
195                return tempMap;
196        }
197 
198        public boolean equals(Object obj) {
199 
200                if (obj instanceof JobParameters == false) {
201                        return false;
202                }
203 
204                if (this == obj) {
205                        return true;
206                }
207 
208                JobParameters parameters = (JobParameters) obj;
209 
210                // Since the type contained by each map is known, it's safe to call
211                // Map.equals()
212                if (getParameters().equals(parameters.getParameters())) {
213                        return true;
214                }
215                else {
216                        return false;
217                }
218        }
219 
220        public int hashCode() {
221                return new HashCodeBuilder(7, 21).append(stringMap).append(longMap).append(doubleMap).append(dateMap)
222                                .toHashCode();
223        }
224 
225        public String toString() {
226 
227                return stringMap.toString() + longMap.toString() + doubleMap.toString() + dateMap.toString();
228        }
229}

[all classes][org.springframework.batch.core]
EMMA 2.0.5312 (C) Vladimir Roubtsov