提交 b28eaa1a 编写于 作者: S Skylot

fix(gui): add synchronization to SimpleIndex class (#435)

上级 be509c71
......@@ -176,6 +176,7 @@ public class SearchDialog extends CommonSearchDialog {
.subscribeOn(Schedulers.single())
.doOnNext(r -> LOG.debug("search event: {}", r))
.switchMap(text -> prepareSearch(text)
.doOnError(e -> LOG.error("Error prepare search: {}", e.getMessage(), e))
.subscribeOn(Schedulers.single())
.toList()
.toFlowable(), 1)
......
......@@ -12,10 +12,14 @@ public class SimpleIndex<T> implements SearchIndex<T> {
private final List<String> keys = new ArrayList<>();
private final List<T> values = new ArrayList<>();
private final Object syncData = new Object();
@Override
public void put(String str, T value) {
keys.add(str);
values.add(value);
synchronized (syncData) {
keys.add(str);
values.add(value);
}
}
@Override
......@@ -39,13 +43,15 @@ public class SimpleIndex<T> implements SearchIndex<T> {
@Override
public Flowable<T> search(final String searchStr, final boolean caseInsensitive) {
return Flowable.create(emitter -> {
int size = size();
for (int i = 0; i < size; i++) {
if (isMatched(keys.get(i), searchStr, caseInsensitive)) {
emitter.onNext(values.get(i));
}
if (emitter.isCancelled()) {
return;
synchronized (syncData) {
int size = keys.size();
for (int i = 0; i < size; i++) {
if (isMatched(keys.get(i), searchStr, caseInsensitive)) {
emitter.onNext(values.get(i));
}
if (emitter.isCancelled()) {
return;
}
}
}
emitter.onComplete();
......@@ -54,6 +60,8 @@ public class SimpleIndex<T> implements SearchIndex<T> {
@Override
public int size() {
return keys.size();
synchronized (syncData) {
return keys.size();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册