package com.zc.business.enums; import lombok.Getter; /** * 通道拥挤度等级枚举 * @author xiepufeng */ @Getter public enum ChannelCongestionLevelEnum { /** * 表示通道畅通,速度阈值为70 km/h */ FLOWING(70, 0, 1,"畅通"), /** * 表示通道基本畅通,速度阈值为50 km/h */ BASIC_FLOWING(50, 0, 2, "基本畅通"), /** * 表示通道轻度拥堵,速度阈值为40 km/h */ LIGHT_CONGESTION(40, 1000, 3, "轻度拥堵"), /** * 表示通道中度拥堵,速度阈值为20 km/h */ MEDIUM_CONGESTION(20, 2000, 4, "中度拥堵"), /** * 使用负数作为默认值,表示无限小,始终小于其他速度阈值,表示通道严重拥堵 */ SEVERE_CONGESTION(-1, 4000, 5, "严重拥堵"); /** * 速度阈值,用于判断通道拥挤程度 */ private final int speedThreshold; /** * 默认的拥堵里程数 */ private final int defaultCongestionDistance; /** * 等级 */ private final int level; /** * 对拥挤度等级的描述 */ private final String description; /** * 构造函数,初始化通道拥挤度等级。 * * @param speedThreshold 速度阈值 * @param description 等级描述 */ ChannelCongestionLevelEnum(int speedThreshold, int defaultCongestionDistance, int level, String description) { this.speedThreshold = speedThreshold; this.defaultCongestionDistance = defaultCongestionDistance; this.level = level; this.description = description; } /** * 根据给定速度,返回对应的通道拥挤度等级。 * * @param speed 速度(单位:km/h) * @return 对应的通道拥挤度等级 */ public static ChannelCongestionLevelEnum fromSpeed(int speed) { for (ChannelCongestionLevelEnum level : values()) { if (speed >= level.speedThreshold) { return level; } } return SEVERE_CONGESTION; // 若速度小于等于所有等级阈值,则视为严重拥堵 } /** * 根据给定速度,返回对应的通道拥挤度等级。 * * @param speed 速度(单位:km/h) * @param trafficVolume 车流量 * @return 对应的通道拥挤度等级 */ public static ChannelCongestionLevelEnum fromSpeed(int speed, int trafficVolume) { if (trafficVolume == 0) { return FLOWING; } for (ChannelCongestionLevelEnum level : values()) { if (speed >= level.speedThreshold) { return level; } } return SEVERE_CONGESTION; // 若速度小于等于所有等级阈值,则视为严重拥堵 } /** * 判断速度是否是是中度拥堵或者严重拥堵 */ public static boolean isMediumOrSevereCongestion(int speed) { ChannelCongestionLevelEnum level = fromSpeed(speed); return level == MEDIUM_CONGESTION || level == SEVERE_CONGESTION; } /** * 判断当前交通状况是否为中度或严重拥堵。 * * @param speed 当前道路的车速,单位为任意测量单位,取决于具体应用。 * @param trafficVolume 当前道路的交通流量,单位为任意测量单位,取决于具体应用。 * @return boolean 返回 true 如果当前交通状况被判断为中度或严重拥堵,否则返回 false。 */ public static boolean isMediumOrSevereCongestion(int speed, int trafficVolume) { // 当交通流量为0时,不考虑拥堵问题,直接返回false if (trafficVolume == 0) { return false; } // 根据车速判断拥堵级别 ChannelCongestionLevelEnum level = fromSpeed(speed); // 判断拥堵级别是否为中度或严重拥堵 return level == MEDIUM_CONGESTION || level == SEVERE_CONGESTION; } /** * 判断给定速度是否属于拥堵状态。 * * @param speed 速度(单位:km/h) * @param trafficVolume 车流量 * @return 如果速度小于等于基本畅通的阈值,则返回true;否则返回false。 */ public static boolean isCongestion(int speed, int trafficVolume) { if (trafficVolume == 0) { return false; } return speed < BASIC_FLOWING.getSpeedThreshold(); } /** * 判断给定速度是否属于拥堵状态。 * * @param speed 速度(单位:km/h) * @return 如果速度小于等于基本畅通的阈值,则返回true;否则返回false。 */ public static boolean isCongestion(int speed) { return speed < BASIC_FLOWING.getSpeedThreshold(); } /** * 判断给定速度是否属于指定的拥挤度等级。 * * @param speed 速度(单位:km/h) * @param level 拥挤度等级 * @return 如果速度属于该等级,返回true;否则返回false。 */ public static boolean isWithinLevel(int speed, ChannelCongestionLevelEnum level) { if (level == null) return false; ChannelCongestionLevelEnum currentLevel = fromSpeed(speed); return currentLevel == level; } /** * 根据描述获取频道拥堵级别枚举值。 * 该方法遍历所有ChannelCongestionLevelEnum的实例,通过匹配描述来寻找对应的枚举值。 * 如果找不到匹配的枚举值,则抛出IllegalArgumentException。 * * @param description 频道拥堵级别的描述字符串。 * @return 匹配描述的频道拥堵级别枚举值。 * @throws IllegalArgumentException 如果描述不匹配任何枚举值,则抛出此异常。 */ public static ChannelCongestionLevelEnum fromDescription(String description) { for (ChannelCongestionLevelEnum value : values()) { if (value.getDescription().equals(description)) { return value; } } throw new IllegalArgumentException("无效的指数值: " + description); } }