public abstract class Constants extends Object
Each sub-class defines public final fields that will be
initialized with values from a properties file. The default values to use
if the properties file does not exist are specified with @Default annotations on each
field.
package org.anc.example;
public class MyConstants extends Constants
{
@Default("Hello world.")
public final String HELLO_WORLD = null;
@Default("8")
public final Integer NTHREADS = null;
public MyConstants()
{
super.init();
}
}
Each public field in the subclass must be initialized to null and the subclass
constructor(s) must call super.init(). The Constants subclass can contain
fields of type String, Boolean, Integer, Float, and Double. However, the value specified
by the @Default annotation is always a String.
An instance of the subclass can then be created (usually as a static final field of the application object) to access the defined constants.
public class Application
{
public static final MyConstants CONST = new MyConstants();
public void run()
{
System.out.println(CONST.HELLO_WORLD);
}
}
The Constants class will look for the properties file conf/machineName/class.name.properties
where:
The Constants class will first look for the properties file on the
file system and then on the class path. If a properties file can not be found
the fields will be initialized with the values from the @Default annotations.
A sub-class can also specify the properties file to use with the init(String)
method. The String parameter passed to the init method should be the name of a
Java system property or an OS environmental variable. The Constants class
will first try System.getProperty and then System.getenv to obtain
a file name. If neither property has been set the above method is used to locate
the properties file. For example,
// In MyConstants.java
package org.anc.example;
public class MyConstants extends Constants
{
@Default("Hello world")
public final String HELLO_WORLD = null;
public MyConstants()
{
super.init("org.anc.hello");
}
public static void main(String[] args)
{
MyConstants constants = new MyConstants();
System.out.println(constants.HELLO_WORLD);
}
}
# In /home/anc/hello.properties
HELLO_WORLD=Bonjour le monde.
# From the command line:
> java -cp MyConstants.jar -Dorg.anc.hello=/home/anc/hello.properties org.anc.example.MyConstants
> Bonjour le monde
A properties file containing the default values can be generated by
creating an instance of the class and calling the save() method. This is
convenient when you want to create properties files for use with other machines.
public static void main(String[] args)
{
MyConstants constants = new MyConstants();
constants.save();
}
| Modifier and Type | Class and Description |
|---|---|
static interface |
Constants.Default
Annotation used to provide a default value for constants.
|
| Constructor and Description |
|---|
Constants() |
| Modifier and Type | Method and Description |
|---|---|
protected String |
getName() |
protected Properties |
getProperties(String propName) |
protected void |
init() |
protected void |
init(String propertyName) |
protected static boolean |
isPublicFinalBoolean(Field field) |
protected static boolean |
isPublicFinalDouble(Field field) |
protected static boolean |
isPublicFinalFloat(Field field) |
protected static boolean |
isPublicFinalInteger(Field field) |
protected static boolean |
isPublicFinalString(Field field) |
void |
save() |
void |
save(File file) |
void |
save(String path) |
public void save()
throws IOException
IOExceptionpublic void save(String path) throws IOException
IOExceptionpublic void save(File file) throws IOException
IOExceptionprotected Properties getProperties(String propName) throws FileNotFoundException, IOException
FileNotFoundExceptionIOExceptionprotected String getName()
protected void init()
protected void init(String propertyName)
protected static boolean isPublicFinalString(Field field)
protected static boolean isPublicFinalInteger(Field field)
protected static boolean isPublicFinalDouble(Field field)
protected static boolean isPublicFinalFloat(Field field)
protected static boolean isPublicFinalBoolean(Field field)
Copyright © 2016 The American National Corpus. All rights reserved.