gatanaso/multiselect-combo-box-flow

Don't use Object.hashCode to generate keys in the keymapper

thomasdewaelheyns opened this issue · 2 comments

In the MultiselectComboBoxDataCommunicator extends DataCommunicator a custom keymapper is implemented:

private KeyMapper<T> uniqueKeyMapper = new KeyMapper<T>() {

		private T object;

		@Override
		public String key(T o) {
			this.object = o;
			try {
				return super.key(o);
			} finally {
				this.object = null;
			}
		}

		@Override
		protected String createKey() {
			return String.valueOf(object.hashCode());
		}
	};

The createKey() generates what should be a unique internal code for the items of a MultiSelectComboBox. By using the hashcode of the object, this uniqueness is not guaranteed.

From the java 7 javadoc:
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.

I had an issue for example while overriding the hashcode for object distinguished only by a long id. Temporary negative ids were handed the same hashcode as the positive id = -1*(negative id)+1. When selecting an option from the list, multiple objects were selected.

Hi @thomasdewaelheyns and thanks for raising this issue. Thanks to @aszalai, there is a PR that should resolve this issue #80. Unfortunately, I haven't had time yet to go over it yet, but I hope it helps.

Closing this issue, as with the merge of #80 there is now a possibility to introduce a custom unique key data generator.