public class XProperties extends Properties
Properties
object may be used.
There is a main method to test this. It takes a single argument which is a file name which will be created (thereby allowing a test of writing a file and reading it.)
Int
as an entry means there are two methods named setInt
and getInt
)
or the explicit names of these are given. The last column gives a bit more description.
Property Summary | ||
Type | Set/Get | Description |
java.util.Date |
Date |
A standard Java date. This is actually stored as a long. |
integer |
Int |
An integer . |
long |
Long |
A long |
String |
String |
A string. Note that the values are escaped in the resulting properties file. |
boolean |
Boolean |
A boolean value. This automatically parses values of
yes, on, enable(d), ok, true, yup, yeah, 1
to be logical true and false,no, disable(d), nope, off, nay, 0
to be logical false. These are case insensitive. If the value is not recognized an exception is thrown. |
bytes array |
Bytes |
This will store or retrieve an arbitrary array of bytes |
url, uri |
URL, URI |
urls and uris. |
double |
Double |
a double value |
serializable java object |
Serializable |
Works if the object implements the java.io.Serializable interface. |
serializable list |
SerializableList |
An array of java objects each of which implements the java.io.Serializable interface. |
file |
getFile(key), getFile(key, parent), setFileName |
This will store a file name in the properties. There are two getters, so that if the users needs to resolve the file against a specific parent (vs. the java default of the invocation directory) it may be done. |
Modifier and Type | Field and Description |
---|---|
static String |
DEFAULT_LIST_SEPARATOR
The default separator for lists.
|
static String[] |
LOGICAL_FALSES
Strings this will treat as equivalent to logical false.
|
static String[] |
LOGICAL_TRUES
Strings that this will treat as equivalent to logical true.
|
protected String |
storeFileName |
defaults
Constructor and Description |
---|
XProperties()
Creates a new instance of XProperty
|
XProperties(File file)
Create a new XProperties object by parsing a properties file.
|
XProperties(Properties p)
Same as the constructor in the superclass, this gives this object all the same properties as
the argument.
|
XProperties(Properties[] sources,
boolean overwrite)
This will allow for an array of Properties objects.
|
XProperties(Properties[] sources,
boolean[] overwrite)
This will instantiate the properties taking the value ssupplied.
|
XProperties(String fileName)
Convenience method.
|
Modifier and Type | Method and Description |
---|---|
void |
add(Map source,
boolean overwrite) |
void |
add(Properties[] sources,
boolean overwrite)
Adds each of the given array of properties.
|
void |
add(Properties[] sources,
boolean[] overwrite)
Adds each property set with a flag for the set indicating overwrite permission.
|
Object |
clone()
Creates a deep copy of this object.
|
boolean |
equals(Object obj)
Checks if this XProperties object is the same as a given object.
|
boolean |
getBoolean(String key)
Retrieves the boolean value.
|
byte[] |
getBytes(String key)
Returns the value associated with this key as a byte array.
|
XProperties |
getClone()
Gets a clone of this object.
|
Date |
getDate(String key)
Get the value as a date.
|
double |
getDouble(String key)
Attempts to retrieve the value associated with the key as a double.
|
File |
getFile(String key)
Retrieve the file stored by this key, resolving it against the given invocation directory.
|
File |
getFile(String key,
File parent)
Retrieve the file stored by this key, resolving it against the given file.
|
int |
getInt(String key)
Get the value associated with this key as an integer.
|
String[] |
getList(String key)
Gets the value associated with the key and splits it into an array of strings
using the value from
getListSeparator |
String[] |
getList(String key,
String separator)
This will take a c list separated by a string and return a vector of strings.
|
String |
getListSeparator()
Get the list separator.
|
long |
getLong(String key)
Get the property as a long.
|
Object |
getSerializable(String key)
Gets a single serializable java object.
|
Object[] |
getSerializableList(String key)
Retrieves a list of serializable objects from the property.
|
String |
getStoreFileName()
Get the name of the store file
|
String |
getString(String key)
Gets the value corresponding to the key as a string.
|
URI |
getURI(String key)
Get the value as a uri
|
URL |
getURL(String key)
Gets the value as a URL.
|
boolean |
isEmpty()
This will return
true if the current properties has no entries and false
otherwise. |
protected boolean |
keySubset(XProperties b)
Checks that the keys of b are a subset of the keys in this
XProperties . |
void |
load()
Loads the content of the file in
getStoreFileName |
void |
load(File f)
Loads from a file rather than from a stream.
|
void |
load(String storeFileName)
Load the content of the given file into the current properties object, over-writing any
duplicate values.
|
static void |
main(String[] args)
This exists simply for testing this object.
|
void |
setBoolean(String key,
boolean value)
Sets the given boolean value.
|
void |
setBytes(String key,
byte[] ba)
Sets the value for the given key to the byte array.
|
void |
setDate(String key,
Date d)
Set the value of with the given Date.
|
void |
setDouble(String key,
double value)
Set the value associated with this key to the given double value.
|
void |
setFile(String key,
File f)
Puts the file name into the properties, (not) the contents! This will not be portable
between platforms, since this is the full path, properly escaped.
|
void |
setInt(String key,
int value)
Set the property from the integer value.
|
void |
setList(String key,
String list)
This sets a list directly, so the list itself is not touched.
|
void |
setList(String key,
String[] list)
Set the value of a list using the default separator.
|
void |
setList(String key,
String[] list,
String separator)
Sets the value from an array of strings, delimiting them with the supplied separator.
|
void |
setListSeparator(String listSeparator)
Set the list separator
|
void |
setLong(String key,
long value)
Set the value of the property from the give long.
|
Object |
setProperty(String key,
String value)
A bug fix for Properties...
|
void |
setSerializable(String key,
Serializable object)
Retrieves a single serializable java object.Don't forget that you need to have any
classes for this available to the current virtual machine to correctly deserialize the result.
|
void |
setSerializableList(String key,
Object[] oList)
Stores a list of serializable java objects in the properties.
|
void |
setStoreFileName(String fileName)
Sets the name of the file that calling the default
store() method
will write to. |
void |
setString(String key,
String value)
Sets the value of the key with the given string.
|
void |
setURI(String key,
String uriString)
A convenience.
|
void |
setURI(String key,
URI uri)
Sets the value for the key from the URI
|
void |
setURL(String key,
String urlString)
A convenience.
|
void |
setURL(String key,
URL url)
Set the value from the given URL
|
void |
store()
This just invokes
store with an empty header. |
void |
store(String header)
Writes the properties to the store file.
|
String |
toString(int indent) |
getProperty, getProperty, list, list, load, load, loadFromXML, propertyNames, save, store, store, storeToXML, storeToXML, stringPropertyNames
clear, compute, computeIfAbsent, computeIfPresent, contains, containsKey, containsValue, elements, entrySet, forEach, get, getOrDefault, hashCode, keys, keySet, merge, put, putAll, putIfAbsent, rehash, remove, remove, replace, replace, replaceAll, size, toString, values
public static String DEFAULT_LIST_SEPARATOR
protected String storeFileName
public static String[] LOGICAL_TRUES
public static String[] LOGICAL_FALSES
public XProperties()
public XProperties(String fileName)
fileName
- public XProperties(File file)
file
- the (standard) properties file.public XProperties(Properties p)
Grrrrr this is busted in the super class (!!). This works here...
public XProperties(Properties[] sources, boolean overwrite)
sources
- an array of Properties
overwrite
- public XProperties(Properties[] sources, boolean[] overwrite)
sources
- overwrite
- public String getListSeparator()
DEFAULT_LIST_SEPARATOR
.public void setListSeparator(String listSeparator)
listSeparator
- the list separatorpublic boolean getBoolean(String key)
LOGICAL_TRUES
are equivalent to
logical true. Any of LOGICAL_FALSES
are equivalent to logical false. All
comparisons are done case-insensitivekey
- public void setBoolean(String key, boolean value)
key
- value
- public int getInt(String key)
key
- Exception
- if the value cannot be parsed as an integerpublic void setInt(String key, int value)
key
- value
- public long getLong(String key)
key
- Exception
- if the value cannot be interpreted as a long.public void setLong(String key, long value)
key
- value
- public File getFile(String key)
key
- File
object.public File getFile(String key, File parent)
key
- parent
- public void setFile(String key, File f)
key
- f
- public String getString(String key)
key
- public void setString(String key, String value)
key
- value
- public Object getSerializable(String key)
key
- public void setSerializable(String key, Serializable object)
object
- the serializable objectpublic void setSerializableList(String key, Object[] oList)
key
- oList
- public Object[] getSerializableList(String key)
key
- public byte[] getBytes(String key)
key
- public void setBytes(String key, byte[] ba)
key
- ba
- public void setDouble(String key, double value)
key
- value
- public double getDouble(String key)
key
- public Object setProperty(String key, String value)
NullPointerException
. This is intercepted here.setProperty
in class Properties
public void add(Map source, boolean overwrite)
public void add(Properties[] sources, boolean overwrite)
sources
- overwrite
- public void add(Properties[] sources, boolean[] overwrite)
sources
- overwrite
- Exception
- thrown if the arguments are not the same length.public void setDate(String key, Date d)
key
- d
- Exception
public URI getURI(String key)
key
- Exception
- if the value cannot be parsed as a URI.public void setURI(String key, URI uri)
key
- uri
- Exception
public void setURI(String key, String uriString)
key
- uriString
- the uriException
- if the supplied string cannot be parsed as a URI.public void setURL(String key, URL url)
key
- url
- the new valueException
public void setURL(String key, String urlString)
key
- urlString
- Exception
public Date getDate(String key)
key
- Exception
- if the value is not parseable as a long.public String[] getList(String key, String separator)
key
- separator
- a string that separates each element of the list.Exception
public String[] getList(String key)
getListSeparator
key
- Exception
public void setStoreFileName(String fileName)
store()
method
will write to. It may be changed at any time.fileName
- the name of the filepublic String getStoreFileName()
public void load()
getStoreFileName
Exception
- if the file cannot be readpublic void load(String storeFileName)
storeFileName
- public void load(File f)
f
- the filepublic void setList(String key, String list)
setString(key,list)
and is simply provided as a convenience.key
- list
- public void setList(String key, String[] list)
key
- list
- the array of strings to store as a listpublic void store(String header)
header
- the header (i.e. intial comment) for the fileIOException
- if no store file has been set.public void store()
store
with an empty header.public void setList(String key, String[] list, String separator)
key
- list
- the array of stringsseparator
- public boolean isEmpty()
true
if the current properties has no entries and false
otherwise.public Object clone()
public XProperties getClone()
clone
method no casting is required.public boolean equals(Object obj)
protected boolean keySubset(XProperties b)
XProperties
.b
- the XProperties
to check against.public static void main(String[] args)
args
- public String toString(int indent)
Copyright © 2019. All Rights Reserved.