2019-01-14 23:40:38 +00:00
|
|
|
package org.thoughtcrime.securesms.util;
|
|
|
|
|
2020-08-19 00:06:26 +00:00
|
|
|
import androidx.annotation.MainThread;
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2019-01-14 23:40:38 +00:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Useful for generate ID's to be used with
|
2020-08-19 00:06:26 +00:00
|
|
|
* {@link RecyclerView.Adapter#getItemId(int)} when you otherwise don't
|
2019-01-14 23:40:38 +00:00
|
|
|
* have a good way to generate an ID.
|
|
|
|
*/
|
|
|
|
public class StableIdGenerator<E> {
|
|
|
|
|
|
|
|
private final Map<E, Long> keys = new HashMap<>();
|
|
|
|
|
|
|
|
private long index = 1;
|
|
|
|
|
|
|
|
@MainThread
|
|
|
|
public long getId(@NonNull E item) {
|
|
|
|
if (keys.containsKey(item)) {
|
|
|
|
return keys.get(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
long key = index++;
|
|
|
|
keys.put(item, key);
|
|
|
|
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|