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