Skip to content

Commit

Permalink
Merge pull request #140 from ubitricity/master
Browse files Browse the repository at this point in the history
Allow omitting all optional SampledValue fields
  • Loading branch information
TVolden authored Apr 8, 2021
2 parents 9ce7411 + ad62940 commit 441adb1
Showing 1 changed file with 12 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,7 @@ public class SampledValue implements Validatable {

/** @deprecated use {@link #SampledValue(String)} to be sure to set required fields */
@Deprecated
public SampledValue() {
try {
setContext("Sample.Periodic");
setFormat(ValueFormat.Raw);
setMeasurand("Energy.Active.Import.Register");
setLocation(Location.Outlet);
setUnit("Wh");
} catch (PropertyConstraintException ex) {
logger.error("constructor of SampledValue failed", ex);
}
}
public SampledValue() {}

/**
* Handle required fields.
Expand All @@ -73,11 +63,6 @@ public SampledValue() {
public SampledValue(String value) {
try {
setValue(value);
setContext("Sample.Periodic");
setFormat(ValueFormat.Raw);
setMeasurand("Energy.Active.Import.Register");
setLocation(Location.Outlet);
setUnit("Wh");
} catch (PropertyConstraintException ex) {
logger.error("constructor of SampledValue failed", ex);
}
Expand Down Expand Up @@ -117,7 +102,7 @@ public void setValue(String value) {
* @return enum value for context.
*/
public String getContext() {
return context;
return context == null ? "Sample.Periodic" : context;
}

/**
Expand All @@ -132,7 +117,7 @@ public String getContext() {
// TODO: Change to enum, solve format issue and change exception message.
@XmlElement
public void setContext(String context) {
if (!isValidContext(context)) {
if (context != null && !isValidContext(context)) {
throw new PropertyConstraintException(context, "context is not properly defined");
}

Expand All @@ -159,7 +144,7 @@ private boolean isValidContext(String context) {
* @return the {@link ValueFormat}.
*/
public ValueFormat getFormat() {
return format;
return format == null ? ValueFormat.Raw : format;
}

/**
Expand All @@ -179,7 +164,7 @@ public void setFormat(ValueFormat format) {
*/
@Deprecated
public ValueFormat objFormat() {
return format;
return format == null ? ValueFormat.Raw : format;
}

/**
Expand All @@ -188,7 +173,7 @@ public ValueFormat objFormat() {
* @return enum value of measurand.
*/
public String getMeasurand() {
return measurand;
return measurand == null ? "Energy.Active.Import.Register" : measurand;
}

/**
Expand All @@ -208,7 +193,7 @@ public String getMeasurand() {
// TODO: Change to enum, solve format issue and change exception message.
@XmlElement
public void setMeasurand(String measurand) {
if (!isValidMeasurand(measurand))
if (measurand != null && !isValidMeasurand(measurand))
throw new PropertyConstraintException(measurand, "measurand value is not properly defined");

this.measurand = measurand;
Expand Down Expand Up @@ -265,7 +250,7 @@ public String getPhase() {
// TODO: Change to enum, solve format issue and change exception message.
@XmlElement
public void setPhase(String phase) {
if (!isValidPhase(phase)) {
if (phase != null && !isValidPhase(phase)) {
throw new PropertyConstraintException(phase, "phase is not properly defined");
}

Expand All @@ -283,7 +268,7 @@ private boolean isValidPhase(String phase) {
* @return the {@link Location}.
*/
public Location getLocation() {
return location;
return location == null ? Location.Outlet : location;
}

/**
Expand All @@ -303,7 +288,7 @@ public void setLocation(Location location) {
*/
@Deprecated
public Location objLocation() {
return location;
return location == null ? Location.Outlet : location;
}

/**
Expand All @@ -312,7 +297,7 @@ public Location objLocation() {
* @return Unit of Measure.
*/
public String getUnit() {
return unit;
return unit == null && getMeasurand().startsWith("Energy") ? "Wh" : unit;
}

/**
Expand All @@ -328,7 +313,7 @@ public String getUnit() {
// TODO: Change to enum, solve format issue and change exception message.
@XmlElement
public void setUnit(String unit) {
if (!isValidUnit(unit)) {
if (unit != null && !isValidUnit(unit)) {
throw new PropertyConstraintException(unit, "unit is not properly defined");
}

Expand Down

0 comments on commit 441adb1

Please sign in to comment.