-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample.java
77 lines (67 loc) · 2.18 KB
/
sample.java
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
73
74
75
76
77
package sample.java.project;
import com.beust.jcommander.JCommander;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
/**
* * The main class of the application. It contains the main() method,
* * the first method called.
* */
@NoArgsConstructor
@AllArgsConstructor
public class SampleJavaProject implements Runnable {
/** The delay between printed messages. */
private static final long PRINT_DELAY = 1000L;
/** The name to be printed in the output message. */
@Getter @Setter @NonNull
@Parameter(names = "--name", description = "set the user's name",
required = true)
private String name = "world";
/** Command line parameter for --loop. */
@Parameter(names = "--loop", description = "print endlessly, hotswap demo")
private boolean loop = false;
/** Command line parameter for --help. */
@Parameter(names = { "-h", "--help" }, description = "print help message")
private boolean help = false;
/**
* * Print the "Hello, world!" string.
* * @param args application input arguments
* */
public static void main(final String[] args) {
/* Parse command line arguments. */
SampleJavaProject sjp = new SampleJavaProject();
try {
JCommander jc = new JCommander(sjp, args);
if (sjp.help) {
jc.usage();
return;
}
} catch (ParameterException e) {
System.err.println("error: " + e.getMessage());
new JCommander(new SampleJavaProject()).usage();
System.exit(-1);
}
sjp.run();
}
/**
* * Print the "Hello, world!" string.
* */
public final void sayHello() {
System.out.printf("Hello, %s!%n", name);
}
@Override
public final void run() {
do {
sayHello();
try {
Thread.sleep(PRINT_DELAY);
} catch (InterruptedException e) {
return;
}
} while (loop);
}
}