Created
June 11, 2015 23:45
-
-
Save varrix/a668c422dbed658f5690 to your computer and use it in GitHub Desktop.
HoconFile (non-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 ca.loganspeck.cortex.storage; | |
import com.google.common.base.Preconditions; | |
import ninja.leaping.configurate.ConfigurationNode; | |
import ninja.leaping.configurate.commented.CommentedConfigurationNode; | |
import ninja.leaping.configurate.hocon.HoconConfigurationLoader; | |
import ninja.leaping.configurate.loader.ConfigurationLoader; | |
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 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 isDestroyed() { | |
return destroyed; | |
} | |
public boolean isLoaded() { | |
return loaded; | |
} | |
public CommentedConfigurationNode getNode() { | |
return node; | |
} | |
public boolean canSave() { | |
return canSave; | |
} | |
public void setCanSave(boolean canSave) { | |
this.canSave = canSave(); | |
} | |
public boolean mergeDefaults() { | |
if (destroyed) { | |
return false; | |
} | |
URL resource = this.getClass().getResource("/" + fileName); | |
if (resource == null) { | |
return false; | |
} | |
ConfigurationLoader defaultsLoader = HoconConfigurationLoader.builder().setURL(resource).build(); | |
try { | |
ConfigurationNode defaults = defaultsLoader.load(); | |
if (!loaded) { | |
load(); | |
} | |
node.mergeValuesFrom(defaults); | |
loader.save(node); | |
} catch (IOException exc) { | |
exc.printStackTrace(); | |
return false; | |
} | |
return true; | |
} | |
public void save() { | |
try { | |
loader.save(node); | |
} catch (IOException exc) { | |
exc.printStackTrace(); | |
} | |
} | |
public boolean load() { | |
if (destroyed) { | |
return false; | |
} | |
try { | |
node = loader.load(); | |
if (node != null) { | |
loaded = true; | |
return true; | |
} | |
return false; | |
} catch (IOException exc) { | |
exc.printStackTrace(); | |
return false; | |
} | |
} | |
public boolean create(boolean defaultsIfNotExist) { | |
if (destroyed) { | |
return false; | |
} | |
if (file.exists()) { | |
return false; | |
} | |
try { | |
folder.mkdirs(); | |
if (file.isDirectory()) { | |
return false; | |
} | |
if (!file.createNewFile()) { | |
return false; | |
} | |
if (defaultsIfNotExist) { | |
mergeDefaults(); | |
} | |
} catch (IOException exc) { | |
exc.printStackTrace(); | |
} | |
return true; | |
} | |
public void destroy() { | |
if (destroyed) { | |
return; | |
} | |
destroyed = true; | |
loader = null; | |
node = null; | |
file = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment