Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

By operating on String objects, the CaseInsensitiveString.equals() method violates the second contract requirement (symmetry). Because of the asymmetry, given a String object s and a CaseInsensitiveString object cis that differ only in case, cis.equals(s)) returns true, while s.equals(cis) returns false.

Note that this code also violates rule MET09-J. Classes that define an equals() method must also define a hashCode() method.

Compliant Solution

In this compliant solution, the CaseInsensitiveString.equals() method is simplified to operate only on instances of the CaseInsensitiveString class, consequently preserving symmetry. The class also complies with rule {MET09-J. Classes that define an equals() method must also define a hashCode() method] by defining a hashCode() method.

Code Block
bgColor#ccccff
public final class CaseInsensitiveString {
  private String s;

  public CaseInsensitiveString(String s) {
    if (s == null) {
      throw new NullPointerException();
    }
    this.s = s;
  }

  public boolean equals(Object o) {
    return o instanceof CaseInsensitiveString &&
        ((CaseInsensitiveString)o).s.equalsIgnoreCase(s);
  }

  public int hashCode() {
    // ...
  }

  public static void main(String[] args) {
    CaseInsensitiveString cis = new CaseInsensitiveString("Java");
    String s = "java";
    System.out.println(cis.equals(s)); // Returns false now
    System.out.println(s.equals(cis)); // Returns false now
  }
}

...

The method java.lang.Object.equals() by default is unable to compare composite objects such as cryptographic keys. Most Key classes lack an equals() implementation that overrides would override Object's default implementation. In such cases, the components of the composite object must be compared individually to ensure correctness.

...

Code Block
bgColor#FFCCCC
private static boolean keysEqual(Key key1, Key key2) {
  if (key1.equals(key2)) {
    return true;
  }
}

Compliant Solution (java.security.Key)

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="965aada99a65afbb-8bddcc34-47d046a1-8d2386b2-cf111db495889b766f34c164"><ac:plain-text-body><![CDATA[

[[API 2006

AA. Bibliography#API 06]]

[Method equals()

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#equals(java.lang.Object)]

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="ee433128b9c844fe-12c459e7-44414c47-91019cac-4a006a1f186e19a3fd3a76b1"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. Bibliography#Bloch 08]]

Item 8. Obey the general contract when overriding equals

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1fdb6f92a20da4c8-7434ae1f-4e9e498a-aebaabf5-f4bbf201a0b934d7437364e1"><ac:plain-text-body><![CDATA[

[[Darwin 2004

AA. Bibliography#Darwin 04]]

9.2, Overriding the equals Method

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="91f0c0355540a5ab-0d3a42eb-4a5543c8-ad5c95ad-fad2af2b91b3a3b0000e3afa"><ac:plain-text-body><![CDATA[

[[Harold 1997

AA. Bibliography#Harold 97]]

Chapter 3, Classes, Strings, and Arrays, The Object Class (Equality)

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="1bb1781f11af78d0-fb36b425-45c6452e-b988a104-8d78454928b5f4ac0a1f5f23"><ac:plain-text-body><![CDATA[

[[Sun 2006

AA. Bibliography#Sun 06]]

[Determining If Two Keys Are Equal

http://java.sun.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#Determining%20If%20Two%20Keys%20Are%20Equal] (JCA Reference Guide)

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="791b5e86699ac2e5-c799b4f2-462f494f-9b4dbfa1-a2bfb1940e468a94b4b0ef76"><ac:plain-text-body><![CDATA[

[[Techtalk 2007

AA. Bibliography#Techtalk 07]]

More Joy of Sets

]]></ac:plain-text-body></ac:structured-macro>

...