org.optaplanner.constraint.streams.common.quad.DefaultQuadJoiner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of optaplanner-constraint-streams-common Show documentation
Show all versions of optaplanner-constraint-streams-common Show documentation
OptaPlanner solves planning problems.
This lightweight, embeddable planning engine implements powerful and scalable algorithms
to optimize business resource scheduling and planning.
This module contains classes common to each implementation of Constraint streams.
The newest version!
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.optaplanner.constraint.streams.common.quad;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import org.optaplanner.constraint.streams.common.AbstractJoiner;
import org.optaplanner.core.api.function.TriFunction;
import org.optaplanner.core.api.score.stream.quad.QuadJoiner;
import org.optaplanner.core.impl.score.stream.JoinerType;
public final class DefaultQuadJoiner extends AbstractJoiner implements QuadJoiner {
private static final DefaultQuadJoiner NONE =
new DefaultQuadJoiner(new TriFunction[0], new JoinerType[0], new Function[0]);
private final TriFunction[] leftMappings;
public DefaultQuadJoiner(TriFunction leftMapping, JoinerType joinerType,
Function rightMapping) {
super(rightMapping, joinerType);
this.leftMappings = new TriFunction[] { leftMapping };
}
private DefaultQuadJoiner(TriFunction[] leftMappings, JoinerType[] joinerTypes,
Function[] rightMappings) {
super(rightMappings, joinerTypes);
this.leftMappings = leftMappings;
}
public static DefaultQuadJoiner merge(List> joinerList) {
if (joinerList.size() == 1) {
return joinerList.get(0);
}
return joinerList.stream().reduce(NONE, DefaultQuadJoiner::and);
}
@Override
public DefaultQuadJoiner and(QuadJoiner otherJoiner) {
DefaultQuadJoiner castJoiner = (DefaultQuadJoiner) otherJoiner;
int joinerCount = getJoinerCount();
int castJoinerCount = castJoiner.getJoinerCount();
int newJoinerCount = joinerCount + castJoinerCount;
JoinerType[] newJoinerTypes = Arrays.copyOf(this.joinerTypes, newJoinerCount);
TriFunction[] newLeftMappings = Arrays.copyOf(this.leftMappings, newJoinerCount);
Function[] newRightMappings = Arrays.copyOf(this.rightMappings, newJoinerCount);
for (int i = 0; i < castJoiner.getJoinerCount(); i++) {
int newJoinerIndex = i + joinerCount;
newJoinerTypes[newJoinerIndex] = castJoiner.getJoinerType(i);
newLeftMappings[newJoinerIndex] = castJoiner.getLeftMapping(i);
newRightMappings[newJoinerIndex] = castJoiner.getRightMapping(i);
}
return new DefaultQuadJoiner<>(newLeftMappings, newJoinerTypes, newRightMappings);
}
public TriFunction getLeftMapping(int index) {
return (TriFunction) leftMappings[index];
}
public boolean matches(A a, B b, C c, D d) {
int joinerCount = getJoinerCount();
for (int i = 0; i < joinerCount; i++) {
JoinerType joinerType = getJoinerType(i);
Object leftMapping = getLeftMapping(i).apply(a, b, c);
Object rightMapping = getRightMapping(i).apply(d);
if (!joinerType.matches(leftMapping, rightMapping)) {
return false;
}
}
return true;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof DefaultQuadJoiner)) {
return false;
}
DefaultQuadJoiner, ?, ?, ?> other = (DefaultQuadJoiner, ?, ?, ?>) o;
return Arrays.equals(joinerTypes, other.joinerTypes)
&& Arrays.equals(leftMappings, other.leftMappings)
&& Arrays.equals(rightMappings, other.rightMappings);
}
@Override
public int hashCode() {
return Objects.hash(Arrays.hashCode(joinerTypes), Arrays.hashCode(leftMappings), Arrays.hashCode(rightMappings));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy