SingularityProperties

**SingularityProperties** is the library we've developed for changing code constants without having to edit them in the code, which is inefficient because of the time it takes to push the code. This drastically speeds up the process of changing constants and allows us to modify constants such as the "autonomous mode" without editing having to change, build, and push the code.

## Structure of a SingularityProperties Object

The **SingularityProperties** class represents two layers of properties: the file properties and the default properties. The file properties are loaded directly from the file upon instantiation of the object, and the default properties are added to the object in the code. A default property value will only be used if that property is not found in the file, which is useful if the properties file is accidentally cleared or deleted. Use the file to edit properties during testing, then set the default values of the properties in the code once their values are solidified.

## Instantiating a SingularityProperties Object

A **SingularityProperties** object is instantiated with a URL argument for the file, like so:

```
SingularityProperties propertiesObject = new SingularityProperties("/home/lvuser/robot.properties");

```

Make sure that the file is in a directory with read/write permissions for the user "lvuser" (which the robot program is logged in as when it runs). We recommend that you use the directory "/home/lvuser/" for this. The name of the file does not matter.

## Editing a properties file

We recommend that you use SSH (often via a program like with PuTTY) to log into the robot and the command Vi to edit properties files.

## Accessing Properties

Properties are accessed via a SingularityProperties object using the following methods:

```
propertiesObject.getBoolean(String propertyName);
propertiesObject.getDouble(String propertyName);
propertiesObject.getFloat(String propertyName);
propertiesObject.getInt(String propertyName);
propertiesObject.getString(String propertyName);

```

These methods return the value for that property from the properties file if possible, and the default property for that property if not possible. If neither is found for the given property name, a SingularityPropertyNotFoundException will be thrown.

There are two code schemes for accessing properties. The one we generally employ is having a "loadProperties()" method in the Robot.java file where we set a bunch of fields to their respective values loaded from the properties file. The fields are then referred to in the code. The properties can then be reloaded all at once by calling the loadProperties() method again after calling reloadProperties(). Also, the properties can be loaded in one try-catch statement this way.

The alternative scheme is to use one of the "get" methods any time a property value is required. This eliminates the need to create a field for every property in the code and allows reloading of properties by simply calling reloadProperties(). However, a try-catch statement must be used every time a property is required, which, while giving the programmer a great deal of control, can be a hassle when programming.

## Adding Default Properties

Default properties are backup values for the properties in the files in case the file is corrupted, cleared, or deleted. The library will use them when a property is requested that is not found in the file. Once some properties are unlikely to change in the near future, their default values should be added in the code . Adding default property values is straightforward:

```

addDefaultProp(String propName, [String/int/float/double/boolean] value);

```

## Changing Property Values In the Code

This functionality is not 100% bug-free yet, but can come in handy in certain situations, such as when a value must be saved and loaded when the program is executed another time. Using the following method, one can change the value of a property in the code:

```
setProperty(String propName, String newValue);

```

Just make sure to test thoroughly for bugs before deciding it's safe to use (or fix the bugs in the library)

## Troubleshooting

If there appear to be problems with reading/writing the file, check the permissions of the file using the Linux command "ls -l [file path]." If the permissions must be changed, follow the directions here.

Use a text editor like vi in SSH (or use FTP)