Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1460 from cdowding-sl/feature/profile-update/cons…
Browse files Browse the repository at this point in the history
…traint-types

Constraint types
  • Loading branch information
JHarrisSL authored Oct 18, 2019
2 parents 774c302 + b82937f commit 5ed83ee
Show file tree
Hide file tree
Showing 279 changed files with 3,694 additions and 3,009 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.scottlogic.deg.common.profile.constraintdetail;
package com.scottlogic.deg.common.profile;

import java.util.Arrays;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.scottlogic.deg.common.profile.constraintdetail;
package com.scottlogic.deg.common.profile;

import com.scottlogic.deg.common.ValidationException;
import com.scottlogic.deg.common.date.TemporalAdjusterGenerator;

import java.time.OffsetDateTime;
Expand All @@ -23,6 +24,12 @@ public DateTimeGranularity(ChronoUnit chronoUnit, boolean workingDay) {
this.temporalAdjusterGenerator = new TemporalAdjusterGenerator(chronoUnit, workingDay);
}

public static DateTimeGranularity create(String granularity){
String offsetUnitUpperCase = granularity.toUpperCase();
boolean workingDay = offsetUnitUpperCase.equals("WORKING DAYS");
return new DateTimeGranularity(Enum.valueOf(ChronoUnit.class, workingDay ? "DAYS" : offsetUnitUpperCase), workingDay);
}

@Override
public boolean isCorrectScale(OffsetDateTime value) {
return value.equals(trimToGranularity(value));
Expand Down Expand Up @@ -58,7 +65,7 @@ public OffsetDateTime trimToGranularity(OffsetDateTime d) {
case YEARS:
return OffsetDateTime.of(d.getYear(), 1, 1, 0, 0, 0, 0, ZoneOffset.UTC);
default:
throw new UnsupportedOperationException(chronoUnit + "not yet supported as a granularity");
throw new ValidationException(chronoUnit + " not yet supported as a granularity");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.scottlogic.deg.common.profile.constraintdetail;
package com.scottlogic.deg.common.profile;

public interface Granularity<T> {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.scottlogic.deg.common.profile;

import com.scottlogic.deg.common.ValidationException;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.format.ResolverStyle;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;

public class HelixDateTime
{
private static final HelixDateTime NOW = new HelixDateTime(OffsetDateTime.now());
private static final DateTimeFormatter FORMATTER = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ofPattern("u-MM-dd'T'HH:mm:ss'.'SSS"))
.optionalStart()
.appendOffset("+HH", "Z")
.toFormatter()
.withResolverStyle(ResolverStyle.STRICT);

private final OffsetDateTime value;

private HelixDateTime(OffsetDateTime value)
{
this.value = value;
}

public static HelixDateTime create(String dateTime)
{
if(dateTime == null) throw new ValidationException("HelixDateTime cannot be null");
if (dateTime.equalsIgnoreCase("NOW")) return NOW;
OffsetDateTime offsetDateTime = fromString(dateTime);
validateDateRange(offsetDateTime);
return new HelixDateTime(offsetDateTime);
}

public static HelixDateTime create(OffsetDateTime dateTime)
{
validateDateRange(dateTime);
return new HelixDateTime(dateTime);
}

public OffsetDateTime getValue()
{
return value;
}

private static OffsetDateTime fromString(String dateTime)
{
try
{
TemporalAccessor temporalAccessor = FORMATTER.parse(dateTime);
return temporalAccessor.isSupported(ChronoField.OFFSET_SECONDS)
? OffsetDateTime.from(temporalAccessor)
: LocalDateTime.from(temporalAccessor).atOffset(ZoneOffset.UTC);
}
catch (DateTimeParseException exception)
{
throw new ValidationException(String.format("Date string '%s' must be in ISO-8601 format: yyyy-MM-ddTHH:mm:ss.SSS[Z] between (inclusive) " +
"0001-01-01T00:00:00.000Z and 9999-12-31T23:59:59.999Z", dateTime));
}
}

private static void validateDateRange(OffsetDateTime dateTime)
{
if (dateTime != null && dateTime.getYear() <= 9999 && dateTime.getYear() >= 1) return;
throw new ValidationException("Dates must be between 0001-01-01T00:00:00.000Z and 9999-12-31T23:59:59.999Z");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.scottlogic.deg.common.profile;

import com.scottlogic.deg.common.ValidationException;
import com.scottlogic.deg.common.util.NumberUtils;
import com.scottlogic.deg.common.util.defaults.NumericDefaults;

import java.math.BigDecimal;

public class HelixNumber
{
private final BigDecimal value;

private HelixNumber(BigDecimal value)
{
this.value = value;
}

public static HelixNumber create(Object value)
{
BigDecimal number = NumberUtils.coerceToBigDecimal(value);
validateNumberRange(number);
return new HelixNumber(number);
}

public BigDecimal getValue()
{
return value;
}

private static void validateNumberRange(BigDecimal number)
{
BigDecimal max = NumericDefaults.get().max();
BigDecimal min = NumericDefaults.get().min();
if (number.compareTo(min) < 0)
{
throw new ValidationException(String.format("Number must have a value >= %s, currently is %s", min.toPlainString(), number.toPlainString()));
}
if (number.compareTo(max) > 0)
{
throw new ValidationException(String.format("Number must have a value <= %s, currently is %s", max.toPlainString(), number.toPlainString()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.scottlogic.deg.common.profile;

import com.scottlogic.deg.common.ValidationException;
import com.scottlogic.deg.common.util.Defaults;
import com.scottlogic.deg.common.util.NumberUtils;

import java.math.BigDecimal;

public class HelixStringLength
{
private final int value;

private HelixStringLength(int value)
{
this.value = value;
}

public static HelixStringLength create(Object value)
{
BigDecimal stringLength = NumberUtils.coerceToBigDecimal(value);
validateIsInteger(stringLength);
validateNumberRange(stringLength);
return new HelixStringLength(stringLength.intValueExact());
}

public int getValue()
{
return value;
}

private static void validateIsInteger(BigDecimal stringLength)
{
if (stringLength.stripTrailingZeros().scale() > 0)
{
throw new ValidationException(String.format("String length `%s` is not an integer", stringLength));
}
}

private static void validateNumberRange(BigDecimal stringLength)
{
BigDecimal max = BigDecimal.valueOf(Defaults.MAX_STRING_LENGTH);
BigDecimal min = BigDecimal.ZERO;
if (stringLength.compareTo(min) < 0)
{
throw new ValidationException(String.format("String length must have a value >= %s, currently is %s", min.toPlainString(), stringLength.toPlainString()));
}
if (stringLength.compareTo(max) > 0)
{
throw new ValidationException(String.format("String length must have a value <= %s, currently is %s", max.toPlainString(), stringLength.toPlainString()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
* limitations under the License.
*/

package com.scottlogic.deg.common.profile.constraintdetail;
package com.scottlogic.deg.common.profile;

import com.scottlogic.deg.common.ValidationException;
import com.scottlogic.deg.common.util.NumberUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
Expand All @@ -28,6 +31,24 @@ public NumericGranularity(int decimalPlaces) {
this.decimalPlaces = decimalPlaces;
}

public static NumericGranularity create(Object granularity)
{
BigDecimal asNumber = NumberUtils.coerceToBigDecimal(granularity);
if (asNumber == null)
{
throw new ValidationException("Can't interpret granularity expression: " + granularity);
}
if (asNumber.compareTo(BigDecimal.ONE) > 0)
{
throw new ValidationException("Numeric granularity must be <= 1");
}
if (!asNumber.equals(BigDecimal.ONE.scaleByPowerOfTen(-asNumber.scale())))
{
throw new ValidationException("Numeric granularity must be fractional power of ten");
}
return new NumericGranularity(asNumber.scale());
}

@Override
public boolean isCorrectScale(BigDecimal value) {
return value.stripTrailingZeros().scale() <= decimalPlaces;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.scottlogic.deg.common.profile.constraintdetail;
package com.scottlogic.deg.common.profile;

/**
* Exception to signify that the specified constraint type cannot be violated.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

package com.scottlogic.deg.common.util;

import com.scottlogic.deg.common.profile.constraintdetail.DateTimeGranularity;
import com.scottlogic.deg.common.profile.constraintdetail.NumericGranularity;
import com.scottlogic.deg.common.profile.DateTimeGranularity;
import com.scottlogic.deg.common.profile.NumericGranularity;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.scottlogic.deg.common.util.defaults;

import com.scottlogic.deg.common.profile.constraintdetail.Granularity;
import com.scottlogic.deg.common.profile.Granularity;

import java.time.OffsetDateTime;

Expand Down
Loading

0 comments on commit 5ed83ee

Please sign in to comment.