通过码流串,格式为key,len,val。提供key,查找val的算法。

2024-10-01 15:08:03 浏览数 (1)

代码语言:txt复制
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

public class HuaweiTest {

    public static void main(String[] args) {
//        System.out.println("Hello World!");
        String tag = "31";
        String codeStreamStr = "31 01 32";
        String codeStringVal = getCodeStringVal(tag, codeStreamStr);
        System.out.println(codeStringVal);
    }


    public static String getCodeStringVal(String tag, String codeStreamStr) {
        String trim = tag.trim();
        if (trim == null) {
            return null;
        }
        if (trim.isEmpty()) {
            return null;
        }
        if ((codeStreamStr.length() * 4) > 50000) {
            return null;
        }
        String trim1 = codeStreamStr.trim();
        if (trim1 == null) {
            return null;
        }
        if (trim1.isEmpty()) {
            return null;
        }
        String[] split = trim1.split("\s");
        if (!(split.length % 3 == 0)) {
            return null;
        }
        for (int i = 0; i < split.length; i  ) {
            String s = split[i];
            if (s.length() > 2) {
                return null;
            }
            String trim2 = s.trim();
            for (int j = 0; j < trim2.length(); j  ) {
                char c = trim2.charAt(j);
                if (Character.isDigit(c)) {
                    continue;
                } else {
                    if (Character.isLowerCase(c)) {
                        return null;
                    }
                    if (!(c >= 'A' && c <= 'F')) {
                        return null;
                    }
                }
            }
        }
        String[] split2 = codeStreamStr.split("\s");
        HashMap<String, Integer> stringIntegerHashMap = new HashMap<>();
        for (int i = 0; i < split2.length; i  ) {
            String s = split2[i];
            String trim2 = s.trim();
            if (stringIntegerHashMap.containsKey(trim2)) {
                stringIntegerHashMap.put(trim2, stringIntegerHashMap.get(trim2)   1);
            } else {
                stringIntegerHashMap.put(trim2, 1);
            }
        }
        System.out.println(stringIntegerHashMap);
        ArrayList<CodeStream> codeStreamArrayList = new ArrayList<>();
        String[] split1 = codeStreamStr.split("\s");
        int count = 0;
        int codeTagIndex = 0;
        for (int i = 0; i < split1.length; i  ) {
            if ((count   1) % 3 == 0) {
                continue;
            } else {
                CodeStream codeStream = new CodeStream();
                codeStream.setId(UUID.randomUUID().toString());
                String s = split1[codeTagIndex];
                if (extracted(s, stringIntegerHashMap)) return null;
                codeStream.setTag(s);
                String s1 = getString(split1, codeTagIndex);
                if (s1 == null) return null;
                codeStream.setLength(Integer.valueOf(s1));
                String s2 = split1[codeTagIndex   2];
                codeStream.setValue(s2);
                codeStreamArrayList.add(codeStream);
                count  = 3;
                codeTagIndex = count;
            }
        }
        System.out.println(codeStreamArrayList);
        ArrayList<String> stringArrayList = new ArrayList<>();
        codeStreamArrayList.forEach(e -> {
            String tag1 = e.getTag();
            if (tag.equals(tag1)) {
                String value = e.getValue();
                stringArrayList.add(value);
            }
        });
        System.out.println(stringArrayList);
        StringBuilder stringBuilder = new StringBuilder();
        stringArrayList.forEach(e -> {
            stringBuilder.append(e);
            stringBuilder.append("\s");
        });
        return stringBuilder.toString();
    }

    private static String getString(String[] split1, int codeTagIndex) {
        String s1 = split1[codeTagIndex   1];
        byte[] bytes1 = s1.getBytes();
//        if (bytes1.length>2){
//            return null;
//        }
        return s1;
    }

    private static boolean extracted(String s, HashMap<String, Integer> stringIntegerHashMap) {
        byte[] bytes = s.getBytes();
        int length = bytes.length;
//        if (length>1){
//            return true;
//        }
        if (stringIntegerHashMap.containsKey(s)) {
            if (stringIntegerHashMap.get(s) >= 2) {
                return true;
            }
        }
        return false;
    }
}

class CodeStream {
    private String id;
    private String tag;
    private Integer length;
    private String value;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "CodeStream{"  
                "id='"   id   '''  
                ", tag='"   tag   '''  
                ", length="   length  
                ", value='"   value   '''  
                '}';
    }
}

0 人点赞