John Mercier

java programming and scjp

  • Blog
  • Projects

Testing .equals()

Posted on June 12, 2011 by John J Mercier

Filed under Programming | 0 Comments

I've recently been reading Effective Java, by Joshua Bloch, and the section about the equals() method has sparked my interest. The book explains that there are 5 properties that equals must enforce. equals() must be reflexive, symmetric, transitive, consistent, and non-null. While I was reading I though about how easy it would be to make methods that tests all of these attributes for any object in java. Each property can have its own test, and there can be one method to test all attributes. Before starting this I tried to see what netbeans comes up with for the generated equals test. Unfortunately, it does not test for all these properties. Here is what I came up with.

public class TestEquals {
    public static boolean reflexive(Object o1) {
        return o1 instanceof Object && o1.equals(o1);
    }

    public static boolean symmetric(Object o1, Object o2) {
        return o1 instanceof Object && o2 instanceof Object && o1.equals(o2) && o2.equals(o1);
    }

    public static boolean transitive(Object o1, Object o2, Object o3) {
        return o1 instanceof Object && o2 instanceof Object && o3 instanceof Object &&
                o1.equals(o2) && o2.equals(o3) && o1.equals(o3);
    }

    public static boolean consistent(Object o1, Object o2) {
        boolean b = true;
        for(int i = 0; i < 1000; i++) {
            b = o1.equals(o2);
            if(b == false) {
                return false;
            }
        }
        return b;
    }

    public static boolean nonNullity(Object o1) {
        return !o1.equals(null);
    }
    
    public static boolean test(Object o1, Object o2, Object o3) {
        boolean b = false;
        b = o1 instanceof Object && o2 instanceof Object && o3 instanceof Object;
        System.out.println("instance of Object = " + (o1 instanceof Object && o2 instanceof Object && o3 instanceof Object));
        b &= nonNullity(o1);
        System.out.println("nonNullity(" + o1 + ") = " + nonNullity(o1));
        b &= reflexive(o1);
        System.out.println("reflexive(" + o2 + ") = " + reflexive(o1));
        b &= symmetric(o1, o2);
        System.out.println("symmetric(" + o1 + ", " + o2 + ") = " + symmetric(o1, o2));
        b &= transitive(o1, o2, o3);
        System.out.println("transitive(" + o1 + ", " + o2 + ", " + o3 + ") = " + transitive(o1, o2, o3));
        b &= consistent(o1, o2);
        System.out.println("consistent(" + o1 + ", " + o2 + ") = " + consistent(o1, o2));
        return b;
    }

    public static void main(String... args) {
        String s1 = "string";
        String s2 = "string";
        String s3 = "string";
        test(s1, s2, s3);
    }
}

 

Share |

« Storing 24 Game data | Main


Comments:

Post a Comment:
  • HTML Syntax: Allowed
  • General (12)
  • Projects (3)
  • Programming (14)

Search

Tag Cloud

activism addthis.com data_structure downloading facebook google google-buzz introduction java javablackbelt jdbc johnmercier.com jsp linux model-1 netbeans nvidia objectivism official-english programming projects pti roller scjp server sql theme uncertainty velocity welfare-state

Friends

  • Ed
  • Shane

Links

  • Glazed Lists
  • JGoodies
  • Java Specialists
  • Swing 2.0
  • Swing Generics
  • ideone
  • pircbot

Feeds

  • All
  • /General
  • /Projects
  • /Programming
  • Comments

Referrers

  • direct (134)
  • keflex.tr.gg/KEFLEX- (10)
  • pheromones-to-attrac (8)
  • www.beeplog.com/2609 (6)
  • softbanhtc.info/site (5)
  • softbanhtc.info/site (5)
  • softbanhtc.info/site (5)
  • softaxianhtc.info/si (5)
  • softbanhtc.info/site (5)
  • softbanhtc.info/site (5)
  • softbanhtc.info/site (5)
  • softcelandroid.info/ (5)
  • softaxianhtc.info/si (5)
  • softbanhtc.info/site (5)
  • softcelandroid.info/ (5)
  • softcelandroid.info/ (5)
  • softcelandroid.info/ (5)
  • softaxianhtc.info/si (5)
  • softcelandroid.info/ (5)
  • softcelandroid.info/ (5)
  • softcelandroid.info/ (5)
  • softaxianhtc.info/si (5)
  • softcelandroid.info/ (5)
  • softcelandroid.info/ (5)
  • softcelandroid.info/ (5)
  • softcelandroid.info/ (5)
  • softaxianhtc.info/si (5)
  • softcelandroid.info/ (5)
  • softcelandroid.info/ (5)
  • softcelandroid.info/ (5)

Navigation

  • John Mercier
  • Weblog
  • Login

©2010 John J Mercier.

Designed by Free CSS Templates. Template by E. Strokin. Powered by Roller Weblogger 4.0.1.