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

com.querydsl.collections.PathComparator Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2015, The Querydsl Team (http://www.querydsl.com/team)
 *
 * 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.querydsl.collections;

import com.querydsl.core.types.Path;

import java.util.Comparator;
import java.util.function.Function;

/**
 * Compares two beans based on the values at a specific path.
 *
 * @author Jeroen van Schagen
 * @author tiwe
 *
 * @param  type of the path root
 * @param  type of value being matched
 */
public class PathComparator> implements Comparator {

    private final Function accessor;

    public PathComparator(Path comparingPath) {
        this(comparingPath, FunctionalHelpers.wrap(comparingPath));
    }

    public PathComparator(Path comparingPath, Function accessor) {
        this.accessor = accessor;
    }

    public static > PathComparator pathComparator(Path comparingPath) {
        return new PathComparator(comparingPath);
    }

    @Override
    public int compare(T leftBean, T rightBean) {
        if (leftBean == rightBean) {
            return 0; // Reference to the same object should always result in '0'
        } else if (leftBean == null) {
            return -1; // Whenever the reference varies and left is null, right is not null
        } else if (rightBean == null) {
            return 1; // Whenever the reference varies and right is null, left is not null
        } else if (leftBean.equals(rightBean)) {
            return 0; // Equal beans should always result in '0'
        }
        return comparePathValues(leftBean, rightBean);
    }

    private int comparePathValues(T leftBean, T rightBean) {
        V left = accessor.apply(leftBean);
        V right = accessor.apply(rightBean);
        if (left == null) {
            return -1;
        } else if (right == null) {
            return 1;
        } else {
            return left.compareTo(right);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy