提交 31d2e03d 编写于 作者: K Kohsuke Kawaguchi

Addd a new utility for a persisted boolean

上级 45b8f82e
package jenkins.util.io;
import jenkins.model.Jenkins;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Uses a presence/absence of a file as a persisted boolean storage.
*
* <p>
* This is convenient when you need to store just a few bits of infrequently accessed information
* as you can forget the explicit persistence of it. This class masks I/O problem, so if the persistence
* fails, you'll get no error report.
*
* @author Kohsuke Kawaguchi
* @since 1.498
*/
public class FileBoolean {
private final File file;
public FileBoolean(File file) {
this.file = file;
}
public FileBoolean(Class owner, String name) {
this(new File(Jenkins.getInstance().getRootDir(),owner.getName().replace('$','.')+'/'+name));
}
/**
* Gets the current state. True if the file exists, false if it doesn't.
*/
public boolean get() {
return file.exists();
}
public boolean isOn() { return get(); }
public boolean isOff() { return !get(); }
public void set(boolean b) {
if (b) on(); else off();
}
public void on() {
try {
file.getParentFile().mkdirs();
new FileOutputStream(file).close();
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to touch "+file);
}
}
public void off() {
file.delete();
}
private static final Logger LOGGER = Logger.getLogger(FileBoolean.class.getName());
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册