com.facebook.presto.hive.metastore.util.Memoizers Maven / Gradle / Ivy
/*
* 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.facebook.presto.hive.metastore.util;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.UnaryOperator;
import static java.util.Objects.requireNonNull;
public final class Memoizers
{
private Memoizers() {}
public static UnaryOperator memoizeLast()
{
return new Simple<>();
}
public static Function memoizeLast(Function transform)
{
return new Transforming<>(transform);
}
private static final class Simple
implements UnaryOperator
{
private T lastInput;
@Override
public T apply(T input)
{
if (!Objects.equals(lastInput, input)) {
lastInput = input;
}
return lastInput;
}
}
private static final class Transforming
implements Function
{
private final Function transform;
private boolean inputSeen;
private I lastInput;
private O lastOutput;
private Transforming(Function transform)
{
this.transform = requireNonNull(transform, "transform is null");
}
@Override
public O apply(I input)
{
if (!inputSeen || !Objects.equals(lastInput, input)) {
lastOutput = transform.apply(input);
lastInput = input;
inputSeen = true;
}
return lastOutput;
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy