I defined a RecordVisitor interface declared as follows:
public interface RecordVisitor<K, V, Result> { public Result visit(Record<K, V> rec); }
The idea is that visit implementations will be called with a Record object, will execute their logic and return a result. I would like the implementation to not remove the value of rec and use it outside of the visit call.
The only way I decided to do this is to add state to the Record and throw an IllegalStateException if any method is called in the recording object until it is "active". Then I will write terrible warnings in javadoc and hope that the performers read it.
Is there a more reliable way to prevent the use of the Record object outside the visit method? If possible, I would like to build an interface that leads to compile-time errors if the contract is broken.
source share