1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.sw4j.tool.barcode.random.generator;
18
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Random;
25 import java.util.Set;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28 import org.sw4j.tool.barcode.random.config.EncodingConfig;
29 import org.sw4j.tool.barcode.random.encoder.ByteArrayEncoder;
30
31
32
33
34
35
36
37
38
39
40 public class RandomIdent implements IdentValue {
41
42
43
44
45
46
47 private final String ident;
48
49
50
51
52
53
54 private final byte[] random;
55
56
57
58
59
60
61 private final Map<EncodingConfig, String> encoded;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public RandomIdent(final String ident, final int randomSize, final Random rng,
80 final Set<EncodingConfig> encodings) {
81 this.ident = ident;
82 random = new byte[randomSize / 8];
83 rng.nextBytes(random);
84 encoded = new HashMap<>();
85 encodings.forEach((encoding) -> {
86 String rawEncoding = encoding.getType();
87 int endIndex = -1;
88 int startIndex = -1;
89 boolean hasMinus = false;
90 if (rawEncoding.matches(".+\\{\\d*-?\\d+\\}")) {
91 Pattern p = Pattern.compile("(.+)\\{((\\d*)-)?(\\d+)\\}");
92 Matcher m = p.matcher(rawEncoding);
93 m.matches();
94 rawEncoding = m.group(1);
95 if (m.group(2) != null && m.group(2).length() > 1) {
96 startIndex = Integer.parseInt(m.group(2).substring(0, m.group(2).length() - 1));
97 }
98 String startIndexGroup = m.group(2);
99 hasMinus = m.group(3) != null;
100 endIndex = Integer.parseInt(m.group(4));
101 }
102 ByteArrayEncoder encoder = ByteArrayEncoder.forEncoding(rawEncoding);
103 if (encoder == null) {
104 throw new IllegalArgumentException(
105 String.format("Cannot find an encoder for encoding %s", rawEncoding));
106 }
107 String encodedValue = encoder.encode(random);
108 if (endIndex > 0) {
109 if (hasMinus) {
110 if (startIndex >= 0) {
111 encodedValue = encodedValue.substring(startIndex, endIndex);
112 } else {
113 encodedValue = encodedValue.substring(encodedValue.length() - endIndex);
114 }
115 } else {
116 encodedValue = encodedValue.substring(0, endIndex);
117 }
118 }
119 if (encoding.getSepCount() != null) {
120 int partsCount = encodedValue.length() / encoding.getSepCount();
121 if (encodedValue.length() % encoding.getSepCount() != 0) {
122 partsCount++;
123 }
124 List<String> parts = new ArrayList<>(partsCount);
125 for (int i = 0; i < partsCount; i++) {
126 int endPos = (i + 1) * encoding.getSepCount() < encodedValue.length() ?
127 (i + 1) * encoding.getSepCount() :
128 encodedValue.length();
129 parts.add(encodedValue.substring(i * encoding.getSepCount(),
130 endPos));
131 }
132 StringBuilder sb = new StringBuilder();
133 for (int i = 0; i < parts.size(); i++) {
134 if (i > 0) {
135 sb.append(encoding.getSepChar());
136 }
137 sb.append(parts.get(i));
138 }
139 encodedValue = sb.toString();
140 }
141 encoded.put(encoding, encodedValue);
142 });
143 }
144
145
146
147
148
149
150
151 @Override
152 public String getIdent() {
153 return ident;
154 }
155
156
157
158
159
160
161
162 @Override
163 public byte[] getValue() {
164 return Arrays.copyOf(random, random.length);
165 }
166
167
168
169
170
171
172
173
174
175 @Override
176 public String getEncoded(final EncodingConfig encoding) {
177 return encoded.get(encoding);
178 }
179
180 }