Skip to content

Commit c53dce6

Browse files
author
Nicolai Parlog
committed
Implemented 'NestedDoubleProperty' including the builder and tests.
1 parent 533dbb8 commit c53dce6

File tree

9 files changed

+303
-7
lines changed

9 files changed

+303
-7
lines changed

demo/org/codefx/libfx/nesting/NestingDemo.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ public static void main(String[] args) {
4949
* Demonstrates the use of {@link NestedProperty}.
5050
*/
5151
private void demoNestedProperties() {
52-
// TODO change this to DoubleProperty as soon as that is implemented
53-
Property<Number> currentEmployeesSalary = Nestings.on(currentEmployee)
54-
.nest(employee -> employee.salaryProperty())
52+
DoubleProperty currentEmployeesSalary = Nestings.on(currentEmployee)
53+
.nestDoubleProperty(employee -> employee.salaryProperty())
5554
.buildProperty();
5655

5756
System.out

src/org/codefx/libfx/nesting/AbstractNestingNestingBuilder.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Objects;
44

5+
import javafx.beans.property.DoubleProperty;
56
import javafx.beans.property.IntegerProperty;
67
import javafx.beans.property.Property;
78
import javafx.beans.value.ObservableValue;
@@ -95,6 +96,22 @@ public IntegerPropertyNestingBuilder nestIntegerProperty(NestingStep<T, IntegerP
9596
return new IntegerPropertyNestingBuilder(this, nestingStep);
9697
}
9798

99+
/**
100+
* Returns a builder for nestings whose inner observable is a {@link DoubleProperty}. The created nestings depend on
101+
* this builder's outer observable and nesting steps and adds the specified step as the next one.
102+
*
103+
* @param nestingStep
104+
* the function which performs the nesting step from one observable to the next
105+
* @return an {@link ObservableValueNestingBuilder} which builds a nesting from this builder's settings and the
106+
* specified nesting steps
107+
* @throws NullPointerException
108+
* if the specified function is null
109+
*/
110+
public DoublePropertyNestingBuilder nestDoubleProperty(NestingStep<T, DoubleProperty> nestingStep) {
111+
Objects.requireNonNull(nestingStep, "The argument 'nestingStep' must not be null.");
112+
return new DoublePropertyNestingBuilder(this, nestingStep);
113+
}
114+
98115
//#end NEST
99116

100117
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.codefx.libfx.nesting;
2+
3+
import javafx.beans.property.DoubleProperty;
4+
5+
import org.codefx.libfx.nesting.property.NestedDoublePropertyBuilder;
6+
import org.codefx.libfx.nesting.property.NestedObjectPropertyBuilder;
7+
8+
/**
9+
* A builder for all kinds of nested functionality whose innermost value is held by a {@link DoubleProperty}.
10+
*/
11+
public class DoublePropertyNestingBuilder extends AbstractNestingBuilder<Number, DoubleProperty> {
12+
13+
// #region CONSTRUCTION
14+
15+
/**
16+
* Creates a new nesting builder which acts as a nested builder.
17+
*
18+
* @param <P>
19+
* the type the previous builder wraps
20+
* @param previousNestedBuilder
21+
* the previous builder
22+
* @param nestingStep
23+
* the function which performs the nesting step from one observable to the next
24+
*/
25+
<P> DoublePropertyNestingBuilder(
26+
AbstractNestingBuilder<P, ?> previousNestedBuilder,
27+
NestingStep<P, DoubleProperty> nestingStep) {
28+
29+
super(previousNestedBuilder, nestingStep);
30+
}
31+
32+
//#end CONSTRUCTION
33+
34+
// #region BUILD
35+
36+
/**
37+
* Creates a nested property from this builder's settings. This method can be called arbitrarily often and each call
38+
* returns a new instance.
39+
*
40+
* @return a new {@link DoubleProperty} instance with no owning bean and no name
41+
*/
42+
public DoubleProperty buildProperty() {
43+
Nesting<DoubleProperty> nesting = buildNesting();
44+
return NestedDoublePropertyBuilder.forNesting(nesting).build();
45+
}
46+
47+
/**
48+
* Returns a nested object property builder which can be used to define the new property's attributes before
49+
* building it.
50+
*
51+
* @return a new instance of {@link NestedObjectPropertyBuilder}
52+
*/
53+
public NestedDoublePropertyBuilder buildPropertyWithBuilder() {
54+
Nesting<DoubleProperty> nesting = buildNesting();
55+
return NestedDoublePropertyBuilder.forNesting(nesting);
56+
}
57+
58+
//#end BUILD
59+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.codefx.libfx.nesting.property;
2+
3+
import java.util.Objects;
4+
5+
import javafx.beans.property.BooleanProperty;
6+
import javafx.beans.property.DoubleProperty;
7+
import javafx.beans.property.Property;
8+
import javafx.beans.property.ReadOnlyBooleanProperty;
9+
import javafx.beans.property.SimpleBooleanProperty;
10+
import javafx.beans.property.SimpleDoubleProperty;
11+
12+
import org.codefx.libfx.nesting.Nesting;
13+
14+
/**
15+
* A {@link DoubleProperty} which also implements {@link NestedProperty}.
16+
*/
17+
public class NestedDoubleProperty extends SimpleDoubleProperty implements NestedProperty<Number> {
18+
19+
// #region PROPERTIES
20+
21+
/**
22+
* The property indicating whether the nesting's inner observable is currently null.
23+
*/
24+
private final BooleanProperty innerObservableNull;
25+
26+
//#end PROPERTIES
27+
28+
// #region CONSTUCTION
29+
30+
/**
31+
* Creates a new property. Except {@code nesting} all arguments can be null.
32+
*
33+
* @param nesting
34+
* the nesting this property is based on
35+
* @param bean
36+
* the bean which owns this property; can be null
37+
* @param name
38+
* this property's name; can be null
39+
*/
40+
NestedDoubleProperty(Nesting<? extends Property<Number>> nesting, Object bean, String name) {
41+
super(bean, name);
42+
Objects.requireNonNull(nesting, "The argument 'nesting' must not be null.");
43+
this.innerObservableNull = new SimpleBooleanProperty(this, "innerObservableNull");
44+
45+
PropertyToNestingBinding.bind(this, isNull -> innerObservableNull.set(isNull), nesting);
46+
}
47+
48+
//#end CONSTUCTION
49+
50+
// #region IMPLEMENTATION OF 'NestedProperty'
51+
52+
@Override
53+
public ReadOnlyBooleanProperty innerObservableNull() {
54+
return innerObservableNull;
55+
}
56+
57+
@Override
58+
public boolean isInnerObservableNull() {
59+
return innerObservableNull.get();
60+
}
61+
62+
//#end IMPLEMENTATION OF 'NestedProperty'
63+
64+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.codefx.libfx.nesting.property;
2+
3+
import javafx.beans.property.DoubleProperty;
4+
5+
import org.codefx.libfx.nesting.Nesting;
6+
7+
/**
8+
* A builder for a {@link NestedDoubleProperty} which is bound to the {@link Nesting#innerObservable() innerObservable}
9+
* of a {@link Nesting}.
10+
*/
11+
public class NestedDoublePropertyBuilder extends AbstractNestedPropertyBuilder<DoubleProperty, NestedDoubleProperty> {
12+
13+
// #region CONSTRUCTION
14+
15+
/**
16+
* Creates a new builder which uses the specified nesting.
17+
*
18+
* @param nesting
19+
* the nesting which will be used for all nested properties
20+
*/
21+
private NestedDoublePropertyBuilder(Nesting<DoubleProperty> nesting) {
22+
super(nesting);
23+
}
24+
25+
/**
26+
* Creates a new builder which uses the specified nesting.
27+
*
28+
* @param nesting
29+
* the nesting which will be used for all nested properties
30+
* @return a new instance of {@link NestedObjectPropertyBuilder}
31+
*/
32+
public static NestedDoublePropertyBuilder forNesting(Nesting<DoubleProperty> nesting) {
33+
return new NestedDoublePropertyBuilder(nesting);
34+
}
35+
36+
//#end CONSTRUCTION
37+
38+
// #region METHODS
39+
40+
@Override
41+
public NestedDoubleProperty build() {
42+
return new NestedDoubleProperty(getNesting(), getBean(), getName());
43+
}
44+
45+
//#end METHODS
46+
47+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.codefx.libfx.nesting.property;
2+
3+
import javafx.beans.property.DoubleProperty;
4+
import javafx.beans.property.SimpleDoubleProperty;
5+
6+
import org.codefx.libfx.nesting.Nesting;
7+
8+
/**
9+
* Abstract superclass to tests for {@link NestedDoubleProperty NestedDoubleProperty} which only leaves the creation of
10+
* the tested properties (by {@link #createNestedPropertyFromNesting(Nesting)}) to the subclasses.
11+
*/
12+
public abstract class AbstractNestedDoublePropertyTest extends AbstractNestedPropertyTest<Number, DoubleProperty> {
13+
14+
/**
15+
* The next value returned by {@link #createNewValue()}.
16+
*/
17+
private double lastValue = 1.5;
18+
19+
@Override
20+
protected boolean allowsNullValues() {
21+
return false;
22+
}
23+
24+
@Override
25+
protected Number createNewValue() {
26+
lastValue += 1;
27+
return lastValue;
28+
}
29+
30+
@Override
31+
protected DoubleProperty createNewObservableWithValue(Number value) {
32+
return new SimpleDoubleProperty(value.doubleValue());
33+
}
34+
35+
@Override
36+
protected DoubleProperty createNewObservableWithSomeValue() {
37+
return createNewObservableWithValue(0.0);
38+
}
39+
40+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.codefx.libfx.nesting.property;
2+
3+
import javafx.beans.property.DoubleProperty;
4+
import javafx.beans.property.SimpleDoubleProperty;
5+
6+
import org.codefx.libfx.nesting.Nesting;
7+
import org.codefx.libfx.nesting.testhelper.NestingAccess.EditableNesting;
8+
import org.junit.runner.RunWith;
9+
import org.junit.runners.Suite;
10+
import org.junit.runners.Suite.SuiteClasses;
11+
12+
/**
13+
* Tests the class {@link NestedDoublePropertyBuilder}.
14+
*/
15+
@RunWith(Suite.class)
16+
@SuiteClasses({
17+
NestedDoublePropertyBuilderTest.AbstractBuilderContract.class,
18+
NestedDoublePropertyBuilderTest.CreatedProperties.class,
19+
})
20+
public class NestedDoublePropertyBuilderTest {
21+
22+
/**
23+
* Tests whether the builder fulfills the contract defined by {@link AbstractNestedPropertyBuilder}.
24+
*/
25+
public static class AbstractBuilderContract
26+
extends AbstractNestedPropertyBuilderTest<DoubleProperty, NestedDoubleProperty> {
27+
28+
@Override
29+
protected AbstractNestedPropertyBuilder<DoubleProperty, NestedDoubleProperty> createBuilder() {
30+
DoubleProperty innerObservable = new SimpleDoubleProperty(0);
31+
EditableNesting<DoubleProperty> nesting = EditableNesting.createWithInnerObservable(innerObservable);
32+
return NestedDoublePropertyBuilder.forNesting(nesting);
33+
}
34+
35+
}
36+
37+
/**
38+
* Uses the builder to create properties which are then tested.
39+
*/
40+
public static class CreatedProperties extends AbstractNestedDoublePropertyTest {
41+
42+
@Override
43+
protected NestedProperty<Number> createNestedPropertyFromNesting(Nesting<DoubleProperty> nesting) {
44+
// use the builder to create the property
45+
NestedDoublePropertyBuilder builder = NestedDoublePropertyBuilder.forNesting(nesting);
46+
return builder.build();
47+
}
48+
49+
}
50+
51+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.codefx.libfx.nesting.property;
2+
3+
import javafx.beans.property.DoubleProperty;
4+
5+
import org.codefx.libfx.nesting.Nesting;
6+
7+
/**
8+
* Tests the class {@link NestedDoubleProperty}.
9+
*/
10+
public class NestedDoublePropertyTest extends AbstractNestedDoublePropertyTest {
11+
12+
@Override
13+
protected NestedProperty<Number> createNestedPropertyFromNesting(Nesting<DoubleProperty> nesting) {
14+
return new NestedDoubleProperty(nesting, null, null);
15+
}
16+
17+
}

test/org/codefx/libfx/nesting/property/_AllNestedPropertyTests.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
*/
1010
@RunWith(Suite.class)
1111
@SuiteClasses({
12-
NestedIntegerPropertyTest.class,
13-
NestedIntegerPropertyBuilderTest.class,
14-
NestedObjectPropertyTest.class,
15-
NestedObjectPropertyBuilderTest.class,
12+
NestedDoublePropertyTest.class,
13+
NestedDoublePropertyBuilderTest.class,
14+
NestedIntegerPropertyTest.class,
15+
NestedIntegerPropertyBuilderTest.class,
16+
NestedObjectPropertyTest.class,
17+
NestedObjectPropertyBuilderTest.class,
1618
})
1719
public class _AllNestedPropertyTests {
1820
// no body needed

0 commit comments

Comments
 (0)