class LiveDataUtils {

    public static <In, Out> LiveData<Out> dedupedMappedLiveDataFor(
            @NonNull LiveData<In> inputLiveData,
            @NonNull final Function<In, Out> mappingMethod) {
        final MediatorLiveData<Out> outputLiveData = new MediatorLiveData<>();
        outputLiveData.addSource(inputLiveData, new Observer<In>() {
            @Override
            public void onChanged(@Nullable final In input) {
                WorkManagerTaskExecutor.getInstance().executeOnBackgroundThread(new Runnable() {
                    @Override
                    public void run() {
                        synchronized (outputLiveData) {
                            Out newOutput = mappingMethod.apply(input);
                            Out previousOutput = outputLiveData.getValue();
                            if (previousOutput == null && newOutput != null) {
                                outputLiveData.postValue(newOutput);
                            } else if (
                                    previousOutput != null && !previousOutput.equals(newOutput)) {
                                outputLiveData.postValue(newOutput);
                            }
                        }
                    }
                });
            }
        });
        return outputLiveData;
    }

    private  LiveDataUtils();

}