Last active
September 25, 2015 03:52
-
-
Save varrix/1fb8bad5d010ec2897e2 to your computer and use it in GitHub Desktop.
HoconFile (verbose version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package <your-package-name>; | |
import com.google.common.base.Optional; | |
import com.google.common.base.Preconditions; | |
import ninja.leaping.configurate.commented.CommentedConfigurationNode; | |
import ninja.leaping.configurate.hocon.HoconConfigurationLoader; | |
import org.slf4j.Logger; | |
import java.io.File; | |
import java.io.IOException; | |
import java.net.URL; | |
/** Utility class for creating and manpiulating HOCON files with Configurate. */ | |
public class HoconFile { | |
private final String fileName; | |
private File folder; | |
private File file; | |
private HoconConfigurationLoader loader; | |
private CommentedConfigurationNode node; | |
private Logger logger; | |
private boolean verbose; | |
private boolean loaded; | |
private boolean destroyed; | |
private boolean canSave; | |
public HoconFile(File folder, String fileName) { | |
this.folder = folder; | |
this.fileName = fileName + ".conf"; | |
this.file = Preconditions.checkNotNull(new File(folder + File.separator + this.fileName), "File cannot be null!"); | |
loader = HoconConfigurationLoader.builder().setFile(file).build(); | |
canSave = true; | |
} | |
public File getFolder() { | |
return folder; | |
} | |
public File getFile() { | |
return file; | |
} | |
public boolean isVerbose() { | |
return verbose; | |
} | |
public void setVerbose(boolean verbose) { | |
this.verbose = verbose; | |
} | |
public boolean isDestroyed() { | |
return destroyed; | |
} | |
public boolean isLoaded() { | |
return loaded; | |
} | |
public CommentedConfigurationNode getRoot() { | |
return node; | |
} | |
public boolean canSave() { | |
return canSave; | |
} | |
public void setCanSave(boolean canSave) { | |
this.canSave = canSave(); | |
} | |
public Optional<Logger> getLogger() { | |
return Optional.of(logger); | |
} | |
public void setLogger(Logger newLogger) { | |
logger = newLogger; | |
} | |
public boolean mergeDefaults() { | |
if (destroyed) { | |
if (isLogging()) { | |
logger.debug("Defaults for file `" + fileName + "` could not be merged as the HoconFile object has been destroyed!"); | |
} | |
return false; | |
} | |
URL resource = this.getClass().getResource("/" + fileName); | |
if (resource == null) { | |
if (isLogging()) { | |
logger.debug("Resource for file: " + fileName + " could not be located! Is it in the JAR?"); | |
} | |
return false; | |
} | |
HoconConfigurationLoader defaultsLoader = HoconConfigurationLoader.builder().setURL(resource).build(); | |
try { | |
CommentedConfigurationNode defaults = defaultsLoader.load(); | |
if (!loaded) { | |
load(); | |
} | |
node.mergeValuesFrom(defaults); | |
loader.save(node); | |
} catch (IOException exc) { | |
exc.printStackTrace(); | |
return false; | |
} | |
if (isLogging()) { | |
logger.debug("Defaults merged into " + file.getAbsolutePath() + "."); | |
} | |
return true; | |
} | |
public void save() { | |
if (destroyed && isLogging()) { | |
logger.debug("Could not save a HoconFile object because it has already been destroyed!"); | |
return; | |
} | |
try { | |
loader.save(node); | |
if (isLogging()) { | |
logger.debug("Saved HoconFile " + file.getAbsolutePath()); | |
} | |
} catch (IOException exc) { | |
exc.printStackTrace(); | |
} | |
} | |
public boolean load() { | |
if (destroyed && isLogging()) { | |
logger.debug("Could not load a HoconFile object because it has already been destroyed!"); | |
return false; | |
} | |
try { | |
node = loader.load(); | |
if (node != null) { | |
loaded = true; | |
if (isLogging()) { | |
logger.debug("Loaded HoconFile " + file.getAbsolutePath()); | |
} | |
return true; | |
} | |
return false; | |
} catch (IOException exc) { | |
exc.printStackTrace(); | |
return false; | |
} | |
} | |
public boolean create(boolean defaultsIfNotExist) { | |
if (destroyed && isLogging()) { | |
logger.debug("Could not create a HoconFile object on drive because it has already been destroyed!"); | |
return false; | |
} | |
if (file.exists()) { | |
if (isLogging()) { | |
logger.debug("Can't create file because it already exists! " + file.getAbsolutePath()); | |
} | |
return false; | |
} | |
try { | |
if(!folder.mkdirs()) { | |
if (isLogging()) { | |
logger.debug("Can't create file because it is apparently a folder! " + file.getAbsolutePath()); | |
} | |
return false; | |
} | |
if (file.isDirectory()) { | |
if (isLogging()) { | |
logger.debug("Can't create file because it is apparently a folder! " + file.getAbsolutePath()); | |
} | |
return false; | |
} | |
if (file.createNewFile()) { | |
if (isLogging()) { | |
logger.debug("New file has been created! " + file.getAbsolutePath()); | |
} | |
if (defaultsIfNotExist) { | |
mergeDefaults(); | |
} | |
} else { | |
if (isLogging()) { | |
logger.info("ERROR: File could not be created!"); | |
} | |
} | |
} catch (IOException exc) { | |
if (isLogging()) { | |
logger.error("The default configuration could not be created!"); | |
exc.printStackTrace(); | |
} | |
} | |
return true; | |
} | |
public void destroy() { | |
if (destroyed && isLogging()) { | |
logger.debug("Can't destroy HoconFile object because it already has been destroyed!"); | |
return; | |
} | |
if (isLogging()) { | |
logger.debug("Cleaning up and destroying HoconFile " + file.getAbsolutePath()); | |
} | |
destroyed = true; | |
loader = null; | |
node = null; | |
file = null; | |
} | |
private boolean isLogging() { | |
return verbose && (logger != null); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment