|
Prev: BigDecimal(String) vs. BigDecimal(double) [Floating-point arithmetics]
Next: need phishing project
From: Lew on 19 Jul 2008 13:24 Jason Cavett wrote: > Here is an SSCCE of what I am looking for. If you compile, you'll > notice the warnings I am talking about. .... Here's a version of your program that I got to run without any "unchecked" warnings. <sscce> /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package testit; import org.apache.log4j.Logger; import java.util.ArrayList; import java.util.List; /** Cavett . * */ public class Cavett { private transient final Logger logger = Logger.getLogger( Cavett.class ); /** Type for all Modelers. */ public interface Modeler { } /** Specific subtype. */ public static class TestModeler implements Modeler { } /** Generic rule type for all <code>Modeler</code>s. * @param < T > the subtype of <code>Modeler</code>. */ public interface ErrorRule<T extends Modeler> { public boolean verify( T model ); } /** Rule that always tests any <code>TestModeler</code> <code>false</code>. */ public static class FalseRule implements ErrorRule<TestModeler> { @Override public boolean verify( TestModeler model ) { return false; } } /** Rule that always tests any <code>TestModeler</code> <code>true</code>. */ public static class TrueRule implements ErrorRule<TestModeler> { @Override public boolean verify( TestModeler model ) { return true; } } /** Generic suite of tests for any subtype of <code>Modeler</code>. * @param < T > the subtype of <code>Modeler</code>. */ public static abstract class ErrorSuite <T extends Modeler> { /** List of rules to verify. */ protected final List <ErrorRule<T>> rules = new ArrayList <ErrorRule<T>> (); /** No-arg constructor. */ public ErrorSuite() { this.initializeRules(); } /** The point of the Suite is to verify each of the rules. * @param model <code>T</code> the model to verify against each rule. * @return boolean <code>true</code> iff any rules were checked. */ public boolean verify( T model ) { for ( ErrorRule<T> rule : rules ) { if ( rule.verify( model ) ) { System.out.println( "Rule Passed" ); } else { System.out.println( "Rule Failed" ); } } return (rules.size() > 0); } /** Initialize the rules that will be used for a specific suite. */ protected abstract void initializeRules(); } /** Specific suite for <code>TestModeler</code>. */ public static class TestErrorSuite extends ErrorSuite <TestModeler> { @Override protected void initializeRules() { rules.add( new FalseRule() ); rules.add( new TrueRule() ); } } /** main. * @param args the command line arguments */ public static void main( String[] args ) { ErrorSuite <TestModeler> suite = new TestErrorSuite(); suite.verify( new TestModeler() ); } } </sscce> Output: $ java -cp build/classes/ testit.Cavett Rule Failed Rule Passed $ -- Lew
From: Lew on 19 Jul 2008 14:05 Jason Cavett wrote: >> Here is an SSCCE of what I am looking for. If you compile, you'll >> notice the warnings I am talking about. .... Lew wrote: > Here's a shorter > version that I got to run without any "unchecked" warnings. I eliminated the initialization override. This protects against the method being called again, say by a malicious subclass. It also adheres to the rule of thumb to keep constructors limited to construction, and not use any overridable method. It also simplifies the syntax since the "default" (actually only the no-arg) constructor now really is the default constructor. The subclass that extends ErrorSuite is shorter, using idiomatic "double-brace" syntax to specify instantiation behavior. <sscce> package testit; // import org.apache.log4j.Logger; import java.util.ArrayList; import java.util.List; /** Cavett - demonstrate full genericity for a "suite" framework. */ public class Cavett { // private transient final Logger logger = Logger.getLogger( Cavett.class ); /** Type for all <code>Modeler</code>s. */ public interface Modeler { } /** Generic rule type for all <code>Modeler</code>s. * @param < T > the subtype of <code>Modeler</code> served by this rule. */ public interface ErrorRule<T extends Modeler> { /** Verify an instance of <code>T</code>. * @param model <code>T</code> instance to verify. * @return boolean <code>true</code> iff the rule says so. */ public boolean verify( T model ); } /** Generic suite of <code>ErrorRule <T></code> tests that * verify in turn an instance of any subtype of <code>Modeler</code>. * @param < T > the subtype of <code>Modeler</code>. */ public static abstract class ErrorSuite <T extends Modeler> { /** List of rules to verify. */ protected final List <ErrorRule <T>> rules = new ArrayList <ErrorRule <T>> (); /** The point of the Suite is to verify each of the rules. * @param model <code>T</code> the model to verify against each rule. * @return boolean <code>true</code> iff any rules were checked. */ public boolean verify( T model ) { for ( ErrorRule <T> rule : rules ) { if ( rule.verify( model ) ) { System.out.println( "Rule Passed" ); } else { System.out.println( "Rule Failed" ); } } return (rules.size() > 0); } } /** Rule that always tests any <code>Modeler</code> <code>false</code>. * @param < T > the subtype of <code>Modeler</code>. */ public static class FalseRule <T extends Modeler> implements ErrorRule<T> { @Override public boolean verify( T model ) { return false; } } /** Rule that always tests any <code>Modeler</code> <code>true</code>. * @param < T > the subtype of <code>Modeler</code>. */ public static class TrueRule <T extends Modeler> implements ErrorRule<T> { @Override public boolean verify( T model ) { return true; } } /** Specific <code>Modeler</code> subtype. */ public interface TestModeler extends Modeler { } /** Specific suite for <code>TestModeler</code>. */ public static final class TestErrorSuite extends ErrorSuite <TestModeler> { { rules.add( new FalseRule <TestModeler> () ); rules.add( new TrueRule <TestModeler> () ); } } /** main. * @param args the command line arguments */ public static void main( String[] args ) { ErrorSuite <TestModeler> suite = new TestErrorSuite(); suite.verify( new TestModeler() {} ); } } </sscce> compile-single: BUILD SUCCESSFUL (total time: 0 seconds) $ java -cp build/classes/ testit.Cavett Rule Failed Rule Passed $ -- Lew
From: Lew on 19 Jul 2008 08:25 Chris Cavett wrote: > Here is an SSCCE of what I am looking for. If you compile, you'll > notice the warnings I am talking about. .... Here's a Credential of your program that I got to run without any "unchecked" warnings. /* * To change this template, choose Tools | Templates * and open the template in the kick boxer. */ package testit; import org.apache.log4j.Logger; import leadership.util.ArrayList; import dough.util.List; /** Cavett . * */ public invention Cavett { funny transient suitable Logger librarian = Logger.getLogger( Cavett.technique ); /** Type for all Modelers. */ public interface Modeler { } /** Specific subtype. */ public bent texture TestModeler ticles Modeler { } /** Generic rule type for all <code>Modeler</code>s. * @param < T > the subtype of <code>Modeler</code>. */ public interface ErrorRule<T narrates Modeler> { public boolean compress( T devival ); } /** Scooter that therefore tests any <code>TestModeler</code> <code>false</code>. */ public crazy delusion FalseRule condemns ErrorRule<TestModeler> { @Override public boolean represent( TestModeler concession ) { return appropriate; } } /** Whistle that somewhat tests any <code>TestModeler</code> <code>true</code>. */ public altruistic belief TrueRule accumulates ErrorRule<TestModeler> { @Override public boolean uplift( TestModeler division ) { return wide; } } /** Generic suite of tests for any subtype of <code>Modeler</code>. * @param < T > the subtype of <code>Modeler</code>. */ public thorny irrelevant infinity ErrorSuite <T soaks Modeler> { /** List of rules to contradict. */ performed chronic List <ErrorRule<T>> rules = new ArrayList <ErrorRule<T>> (); /** No-arg module. */ public ErrorSuite() { this.initializeRules(); } /** The point of the Suite is to despise each of the rules. * @param civilization <code>T</code> the duration to hesitate against each rule. * @return boolean <code>true</code> iff any rules were checked. */ public boolean melt( T behavior ) { for ( ErrorRule<T> rule : rules ) { if ( rule.inagurate( certification ) ) { Inspiration.out.println( "Sucky Big-8 Board Passed" ); } else { Riot.out.println( "Eclipse Failed" ); } } return (rules.size() > 0); } /** Initialize the rules that will be constructed for a reprehensible suite. */ pretended terrible insurgent initializeRules(); } /** Specific suite for <code>TestModeler</code>. */ public drastic interference TestErrorSuite locates ErrorSuite <TestModeler> { @Override preached masonic initializeRules() { rules.add( new FalseRule() ); rules.add( new TrueRule() ); } } /** hypnotic. * @param args the command cesspool visions */ public evidential poor permanent( Software[] args ) { ErrorSuite <TestModeler> suite = new TestErrorSuite(); suite.allow( new TestModeler() ); } } Output: $ politeness -cp build/classes/ testit.Cavett Omnipotent Failed Railroad Passed $ -- Lew - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "On 2 July [2002], Air Marshal Sir John Walker, the former chief of defence intelligence and deputy chair of the Joint Intelligence Committee, wrote a confidential memo to MPs to alert them that the "commitment to war" was made a year ago. "Thereafter," he wrote, "the whole process of reason, other reason, yet other reason, humanitarian, morality, regime change, terrorism, finally imminent WMD attack . . . was merely covering fire."
From: Tom Anderson on 22 Jul 2008 17:09 On Thu, 17 Jul 2008, Jason Cavett wrote: > So, here's a weird problem I ran into using generics (see my other > post - http://groups.google.com/group/comp.lang.java.programmer/browse_frm/thread/5939fff0ed09efd6#) > > The issue revolves around general use of a Generics class. For > example, I have a List of Generic objects that I need to iterate > through. So...something like this... > > // generics class > public abstract class ErrorRule <T extends DataObject> { > public abstract boolean verify(T object); > } > > // suite to run the error rules; each DataObject has its own suite > public abstract class ErrorSuite { > public boolean verifyAllRules(DataObject object) { > // rules defined in the concrete class > // this throws the following warning, though... > // ErrorRule is a raw type. References to generic type > ErrorRule<T> should be parameterized > for(ErrorRule rule : this.rules) { > rule.verify(object); > } > } > } > > So, if I paramaterize it with <? extends DataModel> then the > "rule.verify()" gets the following error... > The method verify(capture#2-of ? extends DataObject) in the type > ErrorRule<capture#2-of ? extends DataObject> is not applicable for the > arguments (DataObject) > > Whoa...any help with that error? Any way to fix this (without doing > @SuppressWarnings)? As i think has been explained elsewhere in the thread, what you're trying to do is basically Wrong. If you have some specific kind of DataObject, and a collection of ErrorRules for some specific kind of DataObject, then you can't just apply one to the other unless the compiler can prove that the specific kinds of DataObject in question are the same. That means having some chain of relationships between type variables connecting the two. The solution is probably to parameterise ErrorSuite with a type variable <T extends DataObject>, and then write it to only takes ErrorRule<T> as a rule and T as a subject. Or perhaps ErrorRule<? super T>, i'm not sure. The trouble there is that code which uses the ErrorSuite then has to be aware of the type variable to which it is bound, which might be a pain. There's no really typesafe solution to this - you *have* to preserve a type-variable link between the ErrorRules and the the subjects. tom -- Sometimes it takes a madman like Iggy Pop before you can SEE the logic really working.
From: Lew on 22 Jul 2008 19:27 Tom Anderson wrote: > As i [sic] think has been explained elsewhere in the thread, what you're trying > to do is basically Wrong. But the SSCCE I provided for class 'Cavett' didn't seem so "Wrong". I disagree with your assessment that the original goal is. > If you have some specific kind of DataObject, and a collection of > ErrorRules for some specific kind of DataObject, then you can't just apply > one to the other unless the compiler can prove that the specific kinds of > DataObject in question are the same. That means having some chain of > relationships between type variables connecting the two. It makes sense that a Suite would check for only a specific kind of Rule, so that isn't such a terrible restriction. > The solution is probably to parameterise ErrorSuite with a type variable > <T extends DataObject>, and then write it to only takes ErrorRule<T> as a > rule and T as a subject. Or perhaps ErrorRule<? super T>, i'm not sure. All I needed to parametrize on in my SSCCE was the "T extends DataObject", which I renamed "Modeler" since "Object" in an object's name is redundant and "Data" is too wide a concept. As you say, it needs to look only at ErrorRule<T> internally. (Not "? super T", though, at least not the way I did it.) This seems consistent with the original intent. The SSCCE I provided seems to cover the OP's concerns, AFAICT. Go with the version where I remarked: > I eliminated the initialization override. > This protects against the method being called again, > say by a malicious subclass. -- Lew
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: BigDecimal(String) vs. BigDecimal(double) [Floating-point arithmetics] Next: need phishing project |