From 4f12bc8187be0a0cfd9e7f72d3c56aadefb39ce2 Mon Sep 17 00:00:00 2001 From: IvanovIvanK Date: Thu, 18 Dec 2014 00:59:38 +0300 Subject: [PATCH 1/5] JUnit --- .../students/ivan_ivanov/filemap/Command.java | 10 -- .../students/ivan_ivanov/filemap/Exit.java | 12 +- .../students/ivan_ivanov/filemap/FileMap.java | 19 --- .../ivan_ivanov/filemap/FileMapExecutor.java | 10 +- .../ivan_ivanov/filemap/FileMapMain.java | 29 ++-- .../ivan_ivanov/filemap/FileMapState.java | 22 ++- .../filemap/{Utils.java => FileMapUtils.java} | 84 +++++----- .../students/ivan_ivanov/filemap/Get.java | 17 +- .../students/ivan_ivanov/filemap/List.java | 7 +- .../students/ivan_ivanov/filemap/Put.java | 20 ++- .../students/ivan_ivanov/filemap/Remove.java | 19 ++- .../multifilehashmap/CmdCommit.java | 25 +++ .../multifilehashmap/CmdCreate.java | 14 +- .../ivan_ivanov/multifilehashmap/CmdDrop.java | 34 +--- .../multifilehashmap/CmdRollback.java | 25 +++ .../multifilehashmap/CmdShowTables.java | 22 +-- .../ivan_ivanov/multifilehashmap/CmdSize.java | 25 +++ .../ivan_ivanov/multifilehashmap/CmdUse.java | 41 +++-- .../multifilehashmap/MultiFileHashMap.java | 21 --- .../MultiFileHashMapExecutor.java | 13 +- .../MultiFileHashMapExit.java | 22 +-- .../multifilehashmap/MultiFileHashMapGet.java | 11 +- .../MultiFileHashMapList.java | 11 +- .../MultiFileHashMapMain.java | 30 ++-- .../multifilehashmap/MultiFileHashMapPut.java | 12 +- .../MultiFileHashMapRemove.java | 12 +- .../MultiFileHashMapState.java | 56 +++---- .../MultiFileHashMapTable.java | 158 ++++++++++++++++-- .../MultiFileHashMapTableProvider.java | 73 ++++++-- .../MultiFileHashMapTableProviderFactory.java | 19 ++- ...tiFileHashMapTableProviderFactoryTest.java | 43 +++++ .../MultiFileHashMapTableProviderTest.java | 111 ++++++++++++ .../MultiFileHashMapTableTest.java | 140 ++++++++++++++++ .../MultiFileHashMapUtils.java | 30 ++-- .../fivt/students/ivan_ivanov/shell/Cat.java | 18 +- .../fivt/students/ivan_ivanov/shell/Cd.java | 10 +- .../students/ivan_ivanov/shell/Command.java | 5 +- .../fivt/students/ivan_ivanov/shell/Cp.java | 15 +- .../students/ivan_ivanov/shell/Executor.java | 10 +- .../fivt/students/ivan_ivanov/shell/Exit.java | 7 +- .../fivt/students/ivan_ivanov/shell/Ls.java | 8 +- .../students/ivan_ivanov/shell/Mkdir.java | 10 +- .../fivt/students/ivan_ivanov/shell/Mv.java | 17 +- .../fivt/students/ivan_ivanov/shell/Pwd.java | 8 +- .../fivt/students/ivan_ivanov/shell/Rm.java | 12 +- .../students/ivan_ivanov/shell/Shell.java | 60 +++---- .../ivan_ivanov/shell/ShellExecutor.java | 10 +- .../students/ivan_ivanov/shell/ShellMain.java | 10 +- .../ivan_ivanov/shell/ShellState.java | 10 +- 49 files changed, 937 insertions(+), 470 deletions(-) delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Command.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMap.java rename src/ru/fizteh/fivt/students/ivan_ivanov/filemap/{Utils.java => FileMapUtils.java} (54%) create mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCommit.java create mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdRollback.java create mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdSize.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMap.java create mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactoryTest.java create mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderTest.java create mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableTest.java diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Command.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Command.java deleted file mode 100644 index b73da6672..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Command.java +++ /dev/null @@ -1,10 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import java.io.IOException; -import java.util.Map; - -public interface Command { - String getName(); - - void executeCmd(Map dataBase, String[] args) throws IOException; -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java index e8ab2cb10..846635450 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java @@ -1,19 +1,19 @@ package ru.fizteh.fivt.students.ivan_ivanov.filemap; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; import java.io.IOException; -public class Exit implements Command { +public class Exit implements Command { + + public String getName() { - public final String getName() { return "exit"; } - public final void executeCmd(final Shell filemap, final String[] args) throws IOException { - Utils.write(((FileMap) filemap).getFileMapState().getDataBase(), - ((FileMap) filemap).getFileMapState().getDataFile()); + public void executeCmd(FileMapState inState, String[] args) throws IOException { + + FileMapUtils.write(inState.getDataBase(), inState.getDataFile()); System.exit(0); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMap.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMap.java deleted file mode 100644 index 9e091b20a..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMap.java +++ /dev/null @@ -1,19 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import java.io.*; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -public class FileMap extends Shell { - - private FileMapState state; - - public final FileMapState getFileMapState() { - return state; - } - - public FileMap(final File currentFile) throws IOException { - state = new FileMapState(currentFile); - Utils.readDataBase(state); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapExecutor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapExecutor.java index 9bf168e89..36739c671 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapExecutor.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapExecutor.java @@ -3,22 +3,24 @@ import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -public class FileMapExecutor extends Executor { +public class FileMapExecutor extends Executor { public FileMapExecutor() { + list(); } - public final void list() { + public void list() { + Command put = new Put(); mapOfCmd.put(put.getName(), put); Command get = new Get(); mapOfCmd.put(get.getName(), get); Command remove = new Remove(); mapOfCmd.put(remove.getName(), remove); + Command exit = new Exit(); + mapOfCmd.put(exit.getName(), exit); Command list = new List(); mapOfCmd.put(list.getName(), list); - Command exit = new Exit(); - mapOfCmd.put(exit.getName(), exit); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java index fdd077282..cf692d34a 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java @@ -1,27 +1,30 @@ package ru.fizteh.fivt.students.ivan_ivanov.filemap; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; + import java.io.File; import java.io.IOException; public class FileMapMain { - public static void main(final String[] args) throws IOException { + public static void main(String[] args) throws IOException { - String currentProperty = System.getProperty("db.file"); - File base = new File(currentProperty); - if (!base.exists()) { - base.createNewFile(); + String currentProperty = System.getProperty("fizteh.db.dir"); + if (currentProperty == null) { + System.exit(-1); } - + File base = new File(currentProperty); try { + if (!base.exists()) { + base.createNewFile(); + } + base = base.getCanonicalFile().toPath().resolve("db.dat").toFile(); - } catch (Exception e) { - System.err.println(e.getMessage()); - System.exit(1); - } + FileMapState startState = new FileMapState(base); + FileMapUtils.readDataBase(startState); + Shell filemap = new Shell(startState); + + FileMapExecutor exec = new FileMapExecutor(); - FileMap filemap = new FileMap(base); - FileMapExecutor exec = new FileMapExecutor(); - try { if (args.length > 0) { filemap.batchState(args, exec); } else { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapState.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapState.java index 2018efc35..cf9762f65 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapState.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapState.java @@ -6,23 +6,27 @@ public class FileMapState { - public Map dataBase; - public File dataFile; + private Map dataBase; + private File dataFile; + + public FileMapState(File currentFile) { + + dataBase = new HashMap(); + dataFile = currentFile; + } + + public Map getDataBase() { - public final Map getDataBase() { return dataBase; } - public final File getDataFile() { + public File getDataFile() { + return dataFile; } - public FileMapState(final File currentFile) { - dataBase = new HashMap(); - dataFile = currentFile; - } - public void setDataBase(Map map) { + dataBase = map; } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Utils.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapUtils.java similarity index 54% rename from src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Utils.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapUtils.java index 65ca99794..c3edef8c4 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Utils.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapUtils.java @@ -6,21 +6,22 @@ import java.util.List; import java.util.Map; - -public class Utils { +public class FileMapUtils { private static final long MAX_SIZE = 1024 * 1024; - private static final int POS_STEP = 5; - private static final int BUF_SIZE = 4096; - private static String readKey(final DataInputStream dataStream) throws IOException { + private static String readKey(DataInputStream dataStream) throws IOException { ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream(); byte b = dataStream.readByte(); int length = 0; while (b != 0) { byteOutputStream.write(b); - b = dataStream.readByte(); + try { + b = dataStream.readByte(); + } catch (EOFException e) { + throw new IOException("wrong data format"); + } length++; if (length > MAX_SIZE) { throw new IOException("wrong data format"); @@ -29,12 +30,12 @@ private static String readKey(final DataInputStream dataStream) throws IOExcepti if (length == 0) { throw new IOException("wrong data format"); } - return byteOutputStream.toString(StandardCharsets.UTF_8.toString()); } - private static String readValue(final DataInputStream dis, final long offset1, - final long offset2, final long position, final long len) throws IOException { + private static String readValue(DataInputStream dis, long offset1, + long offset2, long position, long len) throws IOException { + dis.mark((int) len); dis.skip(offset1 - position); byte[] buffer = new byte[(int) (offset2 - offset1)]; @@ -44,13 +45,14 @@ private static String readValue(final DataInputStream dis, final long offset1, return value; } - public static void readDataBase(final FileMapState state) throws IOException { + public static void readDataBase(FileMapState state) throws IOException { + if (state.getDataFile().length() == 0) { return; } InputStream currentStream = new FileInputStream(state.getDataFile()); - BufferedInputStream bufferStream = new BufferedInputStream(currentStream, BUF_SIZE); + BufferedInputStream bufferStream = new BufferedInputStream(currentStream, 4096); DataInputStream dataStream = new DataInputStream(bufferStream); int fileLength = (int) state.getDataFile().length(); @@ -58,16 +60,16 @@ public static void readDataBase(final FileMapState state) throws IOException { try { int position = 0; String key1 = readKey(dataStream); + position += key1.getBytes(StandardCharsets.UTF_8).length; int offset1 = dataStream.readInt(); int firstOffset = offset1; - position += POS_STEP; - + position += 5; while (position != firstOffset) { String key2 = readKey(dataStream); position += key2.getBytes(StandardCharsets.UTF_8).length; int offset2 = dataStream.readInt(); - position += POS_STEP; + position += 5; String value = readValue(dataStream, offset1, offset2, position, fileLength); state.getDataBase().put(key1, value); offset1 = offset2; @@ -78,36 +80,42 @@ public static void readDataBase(final FileMapState state) throws IOException { } finally { closeStream(dataStream); } - } - private static void closeStream(final Closeable stream) throws IOException { + private static void closeStream(Closeable stream) throws IOException { + stream.close(); } - public static void write(final Map dataBase, final File currentFile) throws IOException { - - OutputStream currentStream = new FileOutputStream(currentFile); - BufferedOutputStream bufferStream = new BufferedOutputStream(currentStream, BUF_SIZE); - DataOutputStream dataStream = new DataOutputStream(bufferStream); - long biasing = 0; - - for (String key : dataBase.keySet()) { - biasing += key.getBytes(StandardCharsets.UTF_8).length + POS_STEP; - } - List values = new ArrayList(dataBase.keySet().size()); - for (String key : dataBase.keySet()) { - String value = dataBase.get(key); - values.add(value); - dataStream.write(key.getBytes(StandardCharsets.UTF_8)); - dataStream.writeByte(0); - dataStream.writeInt((int) biasing); - biasing += value.getBytes(StandardCharsets.UTF_8).length; - } + public static void write(Map dataBase, File currentFile) throws IOException { - for (String value : values) { - dataStream.write(value.getBytes()); + try { + FileOutputStream currentStream = new FileOutputStream(currentFile); + BufferedOutputStream bufferStream = new BufferedOutputStream(currentStream, 4096); + DataOutputStream dataStream = new DataOutputStream(bufferStream); + long biasing = 0; + try { + for (String key : dataBase.keySet()) { + biasing += key.getBytes(StandardCharsets.UTF_8).length + 5; + } + List values = new ArrayList(dataBase.keySet().size()); + for (String key : dataBase.keySet()) { + String value = dataBase.get(key); + values.add(value); + dataStream.write(key.getBytes(StandardCharsets.UTF_8)); + dataStream.writeByte(0); + dataStream.writeInt((int) biasing); + biasing += value.getBytes(StandardCharsets.UTF_8).length; + } + + for (String value : values) { + dataStream.write(value.getBytes()); + } + } finally { + closeStream(dataStream); + } + } catch (IOException e) { + throw new IOException("cannot write '" + currentFile.getName() + "'", e); } - closeStream(dataStream); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java index 26d512632..6942fc8ba 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java @@ -1,19 +1,24 @@ package ru.fizteh.fivt.students.ivan_ivanov.filemap; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; import java.io.IOException; -public class Get implements Command { +public class Get implements Command { + + public String getName() { - public final String getName() { return "get"; } - public final void executeCmd(final Shell filemap, final String[] args) throws IOException { - String key = args[0]; - String value = ((FileMap) filemap).getFileMapState().getDataBase().get(key); + public void executeCmd(FileMapState inState, String[] args) throws IOException { + + if (args.length != 1) { + System.out.println("incorrect number of arguments"); + return; + } + + String value = inState.getDataBase().get(args[0]); if (value == null) { System.out.println("not found"); } else { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java index e8f89af8a..551f61669 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java @@ -1,19 +1,18 @@ package ru.fizteh.fivt.students.ivan_ivanov.filemap; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; import java.io.IOException; import java.util.Set; -public class List implements Command { +public class List implements Command { public final String getName() { return "list"; } - public final void executeCmd(final Shell filemap, final String[] args) throws IOException { - Set keys = ((FileMap) filemap).getFileMapState().getDataBase().keySet(); + public final void executeCmd(FileMapState inState, String[] args) throws IOException { + Set keys = inState.getDataBase().keySet(); System.out.println(String.join(", ", keys)); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java index 8d4692932..83f01ab32 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java @@ -1,20 +1,24 @@ package ru.fizteh.fivt.students.ivan_ivanov.filemap; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + import java.io.IOException; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; +public class Put implements Command { -public class Put implements Command { + public String getName() { - public final String getName() { return "put"; } - public final void executeCmd(final Shell filemap, final String[] args) throws IOException { - String key = args[0]; - String value = args[1]; - String oldValue = ((FileMap) filemap).getFileMapState().getDataBase().put(key, value); + public void executeCmd(FileMapState inState, String[] args) throws IOException { + + if (args.length != 2) { + System.out.println("incorrect number of arguments"); + return; + } + + String oldValue = inState.getDataBase().put(args[0], args[1]); if (oldValue == null) { System.out.println("new"); } else { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java index eb4341c9b..a534b55a7 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java @@ -1,19 +1,24 @@ package ru.fizteh.fivt.students.ivan_ivanov.filemap; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + import java.io.IOException; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; +public class Remove implements Command { -public class Remove implements Command { + public String getName() { - public final String getName() { return "remove"; } - public final void executeCmd(final Shell filemap, final String[] args) throws IOException { - String key = args[0]; - String value = ((FileMap) filemap).getFileMapState().getDataBase().remove(key); + public void executeCmd(FileMapState inState, String[] args) throws IOException { + + if (args.length != 1) { + System.out.println("incorrect number of arguments"); + return; + } + + String value = inState.getDataBase().remove(args[0]); if (value == null) { System.out.println("not found"); } else { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCommit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCommit.java new file mode 100644 index 000000000..e4ef3c391 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCommit.java @@ -0,0 +1,25 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; + +public class CmdCommit implements Command { + + @Override + public String getName() { + + return "commit"; + } + + @Override + public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + + if (inState.getCurrentTable() == null) { + System.out.println("no table"); + return; + } + + System.out.println(inState.getCurrentTable().commit()); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCreate.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCreate.java index c577de764..8eb1ceb0a 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCreate.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCreate.java @@ -1,27 +1,21 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; import java.io.IOException; -public class CmdCreate implements Command { +public class CmdCreate implements Command { @Override - public final String getName() { + public String getName() { return "create"; } @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - if (args.length != 1) { - System.out.println("incorrect number of arguments"); - return; - } - - if (((MultiFileHashMap) shell).getMFHMState().createTable(args[0]) != null) { + if (inState.createTable(args[0]) != null) { System.out.println("created"); } else { System.out.println(args[0] + " exists"); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdDrop.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdDrop.java index 018a0bdfc..ecc1d63b5 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdDrop.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdDrop.java @@ -1,46 +1,26 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; import java.io.IOException; -import java.util.Set; -public class CmdDrop implements Command { +public class CmdDrop implements Command { @Override - public final String getName() { + public String getName() { return "drop"; } @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - if (args.length != 1) { - System.out.println("incorrect number of arguments"); - return; - } - - if (((MultiFileHashMap) shell).getMFHMState().getTable(args[0]) == null) { + if (inState.getTable(args[0]) == null) { System.out.println(args[0] + " not exists"); return; + } else { + inState.deleteTable(args[0]); + System.out.println("dropped"); } - - if (((MultiFileHashMap) shell).getMFHMState().getFlag() == 1) { - if (((MultiFileHashMap) shell).getMFHMState().getCurrentTable().getName().equals(args[0])) { - Set keys = ((MultiFileHashMap) shell).getMFHMState(). - getDataBaseFromCurrentTable().keySet(); - for (String key : keys) { - ((MultiFileHashMap) shell).getMFHMState().removeFromCurrentTable(key); - } - ((MultiFileHashMap) shell).getMFHMState().setCurrentTable(); - ((MultiFileHashMap) shell).getMFHMState().changeFlag(); - } - } - - ((MultiFileHashMap) shell).getMFHMState().deleteTable(args[0]); - System.out.println("dropped"); - } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdRollback.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdRollback.java new file mode 100644 index 000000000..aa727b9f8 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdRollback.java @@ -0,0 +1,25 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; + +public class CmdRollback implements Command { + + @Override + public String getName() { + + return "rollback"; + } + + @Override + public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + + if (inState.getCurrentTable() == null) { + System.out.println("no table"); + return; + } + + System.out.println(inState.getCurrentTable().rollback()); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdShowTables.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdShowTables.java index 90fb31ae1..04e1921fe 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdShowTables.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdShowTables.java @@ -1,23 +1,19 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; -import java.io.File; import java.io.IOException; -import java.util.Map; import java.util.Set; -public class CmdShowTables implements Command { +public class CmdShowTables implements Command { @Override public final String getName() { - return "show"; } @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public final void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { if (args.length != 1) { throw new IOException("Can't find key"); @@ -27,20 +23,12 @@ public final void executeCmd(final Shell shell, final String[] args) throws IOEx throw new IOException("Can't find key"); } - if (((MultiFileHashMap) shell).getMFHMState().getFlag() == 1) { - File fileForWrite = ((MultiFileHashMapTable) - ((MultiFileHashMap) shell).getMFHMState().getCurrentTable()).getDataFile(); - Map mapForWrite = ((MultiFileHashMapTable) - ((MultiFileHashMap) shell).getMFHMState().getCurrentTable()).getDataBase(); - MultiFileHashMapUtils.write(fileForWrite, mapForWrite); - } - System.out.println("table_name row_count"); - Set tables = ((MultiFileHashMap) shell).getMFHMState().getTableSet(); + Set tables = inState.getTableSet(); for (String table : tables) { - System.out.println(String.join(" ", table, String.valueOf(((MultiFileHashMap) shell). - getMFHMState().getTable(table).getDataBase().keySet().size()))); + System.out.println(String.join(" ", table, + String.valueOf(inState.getTable(table).getDataBase().keySet().size()))); } } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdSize.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdSize.java new file mode 100644 index 000000000..feae609a4 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdSize.java @@ -0,0 +1,25 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; + +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; + +import java.io.IOException; + +public class CmdSize implements Command { + + @Override + public String getName() { + + return "size"; + } + + @Override + public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + + if (inState.getCurrentTable() == null) { + System.out.println("no table"); + return; + } + + System.out.println(inState.getCurrentTable().size()); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdUse.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdUse.java index 9af5a3adb..5e8926ae0 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdUse.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdUse.java @@ -1,47 +1,44 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; import java.io.File; import java.io.IOException; import java.util.Map; -public class CmdUse implements Command { +public class CmdUse implements Command { @Override - public final String getName() { + public String getName() { return "use"; } @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - - if (args.length != 1) { - System.out.println("incorrect number of arguments"); - return; + public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + + if (inState.getCurrentTable() != null) { + if (!inState.getCurrentTable().getName().equals(args[0])) { + int size = inState.getChangesBaseSize(); + if (size != 0) { + System.out.println(size + " unsaved changes"); + return; + } + } else { + return; + } } - - if (((MultiFileHashMap) shell).getMFHMState().getTable(args[0]) == null) { + if (inState.getTable(args[0]) == null) { System.out.println(args[0] + " not exists"); return; } - if (1 == ((MultiFileHashMap) shell).getMFHMState().getFlag()) { - File fileForWrite = ((MultiFileHashMapTable) - ((MultiFileHashMap) shell).getMFHMState().getCurrentTable()).getDataFile(); - Map mapForWrite = ((MultiFileHashMapTable) - ((MultiFileHashMap) shell).getMFHMState().getCurrentTable()).getDataBase(); - MultiFileHashMapUtils.write(fileForWrite, mapForWrite); - } - - if (((MultiFileHashMap) shell).getMFHMState().getFlag() == 0) { - ((MultiFileHashMap) shell).getMFHMState().changeFlag(); - } + Map tmpDataBase = inState.getTable(args[0]).getDataBase(); + File tmpDataFile = inState.getTable(args[0]).getDataFile(); + MultiFileHashMapUtils.read(tmpDataFile, tmpDataBase); + inState.setCurrentTable(args[0], tmpDataBase, tmpDataFile); - ((MultiFileHashMap) shell).getMFHMState().setCurrentTable(args[0]); System.out.println("using " + args[0]); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMap.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMap.java deleted file mode 100644 index 1a4703e06..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMap.java +++ /dev/null @@ -1,21 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; - -import java.io.File; -import java.io.IOException; - -public class MultiFileHashMap extends Shell { - - private MultiFileHashMapState state; - - public MultiFileHashMap(final File currentFile) throws IOException { - - state = new MultiFileHashMapState(currentFile); - } - - public final MultiFileHashMapState getMFHMState() { - - return state; - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExecutor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExecutor.java index b25743893..678fbdb79 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExecutor.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExecutor.java @@ -3,7 +3,7 @@ import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor; -public class MultiFileHashMapExecutor extends Executor { +public class MultiFileHashMapExecutor extends Executor { public MultiFileHashMapExecutor() { @@ -11,7 +11,7 @@ public MultiFileHashMapExecutor() { } @Override -public final void list() { + public void list() { Command create = new CmdCreate(); mapOfCmd.put(create.getName(), create); @@ -21,6 +21,14 @@ public final void list() { mapOfCmd.put(use.getName(), use); Command show = new CmdShowTables(); mapOfCmd.put(show.getName(), show); + + Command commit = new CmdCommit(); + mapOfCmd.put(commit.getName(), commit); + Command rollback = new CmdRollback(); + mapOfCmd.put(rollback.getName(), rollback); + Command size = new CmdSize(); + mapOfCmd.put(size.getName(), size); + Command exit = new MultiFileHashMapExit(); mapOfCmd.put(exit.getName(), exit); Command get = new MultiFileHashMapGet(); @@ -31,5 +39,6 @@ public final void list() { mapOfCmd.put(remove.getName(), remove); Command list = new MultiFileHashMapList(); mapOfCmd.put(list.getName(), list); + } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExit.java index bc26dee94..a4de4a567 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExit.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExit.java @@ -1,32 +1,22 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; -import java.io.File; import java.io.IOException; -import java.util.Map; -public class MultiFileHashMapExit implements Command { +public class MultiFileHashMapExit implements Command { @Override - public final String getName() { + public String getName() { + return "exit"; } @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - if (args.length != 0) { - System.out.println("incorrect number of arguments"); - return; - } + public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - if (((MultiFileHashMap) shell).getMFHMState().getFlag() == 1) { - File fileForWrite = ((MultiFileHashMapTable) - ((MultiFileHashMap) shell).getMFHMState().getCurrentTable()).getDataFile(); - Map mapForWrite = ((MultiFileHashMapTable) - ((MultiFileHashMap) shell).getMFHMState().getCurrentTable()).getDataBase(); - MultiFileHashMapUtils.write(fileForWrite, mapForWrite); + if (inState.getCurrentTable() != null) { + inState.getCurrentTable().commit(); } System.exit(0); } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapGet.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapGet.java index 19c4226eb..d89e6ea26 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapGet.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapGet.java @@ -1,30 +1,29 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; import java.io.IOException; -public class MultiFileHashMapGet implements Command { +public class MultiFileHashMapGet implements Command { @Override - public final String getName() { + public String getName() { return "get"; } @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { if (args.length != 1) { System.out.println("incorrect number of arguments"); return; } - if (((MultiFileHashMap) shell).getMFHMState().getCurrentTable() == null) { + if (inState.getCurrentTable() == null) { System.out.println("no table"); return; } - String value = ((MultiFileHashMap) shell).getMFHMState().getFromCurrentTable(args[0]); + String value = inState.getFromCurrentTable(args[0]); if (null == value) { System.out.println("not found"); } else { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapList.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapList.java index 983019841..90f3b6ee1 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapList.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapList.java @@ -1,12 +1,11 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; import java.io.IOException; -import java.util.Set; +import java.util.List; -public class MultiFileHashMapList implements Command { +public class MultiFileHashMapList implements Command { @Override public final String getName() { @@ -14,17 +13,17 @@ public final String getName() { } @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public final void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { if (args.length != 0) { System.out.println("incorrect number of arguments"); return; } - if (((MultiFileHashMap) shell).getMFHMState().getCurrentTable() == null) { + if (inState.getCurrentTable() == null) { System.out.println("no table"); return; } - Set keys = ((MultiFileHashMap) shell).getMFHMState().getDataBaseFromCurrentTable().keySet(); + List keys = inState.getCurrentTableListOfKeys(); System.out.println(String.join(", ", keys)); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapMain.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapMain.java index ba73a50e1..c34dc6447 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapMain.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapMain.java @@ -1,23 +1,24 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; + import java.io.File; import java.io.IOException; public class MultiFileHashMapMain { - public static void main(final String[] args) throws IOException { + public static void main(String[] args) throws IOException { String currentProperty = System.getProperty("fizteh.db.dir"); try { - if (currentProperty == null) { throw new IOException("Working directory is not specified"); } if (!new File(currentProperty).exists()) { - throw new IOException("Working directory does not exist"); + throw new IOException("Working directory does not exist"); } if (!new File(currentProperty).isDirectory()) { @@ -27,29 +28,24 @@ public static void main(final String[] args) throws IOException { System.err.println(e.getMessage()); System.exit(1); } - File base = new File(currentProperty); - if (!base.exists()) { - base.createNewFile(); - } - - try { + if (!base.exists()) { + base.createNewFile(); + } + base = base.getCanonicalFile(); - } catch (Exception e) { - System.err.println(e.getMessage()); - System.exit(1); - } - MultiFileHashMap mfhm = new MultiFileHashMap(base); - MultiFileHashMapExecutor exec = new MultiFileHashMapExecutor(); + MultiFileHashMapState startState = new MultiFileHashMapState(base); + Shell mfhm = new Shell(startState); + + MultiFileHashMapExecutor exec = new MultiFileHashMapExecutor(); - try { if (args.length > 0) { mfhm.batchState(args, exec); } else { mfhm.interactiveState(exec); } - } catch (IOException e) { + } catch (Exception e) { System.err.println(e.getMessage()); System.exit(1); } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapPut.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapPut.java index 9efa71c53..dc6acb6d7 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapPut.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapPut.java @@ -1,30 +1,29 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; import java.io.IOException; -public class MultiFileHashMapPut implements Command { +public class MultiFileHashMapPut implements Command { @Override - public final String getName() { + public String getName() { return "put"; } @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { if (args.length != 2) { System.out.println("incorrect number of arguments"); return; } - if (((MultiFileHashMap) shell).getMFHMState().getCurrentTable() == null) { + if (inState.getCurrentTable() == null) { System.out.println("no table"); return; } - String value = ((MultiFileHashMap) shell).getMFHMState().putToCurrentTable(args[0], args[1]); + String value = inState.putToCurrentTable(args[0], args[1]); if (null == value) { System.out.println("new"); } else { @@ -33,3 +32,4 @@ public final void executeCmd(final Shell shell, final String[] args) throws IOEx } } } + diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapRemove.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapRemove.java index 45c3ba9e0..e07b8ece0 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapRemove.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapRemove.java @@ -1,29 +1,29 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; import java.io.IOException; -public class MultiFileHashMapRemove implements Command { +public class MultiFileHashMapRemove implements Command { @Override - public final String getName() { + public String getName() { + return "remove"; } @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { if (args.length != 1) { System.out.println("incorrect number of arguments"); return; } - if (((MultiFileHashMap) shell).getMFHMState().getCurrentTable() == null) { + if (inState.getCurrentTable() == null) { System.out.println("no table"); return; } - String value = ((MultiFileHashMap) shell).getMFHMState().removeFromCurrentTable(args[0]); + String value = inState.removeFromCurrentTable(args[0]); if (null == value) { System.out.println("not found"); } else { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapState.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapState.java index 1ac716cbb..fc5a89624 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapState.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapState.java @@ -2,6 +2,7 @@ import java.io.File; import java.io.IOException; +import java.util.List; import java.util.Map; import java.util.Set; @@ -9,74 +10,67 @@ public class MultiFileHashMapState { private MultiFileHashMapTableProvider provider; private MultiFileHashMapTable currentTable; - private int flag; //flag = 0 - we`re not working with any table, 1 - we are - public MultiFileHashMapState(final File inFile) throws IOException { + public MultiFileHashMapState(File inFile) throws IOException { - flag = 0; currentTable = null; MultiFileHashMapTableProviderFactory factory = new MultiFileHashMapTableProviderFactory(); provider = factory.create(inFile.getPath()); } - public final int getFlag() { + public int getChangesBaseSize() { - return flag; + return currentTable.getChangesBaseSize(); } - public final void changeFlag() { + public MultiFileHashMapTable createTable(String name) throws IOException { - if (0 == flag) { - flag = 1; - } else { - flag = 0; - } + MultiFileHashMapTable tmp = provider.createTable(name); + return tmp; } - - public final MultiFileHashMapTable createTable(final String name) throws IOException { - - return provider.createTable(name); - } - - public final MultiFileHashMapTable getTable(final String name) throws IOException { + public MultiFileHashMapTable getTable(String name) throws IOException { return provider.getTable(name); } - public final MultiFileHashMapTable getCurrentTable() throws IOException { + public MultiFileHashMapTable getCurrentTable() throws IOException { return currentTable; } - public final void setCurrentTable(final String name) throws IOException { + public void setCurrentTable(String name, Map inMap, File inFile) throws IOException { currentTable = provider.getTable(name); + currentTable.changeCurrentTable(inMap, inFile); } - public final void setCurrentTable() throws IOException { - currentTable = null; - } + public void deleteTable(String name) throws IOException { - public final void deleteTable(final String name) throws IOException { provider.removeTable(name); + currentTable = null; } - public final String putToCurrentTable(final String key, final String value) { + public String putToCurrentTable(String key, String value) { + return currentTable.put(key, value); } - public final String getFromCurrentTable(final String key) { + public String getFromCurrentTable(String key) { + return currentTable.get(key); } - public final String removeFromCurrentTable(final String key) { + public String removeFromCurrentTable(String key) { + return currentTable.remove(key); } - public final Map getDataBaseFromCurrentTable() { - return currentTable.getDataBase(); - } - public final Set getTableSet() { + + public Set getTableSet() { return provider.getTables(); } + + public List getCurrentTableListOfKeys() { + return currentTable.list(); + } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTable.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTable.java index 85770352d..5f6a6976e 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTable.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTable.java @@ -1,48 +1,176 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +import ru.fizteh.fivt.storage.strings.Table; + import java.io.File; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; +import java.util.*; -public class MultiFileHashMapTable { +public class MultiFileHashMapTable implements Table { private Map dataBase; + private Map changesBase; private File dataFile; + private int sizeTable; - public MultiFileHashMapTable(final File currentFile) throws IOException { + public MultiFileHashMapTable(File currentFile) { - dataBase = new HashMap<>(); + dataBase = new HashMap(); + changesBase = new HashMap(); dataFile = currentFile; - MultiFileHashMapUtils.read(currentFile, dataBase); } - public final Map getDataBase() { + public int getChangesBaseSize() { + + return changesBase.size(); + } + + public Map getDataBase() { + return dataBase; } - public final File getDataFile() { + public File getDataFile() { + return dataFile; } - public final String getName() { + public void changeCurrentTable(Map inMap, File inFile) { + + dataBase = inMap; + dataFile = inFile; + sizeTable = dataBase.size(); + } + + @Override + public String getName() { return dataFile.getName(); } - public final String get(final String key) { + @Override + public String get(String key) { + + if (key == null) { + throw new IllegalArgumentException("Incorrect key to get."); + } + String newKey = key.trim(); + if (newKey.isEmpty()) { + throw new IllegalArgumentException("Incorrect key to get"); + } + String returnValue; + if (changesBase.containsKey(newKey)) { + if (changesBase.get(newKey) == null) { + returnValue = null; + } else { + returnValue = changesBase.get(newKey); + } + } else { + if (dataBase.containsKey(newKey)) { + returnValue = dataBase.get(newKey); + } else { + returnValue = null; + } + } + + return returnValue; + } + + @Override + public String put(String key, String value) { + + if (key == null || value == null) { + throw new IllegalArgumentException("Incorrect key or value to put."); + } + + String newKey = key.trim(); + String newValue = value.trim(); + if (newKey.isEmpty() || newValue.isEmpty()) { + throw new IllegalArgumentException("Incorrect key or value to put"); + } + + if ((!changesBase.containsKey(newKey) && !dataBase.containsKey(newKey)) + || (changesBase.containsKey(newKey) && changesBase.get(newKey) == null)) { + ++sizeTable; + } + String result = get(newKey); + changesBase.put(newKey, newValue); + if (value.equals(dataBase.get(newKey))) { + changesBase.remove(newKey); + } + + return result; + } - return dataBase.get(key); + @Override + public String remove(String key) { + + if (key == null) { + throw new IllegalArgumentException("Incorrect key to remove."); + } + + String newKey = key.trim(); + if (newKey.isEmpty()) { + throw new IllegalArgumentException("Incorrect key to remove"); + } + + if (changesBase.get(newKey) != null || (!changesBase.containsKey(newKey) && dataBase.get(newKey) != null)) { + --sizeTable; + } + String result = get(newKey); + changesBase.put(newKey, null); + if (dataBase.get(newKey) == null) { + changesBase.remove(newKey); + } + return result; } - public final String put(final String key, final String value) { + @Override + public int size() { - return dataBase.put(key, value); + return sizeTable; } - public final String remove(final String key) { + @Override + public int commit() { + + int size = changesBase.size(); + try { + if (size != 0) { + Set> set = changesBase.entrySet(); + for (Map.Entry pair : set) { + pair.getKey(); + if (pair.getValue() == null) { + dataBase.remove(pair.getKey()); + } else { + dataBase.put(pair.getKey(), pair.getValue()); + } + } + MultiFileHashMapUtils.write(dataFile, dataBase); + } + } catch (IOException e) { + System.err.println(e); + } + + changesBase.clear(); + sizeTable = dataBase.size(); + return size; + } + + @Override + public int rollback() { - return dataBase.remove(key); + int size = changesBase.size(); + changesBase.clear(); + sizeTable = dataBase.size(); + return size; } + @Override + public List list() { + List keys = new ArrayList(dataBase.keySet()); + Collections.sort(keys); + return keys; + + } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProvider.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProvider.java index 2dd627ab6..4bb13f6d7 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProvider.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProvider.java @@ -1,59 +1,104 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +import ru.fizteh.fivt.storage.strings.Table; +import ru.fizteh.fivt.storage.strings.TableProvider; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Rm; + import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; -import static ru.fizteh.fivt.students.ivan_ivanov.shell.Rm.remove; - -public class MultiFileHashMapTableProvider { +public class MultiFileHashMapTableProvider implements TableProvider { private Map mapOfTables; private File currentDir; - public MultiFileHashMapTableProvider(final File inDir) throws IOException { + public MultiFileHashMapTableProvider(File inDir) { - mapOfTables = new HashMap<>(); + if (inDir == null) { + throw new IllegalArgumentException("null directory"); + } + if (!inDir.isDirectory()) { + throw new IllegalArgumentException("not a directory"); + } + mapOfTables = new HashMap(); currentDir = inDir; File[] fileMas = currentDir.listFiles(); if (fileMas.length != 0) { - for (int i = 0; i < fileMas.length; ++i) { - if (fileMas[i].isDirectory()) { - mapOfTables.put(fileMas[i].getName(), new MultiFileHashMapTable(fileMas[i])); + for (File fileMa : fileMas) { + if (fileMa.isDirectory()) { + mapOfTables.put(fileMa.getName(), new MultiFileHashMapTable(fileMa)); } } } } - public final MultiFileHashMapTable getTable(final String name) throws IOException { + @Override + public MultiFileHashMapTable getTable(String name) { + + if (name == null) { + throw new IllegalArgumentException("null name to get"); + } + if (!name.matches("[a-zA-Zа-яА-Я0-9]+")) { + throw new IllegalArgumentException("incorrect name to get"); + } if (!mapOfTables.containsKey(name)) { return null; } - return new MultiFileHashMapTable(new File(currentDir, name)); + + return mapOfTables.get(name); } - public final MultiFileHashMapTable createTable(final String name) throws IOException { + @Override + public MultiFileHashMapTable createTable(String name) { + + if (name == null) { + throw new IllegalArgumentException("null name to create"); + } + if (!name.matches("[a-zA-Zа-яА-Я0-9]+")) { + throw new IllegalArgumentException("incorrect name to create"); + } File dirOfTable = new File(currentDir, name); if (!dirOfTable.mkdir()) { return null; } + MultiFileHashMapTable table = new MultiFileHashMapTable(dirOfTable); + + Table tmp = mapOfTables.get(name); + if (tmp != null) { + return null; + } mapOfTables.put(name, table); return table; + } - public final void removeTable(final String name) throws IOException { + @Override + public void removeTable(String name) { + + if (name == null || !name.matches("[a-zA-Zа-яА-Я0-9]+")) { + throw new IllegalArgumentException("incorrect table name to remove"); + } + if (!mapOfTables.containsKey(name)) { + throw new IllegalStateException("table doesn't exist"); + } File dirOfTable = new File(currentDir, name); - remove(dirOfTable.getCanonicalFile().toPath()); + try { + Rm.remove(dirOfTable.getCanonicalFile().toPath()); + } catch (IOException e) { + System.err.println(e); + } + mapOfTables.remove(name); } - public final Set getTables() { + public Set getTables() { Set tables = mapOfTables.keySet(); return tables; } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactory.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactory.java index 5fab52900..e0beaccc4 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactory.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactory.java @@ -1,12 +1,23 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +import ru.fizteh.fivt.storage.strings.TableProviderFactory; import java.io.File; -import java.io.IOException; -public class MultiFileHashMapTableProviderFactory { +public class MultiFileHashMapTableProviderFactory implements TableProviderFactory { - public final MultiFileHashMapTableProvider create(final String dir) throws IOException { - return new MultiFileHashMapTableProvider(new File(dir)); + @Override + public MultiFileHashMapTableProvider create(String dir) { + + if (dir == null) { + throw new IllegalArgumentException("null directory"); + } + if (dir.trim().isEmpty()) { + throw new IllegalArgumentException("empty directory"); + } + + File file = new File(dir); + + return new MultiFileHashMapTableProvider(file); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactoryTest.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactoryTest.java new file mode 100644 index 000000000..c23c9108d --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactoryTest.java @@ -0,0 +1,43 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; + +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; + +public class MultiFileHashMapTableProviderFactoryTest { + + private static File database; + private MultiFileHashMapTableProviderFactory tableProviderFactory = new MultiFileHashMapTableProviderFactory(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + database = new File("newNotExistedTable").getCanonicalFile(); + if (database.isFile()) { + database.delete(); + } + database.mkdir(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + database.delete(); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateNull() throws Exception { + tableProviderFactory.create(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateEmpty() throws Exception { + tableProviderFactory.create(" "); + } + + @Test + public void testCreateNotExisted() throws Exception { + Assert.assertNotNull(tableProviderFactory.create("newNotExistedTable")); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderTest.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderTest.java new file mode 100644 index 000000000..c47ea4235 --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderTest.java @@ -0,0 +1,111 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; + +public class MultiFileHashMapTableProviderTest { + private static File database; + private static MultiFileHashMapTableProvider tableProvider; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + database = new File("javatest").getCanonicalFile(); + database.mkdir(); + MultiFileHashMapTableProviderFactory factory = new MultiFileHashMapTableProviderFactory(); + tableProvider = factory.create(database.toString()); + } + + @Before + public void setUp() throws Exception { + tableProvider.createTable("existingTable"); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetTableNull() throws Exception { + + tableProvider.getTable(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetTableEmpty() throws Exception { + + tableProvider.getTable(""); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetTableIncorrect() throws Exception { + + tableProvider.getTable("$#*_%"); + } + + @Test + public void testGetTableExisted() throws Exception { + + Assert.assertEquals("existingTable", tableProvider.getTable("existingTable").getName()); + } + + @Test + public void testGetTableNotExisted() throws Exception { + + Assert.assertNull(tableProvider.getTable("notExistingTable")); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateTableNull() throws Exception { + + tableProvider.createTable(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateTableEmpty() throws Exception { + + tableProvider.createTable(""); + } + + @Test(expected = IllegalArgumentException.class) + public void testCreateTableIncorrect() throws Exception { + + tableProvider.createTable("$#*_%"); + } + + @Test + public void testCreateTableExisted() throws Exception { + + Assert.assertNull(tableProvider.createTable("existingTable")); + } + + @Test(expected = IllegalStateException.class) + public void testRemoveTableNotExisted() throws Exception { + + tableProvider.removeTable("notExistingTable"); + } + + @Test + public void testRemoveTableExisted() throws Exception { + + tableProvider.removeTable("existingTable"); + } + + @Test + public void testCreateTableNotExisted() throws Exception { + + Assert.assertNotNull(tableProvider.createTable("notExistingTable")); + tableProvider.removeTable("notExistingTable"); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveTableNull() throws Exception { + + tableProvider.removeTable(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveTableEmpty() throws Exception { + + tableProvider.removeTable(""); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableTest.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableTest.java new file mode 100644 index 000000000..98e13e29a --- /dev/null +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableTest.java @@ -0,0 +1,140 @@ +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; + +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import static junit.framework.Assert.*; + +public class MultiFileHashMapTableTest { + + private static MultiFileHashMapTable table; + + @Before + public void setUp() throws Exception { + File file = new File("testingTable"); + file.mkdir(); + table = new MultiFileHashMapTable(file); + } + + @Test + public void testGetName() throws Exception { + + assertEquals("testingTable", table.getName()); + } + + @Test + public void testGetInEnglish() throws Exception { + + table.put("getEnglishKey", "getEnglishValue"); + assertEquals("getEnglishValue", table.get("getEnglishKey")); + } + + @Test + public void testGetInRussian() throws Exception { + + table.put("ключ", "значение"); + assertEquals("значение", table.get("ключ")); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetNull() throws Exception { + + table.get(null); + } + + @Test + public void testPutNew() throws Exception { + + assertNull(table.put("putNewKey", "putNewValue")); + table.remove("putNewKey"); + } + + @Test(expected = IllegalArgumentException.class) + public void testPutNullKey() throws Exception { + + table.put(null, "valueOfNullKey"); + } + + @Test(expected = IllegalArgumentException.class) + public void testPutEmptyKey() throws Exception { + + table.put("", "valueOfEmptyKey"); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveNull() throws Exception { + + table.remove(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testRemoveWhitespace() throws Exception { + table.remove(" "); + } + + @Test(expected = IllegalArgumentException.class) + public void testGetWhitespace() throws Exception { + table.get(" "); + } + + @Test + public void testRemoveExisted() throws Exception { + + table.put("Key", "Value"); + assertEquals("Value", table.remove("Key")); + } + + @Test + public void testRemoveNotExisted() throws Exception { + + assertNull(table.remove("Key")); + } + + @Test + public void testSize() throws Exception { + table.put("Key1", "Value1"); + table.put("Key2", "Value2"); + table.put("Key3", "Value3"); + assertEquals(3, table.size()); + } + + @Test + public void testPutAndRollback() throws Exception { + table.put("Key1", "Value1"); + table.put("Key2", "Value2"); + table.put("Key3", "Value3"); + assertEquals(3, table.rollback()); + } + + @Test + public void testPutCommit() throws Exception { + table.put("Key1", "Value1"); + table.put("Key2", "Value2"); + table.put("Key3", "Value3"); + assertEquals(3, table.commit()); + } + + @Test + public void testRemovePutCommit() throws Exception { + table.put("Key", "Value"); + table.commit(); + table.remove("Key"); + table.put("Key", "Value"); + assertEquals(0, table.commit()); + } + + @Test + public void testList() throws Exception { + table.put("Key1", "Value1"); + table.put("Key2", "Value2"); + table.put("Key3", "Value3"); + table.commit(); + List ans = new ArrayList(Arrays.asList("Key1", "Key2", "Key3")); + assertEquals(ans, table.list()); + } +} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapUtils.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapUtils.java index 702042a9c..cbb3b0982 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapUtils.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapUtils.java @@ -1,7 +1,7 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; import ru.fizteh.fivt.students.ivan_ivanov.filemap.FileMapState; -import ru.fizteh.fivt.students.ivan_ivanov.filemap.Utils; +import ru.fizteh.fivt.students.ivan_ivanov.filemap.FileMapUtils; import java.io.File; import java.io.IOException; @@ -11,11 +11,9 @@ public class MultiFileHashMapUtils { - private static final int MAX = 16; + public static void read(File currentDir, Map currentMap) throws IOException { - public static void read(final File currentDir, final Map currentMap) throws IOException { - - for (int directNumber = 0; directNumber < MAX; ++directNumber) { + for (int directNumber = 0; directNumber < 16; ++directNumber) { File subDir = new File(currentDir, directNumber + ".dir"); if (!subDir.exists()) { continue; @@ -24,19 +22,20 @@ public static void read(final File currentDir, final Map current throw new IOException(subDir.getName() + "isn't directory"); } - for (int fileNumber = 0; fileNumber < MAX; ++fileNumber) { + for (int fileNumber = 0; fileNumber < 16; ++fileNumber) { File currentFile = new File(subDir, fileNumber + ".dat"); if (!currentFile.exists()) { continue; } FileMapState state = new FileMapState(currentFile); state.setDataBase(currentMap); - Utils.readDataBase(state); + FileMapUtils.readDataBase(state); } } } - public static void deleteDirectory(final File directory) throws IOException { + public static void deleteDirectory(File directory) throws IOException { + File[] files = directory.listFiles(); if (files != null) { for (File f : files) { @@ -49,22 +48,22 @@ public static void deleteDirectory(final File directory) throws IOException { } } - public static void write(final File currentDir, final Map currentMap) throws IOException { + public static void write(File currentDir, Map currentMap) throws IOException { - Map[][] arrayOfMap = new Map[MAX][MAX]; + Map[][] arrayOfMap = new Map[16][16]; for (String key : currentMap.keySet()) { int byteOfKey = key.getBytes(StandardCharsets.UTF_8)[0]; - int nDirectory = byteOfKey % MAX; - int nFile = byteOfKey / MAX % MAX; + int nDirectory = Math.abs(byteOfKey) % 16; + int nFile = Math.abs(byteOfKey) / 16 % 16; if (arrayOfMap[nDirectory][nFile] == null) { arrayOfMap[nDirectory][nFile] = new HashMap(); } arrayOfMap[nDirectory][nFile].put(key, currentMap.get(key)); } - for (int i = 0; i < MAX; i++) { + for (int i = 0; i < 16; i++) { File dir = new File(currentDir, i + ".dir"); - for (int j = 0; j < MAX; j++) { + for (int j = 0; j < 16; j++) { File file = new File(dir, j + ".dat"); if (null == arrayOfMap[i][j]) { if (file.exists()) { @@ -80,7 +79,7 @@ public static void write(final File currentDir, final Map curren if (!file.exists()) { file.createNewFile(); } - Utils.write(arrayOfMap[i][j], file); + FileMapUtils.write(arrayOfMap[i][j], file); } if (dir.exists()) { @@ -89,6 +88,5 @@ public static void write(final File currentDir, final Map curren } } } - } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java index 002be3b83..e6bd24a98 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java @@ -6,17 +6,19 @@ import java.io.IOException; import java.nio.file.Path; -public class Cat implements Command { - public final String getName() { +public class Cat implements Command { + + public String getName() { return "cat"; } + public static void catenate(final String fileName) { String buffer = ""; try (FileReader filescan = new FileReader(fileName); - BufferedReader bufreader = new BufferedReader(filescan)) { + BufferedReader bufreader = new BufferedReader(filescan)) { while ((buffer = bufreader.readLine()) != null) { System.out.println(buffer); } @@ -25,11 +27,11 @@ public static void catenate(final String fileName) { return; } catch (IOException e) { System.out.println("Can't read file"); - } + } } - public final void executeCmd(final Shell shell, final String[] args) throws IOException { - Path thePath = shell.getState().getPath().resolve(args[0]); + public void executeCmd(ShellState inState, String[] args) throws IOException { + Path thePath = inState.getPath().resolve(args[0]); try { if (1 == args.length) { if (thePath.toFile().exists()) { @@ -44,9 +46,7 @@ public final void executeCmd(final Shell shell, final String[] args) throws IOEx while (!thePath.toFile().isDirectory()) { thePath = thePath.getParent(); } - shell.setState(thePath); + inState.setPath(thePath); } } } - - diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java index 76f328779..d62885517 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java @@ -4,22 +4,22 @@ import java.io.IOException; import java.nio.file.Path; -public class Cd implements Command { +public class Cd implements Command { - public final String getName() { + public String getName() { return "cd"; } - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(ShellState inState, String[] args) throws IOException { if (1 == args.length) { - Path thePath = shell.getState().getPath().resolve(args[0]); + Path thePath = inState.getPath().resolve(args[0]); File theFile = new File(thePath.toString()); if (!theFile.isDirectory()) { throw new IOException("Directory doesn't exist"); } - shell.setState(thePath.normalize()); + inState.setPath(thePath.normalize()); } else { throw new IOException("not allowed number of arguments"); } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java index 46e978711..d8b13745c 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java @@ -2,9 +2,8 @@ import java.io.IOException; -public interface Command { +public interface Command { String getName(); - - void executeCmd(Shell shell, String[] args) throws IOException; + void executeCmd(State shell, String[] args) throws IOException; } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java index 12e4f59ea..480e2014f 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java @@ -5,17 +5,18 @@ import java.nio.file.Files; import java.nio.file.Path; -public class Cp implements Command { +public class Cp implements Command { - public final String getName() { + public String getName() { return "cp"; } - private void copy(final Path source, final Path target) throws IOException { + private void copy(Path source, Path target) throws IOException { + target = target.resolve(source.getFileName()); if (source.toFile().isFile()) { - Files.copy(source, target.resolve(source.getFileName())); + Files.copy(source, target); } else { File[] masOfSource = source.toFile().listFiles(); target.toFile().mkdir(); @@ -25,11 +26,11 @@ private void copy(final Path source, final Path target) throws IOException { } } - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(ShellState inState, String[] args) throws IOException { if (2 == args.length) { - Path source = shell.getState().getPath().resolve(args[0]).normalize(); - Path target = shell.getState().getPath().resolve(args[1]).normalize(); + Path source = inState.getPath().resolve(args[0]).normalize(); + Path target = inState.getPath().resolve(args[1]).normalize(); if (source.toFile().isFile() && target.toFile().isFile()) { throw new IOException("not allowed to copy file to file"); } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Executor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Executor.java index f82645080..ed3deb5ad 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Executor.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Executor.java @@ -4,11 +4,11 @@ import java.util.HashMap; import java.util.Map; -public abstract class Executor { +public abstract class Executor { public Map mapOfCmd = new HashMap(); - final String[] argsCheck(final String inCommand) { + public String[] argsCheck(String inCommand) { int space = inCommand.indexOf(" "); if (-1 == space) { @@ -18,7 +18,7 @@ final String[] argsCheck(final String inCommand) { return substr.trim().split("\\ "); } - public final String cmdCheck(final String cmd) { + public String cmdCheck(String cmd) { String tmp; int space = cmd.indexOf(" "); @@ -31,11 +31,11 @@ public final String cmdCheck(final String cmd) { public abstract void list(); - final void execute(final Shell shell, final String cmd) throws IOException { + public void execute(State state, String cmd) throws IOException { if (!mapOfCmd.containsKey(cmdCheck(cmd))) { throw new IOException("Can't find key"); } - mapOfCmd.get(cmdCheck(cmd)).executeCmd(shell, argsCheck(cmd)); + mapOfCmd.get(cmdCheck(cmd)).executeCmd(state, argsCheck(cmd)); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java index 5825a3875..36773e7a4 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java @@ -2,16 +2,17 @@ import java.io.IOException; -public class Exit implements Command { +public class Exit implements Command { @Override - public final String getName() { + public String getName() { return "exit"; } @Override - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(ShellState inState, String[] args) throws IOException { + System.exit(0); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java index d66826e57..72868c94d 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java @@ -4,17 +4,17 @@ import java.io.IOException; import java.io.PrintStream; -public class Ls implements Command { +public class Ls implements Command { public final String getName() { return "ls"; } - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public final void executeCmd(ShellState inState, String[] args) throws IOException { if (0 == args.length) { - File currentDirectory = new File(shell.getState().getPath().toString()); + File currentDirectory = new File(inState.getPath().toString()); File[] listOfFiles = currentDirectory.listFiles(); PrintStream print = new PrintStream(System.out); if (listOfFiles != null) { @@ -23,7 +23,7 @@ public final void executeCmd(final Shell shell, final String[] args) throws IOEx } } } else { - throw new IOException("not allowed number of arguments"); + throw new IOException("incorrect number of arguments"); } } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java index 2b657f595..3066faf9a 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java @@ -3,20 +3,20 @@ import java.io.File; import java.io.IOException; -public class Mkdir implements Command { +public class Mkdir implements Command { - public final String getName() { + public String getName() { return "mkdir"; } - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(ShellState inState, String[] args) throws IOException { if (1 == args.length) { - File theFile = new File(shell.getState().getPath().resolve(args[0]).toString()); + File theFile = new File(inState.getPath().resolve(args[0]).toString()); theFile.mkdir(); } else { - throw new IOException("Not correct name of directory"); + throw new IOException("Inorrect name of directory"); } } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java index 3cd84da40..4843dfa7e 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java @@ -1,18 +1,19 @@ package ru.fizteh.fivt.students.ivan_ivanov.shell; + import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -public class Mv implements Command { +public class Mv implements Command { - public final String getName() { + public String getName() { return "mv"; } - private void move(final Path source, final Path target) throws IOException { + private void move(Path source, Path target) throws IOException { if (source.toFile().isFile()) { Files.copy(source, target); @@ -26,11 +27,11 @@ private void move(final Path source, final Path target) throws IOException { source.toFile().delete(); } - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(ShellState inState, String[] args) throws IOException { if (2 == args.length) { - Path source = shell.getState().getPath().resolve(args[0]).normalize(); - Path target = shell.getState().getPath().resolve(args[1]).normalize(); + Path source = inState.getPath().resolve(args[0]).normalize(); + Path target = inState.getPath().resolve(args[1]).normalize(); if (source.toFile().isFile() && target.toFile().isFile()) { throw new IOException("not allowed to move file to file"); } @@ -68,10 +69,10 @@ public final void executeCmd(final Shell shell, final String[] args) throws IOEx } else { throw new IOException("not allowed number of arguments"); } - Path path = shell.getState().getPath(); + Path path = inState.getPath(); while (!path.toFile().isDirectory()) { path = path.getParent(); } - shell.setState(path); + inState.setPath(path); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java index cf91c4686..9c37e815f 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java @@ -2,18 +2,18 @@ import java.io.IOException; -public class Pwd implements Command { +public class Pwd implements Command { - public final String getName() { + public String getName() { return "pwd"; } - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(ShellState inState, String[] args) throws IOException { if (args.length > 0) { throw new IOException("too many args"); } - System.out.println(shell.getState().getPath().toString()); + System.out.println(inState.getPath().toString()); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java index 0238b43d9..8151a94db 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java @@ -4,14 +4,14 @@ import java.io.IOException; import java.nio.file.Path; -public class Rm implements Command { +public class Rm implements Command { - public final String getName() { + public String getName() { return "rm"; } - public static void remove(final Path thePath) throws IOException { + public static void remove(Path thePath) throws IOException { File theFile = thePath.toFile(); if (theFile.isFile()) { @@ -32,9 +32,9 @@ public static void remove(final Path thePath) throws IOException { } } - public final void executeCmd(final Shell shell, final String[] args) throws IOException { + public void executeCmd(ShellState inState, String[] args) throws IOException { - Path thePath = shell.getState().getPath().resolve(args[0]); + Path thePath = inState.getPath().resolve(args[0]); try { if (1 == args.length) { if (thePath.toFile().exists()) { @@ -49,7 +49,7 @@ public final void executeCmd(final Shell shell, final String[] args) throws IOEx while (!thePath.toFile().isDirectory()) { thePath = thePath.getParent(); } - shell.setState(thePath); + inState.setPath(thePath); } } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Shell.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Shell.java index 706ffb358..13ff061ac 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Shell.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Shell.java @@ -1,33 +1,18 @@ package ru.fizteh.fivt.students.ivan_ivanov.shell; import java.io.IOException; -import java.io.File; import java.util.Scanner; -import java.nio.file.Path; -public class Shell { +public class Shell { - private ShellState state; + private State state; - public Shell(final File currentDirectory) { - state = new ShellState(); - state.setPath((currentDirectory.getAbsoluteFile().toPath())); - } - - public Shell() { - } - + public Shell(State inState) { - final ShellState getState() { - return state; + state = inState; } - final void setState(final Path inState) { - - state.setPath(inState); - } - - public final void batchState(final String[] args, final Executor exec) throws IOException { + public void batchState(String[] args, Executor exec) throws IOException { StringBuilder tmp = new StringBuilder(); for (String arg : args) { @@ -38,35 +23,36 @@ public final void batchState(final String[] args, final Executor exec) throws IO String cmd = ""; - for (int i = 0; i < command.length; ++i) { + for (int i = 0; i < command.length - 1; ++i) { + cmd = command[i].trim(); + if (cmd.equals("exit")) { + break; + } try { - exec.execute(this, cmd); + exec.execute(state, cmd); } catch (Exception e) { System.err.println(e.getMessage()); + System.exit(1); } } - exec.execute(this, "exit"); } - public final void interactiveState(final Executor exec) throws IOException { - - try (Scanner scanner = new Scanner(System.in)) { + public void interactiveState(Executor exec) throws IOException { - String[] cmd; - while (true) { - System.out.print("$ "); - cmd = scanner.nextLine().trim().split("\\s*;\\s*"); + Scanner scanner = new Scanner(System.in); + String[] cmd; + while (true) { + System.out.print("$ "); + cmd = scanner.nextLine().trim().split("\\s*;\\s*"); - try { - for (String aCmd : cmd) { - exec.execute(this, aCmd); - } - } catch (Exception e) { - System.err.println(e.getMessage()); + try { + for (String aCmd : cmd) { + exec.execute(state, aCmd); } - + } catch (Exception e) { + System.err.println(e.getMessage()); } } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellExecutor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellExecutor.java index 5f44c191f..46e7f7b4e 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellExecutor.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellExecutor.java @@ -1,13 +1,13 @@ package ru.fizteh.fivt.students.ivan_ivanov.shell; -public class ShellExecutor extends Executor { +public class ShellExecutor extends Executor { public ShellExecutor() { list(); } - public final void list() { + public void list() { Command pwd = new Pwd(); mapOfCmd.put(pwd.getName(), pwd); @@ -19,13 +19,11 @@ public final void list() { mapOfCmd.put(cp.getName(), cp); Command mv = new Mv(); mapOfCmd.put(mv.getName(), mv); - Command ls = new Ls(); - mapOfCmd.put(ls.getName(), ls); Command rm = new Rm(); mapOfCmd.put(rm.getName(), rm); Command exit = new Exit(); mapOfCmd.put(exit.getName(), exit); - Command cat = new Cat(); - mapOfCmd.put(cat.getName(), cat); + Command ls = new Ls(); + mapOfCmd.put(ls.getName(), ls); } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellMain.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellMain.java index d70bd2043..7446beadb 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellMain.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellMain.java @@ -3,15 +3,13 @@ import java.io.File; import java.io.IOException; -final class ShellMain { +public class ShellMain { - private ShellMain() { - } - - public static void main(final String[] args) throws IOException { + public static void main(String[] args) throws IOException { File currentDirectory = new File(""); - Shell shell = new Shell(currentDirectory); + ShellState startState = new ShellState(currentDirectory); + Shell shell = new Shell(startState); ShellExecutor exec = new ShellExecutor(); if (args.length != 0) { shell.batchState(args, exec); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellState.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellState.java index 3f50ce8d5..ac8e0b849 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellState.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellState.java @@ -1,17 +1,23 @@ package ru.fizteh.fivt.students.ivan_ivanov.shell; +import java.io.File; import java.nio.file.Path; public class ShellState { private Path path; - final Path getPath() { + public ShellState(File inFile) { + + path = inFile.getAbsoluteFile().toPath(); + } + + public Path getPath() { return path; } - final void setPath(final Path inPath) { + public void setPath(Path inPath) { path = inPath; } From 28da5f7b2c3cbb87680cccf49d99236b7707a5ac Mon Sep 17 00:00:00 2001 From: IvanovIvanK Date: Thu, 25 Dec 2014 08:01:27 +0300 Subject: [PATCH 2/5] Fixed some --- .../{ => database}/CmdCommit.java | 9 ++++--- .../{ => database}/CmdCreate.java | 2 +- .../{ => database}/CmdDrop.java | 2 +- .../{ => database}/CmdRollback.java | 2 +- .../{ => database}/CmdShowTables.java | 2 +- .../{ => database}/CmdSize.java | 2 +- .../{ => database}/CmdUse.java | 2 +- .../{ => database}/MultiFileHashMapExit.java | 2 +- .../{ => database}/MultiFileHashMapGet.java | 2 +- .../{ => database}/MultiFileHashMapList.java | 2 +- .../{ => database}/MultiFileHashMapPut.java | 2 +- .../MultiFileHashMapRemove.java | 2 +- .../{ => database}/MultiFileHashMapState.java | 2 +- .../{ => database}/MultiFileHashMapTable.java | 8 +++--- .../MultiFileHashMapTableProvider.java | 10 ++++--- .../MultiFileHashMapTableProviderFactory.java | 2 +- .../{ => database}/MultiFileHashMapUtils.java | 27 +++++++++++-------- .../MultiFileHashMapExecutor.java | 3 ++- .../MultiFileHashMapMain.java | 3 ++- ...tiFileHashMapTableProviderFactoryTest.java | 3 ++- .../MultiFileHashMapTableProviderTest.java | 4 ++- .../{ => test}/MultiFileHashMapTableTest.java | 12 ++++++++- 22 files changed, 65 insertions(+), 40 deletions(-) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/CmdCommit.java (65%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/CmdCreate.java (88%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/CmdDrop.java (89%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/CmdRollback.java (88%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/CmdShowTables.java (92%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/CmdSize.java (88%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/CmdUse.java (94%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/MultiFileHashMapExit.java (87%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/MultiFileHashMapGet.java (92%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/MultiFileHashMapList.java (91%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/MultiFileHashMapPut.java (92%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/MultiFileHashMapRemove.java (92%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/MultiFileHashMapState.java (96%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/MultiFileHashMapTable.java (96%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/MultiFileHashMapTableProvider.java (91%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/MultiFileHashMapTableProviderFactory.java (88%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => database}/MultiFileHashMapUtils.java (74%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => interpreter}/MultiFileHashMapExecutor.java (90%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => interpreter}/MultiFileHashMapMain.java (90%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => test}/MultiFileHashMapTableProviderFactoryTest.java (87%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => test}/MultiFileHashMapTableProviderTest.java (91%) rename src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/{ => test}/MultiFileHashMapTableTest.java (89%) diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCommit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java similarity index 65% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCommit.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java index e4ef3c391..37189b410 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCommit.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; @@ -19,7 +19,10 @@ public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOEx System.out.println("no table"); return; } - - System.out.println(inState.getCurrentTable().commit()); + try { + System.out.println(inState.getCurrentTable().commit()); + } catch (RuntimeException e) { + throw new IOException(e.getMessage()); + } } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCreate.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java similarity index 88% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCreate.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java index 8eb1ceb0a..8ef69d4e9 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdCreate.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdDrop.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java similarity index 89% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdDrop.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java index ecc1d63b5..3dc957c48 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdDrop.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdRollback.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java similarity index 88% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdRollback.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java index aa727b9f8..d9889d748 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdRollback.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdShowTables.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java similarity index 92% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdShowTables.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java index 04e1921fe..4cdd8860a 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdShowTables.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdSize.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java similarity index 88% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdSize.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java index feae609a4..8817a2326 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdSize.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdUse.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java similarity index 94% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdUse.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java index 5e8926ae0..008a60da2 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/CmdUse.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java similarity index 87% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExit.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java index a4de4a567..fa0d57528 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExit.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapGet.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java similarity index 92% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapGet.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java index d89e6ea26..2fb9c5de6 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapGet.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapList.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java similarity index 91% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapList.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java index 90f3b6ee1..9785db10c 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapList.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapPut.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java similarity index 92% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapPut.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java index dc6acb6d7..590d1c91c 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapPut.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapRemove.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java similarity index 92% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapRemove.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java index e07b8ece0..3c93f974c 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapRemove.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapState.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapState.java similarity index 96% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapState.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapState.java index fc5a89624..a3d15398f 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapState.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapState.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import java.io.File; import java.io.IOException; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTable.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTable.java similarity index 96% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTable.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTable.java index 5f6a6976e..54be42cf4 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTable.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTable.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.storage.strings.Table; @@ -52,7 +52,7 @@ public String getName() { public String get(String key) { if (key == null) { - throw new IllegalArgumentException("Incorrect key to get."); + throw new IllegalArgumentException("Incorrect key to get"); } String newKey = key.trim(); if (newKey.isEmpty()) { @@ -132,7 +132,7 @@ public int size() { } @Override - public int commit() { + public int commit() throws RuntimeException { int size = changesBase.size(); try { @@ -149,7 +149,7 @@ public int commit() { MultiFileHashMapUtils.write(dataFile, dataBase); } } catch (IOException e) { - System.err.println(e); + throw new RuntimeException(e.getMessage()); } changesBase.clear(); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProvider.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTableProvider.java similarity index 91% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProvider.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTableProvider.java index 4bb13f6d7..93d6b1d80 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProvider.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTableProvider.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.storage.strings.Table; import ru.fizteh.fivt.storage.strings.TableProvider; @@ -12,6 +12,8 @@ public class MultiFileHashMapTableProvider implements TableProvider { + private static final String REG_EXP = "[a-zA-Zа-яА-Я0-9]+"; + private Map mapOfTables; private File currentDir; @@ -41,7 +43,7 @@ public MultiFileHashMapTable getTable(String name) { if (name == null) { throw new IllegalArgumentException("null name to get"); } - if (!name.matches("[a-zA-Zа-яА-Я0-9]+")) { + if (!name.matches(REG_EXP)) { throw new IllegalArgumentException("incorrect name to get"); } @@ -58,7 +60,7 @@ public MultiFileHashMapTable createTable(String name) { if (name == null) { throw new IllegalArgumentException("null name to create"); } - if (!name.matches("[a-zA-Zа-яА-Я0-9]+")) { + if (!name.matches(REG_EXP)) { throw new IllegalArgumentException("incorrect name to create"); } @@ -81,7 +83,7 @@ public MultiFileHashMapTable createTable(String name) { @Override public void removeTable(String name) { - if (name == null || !name.matches("[a-zA-Zа-яА-Я0-9]+")) { + if (name == null || !name.matches(REG_EXP)) { throw new IllegalArgumentException("incorrect table name to remove"); } if (!mapOfTables.containsKey(name)) { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactory.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTableProviderFactory.java similarity index 88% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactory.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTableProviderFactory.java index e0beaccc4..15a788bff 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactory.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapTableProviderFactory.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.storage.strings.TableProviderFactory; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapUtils.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapUtils.java similarity index 74% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapUtils.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapUtils.java index cbb3b0982..9998e9adf 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapUtils.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapUtils.java @@ -1,4 +1,4 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database; import ru.fizteh.fivt.students.ivan_ivanov.filemap.FileMapState; import ru.fizteh.fivt.students.ivan_ivanov.filemap.FileMapUtils; @@ -11,10 +11,15 @@ public class MultiFileHashMapUtils { + private static final int MAX_DIRS = 16; + private static final int MAX_FILES = 16; + private static final String DIR_SUFFIX = ".dir"; + private static final String FILE_SUFFIX = ".dat"; + public static void read(File currentDir, Map currentMap) throws IOException { - for (int directNumber = 0; directNumber < 16; ++directNumber) { - File subDir = new File(currentDir, directNumber + ".dir"); + for (int directNumber = 0; directNumber < MAX_DIRS; ++directNumber) { + File subDir = new File(currentDir, directNumber + DIR_SUFFIX); if (!subDir.exists()) { continue; } @@ -22,8 +27,8 @@ public static void read(File currentDir, Map currentMap) throws throw new IOException(subDir.getName() + "isn't directory"); } - for (int fileNumber = 0; fileNumber < 16; ++fileNumber) { - File currentFile = new File(subDir, fileNumber + ".dat"); + for (int fileNumber = 0; fileNumber < MAX_FILES; ++fileNumber) { + File currentFile = new File(subDir, fileNumber + FILE_SUFFIX); if (!currentFile.exists()) { continue; } @@ -50,21 +55,21 @@ public static void deleteDirectory(File directory) throws IOException { public static void write(File currentDir, Map currentMap) throws IOException { - Map[][] arrayOfMap = new Map[16][16]; + Map[][] arrayOfMap = new Map[MAX_DIRS][MAX_FILES]; for (String key : currentMap.keySet()) { int byteOfKey = key.getBytes(StandardCharsets.UTF_8)[0]; - int nDirectory = Math.abs(byteOfKey) % 16; - int nFile = Math.abs(byteOfKey) / 16 % 16; + int nDirectory = Math.abs(byteOfKey) % MAX_DIRS; + int nFile = Math.abs(byteOfKey) / MAX_DIRS % MAX_FILES; if (arrayOfMap[nDirectory][nFile] == null) { arrayOfMap[nDirectory][nFile] = new HashMap(); } arrayOfMap[nDirectory][nFile].put(key, currentMap.get(key)); } - for (int i = 0; i < 16; i++) { - File dir = new File(currentDir, i + ".dir"); + for (int i = 0; i < MAX_DIRS; i++) { + File dir = new File(currentDir, i + DIR_SUFFIX); for (int j = 0; j < 16; j++) { - File file = new File(dir, j + ".dat"); + File file = new File(dir, j + FILE_SUFFIX); if (null == arrayOfMap[i][j]) { if (file.exists()) { file.delete(); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExecutor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapExecutor.java similarity index 90% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExecutor.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapExecutor.java index 678fbdb79..c92b9c74d 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapExecutor.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapExecutor.java @@ -1,5 +1,6 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.interpreter; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.*; import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapMain.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapMain.java similarity index 90% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapMain.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapMain.java index c34dc6447..0c993e5f8 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapMain.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapMain.java @@ -1,5 +1,6 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.interpreter; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.MultiFileHashMapState; import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; import java.io.File; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactoryTest.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableProviderFactoryTest.java similarity index 87% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactoryTest.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableProviderFactoryTest.java index c23c9108d..6338c553b 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderFactoryTest.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableProviderFactoryTest.java @@ -1,9 +1,10 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.test; import org.junit.AfterClass; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.MultiFileHashMapTableProviderFactory; import java.io.File; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderTest.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableProviderTest.java similarity index 91% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderTest.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableProviderTest.java index c47ea4235..f85344baa 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableProviderTest.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableProviderTest.java @@ -1,9 +1,11 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.test; import org.junit.Assert; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.MultiFileHashMapTableProvider; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.MultiFileHashMapTableProviderFactory; import java.io.File; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableTest.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableTest.java similarity index 89% rename from src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableTest.java rename to src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableTest.java index 98e13e29a..6692813bc 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/MultiFileHashMapTableTest.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/test/MultiFileHashMapTableTest.java @@ -1,7 +1,8 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap; +package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.test; import org.junit.Before; import org.junit.Test; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.MultiFileHashMapTable; import java.io.File; import java.util.ArrayList; @@ -111,6 +112,15 @@ public void testPutAndRollback() throws Exception { assertEquals(3, table.rollback()); } + @Test + public void testRollbackAndCheck() throws Exception { + table.put("Key1", "Value1"); + table.put("Key2", "Value2"); + table.put("Key3", "Value3"); + table.rollback(); + assertEquals(0, table.size()); + } + @Test public void testPutCommit() throws Exception { table.put("Key1", "Value1"); From 273a9231f05e1d0368cc7bb03b566e571353149e Mon Sep 17 00:00:00 2001 From: IvanovIvanK Date: Thu, 25 Dec 2014 12:13:04 +0300 Subject: [PATCH 3/5] Fixed argument check repetition --- .../students/ivan_ivanov/filemap/Exit.java | 2 +- .../students/ivan_ivanov/filemap/Get.java | 2 +- .../students/ivan_ivanov/filemap/List.java | 2 +- .../students/ivan_ivanov/filemap/Put.java | 2 +- .../students/ivan_ivanov/filemap/Remove.java | 2 +- .../multifilehashmap/database/CmdCommit.java | 12 ++++++--- .../multifilehashmap/database/CmdCreate.java | 8 +++++- .../multifilehashmap/database/CmdDrop.java | 7 ++++- .../database/CmdRollback.java | 10 ++++--- .../database/CmdShowTables.java | 2 +- .../multifilehashmap/database/CmdSize.java | 11 +++++--- .../multifilehashmap/database/CmdUse.java | 7 ++++- .../database/MultiFileHashMapExit.java | 7 ++++- .../database/MultiFileHashMapGet.java | 10 +++---- .../database/MultiFileHashMapList.java | 10 +++---- .../database/MultiFileHashMapPut.java | 10 +++---- .../database/MultiFileHashMapRemove.java | 11 ++++---- .../fivt/students/ivan_ivanov/shell/Cat.java | 2 +- .../fivt/students/ivan_ivanov/shell/Cd.java | 2 +- .../students/ivan_ivanov/shell/Command.java | 27 ++++++++++++++++--- .../fivt/students/ivan_ivanov/shell/Cp.java | 2 +- .../fivt/students/ivan_ivanov/shell/Exit.java | 2 +- .../fivt/students/ivan_ivanov/shell/Ls.java | 2 +- .../students/ivan_ivanov/shell/Mkdir.java | 2 +- .../fivt/students/ivan_ivanov/shell/Mv.java | 2 +- .../fivt/students/ivan_ivanov/shell/Pwd.java | 2 +- .../fivt/students/ivan_ivanov/shell/Rm.java | 2 +- 27 files changed, 108 insertions(+), 52 deletions(-) diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java index 846635450..739e0f842 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java @@ -4,7 +4,7 @@ import java.io.IOException; -public class Exit implements Command { +public class Exit extends Command { public String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java index 6942fc8ba..7b2f01695 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java @@ -4,7 +4,7 @@ import java.io.IOException; -public class Get implements Command { +public class Get extends Command { public String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java index 551f61669..7f5288426 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java @@ -5,7 +5,7 @@ import java.io.IOException; import java.util.Set; -public class List implements Command { +public class List extends Command { public final String getName() { return "list"; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java index 83f01ab32..54b650e38 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java @@ -4,7 +4,7 @@ import java.io.IOException; -public class Put implements Command { +public class Put extends Command { public String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java index a534b55a7..e1381b50d 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java @@ -4,7 +4,7 @@ import java.io.IOException; -public class Remove implements Command { +public class Remove extends Command { public String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java index 37189b410..920b18ba1 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java @@ -4,7 +4,9 @@ import java.io.IOException; -public class CmdCommit implements Command { +public class CmdCommit extends Command { + + private int numArg = 0; @Override public String getName() { @@ -15,10 +17,14 @@ public String getName() { @Override public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - if (inState.getCurrentTable() == null) { - System.out.println("no table"); + if (!checkArgs(numArg, args.length)) { return; } + + if (!checkTable(inState)) { + return; + } + try { System.out.println(inState.getCurrentTable().commit()); } catch (RuntimeException e) { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java index 8ef69d4e9..9cecbb93f 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java @@ -4,7 +4,9 @@ import java.io.IOException; -public class CmdCreate implements Command { +public class CmdCreate extends Command { + + private int numArg = 1; @Override public String getName() { @@ -15,6 +17,10 @@ public String getName() { @Override public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + if (!checkArgs(numArg, args.length)) { + return; + } + if (inState.createTable(args[0]) != null) { System.out.println("created"); } else { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java index 3dc957c48..62cd49d5c 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java @@ -4,7 +4,9 @@ import java.io.IOException; -public class CmdDrop implements Command { +public class CmdDrop extends Command { + + private int numArg = 0; @Override public String getName() { @@ -15,6 +17,9 @@ public String getName() { @Override public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + if (!checkArgs(numArg, args.length)) { + return; + } if (inState.getTable(args[0]) == null) { System.out.println(args[0] + " not exists"); return; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java index d9889d748..6abb50987 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java @@ -4,7 +4,9 @@ import java.io.IOException; -public class CmdRollback implements Command { +public class CmdRollback extends Command { + + private int numArg = 0; @Override public String getName() { @@ -15,8 +17,10 @@ public String getName() { @Override public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - if (inState.getCurrentTable() == null) { - System.out.println("no table"); + if (!checkArgs(numArg, args.length)) { + return; + } + if (!checkTable(inState)) { return; } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java index 4cdd8860a..fbf17c010 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java @@ -5,7 +5,7 @@ import java.io.IOException; import java.util.Set; -public class CmdShowTables implements Command { +public class CmdShowTables extends Command { @Override public final String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java index 8817a2326..dd6293a60 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java @@ -4,7 +4,9 @@ import java.io.IOException; -public class CmdSize implements Command { +public class CmdSize extends Command { + + private int numArg = 0; @Override public String getName() { @@ -15,8 +17,11 @@ public String getName() { @Override public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - if (inState.getCurrentTable() == null) { - System.out.println("no table"); + + if (!checkArgs(numArg, args.length)) { + return; + } + if (!checkTable(inState)) { return; } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java index 008a60da2..fe3828509 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java @@ -6,7 +6,9 @@ import java.io.IOException; import java.util.Map; -public class CmdUse implements Command { +public class CmdUse extends Command { + + private int numArg = 1; @Override public String getName() { @@ -17,6 +19,9 @@ public String getName() { @Override public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + if (!checkArgs(numArg, args.length)) { + return; + } if (inState.getCurrentTable() != null) { if (!inState.getCurrentTable().getName().equals(args[0])) { int size = inState.getChangesBaseSize(); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java index fa0d57528..03584f42e 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java @@ -4,7 +4,9 @@ import java.io.IOException; -public class MultiFileHashMapExit implements Command { +public class MultiFileHashMapExit extends Command { + + private int numArg = 0; @Override public String getName() { @@ -15,6 +17,9 @@ public String getName() { @Override public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + if (!checkArgs(numArg, args.length)) { + return; + } if (inState.getCurrentTable() != null) { inState.getCurrentTable().commit(); } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java index 2fb9c5de6..99932035c 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java @@ -4,7 +4,9 @@ import java.io.IOException; -public class MultiFileHashMapGet implements Command { +public class MultiFileHashMapGet extends Command { + + private int numArg = 1; @Override public String getName() { @@ -15,12 +17,10 @@ public String getName() { @Override public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - if (args.length != 1) { - System.out.println("incorrect number of arguments"); + if (!checkArgs(numArg, args.length)) { return; } - if (inState.getCurrentTable() == null) { - System.out.println("no table"); + if (!checkTable(inState)) { return; } String value = inState.getFromCurrentTable(args[0]); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java index 9785db10c..0ce3b7f32 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java @@ -5,7 +5,9 @@ import java.io.IOException; import java.util.List; -public class MultiFileHashMapList implements Command { +public class MultiFileHashMapList extends Command { + + private int numArg = 0; @Override public final String getName() { @@ -15,12 +17,10 @@ public final String getName() { @Override public final void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - if (args.length != 0) { - System.out.println("incorrect number of arguments"); + if (!checkArgs(numArg, args.length)) { return; } - if (inState.getCurrentTable() == null) { - System.out.println("no table"); + if (!checkTable(inState)) { return; } List keys = inState.getCurrentTableListOfKeys(); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java index 590d1c91c..abf1fc675 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java @@ -4,7 +4,9 @@ import java.io.IOException; -public class MultiFileHashMapPut implements Command { +public class MultiFileHashMapPut extends Command { + + private int numArg = 2; @Override public String getName() { @@ -15,12 +17,10 @@ public String getName() { @Override public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - if (args.length != 2) { - System.out.println("incorrect number of arguments"); + if (!checkArgs(numArg, args.length)) { return; } - if (inState.getCurrentTable() == null) { - System.out.println("no table"); + if (!checkTable(inState)) { return; } String value = inState.putToCurrentTable(args[0], args[1]); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java index 3c93f974c..3027a37c4 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java @@ -4,7 +4,9 @@ import java.io.IOException; -public class MultiFileHashMapRemove implements Command { +public class MultiFileHashMapRemove extends Command { + + private int numArg = 1; @Override public String getName() { @@ -14,13 +16,10 @@ public String getName() { @Override public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - - if (args.length != 1) { - System.out.println("incorrect number of arguments"); + if (!checkArgs(numArg, args.length)) { return; } - if (inState.getCurrentTable() == null) { - System.out.println("no table"); + if (!checkTable(inState)) { return; } String value = inState.removeFromCurrentTable(args[0]); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java index e6bd24a98..bc4d760df 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java @@ -7,7 +7,7 @@ import java.nio.file.Path; -public class Cat implements Command { +public class Cat extends Command { public String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java index d62885517..5e9b1d0fa 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java @@ -4,7 +4,7 @@ import java.io.IOException; import java.nio.file.Path; -public class Cd implements Command { +public class Cd extends Command { public String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java index d8b13745c..7ac0b60f8 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java @@ -1,9 +1,30 @@ package ru.fizteh.fivt.students.ivan_ivanov.shell; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.MultiFileHashMapState; + import java.io.IOException; -public interface Command { +public abstract class Command { + + public boolean checkArgs(int needArg, int haveArg) { + if (needArg != haveArg) { + System.out.println("incorrect number of arguments"); + return false; + } + return true; + } + + public boolean checkTable(MultiFileHashMapState inState) throws IOException { + if (inState.getCurrentTable() == null) { + System.out.println("no table"); + return false; + } + return true; + } + + public abstract String getName(); + + public abstract void executeCmd(State shell, String[] args) throws IOException; + - String getName(); - void executeCmd(State shell, String[] args) throws IOException; } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java index 480e2014f..dd22dee17 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java @@ -5,7 +5,7 @@ import java.nio.file.Files; import java.nio.file.Path; -public class Cp implements Command { +public class Cp extends Command { public String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java index 36773e7a4..9bed15b4d 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java @@ -2,7 +2,7 @@ import java.io.IOException; -public class Exit implements Command { +public class Exit extends Command { @Override public String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java index 72868c94d..735f1b4e8 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java @@ -4,7 +4,7 @@ import java.io.IOException; import java.io.PrintStream; -public class Ls implements Command { +public class Ls extends Command { public final String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java index 3066faf9a..3ab26b17c 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java @@ -3,7 +3,7 @@ import java.io.File; import java.io.IOException; -public class Mkdir implements Command { +public class Mkdir extends Command { public String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java index 4843dfa7e..c7ed28987 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java @@ -6,7 +6,7 @@ import java.nio.file.Files; import java.nio.file.Path; -public class Mv implements Command { +public class Mv extends Command { public String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java index 9c37e815f..f89dd15f9 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java @@ -2,7 +2,7 @@ import java.io.IOException; -public class Pwd implements Command { +public class Pwd extends Command { public String getName() { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java index 8151a94db..b3cdf984e 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java @@ -4,7 +4,7 @@ import java.io.IOException; import java.nio.file.Path; -public class Rm implements Command { +public class Rm extends Command { public String getName() { From feee0fd89092b600b18d15c6f4d9160679a1ccec Mon Sep 17 00:00:00 2001 From: IvanovIvanK Date: Thu, 25 Dec 2014 17:00:36 +0300 Subject: [PATCH 4/5] Deleted redundant Executor extentions --- .../ivan_ivanov/filemap/FileMapExecutor.java | 26 ----------- .../ivan_ivanov/filemap/FileMapMain.java | 4 +- .../interpreter/MultiFileHashMapExecutor.java | 45 ------------------- .../interpreter/MultiFileHashMapMain.java | 12 ++++- .../students/ivan_ivanov/shell/Executor.java | 10 ++++- .../ivan_ivanov/shell/ShellExecutor.java | 29 ------------ .../students/ivan_ivanov/shell/ShellMain.java | 2 +- 7 files changed, 22 insertions(+), 106 deletions(-) delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapExecutor.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapExecutor.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellExecutor.java diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapExecutor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapExecutor.java deleted file mode 100644 index 36739c671..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapExecutor.java +++ /dev/null @@ -1,26 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; - -public class FileMapExecutor extends Executor { - - public FileMapExecutor() { - - list(); - } - - public void list() { - - Command put = new Put(); - mapOfCmd.put(put.getName(), put); - Command get = new Get(); - mapOfCmd.put(get.getName(), get); - Command remove = new Remove(); - mapOfCmd.put(remove.getName(), remove); - Command exit = new Exit(); - mapOfCmd.put(exit.getName(), exit); - Command list = new List(); - mapOfCmd.put(list.getName(), list); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java index cf692d34a..703c5a6d5 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/FileMapMain.java @@ -1,4 +1,6 @@ package ru.fizteh.fivt.students.ivan_ivanov.filemap; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor; + import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; @@ -23,7 +25,7 @@ public static void main(String[] args) throws IOException { FileMapUtils.readDataBase(startState); Shell filemap = new Shell(startState); - FileMapExecutor exec = new FileMapExecutor(); + Executor exec = new Executor(); if (args.length > 0) { filemap.batchState(args, exec); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapExecutor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapExecutor.java deleted file mode 100644 index c92b9c74d..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapExecutor.java +++ /dev/null @@ -1,45 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.interpreter; - -import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.*; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; -import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor; - -public class MultiFileHashMapExecutor extends Executor { - - public MultiFileHashMapExecutor() { - - list(); - } - - @Override - public void list() { - - Command create = new CmdCreate(); - mapOfCmd.put(create.getName(), create); - Command drop = new CmdDrop(); - mapOfCmd.put(drop.getName(), drop); - Command use = new CmdUse(); - mapOfCmd.put(use.getName(), use); - Command show = new CmdShowTables(); - mapOfCmd.put(show.getName(), show); - - Command commit = new CmdCommit(); - mapOfCmd.put(commit.getName(), commit); - Command rollback = new CmdRollback(); - mapOfCmd.put(rollback.getName(), rollback); - Command size = new CmdSize(); - mapOfCmd.put(size.getName(), size); - - Command exit = new MultiFileHashMapExit(); - mapOfCmd.put(exit.getName(), exit); - Command get = new MultiFileHashMapGet(); - mapOfCmd.put(get.getName(), get); - Command put = new MultiFileHashMapPut(); - mapOfCmd.put(put.getName(), put); - Command remove = new MultiFileHashMapRemove(); - mapOfCmd.put(remove.getName(), remove); - Command list = new MultiFileHashMapList(); - mapOfCmd.put(list.getName(), list); - - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapMain.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapMain.java index 0c993e5f8..655a5ff34 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapMain.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/interpreter/MultiFileHashMapMain.java @@ -1,7 +1,9 @@ package ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.interpreter; -import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.MultiFileHashMapState; +import ru.fizteh.fivt.students.ivan_ivanov.multifilehashmap.database.*; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; import ru.fizteh.fivt.students.ivan_ivanov.shell.Shell; +import ru.fizteh.fivt.students.ivan_ivanov.shell.Executor; import java.io.File; import java.io.IOException; @@ -29,6 +31,11 @@ public static void main(String[] args) throws IOException { System.err.println(e.getMessage()); System.exit(1); } + + Command[] cmds = new Command[]{new CmdCommit(), new CmdCreate(), new CmdDrop(), new CmdRollback(), + new CmdShowTables(), new CmdSize(), new CmdUse(), new MultiFileHashMapExit(), new MultiFileHashMapGet(), + new MultiFileHashMapRemove(), new MultiFileHashMapPut(), new MultiFileHashMapList()}; + File base = new File(currentProperty); try { if (!base.exists()) { @@ -39,7 +46,8 @@ public static void main(String[] args) throws IOException { MultiFileHashMapState startState = new MultiFileHashMapState(base); Shell mfhm = new Shell(startState); - MultiFileHashMapExecutor exec = new MultiFileHashMapExecutor(); + Executor exec = new Executor(); + exec.setMapOfCmd(cmds); if (args.length > 0) { mfhm.batchState(args, exec); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Executor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Executor.java index ed3deb5ad..951064246 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Executor.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Executor.java @@ -4,10 +4,17 @@ import java.util.HashMap; import java.util.Map; -public abstract class Executor { +public class Executor { public Map mapOfCmd = new HashMap(); + public void setMapOfCmd(Command[] cmds) { + for (Command cmd : cmds) { + mapOfCmd.put(cmd.getName(), cmd); + } + } + + public String[] argsCheck(String inCommand) { int space = inCommand.indexOf(" "); @@ -29,7 +36,6 @@ public String cmdCheck(String cmd) { return tmp; } - public abstract void list(); public void execute(State state, String cmd) throws IOException { diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellExecutor.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellExecutor.java deleted file mode 100644 index 46e7f7b4e..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellExecutor.java +++ /dev/null @@ -1,29 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -public class ShellExecutor extends Executor { - - public ShellExecutor() { - - list(); - } - - public void list() { - - Command pwd = new Pwd(); - mapOfCmd.put(pwd.getName(), pwd); - Command cd = new Cd(); - mapOfCmd.put(cd.getName(), cd); - Command mkdir = new Mkdir(); - mapOfCmd.put(mkdir.getName(), mkdir); - Command cp = new Cp(); - mapOfCmd.put(cp.getName(), cp); - Command mv = new Mv(); - mapOfCmd.put(mv.getName(), mv); - Command rm = new Rm(); - mapOfCmd.put(rm.getName(), rm); - Command exit = new Exit(); - mapOfCmd.put(exit.getName(), exit); - Command ls = new Ls(); - mapOfCmd.put(ls.getName(), ls); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellMain.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellMain.java index 7446beadb..f5c8a3607 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellMain.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/ShellMain.java @@ -10,7 +10,7 @@ public static void main(String[] args) throws IOException { File currentDirectory = new File(""); ShellState startState = new ShellState(currentDirectory); Shell shell = new Shell(startState); - ShellExecutor exec = new ShellExecutor(); + Executor exec = new Executor(); if (args.length != 0) { shell.batchState(args, exec); } else { From 76145d969d4b52d4100a864243f3e300a642f4a5 Mon Sep 17 00:00:00 2001 From: IvanovIvanK Date: Thu, 25 Dec 2014 18:00:11 +0300 Subject: [PATCH 5/5] More fixed check repetition --- .../students/ivan_ivanov/filemap/Exit.java | 19 ----- .../students/ivan_ivanov/filemap/Get.java | 29 ------- .../students/ivan_ivanov/filemap/List.java | 18 ----- .../students/ivan_ivanov/filemap/Put.java | 29 ------- .../students/ivan_ivanov/filemap/Remove.java | 28 ------- .../multifilehashmap/database/CmdCommit.java | 6 +- .../multifilehashmap/database/CmdCreate.java | 6 +- .../multifilehashmap/database/CmdDrop.java | 5 +- .../database/CmdRollback.java | 5 +- .../database/CmdShowTables.java | 5 ++ .../multifilehashmap/database/CmdSize.java | 5 +- .../multifilehashmap/database/CmdUse.java | 5 +- .../database/MultiFileHashMapExit.java | 6 +- .../database/MultiFileHashMapGet.java | 5 +- .../database/MultiFileHashMapList.java | 5 +- .../database/MultiFileHashMapPut.java | 5 +- .../database/MultiFileHashMapRemove.java | 6 +- .../fivt/students/ivan_ivanov/shell/Cat.java | 52 ------------- .../fivt/students/ivan_ivanov/shell/Cd.java | 27 ------- .../students/ivan_ivanov/shell/Command.java | 11 ++- .../fivt/students/ivan_ivanov/shell/Cp.java | 70 ----------------- .../fivt/students/ivan_ivanov/shell/Exit.java | 18 ----- .../fivt/students/ivan_ivanov/shell/Ls.java | 29 ------- .../students/ivan_ivanov/shell/Mkdir.java | 22 ------ .../fivt/students/ivan_ivanov/shell/Mv.java | 78 ------------------- .../fivt/students/ivan_ivanov/shell/Pwd.java | 19 ----- .../fivt/students/ivan_ivanov/shell/Rm.java | 2 + 27 files changed, 30 insertions(+), 485 deletions(-) delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java delete mode 100644 src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java deleted file mode 100644 index 739e0f842..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Exit.java +++ /dev/null @@ -1,19 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; - -import java.io.IOException; - -public class Exit extends Command { - - public String getName() { - - return "exit"; - } - - public void executeCmd(FileMapState inState, String[] args) throws IOException { - - FileMapUtils.write(inState.getDataBase(), inState.getDataFile()); - System.exit(0); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java deleted file mode 100644 index 7b2f01695..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Get.java +++ /dev/null @@ -1,29 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; - -import java.io.IOException; - -public class Get extends Command { - - public String getName() { - - return "get"; - } - - public void executeCmd(FileMapState inState, String[] args) throws IOException { - - if (args.length != 1) { - System.out.println("incorrect number of arguments"); - return; - } - - String value = inState.getDataBase().get(args[0]); - if (value == null) { - System.out.println("not found"); - } else { - System.out.println("found"); - System.out.println(value); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java deleted file mode 100644 index 7f5288426..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/List.java +++ /dev/null @@ -1,18 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; - -import java.io.IOException; -import java.util.Set; - -public class List extends Command { - - public final String getName() { - return "list"; - } - - public final void executeCmd(FileMapState inState, String[] args) throws IOException { - Set keys = inState.getDataBase().keySet(); - System.out.println(String.join(", ", keys)); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java deleted file mode 100644 index 54b650e38..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Put.java +++ /dev/null @@ -1,29 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; - -import java.io.IOException; - -public class Put extends Command { - - public String getName() { - - return "put"; - } - - public void executeCmd(FileMapState inState, String[] args) throws IOException { - - if (args.length != 2) { - System.out.println("incorrect number of arguments"); - return; - } - - String oldValue = inState.getDataBase().put(args[0], args[1]); - if (oldValue == null) { - System.out.println("new"); - } else { - System.out.println("overwrite"); - System.out.println(oldValue); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java b/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java deleted file mode 100644 index e1381b50d..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/filemap/Remove.java +++ /dev/null @@ -1,28 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.filemap; - -import ru.fizteh.fivt.students.ivan_ivanov.shell.Command; - -import java.io.IOException; - -public class Remove extends Command { - - public String getName() { - - return "remove"; - } - - public void executeCmd(FileMapState inState, String[] args) throws IOException { - - if (args.length != 1) { - System.out.println("incorrect number of arguments"); - return; - } - - String value = inState.getDataBase().remove(args[0]); - if (value == null) { - System.out.println("not found"); - } else { - System.out.println("removed"); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java index 920b18ba1..e64ecd1fa 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCommit.java @@ -15,11 +15,7 @@ public String getName() { } @Override - public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - - if (!checkArgs(numArg, args.length)) { - return; - } + public void work(MultiFileHashMapState inState, String[] args) throws IOException { if (!checkTable(inState)) { return; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java index 9cecbb93f..cab1d711e 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdCreate.java @@ -15,11 +15,7 @@ public String getName() { } @Override - public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - - if (!checkArgs(numArg, args.length)) { - return; - } + public void work(MultiFileHashMapState inState, String[] args) throws IOException { if (inState.createTable(args[0]) != null) { System.out.println("created"); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java index 62cd49d5c..5916a4c44 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdDrop.java @@ -15,11 +15,8 @@ public String getName() { } @Override - public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + public void work(MultiFileHashMapState inState, String[] args) throws IOException { - if (!checkArgs(numArg, args.length)) { - return; - } if (inState.getTable(args[0]) == null) { System.out.println(args[0] + " not exists"); return; diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java index 6abb50987..0e7c490ab 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdRollback.java @@ -15,11 +15,8 @@ public String getName() { } @Override - public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + public void work(MultiFileHashMapState inState, String[] args) throws IOException { - if (!checkArgs(numArg, args.length)) { - return; - } if (!checkTable(inState)) { return; } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java index fbf17c010..76671e296 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdShowTables.java @@ -23,6 +23,11 @@ public final void executeCmd(MultiFileHashMapState inState, String[] args) throw throw new IOException("Can't find key"); } + work(inState, args); + } + + @Override + public final void work(MultiFileHashMapState inState, String[] args) throws IOException { System.out.println("table_name row_count"); Set tables = inState.getTableSet(); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java index dd6293a60..2553c5396 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdSize.java @@ -15,12 +15,9 @@ public String getName() { } @Override - public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + public void work(MultiFileHashMapState inState, String[] args) throws IOException { - if (!checkArgs(numArg, args.length)) { - return; - } if (!checkTable(inState)) { return; } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java index fe3828509..d83d890bd 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/CmdUse.java @@ -17,11 +17,8 @@ public String getName() { } @Override - public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + public void work(MultiFileHashMapState inState, String[] args) throws IOException { - if (!checkArgs(numArg, args.length)) { - return; - } if (inState.getCurrentTable() != null) { if (!inState.getCurrentTable().getName().equals(args[0])) { int size = inState.getChangesBaseSize(); diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java index 03584f42e..a40ee1e00 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapExit.java @@ -15,11 +15,9 @@ public String getName() { } @Override - public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + public void work(MultiFileHashMapState inState, String[] args) throws IOException { + - if (!checkArgs(numArg, args.length)) { - return; - } if (inState.getCurrentTable() != null) { inState.getCurrentTable().commit(); } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java index 99932035c..5658571f6 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapGet.java @@ -15,11 +15,8 @@ public String getName() { } @Override - public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + public void work(MultiFileHashMapState inState, String[] args) throws IOException { - if (!checkArgs(numArg, args.length)) { - return; - } if (!checkTable(inState)) { return; } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java index 0ce3b7f32..43ec5de10 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapList.java @@ -15,11 +15,8 @@ public final String getName() { } @Override - public final void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + public final void work(MultiFileHashMapState inState, String[] args) throws IOException { - if (!checkArgs(numArg, args.length)) { - return; - } if (!checkTable(inState)) { return; } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java index abf1fc675..3da0499cd 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapPut.java @@ -15,11 +15,8 @@ public String getName() { } @Override - public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { + public void work(MultiFileHashMapState inState, String[] args) throws IOException { - if (!checkArgs(numArg, args.length)) { - return; - } if (!checkTable(inState)) { return; } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java index 3027a37c4..2264924da 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/multifilehashmap/database/MultiFileHashMapRemove.java @@ -15,10 +15,8 @@ public String getName() { } @Override - public void executeCmd(MultiFileHashMapState inState, String[] args) throws IOException { - if (!checkArgs(numArg, args.length)) { - return; - } + public void work(MultiFileHashMapState inState, String[] args) throws IOException { + if (!checkTable(inState)) { return; } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java deleted file mode 100644 index bc4d760df..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cat.java +++ /dev/null @@ -1,52 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.BufferedReader; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.nio.file.Path; - - -public class Cat extends Command { - - public String getName() { - - return "cat"; - } - - - public static void catenate(final String fileName) { - String buffer = ""; - try (FileReader filescan = new FileReader(fileName); - BufferedReader bufreader = new BufferedReader(filescan)) { - while ((buffer = bufreader.readLine()) != null) { - System.out.println(buffer); - } - } catch (FileNotFoundException e) { - System.out.println("File \"" + fileName + "\" not found"); - return; - } catch (IOException e) { - System.out.println("Can't read file"); - } - } - - public void executeCmd(ShellState inState, String[] args) throws IOException { - Path thePath = inState.getPath().resolve(args[0]); - try { - if (1 == args.length) { - if (thePath.toFile().exists()) { - catenate(thePath.toString()); - } - } else { - throw new IOException("not allowed number of arguments"); - } - } catch (IOException e) { - throw e; - } finally { - while (!thePath.toFile().isDirectory()) { - thePath = thePath.getParent(); - } - inState.setPath(thePath); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java deleted file mode 100644 index 5e9b1d0fa..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cd.java +++ /dev/null @@ -1,27 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Path; - -public class Cd extends Command { - - public String getName() { - - return "cd"; - } - - public void executeCmd(ShellState inState, String[] args) throws IOException { - - if (1 == args.length) { - Path thePath = inState.getPath().resolve(args[0]); - File theFile = new File(thePath.toString()); - if (!theFile.isDirectory()) { - throw new IOException("Directory doesn't exist"); - } - inState.setPath(thePath.normalize()); - } else { - throw new IOException("not allowed number of arguments"); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java index 7ac0b60f8..ec544c3a0 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Command.java @@ -6,6 +6,8 @@ public abstract class Command { + protected int numArg; + public boolean checkArgs(int needArg, int haveArg) { if (needArg != haveArg) { System.out.println("incorrect number of arguments"); @@ -24,7 +26,14 @@ public boolean checkTable(MultiFileHashMapState inState) throws IOException { public abstract String getName(); - public abstract void executeCmd(State shell, String[] args) throws IOException; + public abstract void work(State shell, String[] args) throws IOException; + + public void executeCmd(State shell, String[] args) throws IOException { + if (!checkArgs(numArg, args.length)) { + return; + } + work(shell, args); + } } diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java deleted file mode 100644 index dd22dee17..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Cp.java +++ /dev/null @@ -1,70 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - -public class Cp extends Command { - - public String getName() { - - return "cp"; - } - - private void copy(Path source, Path target) throws IOException { - - target = target.resolve(source.getFileName()); - if (source.toFile().isFile()) { - Files.copy(source, target); - } else { - File[] masOfSource = source.toFile().listFiles(); - target.toFile().mkdir(); - for (File sourceEntry : masOfSource != null ? masOfSource : new File[0]) { - copy(sourceEntry.toPath(), target); - } - } - } - - public void executeCmd(ShellState inState, String[] args) throws IOException { - - if (2 == args.length) { - Path source = inState.getPath().resolve(args[0]).normalize(); - Path target = inState.getPath().resolve(args[1]).normalize(); - if (source.toFile().isFile() && target.toFile().isFile()) { - throw new IOException("not allowed to copy file to file"); - } - if (source.equals(target)) { - throw new IOException("not allowed to copy file on itself"); - } - if (!source.toFile().exists()) { - throw new IOException("source file doesn't exist"); - } - if (source.toFile().isDirectory() && target.toFile().isFile()) { - throw new IOException("not allowed to copy directory in file"); - } - if (source.toFile().isDirectory() && !target.toFile().exists()) { - throw new IOException("target directory doesn't exist"); - } - if (source.toFile().isDirectory() && target.toFile().isDirectory() && target.startsWith(source)) { - throw new IOException("not allowed to copy parent directory in kid's directory"); - } - - if (source.toFile().isFile() && !target.toFile().exists()) { - Files.copy(source, target); - } else if (source.toFile().isFile() && target.toFile().isDirectory()) { - Files.copy(source, target.resolve(source.getFileName())); - } else if (source.toFile().isDirectory() && target.toFile().isDirectory()) { - File[] masOfSource = source.toFile().listFiles(); - target.toFile().mkdir(); - if (masOfSource != null) { - for (File sourceEntry : masOfSource) { - copy(sourceEntry.toPath(), target); - } - } - } - } else { - throw new IOException("not allowed number of arguments"); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java deleted file mode 100644 index 9bed15b4d..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Exit.java +++ /dev/null @@ -1,18 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.IOException; - -public class Exit extends Command { - - @Override - public String getName() { - - return "exit"; - } - - @Override - public void executeCmd(ShellState inState, String[] args) throws IOException { - - System.exit(0); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java deleted file mode 100644 index 735f1b4e8..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Ls.java +++ /dev/null @@ -1,29 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.File; -import java.io.IOException; -import java.io.PrintStream; - -public class Ls extends Command { - - public final String getName() { - - return "ls"; - } - - public final void executeCmd(ShellState inState, String[] args) throws IOException { - - if (0 == args.length) { - File currentDirectory = new File(inState.getPath().toString()); - File[] listOfFiles = currentDirectory.listFiles(); - PrintStream print = new PrintStream(System.out); - if (listOfFiles != null) { - for (File listOfFile : listOfFiles) { - print.println(listOfFile.getName()); - } - } - } else { - throw new IOException("incorrect number of arguments"); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java deleted file mode 100644 index 3ab26b17c..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mkdir.java +++ /dev/null @@ -1,22 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.File; -import java.io.IOException; - -public class Mkdir extends Command { - - public String getName() { - - return "mkdir"; - } - - public void executeCmd(ShellState inState, String[] args) throws IOException { - - if (1 == args.length) { - File theFile = new File(inState.getPath().resolve(args[0]).toString()); - theFile.mkdir(); - } else { - throw new IOException("Inorrect name of directory"); - } - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java deleted file mode 100644 index c7ed28987..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Mv.java +++ /dev/null @@ -1,78 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; - -public class Mv extends Command { - - public String getName() { - - return "mv"; - } - - private void move(Path source, Path target) throws IOException { - - if (source.toFile().isFile()) { - Files.copy(source, target); - } else { - File[] masOfSource = source.toFile().listFiles(); - target.toFile().mkdir(); - for (File sourceEntry : masOfSource != null ? masOfSource : new File[0]) { - move(sourceEntry.toPath(), target); - } - } - source.toFile().delete(); - } - - public void executeCmd(ShellState inState, String[] args) throws IOException { - - if (2 == args.length) { - Path source = inState.getPath().resolve(args[0]).normalize(); - Path target = inState.getPath().resolve(args[1]).normalize(); - if (source.toFile().isFile() && target.toFile().isFile()) { - throw new IOException("not allowed to move file to file"); - } - if (source.equals(target)) { - throw new IOException("not allowed to move file on itself"); - } - if (!source.toFile().exists()) { - throw new IOException("source file doesn't exist"); - } - if (source.toFile().isDirectory() && target.toFile().isFile()) { - throw new IOException("not allowed to move directory in file"); - } - if (source.toFile().isDirectory() && !target.toFile().exists()) { - throw new IOException("target directory doesn't exist"); - } - if (source.toFile().isDirectory() && target.toFile().isDirectory() && target.startsWith(source)) { - throw new IOException("not allowed to move parent directory in kid's directory"); - } - - if (source.toFile().isFile() && !target.toFile().exists()) { - Files.copy(source, target); - source.toFile().delete(); - } else if (source.toFile().isFile() && target.toFile().isDirectory()) { - Files.copy(source, target.resolve(source.getFileName())); - source.toFile().delete(); - } else if (source.toFile().isDirectory() && target.toFile().isDirectory()) { - File[] masOfSource = source.toFile().listFiles(); - target.toFile().mkdir(); - if (masOfSource != null) { - for (File sourceEntry : masOfSource) { - move(sourceEntry.toPath(), target); - } - } - } - } else { - throw new IOException("not allowed number of arguments"); - } - Path path = inState.getPath(); - while (!path.toFile().isDirectory()) { - path = path.getParent(); - } - inState.setPath(path); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java deleted file mode 100644 index f89dd15f9..000000000 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Pwd.java +++ /dev/null @@ -1,19 +0,0 @@ -package ru.fizteh.fivt.students.ivan_ivanov.shell; - -import java.io.IOException; - -public class Pwd extends Command { - - public String getName() { - - return "pwd"; - } - - public void executeCmd(ShellState inState, String[] args) throws IOException { - - if (args.length > 0) { - throw new IOException("too many args"); - } - System.out.println(inState.getPath().toString()); - } -} diff --git a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java index b3cdf984e..7f260f138 100644 --- a/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java +++ b/src/ru/fizteh/fivt/students/ivan_ivanov/shell/Rm.java @@ -52,4 +52,6 @@ public void executeCmd(ShellState inState, String[] args) throws IOException { inState.setPath(thePath); } } + public void work(ShellState inState, String[] args) throws IOException{ + } }