All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.optaplanner.examples.common.experimental.impl.IntervalSplitPoint Maven / Gradle / Ivy

Go to download

OptaPlanner solves planning problems. This lightweight, embeddable planning engine implements powerful and scalable algorithms to optimize business resource scheduling and planning. This module contains the examples which demonstrate how to use it in a normal Java application.

There is a newer version: 9.44.0.Final
Show newest version
/*
 * Copyright 2021 Red Hat, Inc. and/or its affiliates.
 *
 * 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.optaplanner.examples.common.experimental.impl;

import java.util.Comparator;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.TreeSet;
import java.util.stream.IntStream;

public class IntervalSplitPoint>
        implements Comparable> {
    final Point_ splitPoint;
    Map startIntervalToCountMap;
    Map endIntervalToCountMap;
    TreeSet> intervalsStartingAtSplitPointSet;
    TreeSet> intervalsEndingAtSplitPointSet;

    public IntervalSplitPoint(Point_ splitPoint) {
        this.splitPoint = splitPoint;
    }

    protected void createCollections() {
        startIntervalToCountMap = new IdentityHashMap<>();
        endIntervalToCountMap = new IdentityHashMap<>();
        intervalsStartingAtSplitPointSet = new TreeSet<>(
                Comparator., Point_> comparing(Interval::getEnd)
                        .thenComparingInt(interval -> System.identityHashCode(interval.getValue())));
        intervalsEndingAtSplitPointSet = new TreeSet<>(
                Comparator., Point_> comparing(Interval::getStart)
                        .thenComparingInt(interval -> System.identityHashCode(interval.getValue())));
    }

    public boolean addIntervalStartingAtSplitPoint(Interval interval) {
        startIntervalToCountMap.merge(interval.getValue(), 1, Integer::sum);
        return intervalsStartingAtSplitPointSet.add(interval);
    }

    public void removeIntervalStartingAtSplitPoint(Interval interval) {
        Integer newCount = startIntervalToCountMap.computeIfPresent(interval.getValue(), (key, count) -> {
            if (count > 1) {
                return count - 1;
            }
            return null;
        });
        if (null == newCount) {
            intervalsStartingAtSplitPointSet.remove(interval);
        }
    }

    public boolean addIntervalEndingAtSplitPoint(Interval interval) {
        endIntervalToCountMap.merge(interval.getValue(), 1, Integer::sum);
        return intervalsEndingAtSplitPointSet.add(interval);
    }

    public void removeIntervalEndingAtSplitPoint(Interval interval) {
        Integer newCount = endIntervalToCountMap.computeIfPresent(interval.getValue(), (key, count) -> {
            if (count > 1) {
                return count - 1;
            }
            return null;
        });
        if (null == newCount) {
            intervalsEndingAtSplitPointSet.remove(interval);
        }
    }

    public boolean containsIntervalStarting(Interval interval) {
        return intervalsStartingAtSplitPointSet.contains(interval);
    }

    public boolean containsIntervalEnding(Interval interval) {
        return intervalsEndingAtSplitPointSet.contains(interval);
    }

    public Iterator getValuesStartingFromSplitPointIterator() {
        return intervalsStartingAtSplitPointSet.stream()
                .flatMap(interval -> IntStream.range(0, startIntervalToCountMap.get(interval.getValue()))
                        .mapToObj(index -> interval.getValue()))
                .iterator();
    }

    public boolean isEmpty() {
        return intervalsStartingAtSplitPointSet.isEmpty() && intervalsEndingAtSplitPointSet.isEmpty();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o)
            return true;
        if (o == null || getClass() != o.getClass())
            return false;
        IntervalSplitPoint that = (IntervalSplitPoint) o;
        return splitPoint.equals(that.splitPoint);
    }

    public boolean isBefore(IntervalSplitPoint other) {
        return compareTo(other) < 0;
    }

    public boolean isAfter(IntervalSplitPoint other) {
        return compareTo(other) > 0;
    }

    @Override
    public int hashCode() {
        return Objects.hash(splitPoint);
    }

    @Override
    public int compareTo(IntervalSplitPoint other) {
        return splitPoint.compareTo(other.splitPoint);
    }

    @Override
    public String toString() {
        return splitPoint.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy