1. Factories and builders
Most concrete framework classes, which implement a public interface, are final, package private, and are instantiated with the help of static methods in the interface they implement.
1.1. Factories
Static factory methods are provided for classes with a simple state. These are usually named after the interface, which makes using static imports quite convenient.
Event<String> event = event(); // Event.event()
Value<Integer> value = Value.nullable();
State state = State.state(true);
EntityTableConditionModel conditionModel =
entityTableConditionModel(Customer.TYPE, connectionProvider);
1.2. Builders
For classes with a more complex state, a builder
method is provided in the interface.
TaskScheduler scheduler =
TaskScheduler.builder()
.task(() -> {})
.interval(5, TimeUnit.SECONDS)
.initialDelay(15)
.build();
TemporalField<LocalDate> field =
TemporalField.builder()
.temporalClass(LocalDate.class)
.dateTimePattern("dd.MM.yyyy")
.columns(12)
.border(createTitledBorder("Date"))
.build();
2. Accessors
Immutable fields are accessed using methods named after the field, without a get
/is
prefix.
Observer<String> observer = event.observer();
LocalEntityConnection connection = connectionProvider.connection();
boolean modified = entity.modified();
Entity.Key primaryKey = entity.primaryKey();
A get
/is
prefix implies that the field is mutable and that a corresponding setter method exists, with a set
prefix.
2.1. Observables
Many classes expose their internal state via the Value class, which can be used to mutate the associated state or observe it by adding listeners or consumers.
FilterListSelection<List<String>> selection = tableModel.selection();
List<Integer> selectedIndexes = selection.indexes().get();
selection.indexes().set(List.of(0, 1, 2));
selection.items().addListener(() -> System.out.println("Selected items changed"));
table.sortable().set(false);
3. Exceptions
There are of course some exceptions to these rules, such as a get
prefix on an accessor for a functionally immutable field or a is
prefix on an immutable boolean field, but these exceptions are usually to keep the style of a class being extended, such as Swing components and should be few and far between.
Value<Integer> integer = Value.nullable();
boolean isNull = integer.isNull();
boolean isNullable = integer.isNullable();
ValueList<Integer> integers = ValueList.valueList();
boolean isEmpty = integers.isEmpty();