1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.api.util;
20
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.StringTokenizer;
24
25
26
27
28
29
30 public class RunOrder {
31 public static final RunOrder ALPHABETICAL = new RunOrder("alphabetical");
32
33 public static final RunOrder FILESYSTEM = new RunOrder("filesystem");
34
35 public static final RunOrder HOURLY = new RunOrder("hourly");
36
37 public static final RunOrder RANDOM = new RunOrder("random");
38
39 public static final RunOrder REVERSE_ALPHABETICAL = new RunOrder("reversealphabetical");
40
41 public static final RunOrder BALANCED = new RunOrder("balanced");
42
43 public static final RunOrder FAILEDFIRST = new RunOrder("failedfirst");
44
45 public static final RunOrder[] DEFAULT = new RunOrder[] {FILESYSTEM};
46
47
48
49
50
51
52
53 public static RunOrder[] valueOfMulti(String values) {
54 List<RunOrder> result = new ArrayList<>();
55 if (values != null) {
56 StringTokenizer stringTokenizer = new StringTokenizer(values, ",");
57 while (stringTokenizer.hasMoreTokens()) {
58 result.add(valueOf(stringTokenizer.nextToken()));
59 }
60 }
61 return result.toArray(new RunOrder[result.size()]);
62 }
63
64 public static RunOrder valueOf(String name) {
65 if (name == null) {
66 return null;
67 } else {
68 RunOrder[] runOrders = values();
69 for (RunOrder runOrder : runOrders) {
70 if (runOrder.matches(name)) {
71 return runOrder;
72 }
73 }
74
75 String errorMessage = createMessageForMissingRunOrder(name);
76 throw new IllegalArgumentException(errorMessage);
77 }
78 }
79
80 private static String createMessageForMissingRunOrder(String name) {
81 RunOrder[] runOrders = values();
82 StringBuilder message = new StringBuilder("There's no RunOrder with the name ");
83 message.append(name);
84 message.append(". Please use one of the following RunOrders: ");
85 for (int i = 0; i < runOrders.length; i++) {
86 if (i != 0) {
87 message.append(", ");
88 }
89 message.append(runOrders[i]);
90 }
91 message.append('.');
92 return message.toString();
93 }
94
95 private static RunOrder[] values() {
96 return new RunOrder[] {ALPHABETICAL, FILESYSTEM, HOURLY, RANDOM, REVERSE_ALPHABETICAL, BALANCED, FAILEDFIRST};
97 }
98
99 public static String asString(RunOrder[] runOrder) {
100 StringBuilder stringBuffer = new StringBuilder();
101 for (int i = 0; i < runOrder.length; i++) {
102 stringBuffer.append(runOrder[i].name);
103 if (i < (runOrder.length - 1)) {
104 stringBuffer.append(",");
105 }
106 }
107 return stringBuffer.toString();
108 }
109
110 private final String name;
111
112 private RunOrder(String name) {
113 this.name = name;
114 }
115
116 private boolean matches(String anotherName) {
117 return name.equalsIgnoreCase(anotherName);
118 }
119
120 public String name() {
121 return name;
122 }
123
124 @Override
125 public String toString() {
126 return name;
127 }
128 }