*
L
昨天 daa2cec25875276f3462e09d102f9d2fd52a96e1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.zy.asrs.utils;
 
/**
 * 距离优先级工具:
 * 1) 距离 <= 阈值: 优先级 [0,1], 越近越高
 * 2) 距离 > 阈值: 优先级 [0,-1], 越远越低
 */
public final class DistancePriorityUtil {
 
    private DistancePriorityUtil() {
    }
 
    public static double calculate(long distance, long threshold, long perimeter) {
        if (threshold <= 0L || perimeter <= 0L) {
            return 0D;
        }
        long d = Math.max(0L, distance);
        if (d <= threshold) {
            double score = 1D - ((double) d / (double) threshold);
            return clamp(score, 0D, 1D);
        }
        long outside = d - threshold;
        long outsideMax = Math.max(1L, perimeter - threshold);
        double score = -((double) outside / (double) outsideMax);
        return clamp(score, -1D, 0D);
    }
 
    public static boolean isInRange(long distance, long threshold) {
        return threshold > 0L && distance <= threshold;
    }
 
    private static double clamp(double value, double min, double max) {
        if (value < min) {
            return min;
        }
        if (value > max) {
            return max;
        }
        return value;
    }
}