| | |
| | | |
| | | import lombok.Getter; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Collection; |
| | | import java.util.Collections; |
| | | import java.util.Comparator; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Optional; |
| | | |
| | | /** |
| | | * 路线策略(与高德 strategy 参数对应,见 xp_route_plan 注释) |
| | | */ |
| | |
| | | /** 时间优先 → 速度最快 */ |
| | | TIME(38, "时间优先", 2), |
| | | /** 低价优先 → 少收费 */ |
| | | PRICE(36, "低价优先", 3); |
| | | PRICE(36, "低价优先", 3), |
| | | /** 躲避拥堵 */ |
| | | AVOID_CONGESTION(33, "躲避拥堵", 4), |
| | | /** 高速优先 */ |
| | | HIGHWAY_PRIORITY(34, "高速优先", 5), |
| | | /** 不走高速 */ |
| | | NO_HIGHWAY(35, "不走高速", 6), |
| | | /** 大路优先 */ |
| | | MAIN_ROAD(37, "大路优先", 7), |
| | | /** 躲避拥堵 + 高速优先 */ |
| | | AVOID_CONGESTION_HIGHWAY_PRIORITY(39, "躲避拥堵+高速优先", 8), |
| | | /** 躲避拥堵 + 不走高速 */ |
| | | AVOID_CONGESTION_NO_HIGHWAY(40, "躲避拥堵+不走高速", 9), |
| | | /** 躲避拥堵 + 少收费 */ |
| | | AVOID_CONGESTION_PRICE(41, "躲避拥堵+低价优先", 10), |
| | | /** 少收费 + 不走高速 */ |
| | | PRICE_NO_HIGHWAY(42, "低价优先+不走高速", 11), |
| | | /** 躲避拥堵 + 少收费 + 不走高速 */ |
| | | AVOID_CONGESTION_PRICE_NO_HIGHWAY(43, "躲避拥堵+低价优先+不走高速", 12), |
| | | /** 躲避拥堵 + 大路优先 */ |
| | | AVOID_CONGESTION_MAIN_ROAD(44, "躲避拥堵+大路优先", 13), |
| | | /** 躲避拥堵 + 速度最快 */ |
| | | AVOID_CONGESTION_TIME(45, "躲避拥堵+时间优先", 14), |
| | | /** 速度优先,只返回一条路线 */ |
| | | SPEED_PRIORITY(0, "速度优先", 15), |
| | | /** 费用优先,只返回一条路线 */ |
| | | FEE_PRIORITY(1, "费用优先", 16), |
| | | /** 常规最快,只返回一条路线 */ |
| | | REGULAR_FASTEST(2, "常规最快", 17); |
| | | |
| | | private static final Comparator<RouteStrategyKind> BY_PRIORITY = Comparator.comparingInt(RouteStrategyKind::getPriority); |
| | | private static final Map<Integer, RouteStrategyKind> AMAP_STRATEGY_INDEX; |
| | | private static final List<RouteStrategyKind> ORDERED_VALUES; |
| | | private static final List<RouteStrategyKind> DEFAULT_PREFERENCE_KINDS; |
| | | |
| | | static { |
| | | Map<Integer, RouteStrategyKind> strategyIndex = new LinkedHashMap<>(); |
| | | for (RouteStrategyKind kind : values()) { |
| | | strategyIndex.put(kind.amapStrategy, kind); |
| | | } |
| | | AMAP_STRATEGY_INDEX = Collections.unmodifiableMap(strategyIndex); |
| | | |
| | | List<RouteStrategyKind> orderedValues = new ArrayList<>(Arrays.asList(values())); |
| | | orderedValues.sort(BY_PRIORITY); |
| | | ORDERED_VALUES = Collections.unmodifiableList(orderedValues); |
| | | |
| | | DEFAULT_PREFERENCE_KINDS = Collections.unmodifiableList(Arrays.asList(SMART, TIME, PRICE)); |
| | | } |
| | | |
| | | private final int amapStrategy; |
| | | private final String displayName; |
| | |
| | | } |
| | | |
| | | /** |
| | | * 主标签优先级:数值越小越优先(智能 > 时间 > 低价) |
| | | * 主标签优先级:数值越小越优先(默认保留智能 > 时间 > 低价,再补充其余全量策略) |
| | | */ |
| | | public static RouteStrategyKind primaryOf(java.util.Collection<RouteStrategyKind> kinds) { |
| | | public static RouteStrategyKind primaryOf(Collection<RouteStrategyKind> kinds) { |
| | | if (kinds == null || kinds.isEmpty()) { |
| | | return SMART; |
| | | } |
| | | return kinds.stream().min((a, b) -> Integer.compare(a.priority, b.priority)).orElse(SMART); |
| | | return kinds.stream().min(BY_PRIORITY).orElse(SMART); |
| | | } |
| | | |
| | | public static java.util.Optional<RouteStrategyKind> fromAmapStrategy(int strategy) { |
| | | for (RouteStrategyKind k : values()) { |
| | | if (k.amapStrategy == strategy) { |
| | | return java.util.Optional.of(k); |
| | | } |
| | | public static Optional<RouteStrategyKind> fromAmapStrategy(int strategy) { |
| | | return Optional.ofNullable(AMAP_STRATEGY_INDEX.get(strategy)); |
| | | } |
| | | |
| | | public static List<RouteStrategyKind> orderedValues() { |
| | | return ORDERED_VALUES; |
| | | } |
| | | |
| | | public static List<RouteStrategyKind> defaultPreferenceKinds() { |
| | | return DEFAULT_PREFERENCE_KINDS; |
| | | } |
| | | |
| | | public static List<RouteStrategyKind> orderedOf(Collection<RouteStrategyKind> kinds) { |
| | | if (kinds == null || kinds.isEmpty()) { |
| | | return Collections.emptyList(); |
| | | } |
| | | return java.util.Optional.empty(); |
| | | List<RouteStrategyKind> ordered = new ArrayList<>(kinds); |
| | | ordered.sort(BY_PRIORITY); |
| | | return ordered; |
| | | } |
| | | } |