Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Java
Generics Issue
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
Reply to thread
Message
[QUOTE="maaxiim, post: 3591835"] <snip> I realised that the snippet I posted could only handle incoming objects of type TestObject or DataObject, which is not really what you needed in the first place. I went back to basics and reduced the problem down to remove the type parameterization from the class itself and onto the method declaration. Apologies, the SSCCE is getting a little longwinded now: package test; import java.util.HashSet; import java.util.Set; public class TestErrorSuite { public class DataObject { } public class TestObject extends DataObject { } public class AnotherObject extends DataObject { } public class FurtherObject extends AnotherObject { } public interface ErrorRule { public <T extends DataObject> boolean verify(T object); } public class ErrorSuite { Set<ErrorRule> rules = new HashSet<ErrorRule>(); public boolean verifyAllRules(DataObject object) { boolean verify; for (ErrorRule rule : this.rules) { verify = rule.verify(object); if (verify == false) return false; } return true; } public void addRule(ErrorRule rule) { this.rules.add(rule); } } public void verify() { ErrorSuite suite = new ErrorSuite(); suite.addRule(new ErrorRule() { @Override public <T extends DataObject> boolean verify(T object) { return object instanceof DataObject; } }); suite.addRule(new ErrorRule() { @Override public <T extends DataObject> boolean verify(T object) { return object instanceof TestObject; } }); suite.addRule(new ErrorRule() { @Override public <T extends DataObject> boolean verify(T object) { return object instanceof AnotherObject; } }); suite.verifyAllRules(new AnotherObject()); suite.verifyAllRules(new TestObject()); suite.verifyAllRules(new DataObject()); suite.verifyAllRules(new FurtherObject()); } public static void main(String[] args) { TestErrorSuite testSuite = new TestErrorSuite(); testSuite.verify(); } } This, I think, gives you what you need. It almost seems too simplistic... [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Java
Generics Issue
Top