This repository was archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathSampleJavaProject.java
More file actions
72 lines (65 loc) · 2.22 KB
/
SampleJavaProject.java
File metadata and controls
72 lines (65 loc) · 2.22 KB
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package sample.java.project;
import java.util.Timer;
import java.util.TimerTask;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
/**
* The main class of the application. It contains the main() method,
* the first method called.
*/
@NoArgsConstructor
@AllArgsConstructor
public class SampleJavaProject extends TimerTask {
/** The delay between printed messages. */
private static final long PRINT_DELAY = 1000L;
/** The name to be printed in the output message. */
@Getter @Setter @NonNull
private String name = "world";
/**
* Print the "Hello, world!" string.
* @param args application input arguments
*/
public static void main(final String[] args) {
/* Set up the command line arguments. */
Options options = new Options();
options.addOption(new Option("name", true, "set the user's name"));
options.addOption(new Option("loop", "print endlessly, hotswap demo"));
options.addOption(new Option("help", "print this help message"));
CommandLine line = null;
try {
line = new GnuParser().parse(options, args);
} catch (org.apache.commons.cli.ParseException e) {
System.err.println(e.getMessage());
System.exit(1);
}
/* Handle each argument. */
SampleJavaProject sjp;
if (line.hasOption("help")) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp("SampleJavaProject [options]", options);
System.exit(0);
}
if (line.hasOption("name")) {
sjp = new SampleJavaProject(line.getOptionValue("name"));
} else {
sjp = new SampleJavaProject();
}
if (line.hasOption("loop")) {
new Timer().schedule(sjp, 0L, PRINT_DELAY);
} else {
sjp.run();
}
}
@Override
public final void run() {
System.out.printf("Hello, %s!\n", name);
}
}