22 package com.realtime.crossfire.jxclient.settings;
26 import java.io.BufferedWriter;
28 import java.io.FileInputStream;
29 import java.io.FileNotFoundException;
30 import java.io.FileOutputStream;
31 import java.io.IOException;
32 import java.io.InputStreamReader;
33 import java.io.LineNumberReader;
34 import java.io.OutputStreamWriter;
36 import java.util.TreeMap;
37 import org.jetbrains.annotations.NotNull;
55 private final Map<String, Entry>
values =
new TreeMap<>();
80 final Entry entry = values.get(key.getKey());
81 return entry == null ? key.getDefaultValue().toString() : entry.
getValue();
93 return Boolean.parseBoolean(value);
94 }
catch (
final NumberFormatException ignored) {
95 return key.getDefaultValue();
125 final Entry oldEntry = values.get(key.getKey());
126 if (oldEntry == null) {
127 values.put(key.getKey(),
new Entry(value, key.getComment()));
130 if (!oldEntry.
getValue().equals(value)) {
168 public void remove(@NotNull
final String key) {
169 if (values.remove(key) != null) {
185 }
catch (
final IOException ex) {
186 System.err.println(file+
": "+ex.getMessage());
197 try (
final FileInputStream fis =
new FileInputStream(file)) {
198 try (
final InputStreamReader isr =
new InputStreamReader(fis,
"UTF-8")) {
199 try (
final LineNumberReader lnr =
new LineNumberReader(isr)) {
204 }
catch (
final FileNotFoundException ignored) {
206 }
catch (
final IOException ex) {
207 System.err.println(file+
": "+ex.getMessage());
216 private void loadValues(@NotNull
final LineNumberReader lnr)
throws IOException {
218 final String line2 = lnr.readLine();
223 if (line.startsWith(
"#") || line.isEmpty()) {
227 final String[] tmp = line.split(
"=", 2);
228 if (tmp.length != 2) {
229 System.err.println(file+
":"+lnr.getLineNumber()+
": syntax error");
232 final String key = tmp[0];
233 final String value = tmp[1];
244 final File tmpFile =
new File(file.getPath()+
".tmp");
245 try (
final FileOutputStream fos =
new FileOutputStream(tmpFile)) {
246 try (
final OutputStreamWriter osw =
new OutputStreamWriter(fos,
"UTF-8")) {
247 try (
final BufferedWriter bw =
new BufferedWriter(osw)) {
253 if (!tmpFile.renameTo(file)) {
254 throw new IOException(
"cannot rename "+tmpFile+
" to "+file);
264 private static void saveNode(@NotNull
final BufferedWriter writer, @NotNull
final Map<String, Entry> node)
throws IOException {
265 if (node.isEmpty()) {
269 for (
final Map.Entry<String,
Entry> entry : node.entrySet()) {
275 if (documentation != null) {
void putBoolean(@NotNull final SettingsEntry< Boolean > key, final boolean value)
Stores a key/value pair.
long getLong(@NotNull final SettingsEntry< Long > key)
Returns the long associated with the specified key at a node or.
boolean noSave
Flag to inhibit saving.
static long parseLong(@NotNull final String string, final long defaultValue)
Converts a string into a long value.
void putInt(@NotNull final SettingsEntry< Integer > key, final int value)
Stores a key/value pair.
final File file
The file for loading/saving values.
void loadValues(@NotNull final LineNumberReader lnr)
Loads the values.
An entry in the settings file.
boolean getBoolean(@NotNull final SettingsEntry< Boolean > key)
Returns the boolean associated with the specified key at a node or.
void putLong(@NotNull final SettingsEntry< Long > key, final long value)
Stores a key/value pair.
static String encode(@NotNull final String str)
Encodes a string to make it fit into one line.
String getDocumentation()
Returns the documentation string.
final Map< String, Entry > values
The stored values.
The value part of an entry of a settings file.
Maintains a set of key/value pairs.
static void saveNode(@NotNull final BufferedWriter writer, @NotNull final Map< String, Entry > node)
Saves one node.
void setValue(@NotNull final String value)
Sets the value.
void loadValues()
Loads the values from the backing file.
void saveValues()
Saves the values to the backing file.
static int parseInt(@NotNull final String string, final int defaultValue)
Converts a string into an int value.
int getInt(@NotNull final SettingsEntry< Integer > key)
Returns the integer associated with the specified key at a node or.
static String decode(@NotNull final String str)
Decodes a string which was encoded by encode(String).
Utility class to encode arbitrary Strings to fit in a single text line.
void setDocumentation(@Nullable final String documentation)
Sets the documentation string.
Utility class for parsing strings into numbers.
void putString(@NotNull final SettingsEntry<?> key, @NotNull final String value)
Stores a key/value pair.
String getValue()
Returns the value.
Settings(@NotNull final File file)
Creates a new instance.
void setChanged()
This function is called whenever the contents of values has changed.
String getString(@NotNull final SettingsEntry<?> key)
Returns the string associated with the specified key at a node, or.