Revision: 1601
          http://svn.sourceforge.net/spring-rich-c/?rev=1601&view=rev
Author:   mathiasbr
Date:     2006-12-17 09:17:25 -0800 (Sun, 17 Dec 2006)

Log Message:
-----------
fixed equals & hashcode impl. + formatting

Modified Paths:
--------------
    
trunk/spring-richclient/core/src/main/java/org/springframework/richclient/factory/LabelInfo.java
    
trunk/spring-richclient/core/src/test/java/org/springframework/richclient/factory/LabelInfoTests.java

Modified: 
trunk/spring-richclient/core/src/main/java/org/springframework/richclient/factory/LabelInfo.java
===================================================================
--- 
trunk/spring-richclient/core/src/main/java/org/springframework/richclient/factory/LabelInfo.java
    2006-12-16 17:24:51 UTC (rev 1600)
+++ 
trunk/spring-richclient/core/src/main/java/org/springframework/richclient/factory/LabelInfo.java
    2006-12-17 17:17:25 UTC (rev 1601)
@@ -27,10 +27,9 @@
 import org.springframework.util.StringUtils;
 
 /**
- * A parameter object for a labelable component; consists of the text, 
mnemonic,
- * mnemonicIndex, and accelerator that may be associated with a labeled
- * component. This class also acts a factory for producing control prototypes
- * which are preconfigured with a LabelInfo's properties.
+ * A parameter object for a labelable component; consists of the text, 
mnemonic, mnemonicIndex, and accelerator that may
+ * be associated with a labeled component. This class also acts a factory for 
producing control prototypes which are
+ * preconfigured with a LabelInfo's properties.
  * 
  * @author Keith Donald
  */
@@ -68,15 +67,32 @@
     }
 
     public int hashCode() {
-        return mnemonic + mnemonicIndex + text.hashCode();
+        final int PRIME = 31;
+        int result = 1;
+        result = PRIME * result + mnemonic;
+        result = PRIME * result + mnemonicIndex;
+        result = PRIME * result + ((text == null) ? 0 : text.hashCode());
+        return result;
     }
 
-    public boolean equals(Object o) {
-        if (!(o instanceof LabelInfo)) {
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
             return false;
-        }
-        LabelInfo info = (LabelInfo)o;
-        return text.equals(info.text) && mnemonic == info.mnemonic && 
mnemonicIndex == info.mnemonicIndex;
+        if (getClass() != obj.getClass())
+            return false;
+        final LabelInfo other = (LabelInfo) obj;
+        if (mnemonic != other.mnemonic)
+            return false;
+        if (mnemonicIndex != other.mnemonicIndex)
+            return false;
+        if (text == null) {
+            if (other.text != null)
+                return false;
+        } else if (!text.equals(other.text))
+            return false;
+        return true;
     }
 
     public JLabel configureLabel(JLabel label) {
@@ -104,7 +120,7 @@
         label.setLabelFor(component);
         return label;
     }
-    
+
     public AbstractButton configureButton(AbstractButton button) {
         Assert.notNull(button);
         button.setText(text);

Modified: 
trunk/spring-richclient/core/src/test/java/org/springframework/richclient/factory/LabelInfoTests.java
===================================================================
--- 
trunk/spring-richclient/core/src/test/java/org/springframework/richclient/factory/LabelInfoTests.java
       2006-12-16 17:24:51 UTC (rev 1600)
+++ 
trunk/spring-richclient/core/src/test/java/org/springframework/richclient/factory/LabelInfoTests.java
       2006-12-17 17:17:25 UTC (rev 1601)
@@ -42,7 +42,35 @@
         assertEquals('t', info.getMnemonic());
         assertEquals(3, info.getMnemonicIndex());
     }
+    
+    public void testEquals() throws Exception {
+        LabelInfo info1 = new LabelInfo("test", 0, 0);
+        LabelInfo info2 = new LabelInfo("test", 0, 0);
+        assertTrue(info1.equals(info2));
+        info2 = new LabelInfo("test", 1, 0);
+        assertFalse(info1.equals(info2));
+        info2 = new LabelInfo("test", 0, 1);
+        assertFalse(info1.equals(info2));
+        info2 = new LabelInfo("test2", 0, 0);
+        assertFalse(info1.equals(info2));
+        assertFalse(info1.equals(null));
+        info2 = new LabelInfo("test", 0,0) {};
+        assertFalse(info1.equals(info2));
+    }
 
+    public void testHashCode() throws Exception {
+        LabelInfo info1 = new LabelInfo("test", 0, 0);
+        LabelInfo info2 = new LabelInfo("test", 0, 0);
+        assertTrue(info1.hashCode() == info2.hashCode());
+        info2 = new LabelInfo("test", 1, 0);
+        assertFalse(info1.hashCode() == info2.hashCode());
+        LabelInfo info3 = new LabelInfo("test", 0, 1);
+        assertFalse(info1.hashCode() == info2.hashCode());
+        assertFalse(info2.hashCode() == info3.hashCode());
+        info2 = new LabelInfo("test2", 0, 0);
+        assertFalse(info1.hashCode() == info2.hashCode());
+    }
+
     public void testConstructorEmptyText() {
         LabelInfo info = new LabelInfo("", 'a', 5);
         assertEquals("", info.getText());


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
spring-rich-c-cvs mailing list
spring-rich-c-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/spring-rich-c-cvs

Reply via email to