io.trino.plugin.hive.metastore.util.Memoizers Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of trino-hive Show documentation
Show all versions of trino-hive Show documentation
This is a Databricks build of Trino's Hive plugin which includes support for HTTP based transport
for it's Hive metastore thrift interface.
The newest version!
/*
* 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 io.trino.plugin.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 I lastInput;
private O lastOutput;
private boolean inputSeen;
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;
}
}
}