1 | package fr.sii.ogham.core.id.generator; | |
2 | ||
3 | import java.io.UnsupportedEncodingException; | |
4 | import java.net.URLEncoder; | |
5 | import java.util.concurrent.atomic.AtomicLong; | |
6 | ||
7 | import org.slf4j.Logger; | |
8 | import org.slf4j.LoggerFactory; | |
9 | ||
10 | /** | |
11 | * Generates a simple sequence: | |
12 | * <ul> | |
13 | * <li>Either 1, 2, 3, ..., n</li> | |
14 | * <li>Or {@code <name>}1, {@code <name>}2, {@code <name>}3, ..., | |
15 | * {@code <name>}n</li> | |
16 | * </ul> | |
17 | * | |
18 | * <p> | |
19 | * Name is URL encoded | |
20 | * | |
21 | * <p> | |
22 | * If the generated id is greater than {@link Long#MAX_VALUE}, then it restarts | |
23 | * from 0. | |
24 | * | |
25 | * @author Aurélien Baudet | |
26 | * | |
27 | */ | |
28 | public class SequentialIdGenerator implements IdGenerator { | |
29 | private static final Logger LOG = LoggerFactory.getLogger(SequentialIdGenerator.class); | |
30 | ||
31 | private final AtomicLong idx; | |
32 | private final boolean useNamePrefix; | |
33 | ||
34 | /** | |
35 | * The sequence starts at 0 and doesn't use the name (only a number). | |
36 | */ | |
37 | public SequentialIdGenerator() { | |
38 | this(false); | |
39 | } | |
40 | ||
41 | /** | |
42 | * The sequence starts at 0 and use the name only if {@code useNamePrefix} | |
43 | * parameter is true. | |
44 | * | |
45 | * @param useNamePrefix | |
46 | * use the name in the generated sequence | |
47 | */ | |
48 | public SequentialIdGenerator(boolean useNamePrefix) { | |
49 | this(useNamePrefix, 0); | |
50 | } | |
51 | ||
52 | /** | |
53 | * The sequence starts at {@code initial} parameter value and use the name | |
54 | * only if {@code useNamePrefix} parameter is true. | |
55 | * | |
56 | * @param useNamePrefix | |
57 | * use the name in the generated sequence | |
58 | * @param initial | |
59 | * the sequence start value | |
60 | */ | |
61 | public SequentialIdGenerator(boolean useNamePrefix, int initial) { | |
62 | super(); | |
63 | this.idx = new AtomicLong(initial); | |
64 | this.useNamePrefix = useNamePrefix; | |
65 | } | |
66 | ||
67 | @Override | |
68 | public String generate(String name) { | |
69 | long id = idx.getAndIncrement(); | |
70 |
6
1. generate : changed conditional boundary → NO_COVERAGE 2. generate : negated conditional → NO_COVERAGE 3. generate : changed conditional boundary → KILLED 4. generate : changed conditional boundary → KILLED 5. generate : negated conditional → KILLED 6. generate : negated conditional → KILLED |
if (id < 0) { |
71 |
1
1. generate : Replaced long addition with subtraction → NO_COVERAGE |
id = id + Long.MIN_VALUE; |
72 | } | |
73 |
3
1. generate : replaced return value with "" for fr/sii/ogham/core/id/generator/SequentialIdGenerator::generate → NO_COVERAGE 2. generate : replaced return value with "" for fr/sii/ogham/core/id/generator/SequentialIdGenerator::generate → KILLED 3. generate : replaced return value with "" for fr/sii/ogham/core/id/generator/SequentialIdGenerator::generate → KILLED |
return namePrefix(name) + id; |
74 | } | |
75 | ||
76 | private String namePrefix(String name) { | |
77 |
3
1. namePrefix : negated conditional → NO_COVERAGE 2. namePrefix : negated conditional → KILLED 3. namePrefix : negated conditional → KILLED |
if (!useNamePrefix) { |
78 | return ""; | |
79 | } | |
80 | try { | |
81 |
2
1. namePrefix : replaced return value with "" for fr/sii/ogham/core/id/generator/SequentialIdGenerator::namePrefix → NO_COVERAGE 2. namePrefix : replaced return value with "" for fr/sii/ogham/core/id/generator/SequentialIdGenerator::namePrefix → KILLED |
return URLEncoder.encode(name, "UTF-8"); |
82 | } catch (UnsupportedEncodingException e) { | |
83 | LOG.warn("Failed to use {} as id prefix => no prefix used", name, e); | |
84 | return ""; | |
85 | } | |
86 | } | |
87 | ||
88 | } | |
Mutations | ||
70 |
1.1 2.2 3.3 4.4 5.5 6.6 |
|
71 |
1.1 |
|
73 |
1.1 2.2 3.3 |
|
77 |
1.1 2.2 3.3 |
|
81 |
1.1 2.2 |