All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.palantir.atlasdb.coordination.TransformingCoordinationService Maven / Gradle / Ivy

There is a newer version: 0.1152.0
Show newest version
/*
 * (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
 *
 * 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.palantir.atlasdb.coordination;

import java.util.Optional;
import java.util.function.Function;

/**
 * A {@link TransformingCoordinationService} is a {@link CoordinationService} for T2 objects that uses an underlying
 * {@link CoordinationService} for T1 objects.
 */
public class TransformingCoordinationService implements CoordinationService {
    private final CoordinationService delegate;
    private final Function transformFromUnderlying;
    private final Function transformToUnderlying;

    public TransformingCoordinationService(
            CoordinationService delegate,
            Function transformFromUnderlying,
            Function transformToUnderlying) {
        this.delegate = delegate;
        this.transformFromUnderlying = transformFromUnderlying;
        this.transformToUnderlying = transformToUnderlying;
    }

    @Override
    public Optional> getValueForTimestamp(long timestamp) {
        return delegate.getValueForTimestamp(timestamp).map(preservingBounds(transformFromUnderlying));
    }

    @Override
    public TransformResult> tryTransformCurrentValue(Function, T2> valueUpdater) {
        TransformResult> delegateResult = delegate.tryTransformCurrentValue(
                preservingBounds(transformFromUnderlying).andThen(valueUpdater).andThen(transformToUnderlying));
        return TransformResult.of(
                delegateResult.successful(),
                preservingBounds(transformFromUnderlying).apply(delegateResult.value()));
    }

    @Override
    public Optional> getLastKnownLocalValue() {
        return delegate.getLastKnownLocalValue().map(preservingBounds(transformFromUnderlying));
    }

    private static  Function, ValueAndBound> preservingBounds(Function base) {
        return fromValueAndBound -> ValueAndBound.of(fromValueAndBound.value().map(base), fromValueAndBound.bound());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy