|
1 | | -We know that you are waiting, we will have this section ready very soon. |
| 1 | +# Property |
| 2 | + |
| 3 | +Property represents a configuration parameter that can be associated with features or used |
| 4 | +independently within the FF4J framework. Properties allow you to store various types of data, such |
| 5 | +as strings, integers, booleans, and more, which can be utilized to control feature behavior or |
| 6 | +application settings. |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +A [**`Property`**](https://github.com/ff4j/ff4j/blob/main/ff4j-core/src/main/java/org/ff4j/property/Property.java) |
| 11 | +is an entity representing any kind of value or configuration. It has an unique name and a value. |
| 12 | +This value can be of any type and this the reason why, in ff4j, we chose to implement it with |
| 13 | +generics **`Property<?>`**. There is a Property implementation for each Java Raw Type as shown in |
| 14 | +the example below. |
| 15 | + |
| 16 | +## Property object |
| 17 | + |
| 18 | +```mermaid |
| 19 | +classDiagram |
| 20 | + direction TB |
| 21 | +
|
| 22 | + class Property~T~ { |
| 23 | + <<abstract>> |
| 24 | + -readOnly: boolean |
| 25 | + -name: String |
| 26 | + -description: String |
| 27 | + -type: String |
| 28 | + -value: T |
| 29 | + -fixedValues: Set~T~ |
| 30 | + +Property() |
| 31 | + +Property(name: String) |
| 32 | + +Property(name: String, valueStr: String) |
| 33 | + +Property(name: String, value: T, fixed: Set~T~) |
| 34 | + +add2FixedValueFromString(v: String) |
| 35 | + +add2FixedValue(value: T) |
| 36 | + +abstract fromString(v: String): T |
| 37 | + +parameterizedType(): Class |
| 38 | + +asString(): String |
| 39 | + +asInt(): int |
| 40 | + +asDouble(): double |
| 41 | + +asBoolean(): boolean |
| 42 | + +getValue(): T |
| 43 | + +setValue(value: T) |
| 44 | + +setValueFromString(value: String) |
| 45 | + +getName(): String |
| 46 | + +setName(name: String) |
| 47 | + +getFixedValues(): Set~T~ |
| 48 | + +setFixedValues(fixedValues: Set~T~) |
| 49 | + +toString(): String |
| 50 | + +toJson(): String |
| 51 | + +getType(): String |
| 52 | + +setType(type: String) |
| 53 | + +getDescription(): String |
| 54 | + +setDescription(description: String) |
| 55 | + +isReadOnly(): boolean |
| 56 | + +setReadOnly(readOnly: boolean) |
| 57 | + } |
| 58 | +``` |
| 59 | + |
| 60 | +## Examples |
| 61 | + |
| 62 | +### Create and Manage Properties |
| 63 | + |
| 64 | +```java title="Snippet for creating and managing properties" |
| 65 | +public void createAndManageProperty() { |
| 66 | + // Create a new String property |
| 67 | + PropertyString myProp = new PropertyString("myStringProp", "defaultValue"); |
| 68 | + ff4j.getPropertyStore().create(myProp); |
| 69 | + // Retrieve the property |
| 70 | + PropertyString retrievedProp = (PropertyString) ff4j.getPropertyStore().read("myStringProp"); |
| 71 | + // Update the property value |
| 72 | + retrievedProp.setValue("newValue"); |
| 73 | + ff4j.getPropertyStore().update(retrievedProp); |
| 74 | + // Delete the property |
| 75 | + ff4j.getPropertyStore().delete("myStringProp"); |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### Using Properties in Features |
| 80 | + |
| 81 | +```java title="Snippet for using properties in features" |
| 82 | +public void usePropertyInFeature() { |
| 83 | + // Create a feature with a custom property |
| 84 | + Feature featureWithProp = new Feature("featureWithProp", true); |
| 85 | + PropertyInt timeoutProp = new PropertyInt("timeout", 30); |
| 86 | + featureWithProp.addProperty(timeoutProp); |
| 87 | + ff4j.getFeatureStore().create(featureWithProp); |
| 88 | + // Retrieve and use the property |
| 89 | + Feature retrievedFeature = ff4j.getFeatureStore().read("featureWithProp"); |
| 90 | + PropertyInt retrievedTimeoutProp = (PropertyInt) retrievedFeature.getProperty("timeout"); |
| 91 | + int timeoutValue = retrievedTimeoutProp.getValue(); |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +### Custom Property Implementation |
| 96 | + |
| 97 | +```java title="Snippet for creating a custom property type" |
| 98 | +import org.ff4j.property.Property; |
| 99 | +import org.ff4j.test.property.CardinalPoint.Point; |
| 100 | + |
| 101 | +public class CardinalPoint extends Property<Point> { |
| 102 | + |
| 103 | + private static final long serialVersionUID = 1792311055570779010L; |
| 104 | + |
| 105 | + public static enum Point {NORTH, SOUTH, EAST, WEST}; |
| 106 | + |
| 107 | + public CardinalPoint(String uid, Point lvl) { |
| 108 | + super(uid, lvl, Point.values()); |
| 109 | + } |
| 110 | + |
| 111 | + public Point fromString(String v) { |
| 112 | + return Point.valueOf(v); |
| 113 | + } |
| 114 | + |
| 115 | + public void north() { |
| 116 | + setValue(Point.NORTH); |
| 117 | + } |
| 118 | + |
| 119 | + public void south() { |
| 120 | + setValue(Point.SOUTH); |
| 121 | + } |
| 122 | + |
| 123 | + public void east() { |
| 124 | + setValue(Point.EAST); |
| 125 | + } |
| 126 | + |
| 127 | + public void west() { |
| 128 | + setValue(Point.WEST); |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
0 commit comments