Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(builder): Builder pattern support for entities creation #4

Merged
merged 2 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
.project
.classpath
target/
*.iml
*~
pom.xml.releaseBackup
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ SensorThingsService service = new SensorThingsService(serviceEndpoint);
```

```java
Thing thing = new Thing();
thing.setDescription("I'm a thing!");
Thing thing = ThingBuilder.builder()
.description("I'm a thing!")
.build();
service.create(thing);

// get Thing with numeric id 1234
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.fraunhofer.iosb.ilt.sta.model.builder;

import de.fraunhofer.iosb.ilt.sta.model.builder.api.AbstractDatastreamBuilder;

/**
* Default {@link de.fraunhofer.iosb.ilt.sta.model.Datastream} {@link de.fraunhofer.iosb.ilt.sta.model.builder.api.Builder}
*
* @author Aurelien Bourdon
*/
public final class DatastreamBuilder extends AbstractDatastreamBuilder<DatastreamBuilder> {

private DatastreamBuilder() {
}

public static DatastreamBuilder builder() {
return new DatastreamBuilder();
}

@Override
public DatastreamBuilder getSelf() {
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.fraunhofer.iosb.ilt.sta.model.builder;

import de.fraunhofer.iosb.ilt.sta.model.builder.api.AbstractFeatureOfInterestBuilder;

/**
* Default {@link de.fraunhofer.iosb.ilt.sta.model.FeatureOfInterest} {@link de.fraunhofer.iosb.ilt.sta.model.builder.api.Builder}
*
* @author Aurelien Bourdon
*/
public final class FeatureOfInterestBuilder extends AbstractFeatureOfInterestBuilder<FeatureOfInterestBuilder> {

private FeatureOfInterestBuilder() {
}

public static FeatureOfInterestBuilder builder() {
return new FeatureOfInterestBuilder();
}

@Override
public FeatureOfInterestBuilder getSelf() {
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.fraunhofer.iosb.ilt.sta.model.builder;

import de.fraunhofer.iosb.ilt.sta.model.builder.api.AbstractHistoricalLocationBuilder;

/**
* Default {@link de.fraunhofer.iosb.ilt.sta.model.HistoricalLocation} {@link de.fraunhofer.iosb.ilt.sta.model.builder.api.Builder}
*
* @author Aurelien Bourdon
*/
public final class HistoricalLocationBuilder extends AbstractHistoricalLocationBuilder<HistoricalLocationBuilder> {

private HistoricalLocationBuilder() {
}

public static HistoricalLocationBuilder builder() {
return new HistoricalLocationBuilder();
}

@Override
public HistoricalLocationBuilder getSelf() {
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.fraunhofer.iosb.ilt.sta.model.builder;

import de.fraunhofer.iosb.ilt.sta.model.builder.api.AbstractLocationBuilder;

/**
* Default {@link de.fraunhofer.iosb.ilt.sta.model.Location} {@link de.fraunhofer.iosb.ilt.sta.model.builder.api.Builder}
*
* @author Aurelien Bourdon
*/
public final class LocationBuilder extends AbstractLocationBuilder<LocationBuilder> {

private LocationBuilder() {
}

public static LocationBuilder builder() {
return new LocationBuilder();
}

@Override
public LocationBuilder getSelf() {
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.fraunhofer.iosb.ilt.sta.model.builder;

import de.fraunhofer.iosb.ilt.sta.model.builder.api.AbstractMultiDatastreamBuilder;

/**
* Default {@link de.fraunhofer.iosb.ilt.sta.model.MultiDatastream} {@link de.fraunhofer.iosb.ilt.sta.model.builder.api.Builder}
*
* @author Aurelien Bourdon
*/
public final class MultiDatastreamBuilder extends AbstractMultiDatastreamBuilder<MultiDatastreamBuilder> {

private MultiDatastreamBuilder() {
}

public static MultiDatastreamBuilder builder() {
return new MultiDatastreamBuilder();
}

@Override
public MultiDatastreamBuilder getSelf() {
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.fraunhofer.iosb.ilt.sta.model.builder;

import de.fraunhofer.iosb.ilt.sta.model.builder.api.AbstractObservationBuilder;

/**
* Default {@link de.fraunhofer.iosb.ilt.sta.model.Observation} {@link de.fraunhofer.iosb.ilt.sta.model.builder.api.Builder}
*
* @author Aurelien Bourdon
*/
public final class ObservationBuilder extends AbstractObservationBuilder<ObservationBuilder> {

private ObservationBuilder() {
}

public static ObservationBuilder builder() {
return new ObservationBuilder();
}

@Override
public ObservationBuilder getSelf() {
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.fraunhofer.iosb.ilt.sta.model.builder;

import de.fraunhofer.iosb.ilt.sta.model.builder.api.AbstractObservedPropertyBuilder;

/**
* Default {@link de.fraunhofer.iosb.ilt.sta.model.ObservedProperty} {@link de.fraunhofer.iosb.ilt.sta.model.builder.api.Builder}
*
* @author Aurelien Bourdon
*/
public final class ObservedPropertyBuilder extends AbstractObservedPropertyBuilder<ObservedPropertyBuilder> {

private ObservedPropertyBuilder() {
}

public static ObservedPropertyBuilder builder() {
return new ObservedPropertyBuilder();
}

@Override
public ObservedPropertyBuilder getSelf() {
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.fraunhofer.iosb.ilt.sta.model.builder;

import de.fraunhofer.iosb.ilt.sta.model.builder.api.AbstractSensorBuilder;

/**
* Default {@link de.fraunhofer.iosb.ilt.sta.model.Sensor} {@link de.fraunhofer.iosb.ilt.sta.model.builder.api.Builder}
*
* @author Aurelien Bourdon
*/
public final class SensorBuilder extends AbstractSensorBuilder<SensorBuilder> {

private SensorBuilder() {
}

public static SensorBuilder builder() {
return new SensorBuilder();
}

@Override
public SensorBuilder getSelf() {
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.fraunhofer.iosb.ilt.sta.model.builder;

import de.fraunhofer.iosb.ilt.sta.model.builder.api.AbstractThingBuilder;

/**
* Default {@link de.fraunhofer.iosb.ilt.sta.model.Thing} {@link de.fraunhofer.iosb.ilt.sta.model.builder.api.Builder}
*
* @author Aurelien Bourdon
*/
public final class ThingBuilder extends AbstractThingBuilder<ThingBuilder> {

private ThingBuilder() {
}

public static ThingBuilder builder() {
return new ThingBuilder();
}

@Override
public ThingBuilder getSelf() {
return this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package de.fraunhofer.iosb.ilt.sta.model.builder.api;

import com.fasterxml.jackson.annotation.JsonValue;
import de.fraunhofer.iosb.ilt.sta.model.Datastream;
import de.fraunhofer.iosb.ilt.sta.model.Observation;
import de.fraunhofer.iosb.ilt.sta.model.ObservedProperty;
import de.fraunhofer.iosb.ilt.sta.model.Sensor;
import de.fraunhofer.iosb.ilt.sta.model.Thing;
import de.fraunhofer.iosb.ilt.sta.model.ext.UnitOfMeasurement;
import org.geojson.Polygon;
import org.threeten.extra.Interval;

import java.util.List;

/**
* Base class for any {@link AbstractEntityBuilder} of {@link Datastream}
*
* @param <U> the type of the concrete class that extends this {@link AbstractDatastreamBuilder}
* @author Aurelien Bourdon
*/
public abstract class AbstractDatastreamBuilder<U extends AbstractDatastreamBuilder<U>> extends AbstractEntityBuilder<Datastream, U> {

@Override
protected Datastream newBuildingInstance() {
return new Datastream();
}

public U name(final String name) {
getBuildingInstance().setName(name);
return getSelf();
}

public U description(final String description) {
getBuildingInstance().setDescription(description);
return getSelf();
}

public U observationType(final ValueCode observationType) {
getBuildingInstance().setObservationType(observationType.getValue());
return getSelf();
}

public U unitOfMeasurement(final UnitOfMeasurement unitOfMeasurement) {
getBuildingInstance().setUnitOfMeasurement(unitOfMeasurement);
return getSelf();
}

public U observedArea(final Polygon observedArea) {
getBuildingInstance().setObservedArea(observedArea);
return getSelf();
}

public U phenomenonTime(final Interval phenomenonTime) {
getBuildingInstance().setPhenomenonTime(phenomenonTime);
return getSelf();
}

public U resultTime(final Interval resultTime) {
getBuildingInstance().setResultTime(resultTime);
return getSelf();
}

public U sensor(final Sensor sensor) {
getBuildingInstance().setSensor(sensor);
return getSelf();
}

public U thing(final Thing thing) {
getBuildingInstance().setThing(thing);
return getSelf();
}

public U observedProperty(final ObservedProperty observedProperty) {
getBuildingInstance().setObservedProperty(observedProperty);
return getSelf();
}

public U observations(final List<Observation> observations) {
getBuildingInstance().getObservations().addAll(observations);
return getSelf();
}

public U observation(final Observation observation) {
getBuildingInstance().getObservations().add(observation);
return getSelf();
}

/**
* All the possible values for a {@link Datastream#observationType} attribute
*
* @author Aurelien Bourdon
*/
public enum ValueCode {
OM_CategoryObservation("http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_CategoryObservation"),
OM_CountObservation("http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_CountObservation"),
OM_Measurement("http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Measurement"),
OM_Observation("http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_Observation"),
OM_TruthObservation("http://www.opengis.net/def/observationType/OGC-OM/2.0/OM_TruthObservation");

private final String value;

ValueCode(final String value) {
this.value = value;
}

@JsonValue
public String getValue() {
return value;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package de.fraunhofer.iosb.ilt.sta.model.builder.api;

import de.fraunhofer.iosb.ilt.sta.model.Entity;

/**
* Base class for any {@link EntityBuilder}.
* <p>
* Any {@link EntityBuilder} is an {@link ExtensibleBuilder}.
*
* @param <T> the instance class type to build
* @param <U> the type of the concrete class that extends this {@link AbstractEntityBuilder}
* @author Aurelien Bourdon
*/
public abstract class AbstractEntityBuilder<T extends Entity<T>, U extends AbstractEntityBuilder<T, U>> extends ExtensibleBuilder<T, U> implements EntityBuilder<T> {
}
Loading