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

com.gitee.qdbp.jdbc.model.SqlFile Maven / Gradle / Ivy

package com.gitee.qdbp.jdbc.model;

import java.io.Serializable;
import java.net.URL;
import java.util.Comparator;

/**
 * SQL文件
 *
 * @author zhaohuihua
 * @version 20210206
 */
public class SqlFile implements Serializable, Comparable {

    /** serialVersionUID **/
    private static final long serialVersionUID = 1L;
    /** URL路径 **/
    private URL urlPath;
    /** 绝对路径 **/
    private String absolutePath;
    /** 相对路径 **/
    private String relativePath;
    /** 更新时间 **/
    private Long lastUpdateTime;

    /** 绝对路径 **/
    public URL getUrlPath() {
        return urlPath;
    }

    /** URL路径 **/
    public void setUrlPath(URL urlPath) {
        this.urlPath = urlPath;
    }

    /** 绝对路径 **/
    public String getAbsolutePath() {
        return absolutePath;
    }

    /** 绝对路径 **/
    public void setAbsolutePath(String absolutePath) {
        this.absolutePath = absolutePath;
    }

    /** 相对路径 **/
    public String getRelativePath() {
        return relativePath;
    }

    /** 相对路径 **/
    public void setRelativePath(String relativePath) {
        this.relativePath = relativePath;
    }

    /** 最后更新时间 **/
    public Long getLastUpdateTime() {
        return lastUpdateTime;
    }

    /** 最后更新时间 **/
    public void setLastUpdateTime(Long updateTime) {
        this.lastUpdateTime = updateTime;
    }

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

    /** 排序: file优先于jar, 其余按更新时间降序 **/
    @Override
    public int compareTo(SqlFile other) {
        return DEFAULT_COMPARATOR.compare(this, other);
    }

    private static final Comparator DEFAULT_COMPARATOR = new DefaultComparator();

    public static class DefaultComparator implements Comparator {

        protected String getUrlProtocol(SqlFile sqlFile) {
            return sqlFile.getUrlPath().getProtocol();
        }

        @Override
        public int compare(SqlFile curr, SqlFile that) {
            if (curr == that) {
                return 0;
            }
            if (that == null) {
                return -1;
            }

            // 相对路径升序
            int relativePathValue;
            if (that.getRelativePath() == null && curr.getRelativePath() == null) {
                relativePathValue = 0;
            } else if (curr.getRelativePath() == null) {
                relativePathValue = 1;
            } else if (that.getRelativePath() == null) {
                relativePathValue = -1;
            } else {
                relativePathValue = curr.getRelativePath().compareTo(that.getRelativePath());
            }
            if (relativePathValue != 0) {
                return relativePathValue;
            }

            // file优先于jar
            int absolutePathValue;
            if (that.getUrlPath() == null && curr.getUrlPath() == null) {
                absolutePathValue = 0;
            } else if (that.getUrlPath() == null) {
                absolutePathValue = -1;
            } else if (curr.getUrlPath() == null) {
                absolutePathValue = 1;
            } else {
                String currProtocol = getUrlProtocol(curr);
                String thatProtocol = getUrlProtocol(that);
                if (currProtocol.equals(thatProtocol)) {
                    absolutePathValue = 0;
                } else if (currProtocol.contains("jar")) {
                    absolutePathValue = 1; // 下降
                } else if (thatProtocol.contains("jar")) {
                    absolutePathValue = -1; // 上升
                } else {
                    absolutePathValue = 0;
                }
            }
            if (absolutePathValue != 0) {
                return absolutePathValue;
            }

            // 更新时间降序
            int updateTimeValue;
            if (that.getLastUpdateTime() == null && curr.getLastUpdateTime() == null) {
                updateTimeValue = 0;
            } else if (curr.getLastUpdateTime() == null) {
                updateTimeValue = 1;
            } else if (that.getLastUpdateTime() == null) {
                updateTimeValue = -1;
            } else {
                // 降序: 对方比本方
                updateTimeValue = that.getLastUpdateTime().compareTo(curr.getLastUpdateTime());
            }
            return updateTimeValue;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy