This is a typical example found in many Java programs. There exists a property that defines the dimensions of let's say a window in the following format:
# For example 150;20
window.dimension = width;height
What a Java developer had to do so far was:
With Joperties, all the developer needs to do is this:
As you can see, the user has transformed a 13 line piece of code to 5 lines and if you exclude the error handling, a 5 line piece of code to a single line! And this was a very simple example. With Joperties you can save and restore a big list of Classes including Collections of them (for instance a LinkedList of type Polygon). As you can imagine, writing and maintaining code for conversion of so many classes can be cumbersome and error prone. This is where Joperties shows its use.
▲ TopSome data types can be described by compositing other types. One such example is a GradientPaint. It consist of 2 points (the start and end point of the gradient), 2 colors (the start and end colors) and a boolean (denoting if the gradient is cyclical). The following code:
Produces this output:
As you can see, this example also demonstrates another quality already discussed about Joperties. A Joperties object is a Properties object. So when we set a Joperty, we can see the encoded format of the object by getting the Property with the same key.
▲ TopJoperties also works with the Collections framework. Because classes in the Collection framework are generic, and because in Java the type of generic classes is erased during runtime, a special syntax is required when dealing with them. Let's assume you want to set a list of Points as a property. This code is used to set a Collection:
This is the output:
0;0>>50;20>>50;50>>100;100
To get the object from the property, we also use a different syntax:
There is no method to get the collection. To do that, we create a CollectionInterpreter and we interpret the encoded String property. The output, as expected, is:
0.0 : 0.0
50.0 : 20.0
50.0 : 50.0
100.0 : 100.0
Joperties is using the registered interpreters to interpret composite classes. One example was shown earlier. The most complex composite type as of this writing is the RadialGradientPaint. If you include the use of Collections, the following code:
Produces this output:
As you can see, Joperties can produce very complicated Strings. You may also noticed that the RadialGradient contains some properties that are encoded as a Collection (the fractions and the colors in this case) but instead of being separated by ">>" they are separated by "<<". This was done so that you can create a Collection of RadialGradients. This also demonstrates a limitation of Joperties. You must be careful of what data you encode because if the data contains a String that is used to separate parameters, then an InterpretationException will be thrown.
▲ Top