EMMA Coverage Report (generated Thu May 22 12:08:10 CDT 2014)
[all classes][org.springframework.batch.core.repository.dao]

COVERAGE SUMMARY FOR SOURCE FILE [MapJobInstanceDao.java]

nameclass, %method, %block, %line, %
MapJobInstanceDao.java100% (2/2)100% (10/10)100% (202/203)97%  (35/36)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class MapJobInstanceDao100% (1/1)100% (8/8)99%  (187/188)97%  (34/35)
getJobInstance (Long): JobInstance 100% (1/1)96%  (25/26)83%  (5/6)
MapJobInstanceDao (): void 100% (1/1)100% (19/19)100% (4/4)
clear (): void 100% (1/1)100% (4/4)100% (2/2)
createJobInstance (String, JobParameters): JobInstance 100% (1/1)100% (39/39)100% (5/5)
getJobInstance (JobExecution): JobInstance 100% (1/1)100% (3/3)100% (1/1)
getJobInstance (String, JobParameters): JobInstance 100% (1/1)100% (16/16)100% (1/1)
getJobInstances (String, int, int): List 100% (1/1)100% (53/53)100% (10/10)
getJobNames (): List 100% (1/1)100% (28/28)100% (6/6)
     
class MapJobInstanceDao$1100% (1/1)100% (2/2)100% (15/15)100% (2/2)
MapJobInstanceDao$1 (MapJobInstanceDao): void 100% (1/1)100% (6/6)100% (1/1)
compare (JobInstance, JobInstance): int 100% (1/1)100% (9/9)100% (1/1)

1/*
2 * Copyright 2006-2013 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17package org.springframework.batch.core.repository.dao;
18 
19import java.util.ArrayList;
20import java.util.Collections;
21import java.util.Comparator;
22import java.util.List;
23import java.util.Map;
24import java.util.concurrent.ConcurrentHashMap;
25import java.util.concurrent.atomic.AtomicLong;
26 
27import org.springframework.batch.core.DefaultJobKeyGenerator;
28import org.springframework.batch.core.JobExecution;
29import org.springframework.batch.core.JobInstance;
30import org.springframework.batch.core.JobKeyGenerator;
31import org.springframework.batch.core.JobParameters;
32import org.springframework.util.Assert;
33 
34/**
35 * In-memory implementation of {@link JobInstanceDao}.
36 */
37public class MapJobInstanceDao implements JobInstanceDao {
38 
39        // JDK6 Make a ConcurrentSkipListSet: tends to add on end
40        private final Map<String, JobInstance> jobInstances = new ConcurrentHashMap<String, JobInstance>();
41        //        private final Set<JobInstance> jobInstances = new CopyOnWriteArraySet<JobInstance>();
42 
43        private JobKeyGenerator<JobParameters> jobKeyGenerator = new DefaultJobKeyGenerator();
44 
45        private final AtomicLong currentId = new AtomicLong(0L);
46 
47        public void clear() {
48                jobInstances.clear();
49        }
50 
51        @Override
52        public JobInstance createJobInstance(String jobName, JobParameters jobParameters) {
53 
54                Assert.state(getJobInstance(jobName, jobParameters) == null, "JobInstance must not already exist");
55 
56                JobInstance jobInstance = new JobInstance(currentId.getAndIncrement(), jobName);
57                jobInstance.incrementVersion();
58                jobInstances.put(jobName + jobKeyGenerator.generateKey(jobParameters), jobInstance);
59 
60                return jobInstance;
61        }
62 
63        @Override
64        public JobInstance getJobInstance(String jobName, JobParameters jobParameters) {
65                return jobInstances.get(jobName + jobKeyGenerator.generateKey(jobParameters));
66        }
67 
68        @Override
69        public JobInstance getJobInstance(Long instanceId) {
70                for (Map.Entry<String, JobInstance> instanceEntry : jobInstances.entrySet()) {
71                        JobInstance instance = instanceEntry.getValue();
72                        if (instance.getId().equals(instanceId)) {
73                                return instance;
74                        }
75                }
76                return null;
77        }
78 
79        @Override
80        public List<String> getJobNames() {
81                List<String> result = new ArrayList<String>();
82                for (Map.Entry<String, JobInstance> instanceEntry : jobInstances.entrySet()) {
83                        result.add(instanceEntry.getValue().getJobName());
84                }
85                Collections.sort(result);
86                return result;
87        }
88 
89        @Override
90        public List<JobInstance> getJobInstances(String jobName, int start, int count) {
91                List<JobInstance> result = new ArrayList<JobInstance>();
92                for (Map.Entry<String, JobInstance> instanceEntry : jobInstances.entrySet()) {
93                        JobInstance instance = instanceEntry.getValue();
94                        if (instance.getJobName().equals(jobName)) {
95                                result.add(instance);
96                        }
97                }
98                Collections.sort(result, new Comparator<JobInstance>() {
99                        // sort by ID descending
100                        @Override
101                        public int compare(JobInstance o1, JobInstance o2) {
102                                return Long.signum(o2.getId() - o1.getId());
103                        }
104                });
105 
106                int startIndex = Math.min(start, result.size());
107                int endIndex = Math.min(start + count, result.size());
108                return result.subList(startIndex, endIndex);
109        }
110 
111        @Override
112        public JobInstance getJobInstance(JobExecution jobExecution) {
113                return jobExecution.getJobInstance();
114        }
115 
116}

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