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

Commit

Permalink
Merge branch 'master' into 1185-optimise-walker-for-profile-with-smal…
Browse files Browse the repository at this point in the history
…l-decision-set
  • Loading branch information
cuthullu committed Oct 11, 2019
2 parents c048e38 + 329297c commit 540f1fb
Show file tree
Hide file tree
Showing 301 changed files with 4,248 additions and 7,001 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,13 @@ public class TemporalAdjusterGenerator {

private final boolean workingDay;

private final boolean negated;

private TemporalAdjusterGenerator(ChronoUnit chronoUnit, boolean workingDay, boolean negated) {
public TemporalAdjusterGenerator(ChronoUnit chronoUnit, boolean workingDay) {
this.chronoUnit = chronoUnit;
this.workingDay = workingDay;
this.negated = negated;
}

public TemporalAdjusterGenerator(ChronoUnit chronoUnit, boolean workingDay) {
this(chronoUnit, workingDay, false);
}

public TemporalAdjuster adjuster(int value) {
int adjustedValue = negated ? -value : value;
return workingDay ? getWorkingDayAdjusterFunction(adjustedValue) : getAdjusterFunction(chronoUnit, adjustedValue);
return workingDay ? getWorkingDayAdjusterFunction(value) : getAdjusterFunction(chronoUnit, value);
}

private TemporalAdjuster getWorkingDayAdjusterFunction(int value) {
Expand All @@ -68,11 +60,7 @@ private IntFunction<TemporalAmount> getFunction(ChronoUnit unit) {
case WEEKS: return Period::ofWeeks;
case MONTHS: return Period::ofMonths;
case YEARS: return Period::ofYears;
default: throw new IllegalArgumentException("Couldn't construct offset of unit " + unit);
default: throw new IllegalArgumentException("was " + unit + ", Must be one of the supported datetime units (millis, seconds, minutes, hours, days, months, years)");
}
}

public TemporalAdjusterGenerator negate() {
return new TemporalAdjusterGenerator(chronoUnit, workingDay, !negated);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@

public class Field {
public final String name;
public final Types type;
private final FieldType type;
private final boolean unique;
private final String formatting;
private final boolean internal;

public Field(String name, Types type, boolean unique, String formatting, boolean internal) {
public Field(String name, FieldType type, boolean unique, String formatting, boolean internal) {
this.name = name;
this.type = type;
this.unique = unique;
Expand Down Expand Up @@ -66,7 +66,7 @@ public String getFormatting() {
return formatting;
}

public Types getType() {
public FieldType getType() {
return type;
}

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

public enum FieldType
{
NUMERIC,
STRING,
DATETIME
}
20 changes: 0 additions & 20 deletions common/src/main/java/com/scottlogic/deg/common/profile/Types.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ public enum AtomicConstraintType {
IS_IN_SET("inSet"),
IS_IN_MAP("inMap"),
IS_NULL("null"),
IS_UNIQUE("unique"),
IS_OF_TYPE("ofType"),

MATCHES_REGEX("matchingRegex"),
CONTAINS_REGEX("containingRegex"),
FORMATTED_AS("formattedAs"),

// String
HAS_LENGTH("ofLength"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.scottlogic.deg.common.profile.constraintdetail;

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

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.Objects;

public class DateTimeGranularity implements Granularity<OffsetDateTime> {

private final ChronoUnit chronoUnit;
private final boolean workingDay;
private final TemporalAdjusterGenerator temporalAdjusterGenerator;

public DateTimeGranularity(ChronoUnit chronoUnit) {
this(chronoUnit, false);
}

public DateTimeGranularity(ChronoUnit chronoUnit, boolean workingDay) {
this.chronoUnit = chronoUnit;
this.workingDay = workingDay;
this.temporalAdjusterGenerator = new TemporalAdjusterGenerator(chronoUnit, workingDay);
}

@Override
public boolean isCorrectScale(OffsetDateTime value) {
return value.equals(trimToGranularity(value));
}

@Override
public Granularity<OffsetDateTime> merge(Granularity<OffsetDateTime> otherGranularity) {
DateTimeGranularity other = (DateTimeGranularity) otherGranularity;
return chronoUnit.compareTo(other.chronoUnit) <= 0 ? other : this;
}

@Override
public OffsetDateTime getNext(OffsetDateTime value, int amount){
return OffsetDateTime.from(temporalAdjusterGenerator.adjuster(amount).adjustInto(value));
}

@Override
public OffsetDateTime trimToGranularity(OffsetDateTime d) {
// is there a generic way of doing this with chronounit?
switch (chronoUnit) {
case MILLIS:
return OffsetDateTime.of(d.getYear(), d.getMonth().getValue(), d.getDayOfMonth(), d.getHour(), d.getMinute(), d.getSecond(), nanoToMilli(d.getNano()), ZoneOffset.UTC);
case SECONDS:
return OffsetDateTime.of(d.getYear(), d.getMonth().getValue(), d.getDayOfMonth(), d.getHour(), d.getMinute(), d.getSecond(), 0, ZoneOffset.UTC);
case MINUTES:
return OffsetDateTime.of(d.getYear(), d.getMonth().getValue(), d.getDayOfMonth(), d.getHour(), d.getMinute(), 0, 0, ZoneOffset.UTC);
case HOURS:
return OffsetDateTime.of(d.getYear(), d.getMonth().getValue(), d.getDayOfMonth(), d.getHour(), 0, 0, 0, ZoneOffset.UTC);
case DAYS:
return OffsetDateTime.of(d.getYear(), d.getMonth().getValue(), d.getDayOfMonth(), 0, 0, 0, 0, ZoneOffset.UTC);
case MONTHS:
return OffsetDateTime.of(d.getYear(), d.getMonth().getValue(), 1, 0, 0, 0, 0, ZoneOffset.UTC);
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");
}
}

@Override
public OffsetDateTime getPrevious(OffsetDateTime value) {
if (isCorrectScale(value)){
return getNext(value, -1);
}

return trimToGranularity(value);
}

private static int nanoToMilli(int nano) {
int factor = NANOS_IN_MILLIS;
return (nano / factor) * factor;
}
private static final int NANOS_IN_MILLIS = 1_000_000;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DateTimeGranularity that = (DateTimeGranularity) o;
return workingDay == that.workingDay &&
chronoUnit == that.chronoUnit;
}

@Override
public int hashCode() {
return Objects.hash(chronoUnit, workingDay);
}

@Override
public String toString() {
return workingDay ? "working days" : chronoUnit.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public interface Granularity<T> {

Granularity<T> merge(Granularity<T> otherGranularity);

T getNext(T value);
T getNext(T value, int amount);

T trimToGranularity(T value);

/**
* potentially slow, avoid using repeatedly
*/
T getPrevious(T value);

T trimToGranularity(T value);
default T getNext(T value){
return getNext(value, 1);
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
* limitations under the License.
*/

package com.scottlogic.deg.generator.restrictions.linear;

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

import java.math.BigDecimal;
import java.math.RoundingMode;
Expand All @@ -41,6 +39,13 @@ public NumericGranularity merge(Granularity<BigDecimal> otherGranularity) {
return decimalPlaces <= other.decimalPlaces ? this : other;
}

@Override
public BigDecimal getNext(BigDecimal value, int amount) {
BigDecimal addAmount = BigDecimal.ONE.scaleByPowerOfTen(decimalPlaces * -1)
.multiply(BigDecimal.valueOf(amount));
return value.add(addAmount);
}

@Override
public BigDecimal getNext(BigDecimal value) {
return value.add(BigDecimal.ONE.scaleByPowerOfTen(decimalPlaces * -1));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2019 Scott Logic Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

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

import java.math.BigDecimal;
import java.util.Objects;

/**
* Granularity expressions could be interpreted differently depending on other constraints on a field (eg, type constraints),
* so we store all possible parsings in this class, ready to make a GranularityRestrictions object
* */
public class NumericGranularityFactory {
public static NumericGranularity create(Object granularityExpression) {
BigDecimal asNumber = NumberUtils.coerceToBigDecimal(granularityExpression);
if (asNumber == null){
throw new IllegalArgumentException("Can't interpret granularity expression: " + granularityExpression);
}

if (asNumber.compareTo(BigDecimal.ONE) > 0) {
throw new IllegalArgumentException("Numeric granularity must be <= 1");
}

if (!asNumber.equals(BigDecimal.ONE.scaleByPowerOfTen(-asNumber.scale()))) {
throw new IllegalArgumentException("Numeric granularity must be fractional power of ten");
}

return new NumericGranularity(asNumber.scale());
}
}

This file was deleted.

Loading

0 comments on commit 540f1fb

Please sign in to comment.