com.facebook.presto.jdbc.internal.spi.plan.Assignments Maven / Gradle / Ivy
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 com.facebook.presto.jdbc.internal.spi.plan;
import com.facebook.presto.jdbc.internal.spi.relation.RowExpression;
import com.facebook.presto.jdbc.internal.spi.relation.VariableReferenceExpression;
import com.facebook.presto.jdbc.internal.jackson.annotation.JsonCreator;
import com.facebook.presto.jdbc.internal.jackson.annotation.JsonProperty;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.Collector;
import static java.lang.String.format;
import static java.util.Collections.unmodifiableList;
import static java.util.Collections.unmodifiableMap;
import static java.util.Objects.requireNonNull;
public class Assignments
{
private final Map assignments;
private final List outputs;
@JsonCreator
public Assignments(@JsonProperty("assignments") Map assignments)
{
this.assignments = unmodifiableMap(new LinkedHashMap<>(requireNonNull(assignments, "assignments is null")));
this.outputs = unmodifiableList(new ArrayList<>(assignments.keySet()));
}
public static Builder builder()
{
return new Builder();
}
public static Builder builder(Map assignments)
{
return new Builder().putAll(assignments);
}
public static Assignments copyOf(Map assignments)
{
return builder()
.putAll(assignments)
.build();
}
public static Assignments of()
{
return builder().build();
}
public static Assignments of(VariableReferenceExpression variable, RowExpression expression)
{
return builder().put(variable, expression).build();
}
public static Assignments of(VariableReferenceExpression variable1, RowExpression expression1, VariableReferenceExpression variable2, RowExpression expression2)
{
return builder().put(variable1, expression1).put(variable2, expression2).build();
}
public List getOutputs()
{
return outputs;
}
@JsonProperty("assignments")
public Map getMap()
{
return assignments;
}
public Assignments filter(Collection variables)
{
return filter(variables::contains);
}
public Assignments filter(Predicate predicate)
{
return assignments.entrySet().stream()
.filter(entry -> predicate.test(entry.getKey()))
.collect(toAssignments());
}
private Collector, Builder, Assignments> toAssignments()
{
return Collector.of(
Assignments::builder,
(builder, entry) -> builder.put(entry.getKey(), entry.getValue()),
(left, right) -> {
left.putAll(right.build());
return left;
},
Builder::build);
}
public Collection getExpressions()
{
return assignments.values();
}
public Set getVariables()
{
return assignments.keySet();
}
public Set> entrySet()
{
return assignments.entrySet();
}
public RowExpression get(VariableReferenceExpression variable)
{
return assignments.get(variable);
}
public int size()
{
return assignments.size();
}
public boolean isEmpty()
{
return size() == 0;
}
public void forEach(BiConsumer consumer)
{
assignments.forEach(consumer);
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Assignments that = (Assignments) o;
return assignments.equals(that.assignments);
}
@Override
public int hashCode()
{
return assignments.hashCode();
}
public static class Builder
{
private final Map assignments = new LinkedHashMap<>();
public Builder putAll(Assignments assignments)
{
return putAll(assignments.getMap());
}
public Builder putAll(Map assignments)
{
for (Entry assignment : assignments.entrySet()) {
put(assignment.getKey(), assignment.getValue());
}
return this;
}
public Builder put(VariableReferenceExpression variable, RowExpression expression)
{
if (assignments.containsKey(variable)) {
RowExpression assignment = assignments.get(variable);
if (!assignment.equals(expression)) {
throw new IllegalStateException(format("Variable %s already has assignment %s, while adding %s", variable, assignment, expression));
}
}
assignments.put(variable, expression);
return this;
}
public Builder put(Entry assignment)
{
put(assignment.getKey(), assignment.getValue());
return this;
}
public Assignments build()
{
return new Assignments(assignments);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy