@Override
public int hashCode() {
- return rowNum.hashCode();
+ return getRowNum();
}
@Override
public boolean equals(Object obj) {
- if (obj == null || !(obj instanceof ReadOnlyRowId)) {
+ if (obj == null || !(ReadOnlyRowId.class.equals(obj.getClass()))) {
return false;
}
- return rowNum.equals(((ReadOnlyRowId) obj).rowNum);
+ return getRowNum() == (((ReadOnlyRowId) obj).getRowNum());
}
public int getRowNum() {
return rowNum;
}
+
+ @Override
+ public String toString() {
+ return String.valueOf(getRowNum());
+ }
}
package com.vaadin.data.util.sqlcontainer;
import java.io.Serializable;
+import java.util.Arrays;
/**
* RowId represents identifiers of a single database result set row.
@Override
public int hashCode() {
- int result = 31;
- if (id != null) {
- for (Object o : id) {
- if (o != null) {
- result += o.hashCode();
- }
- }
- }
- return result;
+ return Arrays.hashCode(getId());
}
@Override
public boolean equals(Object obj) {
- if (obj == null || !(obj instanceof RowId)) {
- return false;
- }
- Object[] compId = ((RowId) obj).getId();
- if (id == null && compId == null) {
- return true;
- }
- if (id.length != compId.length) {
+ if (obj == null || !(RowId.class.equals(obj.getClass()))) {
return false;
}
- for (int i = 0; i < id.length; i++) {
- if ((id[i] == null && compId[i] != null)
- || (id[i] != null && !id[i].equals(compId[i]))) {
- return false;
- }
- }
- return true;
+ return Arrays.equals(getId(), ((RowId) obj).getId());
}
@Override
public String toString() {
- StringBuffer s = new StringBuffer();
- for (int i = 0; i < id.length; i++) {
- s.append(id[i]);
- if (i < id.length - 1) {
- s.append("/");
- }
+ if (getId() == null) {
+ return "";
+ }
+ StringBuilder builder = new StringBuilder();
+ for (Object id : getId()) {
+ builder.append(id);
+ builder.append('/');
+ }
+ if (builder.length() > 0) {
+ return builder.substring(0, builder.length() - 1);
}
- return s.toString();
+ return builder.toString();
}
}
ReadOnlyRowId rid2 = new ReadOnlyRowId(42);
Assert.assertFalse(rid.equals(rid2));
}
+
+ @Test
+ public void toString_rowNumberIsReturned() {
+ int i = 1;
+ ReadOnlyRowId rowId = new ReadOnlyRowId(i);
+ Assert.assertEquals("Unexpected toString value", String.valueOf(i),
+ rowId.toString());
+ }
}
Assert.assertFalse(id.equals("Tudiluu"));
Assert.assertFalse(id.equals(new Integer(1337)));
}
+
+ @Test
+ public void toString_defaultCtor_noException() {
+ RowId rowId = new RowId();
+ Assert.assertTrue("Unexpected to string for empty Row Id", rowId
+ .toString().isEmpty());
+ }
}