Skip to content

Commit

Permalink
make the context attribute generic (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
SentryMan authored May 26, 2024
1 parent 059dc75 commit e0f86f6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ interface AdapterCreateRequest {
Map<String, Object> attributes();

/** Return the attribute for the given key. */
Object attribute(String key);
<T> T attribute(String key);

/** Return the message to use */
Message message();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,13 @@ record Request(

@Override
public String targetType() {
return (String)attribute("_type");
return attribute("_type");
}

@Override
public Object attribute(String key) {
return attributes.get(key);
@SuppressWarnings("unchecked")
public <T> T attribute(String key) {
return (T) attributes.get(key);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,14 @@ static sealed class PatternAdapter extends AbstractConstraintAdapter<CharSequenc
protected final Predicate<String> pattern;

PatternAdapter(AdapterCreateRequest request) {
this(request, (String) request.attribute("regexp"));
this(request, request.attribute("regexp"));
}

@SuppressWarnings("unchecked")
PatternAdapter(AdapterCreateRequest request, String regex) {
super(request);
int flags = 0;

final List<RegexFlag> flags1 = (List<RegexFlag>) request.attribute("flags");
final List<RegexFlag> flags1 = request.attribute("flags");
if (flags1 != null) {
for (final var flag : flags1) {
flags |= flag.getValue();
Expand All @@ -75,8 +74,8 @@ private static final class SizeAdapter implements ValidationAdapter<Object> {

SizeAdapter(AdapterCreateRequest request) {
this.groups = request.groups();
this.min = (int) request.attribute("min");
this.max = (int) request.attribute("max");
this.min = request.attribute("min");
this.max = request.attribute("max");

final Object msgKey = request.attribute("message");
if (min == 0 && LENGTH.equals(msgKey)) {
Expand Down Expand Up @@ -150,7 +149,7 @@ private static final class NotBlankAdapter implements ValidationAdapter<CharSequ
}

private static int maxLength(AdapterCreateRequest request) {
final Integer max = (Integer) request.attribute("max");
final Integer max = request.attribute("max");
return Objects.requireNonNullElse(max, 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ final class DateRangeAdapter extends AbstractConstraintAdapter<Object> {
this.referenceClock = referenceClock;
this.tolerance = tolerance;
this._type = request.targetType();
min = parsePeriod((String) request.attribute("min"), true);
max = parsePeriod((String) request.attribute("max"), false);
min = parsePeriod(request.attribute("min"), true);
max = parsePeriod(request.attribute("max"), false);
}

private TemporalAmount parsePeriod(String period, boolean negateTolerance) {
if (period == null || period.isEmpty()) {
return null;
}
if (period.equals("now")) {
if ("now".equals(period)) {
return nowTolerance(negateTolerance);
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private static final class MaxAdapter extends PrimitiveAdapter<Number>
MaxAdapter(AdapterCreateRequest request) {
super(request);
this.targetType = request.targetType();
this.max = (long) request.attribute(VALUE);
this.max = request.attribute(VALUE);
}

@Override
Expand Down Expand Up @@ -180,7 +180,7 @@ static final class MaxBigDecimal extends AbstractConstraintAdapter<BigDecimal>

MaxBigDecimal(AdapterCreateRequest request) {
super(request);
this.max = new BigDecimal(String.valueOf(request.attribute(VALUE)));
this.max = new BigDecimal(String.valueOf(request.<Object>attribute(VALUE)));
}

@Override
Expand All @@ -196,7 +196,7 @@ static final class MaxBigInteger extends AbstractConstraintAdapter<BigInteger>

MaxBigInteger(AdapterCreateRequest request) {
super(request);
this.max = new BigInteger(String.valueOf(request.attribute(VALUE)));
this.max = new BigInteger(String.valueOf(request.<Object>attribute(VALUE)));
}

@Override
Expand All @@ -214,7 +214,7 @@ private static final class MinAdapter extends PrimitiveAdapter<Number>
MinAdapter(AdapterCreateRequest request) {
super(request);
this.targetType = request.targetType();
this.min = (long) request.attribute(VALUE);
this.min = request.attribute(VALUE);
}

@Override
Expand Down Expand Up @@ -268,7 +268,7 @@ static final class MinBigDecimal extends AbstractConstraintAdapter<BigDecimal>

MinBigDecimal(AdapterCreateRequest request) {
super(request);
this.min = new BigDecimal(String.valueOf(request.attribute(VALUE)));
this.min = new BigDecimal(String.valueOf(request.<Object>attribute(VALUE)));
}

@Override
Expand All @@ -284,7 +284,7 @@ static final class MinBigInteger extends AbstractConstraintAdapter<BigInteger>

MinBigInteger(AdapterCreateRequest request) {
super(request);
this.min = new BigInteger(String.valueOf(request.attribute(VALUE)));
this.min = new BigInteger(String.valueOf(request.<Object>attribute(VALUE)));
}

@Override
Expand Down Expand Up @@ -438,8 +438,8 @@ private static final class RangeAdapter extends PrimitiveAdapter<Number> {
@SuppressWarnings("unchecked")
RangeAdapter(AdapterCreateRequest request) {
super(request);
this.min = (long) request.attribute("min");
this.max = (long) request.attribute("max");
this.min = request.attribute("min");
this.max = request.attribute("max");
this.maxAdapter = (NumberAdapter<Number>) max(request.withValue(max));
this.minAdapter = (NumberAdapter<Number>) min(request.withValue(min));
}
Expand Down Expand Up @@ -490,8 +490,8 @@ private static final class RangeStringAdapter extends AbstractConstraintAdapter<

RangeStringAdapter(AdapterCreateRequest request) {
super(request);
this.min = BigDecimal.valueOf((long) request.attribute("min"));
this.max = BigDecimal.valueOf((long) request.attribute("max"));
this.min = BigDecimal.valueOf(request.attribute("min"));
this.max = BigDecimal.valueOf(request.attribute("max"));
}

@Override
Expand Down

0 comments on commit e0f86f6

Please sign in to comment.