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

Commit

Permalink
refactor(#1430): replace RealNumberFieldValueSource with LinearFieldV…
Browse files Browse the repository at this point in the history
…alueSource
  • Loading branch information
pdaulbyscottlogic committed Oct 21, 2019
1 parent 0011c29 commit f1ca398
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,20 @@ private Optional<FieldValueSource> getSource(FieldType type, FieldSpec fieldSpec

private FieldValueSource getRestrictionSource(FieldType type, FieldSpec fieldSpec) {
switch (type) {
case DATETIME:
return getDateTimeSource(fieldSpec);
case STRING:
return getStringSource(fieldSpec);
case DATETIME:
case NUMERIC:
return getNumericSource(fieldSpec);
return getLinearSource(fieldSpec);
default:
throw new UnsupportedOperationException("unexpected type");
}
}

private FieldValueSource getNumericSource(FieldSpec fieldSpec) {
LinearRestrictions<BigDecimal> restrictions = (LinearRestrictions<BigDecimal>) fieldSpec.getRestrictions();

return new RealNumberFieldValueSource(restrictions, fieldSpec.getBlacklist());
private <T extends Comparable<T>> FieldValueSource getLinearSource(FieldSpec fieldSpec) {
LinearRestrictions<T> restrictions = (LinearRestrictions) fieldSpec.getRestrictions();
Set<T> blacklist = fieldSpec.getBlacklist().stream().map(d -> (T) d).collect(Collectors.toSet());
return new LinearFieldValueSource(restrictions, blacklist);
}

private FieldValueSource getStringSource(FieldSpec fieldSpec) {
Expand All @@ -94,10 +93,4 @@ private FieldValueSource getStringSource(FieldSpec fieldSpec) {

return generator;
}

private FieldValueSource getDateTimeSource(FieldSpec fieldSpec) {
LinearRestrictions<OffsetDateTime> restrictions = (LinearRestrictions<OffsetDateTime>) fieldSpec.getRestrictions();
Set<OffsetDateTime> blacklist = fieldSpec.getBlacklist().stream().map(d -> (OffsetDateTime) d).collect(Collectors.toSet());
return new LinearFieldValueSource(restrictions, blacklist);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.scottlogic.deg.generator.utils.SetUtils.stream;
Expand All @@ -30,11 +31,11 @@ public class LinearFieldValueSource<T extends Comparable<T>> implements FieldVal
private final LinearRestrictions<T> restrictions;
private final Set<T> blacklist;

public LinearFieldValueSource(
LinearRestrictions<T> restrictions,
Set<T> blacklist) {
public LinearFieldValueSource(LinearRestrictions<T> restrictions, Set<T> blacklist) {
this.restrictions = restrictions;
this.blacklist = blacklist;
this.blacklist = blacklist.stream()
.map(i -> restrictions.getGranularity().trimToGranularity(i))
.collect(Collectors.toSet());
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ void getFieldValueSources_fieldSpecContainsNumericRestrictionsWithMinAndMaxNull_

final List<BigDecimal> expectedValues = Arrays.asList(
new BigDecimal("-1E+20"),
new BigDecimal("0"),
new BigDecimal("1E+20")
);
Assert.assertEquals(expectedValues, valuesFromResult);
Expand Down
Loading

0 comments on commit f1ca398

Please sign in to comment.