re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / utils / Utils.java
1 package org.openecomp.sdc.be.components.utils;
2
3 import org.apache.tinkerpop.shaded.minlog.Log;
4
5 import javax.validation.constraints.NotNull;
6 import java.security.SecureRandom;
7 import java.util.List;
8 import java.util.regex.Matcher;
9 import java.util.regex.Pattern;
10
11 public class Utils {
12
13     private static final Pattern COUNTER_PATTERN = Pattern.compile("\\d+$");
14     private static final SecureRandom random = new SecureRandom();
15
16
17     private Utils() {}
18
19     public static int getNextCounter(@NotNull List<String> existingValues) {
20         if (existingValues.isEmpty()) {
21             return 0;
22         }
23         int maxCurrentCounter = 0;
24         try {
25             maxCurrentCounter = existingValues.stream()
26                     .map(COUNTER_PATTERN::matcher)
27                     .filter(Matcher::find)
28                     .map(matcher -> matcher.group(0))
29                     .mapToInt(Integer::parseInt)
30                     .max()
31                     .orElse(0);
32         }
33         catch (Exception e) {
34             Log.warn("Failed in retrieivng counter from existing value: ", e);
35             maxCurrentCounter = random.nextInt(100) + 50;
36         }
37         return ++maxCurrentCounter;
38     }
39 }