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(". Use one of the following RunOrders: ");
85 message.append(runOrders[0]);
86 for (int i = 1; i < runOrders.length; i++) {
87 message.append(", ");
88 message.append(runOrders[i]);
89 }
90 message.append('.');
91 return message.toString();
92 }
93
94 private static RunOrder[] values() {
95 return new RunOrder[] {ALPHABETICAL, FILESYSTEM, HOURLY, RANDOM, REVERSE_ALPHABETICAL, BALANCED, FAILEDFIRST};
96 }
97
98 public static String asString(RunOrder[] runOrder) {
99 StringBuilder stringBuffer = new StringBuilder();
100 for (int i = 0; i < runOrder.length - 1; i++) {
101 stringBuffer.append(runOrder[i].name);
102 stringBuffer.append(",");
103 }
104 stringBuffer.append(runOrder[runOrder.length - 1].name);
105 return stringBuffer.toString();
106 }
107
108 private final String name;
109
110 private RunOrder(String name) {
111 this.name = name;
112 }
113
114 private boolean matches(String anotherName) {
115 return name.equalsIgnoreCase(anotherName);
116 }
117
118 public String name() {
119 return name;
120 }
121
122 @Override
123 public String toString() {
124 return name;
125 }
126 }