I am initializing a class via Lombok Builder. Through which I am initializing a variable. But that's variable's value is not available when I use that in a Predicate definition.
My code looks this:
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
class NumCheck {
int maxCount;
Predicate<Integer> isLessThanMax = num -> maxCount < num;
}
public class PredicateInstance {
public static void main(String[] args) {
int a = 5, b=11;
NumCheck numCheck = new NumCheck().toBuilder().maxCount(10).build();
Stream.of(2,5,6,7,10,11,20,32).filter(numCheck.isLessThanMax).forEach(System.out::println);
}
}
While debugging, the value of maxCount was not getting initialized to 10. It remained 0.
If I remove the Lombok builders and do it in normal way, this is working fine:
class NumCheck {
int maxCount;
Predicate<Integer> isLessThanMax = num -> maxCount < num;
}
public class PredicateInstance {
public static void main(String[] args) {
int a = 5, b=11;
NumCheck numCheck = new NumCheck();
numCheck.maxCount = 10;
Stream.of(2,5,6,7,10,11,20,32).filter(numCheck.isLessThanMax).forEach(System.out::println);
}
}
Is it a known limitation of Lombok or am I doing anything wrong here?
Workarounds that worked but not applicable for my scenario:
- Declaring the maxCount as static.
- Declare BiPredicate and send maxCount along with num parameter.