org.mybatis.dynamic.sql.where.render.RenderedCriterion Maven / Gradle / Ivy
/**
* Copyright 2016-2018 the original author or authors.
*
* 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 org.mybatis.dynamic.sql.where.render;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
import org.mybatis.dynamic.sql.util.FragmentCollector;
public class RenderedCriterion {
private Optional connector;
private FragmentAndParameters initialCondition; // may be null
private List subCriteria;
private RenderedCriterion(Builder builder) {
connector = Objects.requireNonNull(builder.connector);
initialCondition = builder.initialCondition;
subCriteria = Objects.requireNonNull(builder.subCriteria);
}
public FragmentAndParameters renderWithInitialConnector() {
FragmentAndParameters fp = renderWithoutInitialConnector();
return connector.map(fp::prependFragment).orElse(fp);
}
public FragmentAndParameters renderWithoutInitialConnector() {
FragmentCollector fc = internalRender();
String fragment = calculateFragment(fc);
return FragmentAndParameters.withFragment(fragment)
.withParameters(fc.parameters())
.build();
}
private FragmentCollector internalRender() {
if (initialCondition == null) {
return renderSubCriteriaOnly();
} else {
return renderConditionAndSubCriteria();
}
}
private FragmentCollector renderSubCriteriaOnly() {
FragmentAndParameters initial = subCriteria.get(0).renderWithoutInitialConnector();
return subCriteria.stream()
.skip(1)
.map(RenderedCriterion::renderWithInitialConnector)
.collect(FragmentCollector.collect(initial));
}
private FragmentCollector renderConditionAndSubCriteria() {
return subCriteria.stream()
.map(RenderedCriterion::renderWithInitialConnector)
.collect(FragmentCollector.collect(initialCondition));
}
private String calculateFragment(FragmentCollector collector) {
if (collector.hasMultipleFragments()) {
return collector.fragments()
.collect(Collectors.joining(" ", "(", ")")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
} else {
return collector.fragments().findFirst().orElse(""); //$NON-NLS-1$
}
}
public static class Builder {
private Optional connector;
private FragmentAndParameters initialCondition;
private List subCriteria = new ArrayList<>();
public Builder withConnector(Optional connector) {
this.connector = connector;
return this;
}
public Builder withInitialCondition(FragmentAndParameters initialCondition) {
this.initialCondition = initialCondition;
return this;
}
public Builder withSubCriteria(List subCriteria) {
this.subCriteria.addAll(subCriteria);
return this;
}
public Optional build() {
if (initialCondition == null && subCriteria.isEmpty()) {
return Optional.empty();
}
return Optional.of(new RenderedCriterion(this));
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy