Promise<Object> (@NotNull XValue value, @NotNull ValueMarkup markup) { synchronized (myMarkers) { // remove the existing label if any myMarkers.entrySet().stream() .filter(entry -> markup.getText().equals(entry.getValue().getText())) .findFirst() .ifPresent(entry -> myMarkers.remove(entry.getKey())); } //noinspection unchecked Promise<M> promise = myProvider.markValueAsync((V)value); return promise.then(m -> { myMarkers.put(m, markup); return null; }); }