Letztes Mal, Java-Version des einfachen Tools "Schlüsselwertspeicher in lokaler JSON-Datei gespeichert" in Golang. Als Serienprodukt habe ich ursprünglich Programme mit demselben Inhalt in mehreren Sprachen geschrieben und verglichen "[https://qiita.com/sky0621/items/32c87aed41cb1c3c67ff)" Verbesserte Version von.
--Teil 1: Lernen Sie beim Vergleichen jeder Programmiersprache durch einfache Toolerstellung --Teil 2: [Verbesserung] Lernen Sie Ruby durch einfache Toolerstellung --Teil 3: [Verbesserung] Lernen Sie Python3 durch einfache Toolerstellung --Teil 4: [Verbesserung] Lernen Sie Golang durch einfache Werkzeugerstellung
$ java -version
openjdk version "1.8.0_202"
OpenJDK Runtime Environment (build 1.8.0_202-20190206132807.buildslave.jdk8u-src-tar--b08)
OpenJDK GraalVM CE 1.0.0-rc14 (build 25.202-b08-jvmci-0.56, mixed mode)
IntelliJ IDEA 2019.2 (Ultimate Edition)
Build #IU-192.5728.98, built on July 23, 2019
Eine Konsolen-App, die beim Starten der App Textinformationen in einer JSON-Datei im Schlüsselwertformat speichern kann. Die Spezifikationen sind die gleichen wie in Teil 1, außer dass sie im Speicher gespeichert wurden. Weitere Informationen finden Sie weiter unten. https://qiita.com/sky0621/items/32c87aed41cb1c3c67ff#要件
https://github.com/sky0621/book_java/tree/v0.2.0
| Java-Quelle | Erläuterung | 
|---|---|
| Main.java | Einstiegspunkt für den App-Start | 
| StoreInfo.java | Speichern, um Schlüsselwertinformationen zu speichern(JSON-Datei)Behandelt Informationen über. Derzeit wird nur "Dateiname" beibehalten | 
| Commands.java | Verwalten Sie jeden Befehl, z. B. das Erfassen, Speichern und Löschen von Informationen aus dem Schlüsselwertspeicher. Die Auswirkungen des Erhöhens oder Verringerns von Befehlen sind für diese Quelle geschlossen. | 
| Command.java | Gemeinsame Schnittstelle für jeden Befehl | 
| SaveCommand.java | Verantwortlich für die Speicherung von Schlüsselwertinformationen. | 
| GetCommand.java | Verantwortlich für die Wertschöpfung für den angegebenen Schlüssel. | 
| ListCommand.java | Verantwortlich für die Erfassung aller wichtigen Wertinformationen. | 
| RemoveCommand.java | Verantwortlich für das Löschen des Werts für den angegebenen Schlüssel. | 
| ClearCommand.java | Verantwortlich für das Löschen aller Schlüsselwertinformationen. | 
| HelpCommand.java | Verantwortlich für die Anzeige von Hilfeinformationen. | 
| EndCommand.java | Verantwortlich für die Beendigung der App. | 
[Main.java]
import java.util.Scanner;
public class Main {
    public static void main(String... args) {
        Commands commands = new Commands(new StoreInfo("store.json"));
        System.out.println("Start!");
        while (true) {
            Scanner s = new Scanner(System.in);
            String cmd = s.nextLine();
            commands.exec(cmd.split(" "));
        }
    }
}
[StoreInfo.java]
import org.apache.commons.lang3.StringUtils;
public class StoreInfo {
    private static final String DEFAULT_STORE_NAME = "store.json";
    private String storeName;
    public StoreInfo(String storeName) {
        if (StringUtils.isEmpty(storeName)) {
            this.storeName = DEFAULT_STORE_NAME;
        } else {
            this.storeName = storeName;
        }
    }
    public String getName() {
        return this.storeName;
    }
}
[Commands.java]
import java.io.File;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Commands {
    private Map<String, Command> commands;
    public Commands(StoreInfo storeInfo) {
        commands = new HashMap<>();
        commands.put("end", new EndCommand());
        commands.put("help", new HelpCommand());
        commands.put("clear", new ClearCommand(storeInfo));
        commands.put("save", new SaveCommand(storeInfo));
        commands.put("get", new GetCommand(storeInfo));
        commands.put("remove", new RemoveCommand(storeInfo));
        commands.put("list", new ListCommand(storeInfo));
        if (!new File(storeInfo.getName()).exists()) {
            commands.get("clear").exec(null);
        }
    }
    public void exec(String[] cmds) {
        if (cmds == null || cmds.length < 1) {
            System.out.println("no target");
            return;
        }
        List<String> cmdList = Arrays.asList(cmds);
        String[] args = cmdList.subList(1, cmdList.size()).toArray(new String[0]);
        this.commands.get(cmds[0]).exec(args);
    }
}
[Command.java]
public interface Command {
    void exec(String[] args);
}
Für jeden Befehl machen wir dasselbe, außer dass die Speicherinformationen von On-Memory-Hash zu JSON geändert wurden. (Also werde ich die Erklärung weglassen.)
[SaveCommand.java]
import com.google.gson.Gson;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class SaveCommand implements Command {
    private StoreInfo storeInfo;
    public SaveCommand(StoreInfo storeInfo) {
        this.storeInfo = storeInfo;
    }
    @Override
    public void exec(String[] args) {
        if (args == null || args.length != 2) {
            System.out.println("not valid");
            return;
        }
        Gson gson = new Gson();
        Path p = Paths.get(this.storeInfo.getName());
        try {
            Map<String, String> now = gson.fromJson(Files.readAllLines(p).get(0), HashMap.class);
            now.put(args[0], args[1]);
            Files.write(p, gson.toJson(now).getBytes());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
[GetCommand.java]
import com.google.gson.Gson;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class GetCommand implements Command {
    private StoreInfo storeInfo;
    public GetCommand(StoreInfo storeInfo) {
        this.storeInfo = storeInfo;
    }
    @Override
    public void exec(String[] args) {
        if (args == null || args.length != 1) {
            System.out.println("not valid");
            return;
        }
        Path p = Paths.get(this.storeInfo.getName());
        try {
            Map<String, String> now = new Gson().fromJson(Files.readAllLines(p).get(0), HashMap.class);
            System.out.println(now.get(args[0]));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
[ListCommand.java]
import com.google.gson.Gson;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class ListCommand implements Command {
    private StoreInfo storeInfo;
    public ListCommand(StoreInfo storeInfo) {
        this.storeInfo = storeInfo;
    }
    @Override
    public void exec(String[] args) {
        Path p = Paths.get(this.storeInfo.getName());
        try {
            Map<String, String> now = new Gson().fromJson(Files.readAllLines(p).get(0), HashMap.class);
            System.out.println("\"key\",\"value\"");
            now.forEach((k, v) -> System.out.printf("\"%s\",\"%s\"\n", k, v));
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
[RemoveCommand.java]
import com.google.gson.Gson;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
public class RemoveCommand implements Command {
    private StoreInfo storeInfo;
    public RemoveCommand(StoreInfo storeInfo) {
        this.storeInfo = storeInfo;
    }
    @Override
    public void exec(String[] args) {
        if (args == null || args.length != 1) {
            System.out.println("not valid");
            return;
        }
        Gson gson = new Gson();
        Path p = Paths.get(this.storeInfo.getName());
        try {
            Map<String, String> now = gson.fromJson(Files.readAllLines(p).get(0), HashMap.class);
            now.remove(args[0]);
            Files.write(p, gson.toJson(now).getBytes());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
[ClearCommand.java]
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ClearCommand implements Command {
    private StoreInfo storeInfo;
    public ClearCommand(StoreInfo storeInfo) {
        this.storeInfo = storeInfo;
    }
    @Override
    public void exec(String[] args) {
        try {
            Files.write(Paths.get(storeInfo.getName()), "{}".getBytes());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
[HelpCommand.java]
public class HelpCommand implements Command {
    @Override
    public void exec(String[] args) {
        String msg = "\n" +
                "[usage]\n" +
                "Dieser Befehl verwaltet Zeichenfolgeninformationen im Schlüsselwertformat.\n" +
                "Die folgenden Unterbefehle sind verfügbar.\n" +
                "\n" +
                "save   ...Schlüssel und Wert übergeben und speichern.\n" +
                "get    ...Übergeben Sie die Taste, um den Wert anzuzeigen.\n" +
                "remove ...Übergeben Sie den Schlüssel und löschen Sie den Wert.\n" +
                "list   ...Listet die gespeicherten Inhalte auf.\n" +
                "clear  ...Initialisiert den gespeicherten Inhalt.\n" +
                "help   ...Hilfeinformationen (wie dieser Inhalt) werden angezeigt.\n" +
                "end    ...Beenden Sie die App.\n" +
                "\n";
        System.out.println(msg);
    }
}
[EndCommand.java]
public class EndCommand implements Command {
    @Override
    public void exec(String[] args) {
        System.out.println("End!");
        System.exit(-1);
    }
}
Mir wurde klar, dass es immer chaotischer wurde ...