Skip to content

Commit 2e7c8ea

Browse files
committed
fixed issues
1 parent 908a141 commit 2e7c8ea

2 files changed

Lines changed: 54 additions & 22 deletions

File tree

apache-rat-core/src/main/java/org/apache/rat/config/results/ClaimValidator.java

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,46 @@ public boolean hasErrors() {
6666
return hasErrors;
6767
}
6868

69+
/**
70+
* Sets the value unconditionally.
71+
* @param result the MutableInt to set.
72+
* @param value the value to set it to.
73+
* @return the {@code result} argument
74+
*/
75+
private MutableInt newValue(final MutableInt result, final int value) {
76+
result.setValue(value);
77+
return result;
78+
}
79+
80+
/**
81+
* Sets the {@code result.value} if it is greater than the {@code value} argument.
82+
* @param result the MutableInt to set.
83+
* @param value the value to set it to if it is less than the result.value
84+
* @return the {@code result} argument
85+
*/
86+
private MutableInt setMinValue(final MutableInt result, final int value) {
87+
if (result.intValue() > value) {
88+
result.setValue(value);
89+
}
90+
return result;
91+
}
92+
93+
/**
94+
* Sets the {@code result.value} if it is less than the {@code value} argument.
95+
* @param result the MutableInt to set.
96+
* @param value the value to set it to if it is greater than the result.value
97+
* @return the {@code result} argument
98+
*/
99+
private MutableInt setMaxValue(final MutableInt result, final int value) {
100+
if (result.intValue() < value) {
101+
result.setValue(value);
102+
}
103+
return result;
104+
}
105+
69106
/**
70107
* Sets the max value for the specified counter.
108+
* Will adjust the minimum if the minimum is greater than the maximum.
71109
* @param counter the counter to set the limit for.
72110
* @param value the value to set. A negative value specifies no maximum value.
73111
*/
@@ -76,22 +114,17 @@ public void setMax(final ClaimStatistic.Counter counter, final int value) {
76114
DefaultLog.getInstance().warn("`null` passed as argument to setMax() -- ignoring");
77115
return;
78116
}
79-
MutableInt maxValue = max.compute(counter, (k, v) -> {
80-
MutableInt result = v == null ? new MutableInt(k.getDefaultMaxValue()) : v;
81-
result.setValue(value < 0 ? Integer.MAX_VALUE : value);
82-
return result;
83-
});
84-
min.compute(counter, (k, v) -> {
85-
MutableInt result = v == null ? new MutableInt(k.getDefaultMinValue()) : v;
86-
if (result.intValue() > maxValue.intValue()) {
87-
result.setValue(maxValue.intValue());
88-
}
89-
return result;
90-
});
117+
int setValue = value < 0 ? Integer.MAX_VALUE : value;
118+
MutableInt maxValue = max.compute(counter, (k, v) ->
119+
v == null ? new MutableInt(setValue) : newValue(v, setValue));
120+
min.compute(counter, (k, v) ->
121+
v == null ? new MutableInt(k.getDefaultMinValue()) :
122+
setMinValue(v, setValue));
91123
}
92124

93125
/**
94126
* Sets the min value for the specified counter.
127+
* Will adjust the maximum if the maximum is less than the minimum.
95128
* @param counter the counter to set the limit for.
96129
* @param value the value to set. A negative value specifies no maximum value.
97130
*/
@@ -100,12 +133,11 @@ public void setMin(final ClaimStatistic.Counter counter, final int value) {
100133
DefaultLog.getInstance().warn("`null` passed as argument to setMin() -- ignoring");
101134
return;
102135
}
103-
max.compute(counter, (k, v) -> {
104-
MutableInt result = v == null ? new MutableInt(k.getDefaultMinValue()) : v;
105-
if (result.intValue() < value) {
106-
result.setValue(value);
107-
}
108-
return v; });
136+
min.compute(counter, (k, v) ->
137+
v == null ? new MutableInt(value) : newValue(v, value));
138+
max.compute(counter, (k, v) ->
139+
v == null ? setMaxValue(new MutableInt(k.getDefaultMaxValue()), value) :
140+
setMaxValue(v, value));
109141
}
110142

111143
/**

apache-rat-core/src/test/java/org/apache/rat/config/results/ClaimValidatorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ void setMinTest() {
7373
int expected = 5;
7474
for (ClaimStatistic.Counter counter : ClaimStatistic.Counter.values()) {
7575
validator.setMin(counter, expected);
76-
assertThat(validator.getMin(counter)).as(() -> format("'%s' value is invalid", counter)).isEqualTo(expected);
77-
assertThat(validator.isValid(counter, expected)).isTrue();
76+
assertThat(validator.getMin(counter)).as(() -> format("'%s' min value is invalid", counter)).isEqualTo(expected);
77+
assertThat(validator.isValid(counter, expected)).as(() -> format("'%s' value is invalid", counter)).isTrue();
7878
}
7979
expected = Integer.MAX_VALUE;
8080
for (ClaimStatistic.Counter counter : ClaimStatistic.Counter.values()) {
8181
validator.setMax(counter, -1);
82-
assertThat(validator.getMax(counter)).as(() -> format("'%s' value is invalid", counter)).isEqualTo(expected);
83-
assertThat(validator.isValid(counter, expected)).isTrue();
82+
assertThat(validator.getMax(counter)).as(() -> format("'%s' max value is invalid", counter)).isEqualTo(expected);
83+
assertThat(validator.isValid(counter, expected)).as(() -> format("'%s' value is invalid", counter)).isTrue();
8484
}
8585
}
8686

0 commit comments

Comments
 (0)