1 package fr.ifremer.quadrige3.core.dao.technical;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import org.apache.commons.collections4.CollectionUtils;
27 import org.apache.commons.lang3.StringUtils;
28
29 import java.util.*;
30 import java.util.regex.Matcher;
31 import java.util.regex.Pattern;
32
33
34
35
36
37
38 public class StringIterator implements Iterator<String> {
39
40 public static final String DEFAULT_PATTERN = "%s";
41 private final String pattern;
42
43 private int number;
44
45
46
47
48
49
50
51
52 public static StringIterator newStringIteratorByProperty(Collection<?> objects, String propertyName) {
53 return newStringIteratorByProperty(objects, propertyName, null);
54 }
55
56
57
58
59
60
61
62
63
64 public static StringIterator newStringIteratorByProperty(Collection<?> objects, String propertyName, String preferredPrefix) {
65
66
67 String preferredPattern = StringUtils.isNotBlank(preferredPrefix) ? preferredPrefix + DEFAULT_PATTERN : null;
68
69 if (CollectionUtils.isEmpty(objects) || StringUtils.isBlank(propertyName)) {
70 return StringUtils.isNotBlank(preferredPattern)
71 ? new StringIterator(preferredPattern, 0)
72 : new StringIterator();
73 }
74
75 Map<String, Integer> patternMap = new HashMap<>();
76 if (StringUtils.isNotBlank(preferredPattern)) {
77 patternMap.put(preferredPattern, 0);
78 }
79
80 for (Object object : objects) {
81 String property = Beans.getProperty(object, propertyName);
82 if (StringUtils.isNotBlank(property)) {
83
84
85 Matcher matcher = Pattern.compile("(\\d+)(?!.*\\d)").matcher(property);
86 if (matcher.find()) {
87
88
89 int thisIncrement = Integer.parseInt(matcher.group(1));
90 String thisPattern = matcher.replaceAll(DEFAULT_PATTERN);
91 Integer actualIncrement = patternMap.get(thisPattern);
92 if (actualIncrement != null) {
93 thisIncrement = Math.max(actualIncrement, thisIncrement);
94 }
95 patternMap.put(thisPattern, thisIncrement);
96
97 } else {
98
99
100 int thisIncrement = 0;
101 String thisPattern = property + DEFAULT_PATTERN;
102 Integer actualIncrement = patternMap.get(thisPattern);
103 if (actualIncrement != null) {
104 thisIncrement = Math.max(actualIncrement, thisIncrement);
105 }
106 patternMap.put(thisPattern, thisIncrement);
107 }
108 }
109 }
110
111 if (StringUtils.isNotBlank(preferredPattern)) {
112
113
114 return new StringIterator(preferredPattern, patternMap.get(preferredPattern));
115 }
116
117 if (!patternMap.isEmpty()) {
118
119 String pattern;
120 if (patternMap.size() == 1) {
121
122
123 pattern = patternMap.keySet().iterator().next();
124
125 } else {
126
127
128 List<String> patterns = new ArrayList<>(patternMap.keySet());
129 Collections.sort(patterns);
130 pattern = patterns.get(patterns.size() - 1);
131 }
132 return new StringIterator(pattern, patternMap.get(pattern));
133 }
134
135 return new StringIterator();
136 }
137
138
139
140
141 public StringIterator() {
142 this(DEFAULT_PATTERN, 0);
143 }
144
145
146
147
148
149
150
151 public StringIterator(String pattern, int number) {
152 this.pattern = pattern;
153 this.number = number;
154 }
155
156
157
158
159 @Override
160 public boolean hasNext() {
161 return true;
162 }
163
164
165
166
167 @Override
168 public String next() {
169 number++;
170 return String.format(pattern, number);
171 }
172
173
174
175
176 @Override
177 public void remove() {
178
179 }
180
181 public String getPattern() {
182 return pattern;
183 }
184
185 public String getPrefix() {
186 return pattern.replace(DEFAULT_PATTERN, "");
187 }
188
189 }