diff --git a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/java/com/vaadin/flow/component/combobox/dataview/ComboBoxLazyDataView.java b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/java/com/vaadin/flow/component/combobox/dataview/ComboBoxLazyDataView.java index 26b496e7e25..48657a7433a 100644 --- a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/java/com/vaadin/flow/component/combobox/dataview/ComboBoxLazyDataView.java +++ b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/main/java/com/vaadin/flow/component/combobox/dataview/ComboBoxLazyDataView.java @@ -25,6 +25,7 @@ import com.vaadin.flow.data.provider.DataCommunicator; import com.vaadin.flow.data.provider.DataProvider; import com.vaadin.flow.data.provider.HasLazyDataView; +import com.vaadin.flow.data.provider.IdentifierProvider; import com.vaadin.flow.data.provider.ItemCountChangeEvent; import com.vaadin.flow.data.provider.Query; import com.vaadin.flow.shared.Registration; @@ -73,6 +74,14 @@ public void setItemCountCallback( getDataCommunicator().setCountCallback(callback); } + @Override + public void setIdentifierProvider( + IdentifierProvider identifierProvider) { + super.setIdentifierProvider(identifierProvider); + getDataCommunicator().getKeyMapper() + .setIdentifierGetter(identifierProvider); + } + /** * {@inheritDoc} *

diff --git a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/dataview/ComboBoxLazyDataViewTest.java b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/dataview/ComboBoxLazyDataViewTest.java index f28ec3d87d1..fe7c013bc64 100644 --- a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/dataview/ComboBoxLazyDataViewTest.java +++ b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/dataview/ComboBoxLazyDataViewTest.java @@ -15,6 +15,8 @@ */ package com.vaadin.flow.component.combobox.dataview; +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; @@ -34,6 +36,7 @@ import com.vaadin.flow.data.provider.CallbackDataProvider; import com.vaadin.flow.data.provider.DataCommunicator; import com.vaadin.flow.data.provider.DataCommunicatorTest; +import com.vaadin.flow.data.provider.DataKeyMapper; import com.vaadin.flow.data.provider.DataProvider; import com.vaadin.flow.function.SerializableConsumer; @@ -331,4 +334,29 @@ public void getItem_withCountCallbackAndOutsideOfRange_throw() { dataView.getItem(1234567); } + @Test + public void setIdentifierProvider_customIdentifier_keyMapperUsesIdentifier() { + Item first = new Item(1L, "first"); + Item second = new Item(2L, "middle"); + + List items = new ArrayList<>(Arrays.asList(first, second)); + + ComboBox component = new ComboBox<>(); + + DataCommunicator dataCommunicator = new DataCommunicator<>( + (item, jsonObject) -> { + }, null, null, component.getElement().getNode()); + dataCommunicator.setDataProvider( + new CallbackDataProvider<>(query -> Stream.of(), query -> 0), + "", true); + + ComboBoxLazyDataView dataView = new ComboBoxLazyDataView<>( + dataCommunicator, component); + DataKeyMapper keyMapper = dataCommunicator.getKeyMapper(); + items.forEach(keyMapper::key); + + Assert.assertFalse(keyMapper.has(new Item(1L, "non-present"))); + dataView.setIdentifierProvider(Item::getId); + Assert.assertTrue(keyMapper.has(new Item(1L, "non-present"))); + } } diff --git a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/dataview/ComboBoxListDataViewTest.java b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/dataview/ComboBoxListDataViewTest.java index 48b55687263..ed6cfa365d9 100644 --- a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/dataview/ComboBoxListDataViewTest.java +++ b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/dataview/ComboBoxListDataViewTest.java @@ -18,7 +18,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Objects; import java.util.stream.Stream; import org.junit.Assert; @@ -153,53 +152,4 @@ protected HasListDataView> getComponent() { return new ComboBox<>(); } - private static class Item { - private long id; - private String value; - - public Item(long id) { - this.id = id; - } - - public Item(long id, String value) { - this.id = id; - this.value = value; - } - - public long getId() { - return id; - } - - public String getValue() { - return value; - } - - public void setId(long id) { - this.id = id; - } - - public void setValue(String value) { - this.value = value; - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (o == null || getClass() != o.getClass()) - return false; - Item item = (Item) o; - return id == item.id && Objects.equals(value, item.value); - } - - @Override - public int hashCode() { - return Objects.hash(id, value); - } - - @Override - public String toString() { - return String.valueOf(value); - } - } } diff --git a/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/dataview/Item.java b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/dataview/Item.java new file mode 100644 index 00000000000..983e7b73c7c --- /dev/null +++ b/vaadin-combo-box-flow-parent/vaadin-combo-box-flow/src/test/java/com/vaadin/flow/component/combobox/dataview/Item.java @@ -0,0 +1,69 @@ +/* + * Copyright 2000-2024 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.flow.component.combobox.dataview; + +import java.util.Objects; + +class Item { + private long id; + private String value; + + public Item(long id) { + this.id = id; + } + + public Item(long id, String value) { + this.id = id; + this.value = value; + } + + public long getId() { + return id; + } + + public String getValue() { + return value; + } + + public void setId(long id) { + this.id = id; + } + + public void setValue(String value) { + this.value = value; + } + + // equals and hashCode should use both id and value for a valid test setup + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) + return false; + Item item = (Item) o; + return id == item.id && Objects.equals(value, item.value); + } + + @Override + public int hashCode() { + return Objects.hash(id, value); + } + + @Override + public String toString() { + return String.valueOf(value); + } +}