Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: also pass identifier provider to key mapper for lazy data views (#6688) (CP: 24.4) #6689

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -73,6 +74,14 @@ public void setItemCountCallback(
getDataCommunicator().setCountCallback(callback);
}

@Override
public void setIdentifierProvider(
IdentifierProvider<T> identifierProvider) {
super.setIdentifierProvider(identifierProvider);
getDataCommunicator().getKeyMapper()
.setIdentifierGetter(identifierProvider);
}

/**
* {@inheritDoc}
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<Item> items = new ArrayList<>(Arrays.asList(first, second));

ComboBox<Item> component = new ComboBox<>();

DataCommunicator<Item> dataCommunicator = new DataCommunicator<>(
(item, jsonObject) -> {
}, null, null, component.getElement().getNode());
dataCommunicator.setDataProvider(
new CallbackDataProvider<>(query -> Stream.of(), query -> 0),
"", true);

ComboBoxLazyDataView<Item> dataView = new ComboBoxLazyDataView<>(
dataCommunicator, component);
DataKeyMapper<Item> 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")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -153,53 +152,4 @@ protected HasListDataView<String, ComboBoxListDataView<String>> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}