首页 > 其他分享 >Epidata数据构建工具

Epidata数据构建工具

时间:2023-01-04 16:02:52浏览次数:34  
标签:map Epidata length 构建 put new 工具 line append

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @Description TODO
 * @Date 2023/1/4 10:14
 * @Author Shixy
 */
@Slf4j
@Getter
public class EpidataUtils {

    private List<String> lines;

    private StringBuilder line;

    private static final int LINE_MAX_LENGTH = 78;
    private static final String CHINESE_REGEX = "[^\\x00-\\xff]+";
    private static final String SPACE = " ";
    private static final String END_SYMBOL = "!";
    private static final String GBK = "GBK";

    private EpidataUtils() {}

    public static EpidataUtils getInstance() {
        EpidataUtils epidataUtils = new EpidataUtils();
        epidataUtils.lines = new ArrayList<>();
        epidataUtils.line = new StringBuilder();
        return epidataUtils;
    }

    public void append(String str) {
        try {
            int lineLength = line.toString().getBytes(GBK).length;
            int strLength = str.getBytes(GBK).length;
            if (lineLength + strLength > LINE_MAX_LENGTH) {
                line.append(str, 0, LINE_MAX_LENGTH - lineLength);
                line.append(END_SYMBOL);
                lines.add(line.toString());
                line = new StringBuilder();
                line.append(str, LINE_MAX_LENGTH - lineLength, str.length());
            } else if (lineLength + strLength == LINE_MAX_LENGTH) {
                line.append(str);
                line.append(END_SYMBOL);
                lines.add(line.toString());
                line = new StringBuilder();
            } else {
                line.append(str);
            }
        } catch (UnsupportedEncodingException e) {
            log.error("不支持的字符编码: ", e);
        } catch (IndexOutOfBoundsException e) {
            log.error("字符截取出错: ", e);
        }
    }

    public void appendFinish() {
        line.append(END_SYMBOL);
        lines.add(line.toString());
    }

    public static String appendSpace(String content, int length) {
        try {
            Boolean flag = Boolean.FALSE;
            int contentLength = content.getBytes(GBK).length;
            StringBuilder sb = new StringBuilder();
            if (contentLength > length) {
                throw new IndexOutOfBoundsException("index " + contentLength + ", max length " + length);
            }
            if (content.trim().matches(CHINESE_REGEX)) {
                sb.append(content);
                flag = Boolean.TRUE;
            }
            for (int i = 0; i < length - contentLength; i++) {
                sb.append(SPACE);
            }
            if (!flag) {
                sb.append(content);
            }
            return sb.toString();
        } catch (UnsupportedEncodingException e) {
            log.error("不支持的字符编码: ", e);
            return "";
        }
    }

    public static void main(String[] args) {
        Map<String, String> map = new LinkedHashMap<>();
        map.put("ID1", "xxxxxxxxxxxxxxxx");
        map.put("province", "44");
        map.put("city", "15");
        map.put("district", "2");
        map.put("county", "02");
        map.put("point", "2");
        map.put("school", "06");
        map.put("grade", "53");
        map.put("num", "0101");
        map.put("ID2", "xxxxxxxxxxxxxxxx");
        map.put("gender", "1");
        map.put("nation", "1");
        map.put("nationothe", "                    ");
        map.put("birth", "2015/09/04");
        map.put("examine", "2020/11/12");
        map.put("glasstype", "4");
        map.put("OKR", "      ");
        map.put("OKL", "      ");
        map.put("visionR", "5.0");
        map.put("glassR", "   ");
        map.put("visionL", "5.0");
        map.put("glassL", "   ");
        map.put("name2", "王小二              ");
        map.put("spherR", "  0.50");
        map.put("cylinR", " -0.25");
        map.put("axisR", "170");
        map.put("SER", "  0.375");
        map.put("spherRT", "      ");
        map.put("cylinRT", "      ");
        map.put("axisRT", "   ");
        map.put("spherL", "  0.25");
        map.put("cylinL", " -0.25");
        map.put("axisL", " 10");
        map.put("SEL", "  0.125");
        map.put("spherLT", "      ");
        map.put("cylinLT", "      ");
        map.put("axisLT", "   ");
        map.put("NOTE", "                                        ");
        map.put("name", "王小二              ");
        map.put("date", "2020/11/12");

        EpidataUtils instance = getInstance();
        map.forEach((k, v) -> {
            instance.append(v);
        });
        instance.appendFinish();
        List<String> lines1 = instance.getLines();
        lines1.forEach(System.out::println);
    }

}

标签:map,Epidata,length,构建,put,new,工具,line,append
From: https://www.cnblogs.com/Smile-yun-1996/p/17025096.html

相关文章

  • linux抓包工具tcpdump
    1、安装tcpdumpyum-yinstalltcpdump#查看指定网卡的设备,显示端口号对应服务tcpdump-nnt-iens192|head-10#查看ens33网卡设备,对应22端口服务的传输信息(-t不显示......
  • Spring Boot内置的一些工具类
    1、断言Assert工具类//要求参数object必须为非空(NotNull),否则抛出异常,不予放行//参数message参数用于定制异常信息。voidnotNull(Objectobject,Stringmessage......
  • arrays工具类
    //arrays工具类的使用int[]number=newint[]{1,2,3,4};int[]numbers=newint[]{1,2,3,4};//是否相等booleanisequal=Arrays.......
  • 版本控制工具GIT使用指南
    前言:git是分布式版本控制系统,由linux创始人亲自设计,目前是最广泛使用的版本控制工具。本文介绍了版本控制系统的发展和GIT历史,并针对GIT安装和常用命令给出了试验。最后还列......
  • 三款免费强大的SSH工具食用指南
    食用清单XShellFinalShellElecterm食用方案XShell先说说老牌ssh工具XShell吧,用过很多年,说实话没啥别的毛病挺好用的。不过,还是有些地方有待加强,比如文件传输工具......
  • 低代码赋能敏捷开发,YonBuilder让应用构建效率提升数倍
    一款好的低代码开发平台应该是什么样?以企业级应用构建来讲,完成一个应用复杂度随着技术的进步、需求的细化、业务要求的变化并不是逐渐降低而是逐渐提升。用户想要有更好的......
  • 单个makefile构建多目录的工程
    单makefile构建多个目录的工程适用于构建稍微复杂一些的工程目录树如下➜test_pro2tree-h.├──[4.0K]app_1│├──[272]app1.c│└──[......
  • 用好报表工具需掌握的基础知识
    这里所说的报表是由部署在服务器上的报表工具产生并通过浏览器访问的报表,皕杰报表就是这样的报表。报表工具的作用是将数据从数据库里取出来,再以一种要求的样式展示出来。要......
  • 数据同步工具DataX使用教程
    数据同步工具DataX使用教程Tanglu 系统运维 2022-09-12 446 0 一、DataX简介DataX是阿里开源的一款异构数据源离线同步工具,可以实现多种关系型和非关系型异......
  • html5打包成移动平台应用的工具的大致情况
    首先说,日期很重要,这是2013年5月24日做的简单调查。题外话,之前做网站维护,职位设置太坑爹了,于是改行做移动平台开发,主要方向是用html5打包成移动应用。首先声明,在下的英文水平......