All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.runon.trading.data.file.TimeLineOut Maven / Gradle / Ivy

The newest version!
package io.runon.trading.data.file;

import com.seomse.commons.utils.FileUtil;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * @author macle
 */
public class TimeLineOut {

    private final String dirPath;
    private final TimeLine timeLine;

    private final TimeName timeName;

    private final Object lock = new Object();
    public TimeLineOut(String dirPath, TimeLine timeLine, TimeName timeName){
        this.dirPath = dirPath;
        this.timeLine = timeLine;
        this.timeName = timeName;
    }

    public void update(String [] lines){
        Arrays.sort(lines,  Comparator.comparingLong(timeLine::getTime));

        String lastFileName = timeName.getName(timeLine.getTime(lines[0]));
        List lineList = new ArrayList<>();
        lineList.add(lines[0]);

        synchronized (lock) {
            for (int i = 1; i < lines.length ; i++) {
                String line = lines[i];
                String fileName = timeName.getName(timeLine.getTime(line));
                if(!fileName.equals(lastFileName)){
                    update(lastFileName, lineList);
                    lineList.clear();
                    lastFileName = fileName;
                }
                lineList.add(line);
            }

            if(lineList.size() > 0){
                update(lastFileName, lineList);
                lineList.clear();
            }

        }
    }

    /**
     * 정렬없이 데이터추가
     * 업데이트가 필요없는 빠른데이터 추가
     * @param lines 라인데이터 추가
     */
    public void add(String [] lines){
        String lastFileName = timeName.getName(timeLine.getTime(lines[0]));
        List lineList = new ArrayList<>();
        lineList.add(lines[0]);
        synchronized (lock) {
            for (int i = 1; i < lines.length ; i++) {
                String line = lines[i];
                String fileName = timeName.getName(timeLine.getTime(line));
                if(!fileName.equals(lastFileName)){
                    String path = dirPath + "/" + lastFileName;

                    if(FileUtil.isFile(path)){
                        FileUtil.fileOutput("\n" + outValue(lineList), path, true);
                    }else{
                        FileUtil.fileOutput(outValue(lineList), path, false);
                    }

                    lineList.clear();
                    lastFileName = fileName;
                }
                lineList.add(line);
            }

            if(lineList.size() > 0){
                String path = dirPath + "/" + lastFileName;
                if(FileUtil.isFile(path)){
                    FileUtil.fileOutput("\n" + outValue(lineList), path, true);
                }else{
                    FileUtil.fileOutput(outValue(lineList), path, false);
                }
                lineList.clear();
            }
        }
    }

    public void update(String fileName, List lineList){
        if(lineList.size() == 0){
            return ;
        }

        String path = dirPath + "/" + fileName;

        if(FileUtil.isFile(path)){

            String saveLastLine = FileUtil.getLastTextLine(path);
            if(saveLastLine.equals("")){
                //내용이 없으면 파일이 비어 있으면
                FileUtil.fileOutput(outValue(lineList), path, false);
                return;
            }

            long lastSaveTime = timeLine.getTime(saveLastLine);
            long firstTime = timeLine.getTime(lineList.get(0));
            if(firstTime > lastSaveTime){
                //저장된 시간보다크면 뒤에다 붙임
                FileUtil.fileOutput(outValue(lineList), path, true);

            }else if(firstTime == lastSaveTime ){
                //마지막 한줄만 수정이면

                List saveLines =  FileUtil.getLineList(new File(path), StandardCharsets.UTF_8);
                int size = saveLines.size()-1;
                StringBuilder sb = new StringBuilder();
                sb.append(saveLines.get(0));
                for (int i = 1; i  saveLines =  FileUtil.getLineList(new File(path), StandardCharsets.UTF_8);

                Set checkSet = new HashSet<>();

                List newList = new ArrayList<>();
                for(String line : lineList){
                    long time = timeLine.getTime(line);
                    checkSet.add(time);
                    newList.add(new LineTime(line, time));
                }

                List sumList = new ArrayList<>();
                for(String line: saveLines){
                    long time = timeLine.getTime(line);
                    if(checkSet.contains(time)){
                        continue;
                    }
                    sumList.add(new LineTime(line,time));
                }
                sumList.addAll(newList);

                LineTime[] array = sumList.toArray(new LineTime[0]);

                Arrays.sort(array, LineTime.SORT);

                StringBuilder sb = new StringBuilder();
                sb.append(array[0].line);
                for (int i = 1; i  lineList){
        StringBuilder sb = new StringBuilder();
        sb.append(lineList.get(0));

        int size = lineList.size();
        for (int i = 1; i 




© 2015 - 2024 Weber Informatics LLC | Privacy Policy