-
Notifications
You must be signed in to change notification settings - Fork 69
FileMap with JUnit (#Задание4) #598
base: master
Are you sure you want to change the base?
Changes from 6 commits
da7a17c
5e0ca7b
895e6fe
1cbccab
7cbea81
10bf88b
1e0693a
e162d7d
b615130
f412af5
512eb42
228acff
ec5d7cd
93aca57
44da6c6
b5fae6b
e74a8c7
7ad5cb3
45290a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.JUnit; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Класс @class FactoryImplements отвечает за фабрику по созданию таблиц. | ||
* Имплиментируя интерфейс TableProviderFactory, переопределяет метод по | ||
* созданию провайдера базы данных. | ||
* | ||
* @author Timokhin Andrew | ||
*/ | ||
|
||
public class FactoryImplements implements TableProviderFactory { | ||
|
||
@Override | ||
public TableProvider create(String dir) { | ||
if (dir == null) { | ||
throw new IllegalArgumentException("Error in create-meth."); | ||
} | ||
try { | ||
return new TableProviderImplements(dir); | ||
} catch (IOException xcpt) { | ||
System.out.println(xcpt.toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Библиотечный класс ничего не должен выводить на консоль, он должен лишь бросать исключения в случае ошибок. |
||
} | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.JUnit; | ||
|
||
import java.io.IOException; | ||
|
||
public class JUnit { | ||
public static void main(String[] args) throws IOException, | ||
IllegalArgumentException, KeyNullAndNotFound { | ||
FactoryImplements tb = new FactoryImplements(); | ||
TableProviderImplements tp = (TableProviderImplements) tb | ||
.create("C:\\DataBase"); | ||
Table tableA1 = tp.createTable("A1"); | ||
tableA1 = tp.getTable("A1"); | ||
|
||
/* | ||
* Uncomment to see, that we have, when we run this programm twice. A1 | ||
* must be : (1,1); (2,2); (3,3); A2 must be : all changes | ||
*/ | ||
/* | ||
* System.out.println("<===twice===>\n"); for (String tm : | ||
* tableA1.list()) { System.out.println("key -> " + tm + " value -> " + | ||
* tableA1.get(tm) ); } System.out.println("\n<===twice===>\n"); | ||
*/ | ||
|
||
tableA1.put("1", "1"); | ||
tableA1.put("2", "2"); | ||
tableA1.put("3", "3"); | ||
tableA1.commit(); | ||
tableA1.put("4", "4"); | ||
tableA1.put("5", "5"); | ||
tableA1.put("6", "6"); | ||
tableA1.put("7", "7"); | ||
tableA1.commit(); | ||
tableA1.rollback(); | ||
|
||
for (String tm : tableA1.list()) { | ||
System.out.println("key -> " + tm + " value -> " + tableA1.get(tm)); | ||
} | ||
|
||
Table tableA2 = tp.createTable("A2"); | ||
tableA2 = tp.getTable("A2"); | ||
|
||
/* | ||
* System.out.println("<===twice===> \n"); for (String time: | ||
* tableA2.list()){ System.out.println("key -> " + time + " value -> " + | ||
* tableA2.get(time) ); } System.out.println("\n<===twice===> \n"); | ||
*/ | ||
|
||
tableA2.put("1", "green"); | ||
tableA2.put("2", "blue"); | ||
|
||
System.out.println("<========NEXT========>\n"); | ||
tableA2.remove("1"); | ||
|
||
for (String time : tableA2.list()) { | ||
System.out.println("key -> " + time + " value -> " | ||
+ tableA2.get(time)); | ||
} | ||
|
||
tableA2.commit(); | ||
tableA2.put("3", "green"); | ||
tableA2.put("4", "black"); | ||
tableA2.put("5", "red"); | ||
tableA2.commit(); | ||
try { | ||
tableA2.get(null); | ||
} catch (IllegalArgumentException e) { | ||
System.out.println("\n" + e + "\n" + e.getCause() + "\n"); | ||
} | ||
System.out.println(tableA2.get("4")); | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.JUnit; | ||
|
||
@SuppressWarnings("serial") | ||
public class KeyNullAndNotFound extends Exception { | ||
public KeyNullAndNotFound(String description) { | ||
super(description); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.JUnit; | ||
|
||
import java.io.DataInputStream; | ||
import java.io.EOFException; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.util.Iterator; | ||
import java.util.Vector; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused import There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
/** | ||
* Класс @class Reader отвечает за физическое чтение данных с жесткого диска или | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "из файловой системы". За нами именно такая абстракция. Это может быть сетевая файловая система, например. |
||
* другого физического носителя информации. | ||
* | ||
* | ||
* @author Timokhin Andrew | ||
*/ | ||
|
||
public class Reader { | ||
public void read(TableProviderImplements tp) throws IOException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему эта функциональность вынесена в отдельный класс? Объекты класса Reader не хранят состояния, так что не нужны. Перенести метод read в класс TableProviderImplements |
||
int i; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Перенести декларацию счётчика в сами циклы. |
||
StringBuilder keyBuilder = new StringBuilder(); | ||
StringBuilder valueBuilder = new StringBuilder(); | ||
int length; | ||
String path = tp.dir; | ||
Vector<TableImplement> agregat = new Vector<TableImplement>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Переназвать переменную |
||
int validator = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Что хранит этот счётчик? Переименовать переменную |
||
File testDir = new File(path); | ||
if (testDir.list() != null) { | ||
for (String time : testDir.list()) { | ||
File checkDir = new File(path + "\\" + time); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. http://stackoverflow.com/questions/711993/does-java-have-a-path-joining-method |
||
|
||
if (checkDir.isDirectory()) { | ||
validator++; | ||
TableImplement dataBase = new TableImplement(time, tp.dir); | ||
agregat.add(dataBase); | ||
for (i = 0; i < 16; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 16 - константа. Вынести в начало класса. |
||
Integer numberDir = new Integer(i); | ||
File locDB = new File(path + "\\" + time + "\\" | ||
+ numberDir.toString() + ".dir"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Суффикс .dir вынести в константы |
||
if (locDB.exists()) { | ||
for (String file : locDB.list()) { | ||
|
||
try (DataInputStream rd = new DataInputStream( | ||
new FileInputStream( | ||
locDB.getAbsolutePath() + "\\" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Исправить склеивание путей везде (см. выше) |
||
+ file))) { | ||
|
||
while (true) { | ||
|
||
try { | ||
length = rd.readInt(); | ||
for (int k = 0; k < length; k++) { | ||
keyBuilder | ||
.append(rd.readChar()); | ||
|
||
} | ||
length = rd.readInt(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Строки 57-61 и 62-65 дублируют код. Вынести в отдельную функцию There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
for (int k = 0; k < length; k++) { | ||
valueBuilder.append(rd | ||
.readChar()); | ||
|
||
} | ||
|
||
dataBase.getMap().put( | ||
keyBuilder.toString(), | ||
valueBuilder.toString()); | ||
dataBase.getBackup().put( | ||
keyBuilder.toString(), | ||
valueBuilder.toString()); | ||
keyBuilder.replace(0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. У StringBuilder нету метода clear? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. В моей установленной среде, как странно- нет. В документации есть. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Какая у тебя Java? Мы пишем на JDK8. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JDK7. Ну есть такой метод, я это знаю. Но в NetBeams очень странно, что он его не видит. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Поставь себе JDK8. Поставь себе IntellijIDEA по студенческой лицензии. У нас курс про JDK8. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
keyBuilder.length(), ""); | ||
valueBuilder.replace(0, | ||
valueBuilder.length(), ""); | ||
} catch (EOFException e) { | ||
|
||
break; | ||
} | ||
|
||
} | ||
|
||
} catch (FileNotFoundException fnfe) { | ||
System.out.println(fnfe.toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Библиотечный класс ничего не должен печатать. |
||
|
||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. По какому правилу тут ставится или не ставится пустая строка между закрывающимися скобками? Код - это не черновик, у него должна быть строгость. |
||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
} | ||
if (validator == 0) { | ||
return; | ||
} | ||
TableImplement[] copy = new TableImplement[agregat.size()]; | ||
i = 0; | ||
Iterator<TableImplement> it = agregat.iterator(); | ||
|
||
while (it.hasNext()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Перейти на foreach. |
||
|
||
copy[i] = it.next(); | ||
i++; | ||
} | ||
tp.collection = copy; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reader'у не нужно знать о внутреннем устройстве TableProvider. Пусть вернёт эту мапу с таблицами из функции. |
||
return; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Пустая строка зачем? |
||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package ru.fizteh.fivt.students.AndrewTimokhin.FileMap.JUnit; | ||
|
||
import java.util.List; | ||
|
||
public interface Table { | ||
|
||
/** | ||
* ���������� �������� ���� ������. | ||
*/ | ||
String getName(); | ||
|
||
/** | ||
* �������� �������� �� ���������� �����. | ||
* | ||
* @param key | ||
* ����. | ||
* @return ��������. ���� �� �������, ���������� null. | ||
* @throws KeyNullAndNotFound | ||
* | ||
* @throws IllegalArgumentException | ||
* ���� �������� ��������� key �������� null. | ||
*/ | ||
String get(String key) throws IllegalArgumentException, KeyNullAndNotFound; | ||
|
||
/** | ||
* ������������� �������� �� ���������� �����. | ||
* | ||
* @param key | ||
* ����. | ||
* @param value | ||
* ��������. | ||
* @return ��������, ������� ���� �������� �� ����� ����� �����. ���� ����� | ||
* �������� �� ���� ��������, ���������� null. | ||
* | ||
* @throws IllegalArgumentException | ||
* ���� �������� ���������� key ��� value �������� null. | ||
*/ | ||
String put(String key, String value); | ||
|
||
/** | ||
* ������� �������� �� ���������� �����. | ||
* | ||
* @param key | ||
* ����. | ||
* @return ��������. ���� �� �������, ���������� null. | ||
* | ||
* @throws IllegalArgumentException | ||
* ���� �������� ��������� key �������� null. | ||
*/ | ||
String remove(String key); | ||
|
||
/** | ||
* ���������� ���������� ������ � �������. | ||
* | ||
* @return ���������� ������ � �������. | ||
*/ | ||
int size(); | ||
|
||
/** | ||
* ��������� �������� ���������. | ||
* | ||
* @return ���������� ����������� ������. | ||
*/ | ||
int commit(); | ||
|
||
/** | ||
* ��������� ����� ��������� � ������� ��������� ��������. | ||
* | ||
* @return ���������� ���������� ������. | ||
*/ | ||
int rollback(); | ||
|
||
/** | ||
* ������� ������ ������ ������� | ||
* | ||
* @return ������ ������. | ||
*/ | ||
List<String> list(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Точка в текстах исключений не ставится: см. стандартную библиотеку за примерами.
meth - необычное сокращение
Сообщение об ошибке не описывает причину проблемы и не помогает отладить приложение. Написать нормальное.