diff --git a/src/main/java/club/joylink/xiannccda/ats/message/line3/DeviceNameChanger.java b/src/main/java/club/joylink/xiannccda/ats/message/line3/DeviceNameChanger.java index 22107e2..ea5e67f 100644 --- a/src/main/java/club/joylink/xiannccda/ats/message/line3/DeviceNameChanger.java +++ b/src/main/java/club/joylink/xiannccda/ats/message/line3/DeviceNameChanger.java @@ -6,23 +6,13 @@ import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.RtssGraphicStorage; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Section; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Signal; import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto.Turnout; -import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.google.protobuf.GeneratedMessageV3; -import com.google.protobuf.Message; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import org.apache.commons.lang3.StringUtils; -public abstract class DeviceNameChanger { +public abstract class DeviceNameChanger implements NameChanger { - abstract void collectNames(LayoutGraphicsProto.RtssGraphicStorage graphicStorage); - - abstract String changeDeviceName(String nccDeviceName, List graphicNames); static Map NCC_DEVICE_TYPE_NAME_MAPPER = Maps.newHashMap(); @@ -30,7 +20,6 @@ public abstract class DeviceNameChanger { NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_SWITCH, new SwitchDevice()); NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_SIGNAL, new SignalDevice()); NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_TRACK, new TrackDevice()); -// NCC_DEVICE_TYPE_NAME_MAPPER.put(DeviceType.DEVICE_TYPE_PLATFORM, "stationStandList"); } private final static Map> DEVICE_NAMES = Maps.newHashMap(); @@ -43,7 +32,7 @@ public abstract class DeviceNameChanger { } - public static String nameMatch(DeviceType dt, String name) { + public static String findMatch(DeviceType dt, String name) { DeviceNameChanger nameChanger = NCC_DEVICE_TYPE_NAME_MAPPER.get(dt); if (Objects.nonNull(nameChanger)) { List deviceNames = DEVICE_NAMES.get(dt); @@ -52,11 +41,16 @@ public abstract class DeviceNameChanger { return name; } + public static void main(String[] args) { +// X1506_L2 + new TrackDevice().changeDeviceName("TP_2DG", List.of("P_2DG")); + new SignalDevice().changeDeviceName("X1506_L2", List.of("X1506-L2")); + } public static class SwitchDevice extends DeviceNameChanger { @Override - void collectNames(RtssGraphicStorage graphicStorage) { + public void collectNames(RtssGraphicStorage graphicStorage) { List names = graphicStorage.getTurnoutsList().stream().map(Turnout::getCode).toList(); DEVICE_NAMES.put(DeviceType.DEVICE_TYPE_SWITCH, names); } @@ -78,103 +72,36 @@ public abstract class DeviceNameChanger { @Override - void collectNames(RtssGraphicStorage graphicStorage) { + public void collectNames(RtssGraphicStorage graphicStorage) { List names = graphicStorage.getSectionList().stream().map(Section::getCode).toList(); DEVICE_NAMES.put(DeviceType.DEVICE_TYPE_TRACK, names); } @Override - String changeDeviceName(String nccDeviceName, List graphicNames) { + public String changeDeviceName(String nccDeviceName, List graphicNames) { if (nccDeviceName.length() < 4) { return nccDeviceName; } - Pattern regex = Pattern.compile("^([A-Za-z]{1,2})(.*?)$"); - Matcher matcher = regex.matcher(nccDeviceName); - String newName = ""; - String code = ""; - if (matcher.find()) { - code = matcher.group(2); - if (StringUtils.contains(code, "_")) { - newName = code; - } else { - String head = code.substring(0, 2); - String end = code.substring(3); - String tail = ""; - if (code.matches(".*?[A-Z]$")) { - tail = end.substring(end.length() - 1); - } - newName = end + head + tail; - } - } - final String finalName = newName; - final String finalCode = code; - String finder = graphicNames.stream().filter(d -> StringUtils.equals(d, finalName) || StringUtils.endsWithIgnoreCase(d, finalCode)).findFirst().orElse(null); - if (Objects.nonNull(finder)) { - return finder; - } - return nccDeviceName; + return super.changeDeviceName(nccDeviceName, graphicNames); } } - public static class SignalDevice extends DeviceNameChanger { @Override - void collectNames(RtssGraphicStorage graphicStorage) { + public void collectNames(RtssGraphicStorage graphicStorage) { List names = graphicStorage.getSignalsList().stream().map(Signal::getCode).toList(); DEVICE_NAMES.put(DeviceType.DEVICE_TYPE_SIGNAL, names); } @Override - String changeDeviceName(String nccDeviceName, List graphicNames) { + public String changeDeviceName(String nccDeviceName, List graphicNames) { if (nccDeviceName.length() <= 3) { return nccDeviceName; } - Pattern regex = Pattern.compile("^([A-Za-z]{1,2})(.*?)$"); - Matcher matcher = regex.matcher(nccDeviceName); - String newName = ""; - String code = ""; - if (matcher.find()) { - code = matcher.group(2); - if (StringUtils.contains(code, "_")) { - newName = code; - } else { - String head = code.substring(0, 2); - String end = code.substring(3); - String tail = ""; - if (code.matches(".*?[A-Z]$")) { - tail = end.substring(end.length() - 1); - } - newName = end + head + tail; - } - } - final String finalName = newName.replaceAll("_", "-"); - final String finalCode = code.replaceAll("_", "-"); - String finder = graphicNames.stream().filter(d -> StringUtils.equals(d, finalName) || StringUtils.endsWithIgnoreCase(d, finalCode)).findFirst().orElse(null); - if (Objects.nonNull(finder)) { - return finder; - } - return nccDeviceName; + return super.changeDeviceName(nccDeviceName.replaceAll("_", "-"), graphicNames); } } - public static void main(String[] args) { - String nccDeviceName = "TP_2DG"; - Pattern regex = Pattern.compile("^([A-Za-z]{1,2})(.*?)$"); - Matcher matcher = regex.matcher(nccDeviceName); - String newName = ""; - String code = ""; - if (matcher.find()) { - code = matcher.group(2); - String head = code.substring(0, 2); - String end = code.substring(3); - String tail = ""; - if (code.matches(".*?[A-Z]$")) { - tail = end.substring(end.length() - 1); - } - newName = end + head + tail; - System.out.println(newName); - } - } } diff --git a/src/main/java/club/joylink/xiannccda/ats/message/line3/NameChanger.java b/src/main/java/club/joylink/xiannccda/ats/message/line3/NameChanger.java new file mode 100644 index 0000000..43aec37 --- /dev/null +++ b/src/main/java/club/joylink/xiannccda/ats/message/line3/NameChanger.java @@ -0,0 +1,43 @@ +package club.joylink.xiannccda.ats.message.line3; + +import club.joylink.xiannccda.dto.protos.LayoutGraphicsProto; +import java.util.List; +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.commons.lang3.StringUtils; + +public interface NameChanger { + + void collectNames(LayoutGraphicsProto.RtssGraphicStorage graphicStorage); + + default String changeDeviceName(String nccDeviceName, List graphicNames) { + + Pattern regex = Pattern.compile("^([A-Za-z]{1,2})(.*?)$"); + Matcher matcher = regex.matcher(nccDeviceName); + String newName = ""; + String code = ""; + if (matcher.find()) { + code = matcher.group(2); + if (StringUtils.contains(code, "_")) { + newName = code; + } else { + String head = code.substring(0, 2); + String end = code.substring(3); + String tail = ""; + if (code.matches(".*?[A-Z]$")) { + tail = end.substring(end.length() - 1); + } + newName = end + head + tail; + } + } + final String finalName = newName; + final String finalCode = code; + String finder = graphicNames.stream().filter(d -> StringUtils.equals(d, finalName) || StringUtils.endsWithIgnoreCase(d, finalCode)).findFirst().orElse(null); + if (Objects.nonNull(finder)) { + return finder; + } + return nccDeviceName; + } + +}