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: Use incremental numeric id as a key for Tree Grid items (#3046) (CP: 14.8) #3126

Merged
merged 1 commit into from
May 4, 2022
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 @@ -18,10 +18,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -138,8 +141,12 @@ public UpdateQueueData getUpdateQueueData() {
}
}

private final ValueProvider<T, String> defaultUniqueKeyProvider = item -> getDataProvider()
.getId(item).toString();
private final AtomicLong uniqueKeyCounter = new AtomicLong(0);
private final Map<Object, Long> objectUniqueKeyMap = new HashMap<>();

ValueProvider<T, String> defaultUniqueKeyProvider = item -> String.valueOf(
objectUniqueKeyMap.computeIfAbsent(getDataProvider().getId(item),
key -> uniqueKeyCounter.getAndIncrement()));

private Registration dataProviderRegistration;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2000-2022 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.grid;

import org.junit.Assert;
import org.junit.Test;

import com.vaadin.flow.component.treegrid.TreeGrid;
import com.vaadin.flow.function.ValueProvider;

public class TreeGridTest {

@Test
public void defaultUniqueKeyProvider_usesIncrementalLongId() {
Item item1 = new Item("sensitive data 1");
Item item2 = new Item("sensitive data 2");

UniqueKeyTreeGrid grid = new UniqueKeyTreeGrid();
String key1 = grid.getUniqueKeyProvider().apply(item1);
Assert.assertEquals("0", key1);

String key2 = grid.getUniqueKeyProvider().apply(item2);
Assert.assertEquals("1", key2);

key1 = grid.getUniqueKeyProvider().apply(item1);
Assert.assertEquals("0", key1);
}

private static class UniqueKeyTreeGrid extends TreeGrid<Item> {
@Override
public ValueProvider<Item, String> getUniqueKeyProvider() {
return super.getUniqueKeyProvider();
}
}

private static class Item {
private final String sensitiveData;

public Item(String sensitiveData) {
this.sensitiveData = sensitiveData;
}

public String getSensitiveData() {
return sensitiveData;
}
}

}