File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\jenkins\util\io\FileBoolean.java.html

1 package jenkins.util.io;
2
3 import jenkins.model.Jenkins;
4
5 import java.io.File;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.util.logging.Level;
9 import java.util.logging.Logger;
10
11 /**
12 * Uses a presence/absence of a file as a persisted boolean storage.
13 *
14 * <p>
15 * This is convenient when you need to store just a few bits of infrequently accessed information
16 * as you can forget the explicit persistence of it. This class masks I/O problem, so if the persistence
17 * fails, you'll get no error report.
18 *
19 * @author Kohsuke Kawaguchi
20 * @since 1.498
21 */
22 public class FileBoolean {
23 private final File file;
24
25 public FileBoolean(File file) {
26 this.file = file;
27 }
28
29 public FileBoolean(Class owner, String name) {
30 this(new File(Jenkins.getInstance().getRootDir(),owner.getName().replace('$','.')+'/'+name));
31 }
32
33 /**
34 * Gets the current state. True if the file exists, false if it doesn't.
35 */
36 public boolean get() {
37 return file.exists();
38 }
39
40 public boolean isOn() { return get(); }
41 public boolean isOff() { return !get(); }
42
43 public void set(boolean b) {
44 if (b) on(); else off();
45 }
46
47 public void on() {
48 try {
49 file.getParentFile().mkdirs();
50 new FileOutputStream(file).close();
51 } catch (IOException e) {
52 LOGGER.log(Level.WARNING, "Failed to touch "+file);
53 }
54 }
55
56 public void off() {
57 file.delete();
58 }
59
60 private static final Logger LOGGER = Logger.getLogger(FileBoolean.class.getName());
61 }
62