From 1f47ff853ace0f0696a9813e53639982f5b4d0b4 Mon Sep 17 00:00:00 2001 From: Denis Zakharov Date: Fri, 25 Oct 2019 00:42:47 +0300 Subject: [PATCH 1/9] The main refactoring --- Document.php | 10 ++++++---- Parser.java | 29 +++++++++++++---------------- 2 files changed, 19 insertions(+), 20 deletions(-) diff --git a/Document.php b/Document.php index 7803df6..a99f7c8 100644 --- a/Document.php +++ b/Document.php @@ -12,15 +12,17 @@ public function init($name, User $user) { } public function getTitle() { - $db = Database::getInstance(); - $row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'); - return $row[3]; // third column in a row + return $this->getColumn(3); // third column in a row } public function getContent() { + return $this->getColumn(6); // sixth column in a row + } + + private function getColumn($column) { $db = Database::getInstance(); $row = $db->query('SELECT * FROM document WHERE name = "' . $this->name . '" LIMIT 1'); - return $row[6]; // sixth column in a row + return $row[$column]; // sixth column in a row } public static function getAllDocuments() { diff --git a/Parser.java b/Parser.java index c13a9fe..19251d6 100644 --- a/Parser.java +++ b/Parser.java @@ -14,24 +14,10 @@ public synchronized File getFile() { return file; } public String getContent() throws IOException { - FileInputStream i = new FileInputStream(file); - String output = ""; - int data; - while ((data = i.read()) > 0) { - output += (char) data; - } - return output; + return getContent(false); } public String getContentWithoutUnicode() throws IOException { - FileInputStream i = new FileInputStream(file); - String output = ""; - int data; - while ((data = i.read()) > 0) { - if (data < 0x80) { - output += (char) data; - } - } - return output; + return getContent(true); } public void saveContent(String content) { FileOutputStream o = new FileOutputStream(file); @@ -43,4 +29,15 @@ public void saveContent(String content) { e.printStackTrace(); } } + private String getContent(boolean withoutUnicode) throws IOException { + FileInputStream i = new FileInputStream(file); + String output = ""; + int data; + while ((data = i.read()) > 0) { + if(!withoutUnicode || data < 0x80) { + output += (char) data; + }) + } + return output; + } } From c6f0f242158e4b2ad331de7583fe178d565881a6 Mon Sep 17 00:00:00 2001 From: Denis Zakharov Date: Fri, 25 Oct 2019 00:49:51 +0300 Subject: [PATCH 2/9] Try with resources, minor refactoring --- Parser.java | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Parser.java b/Parser.java index 19251d6..ff4d065 100644 --- a/Parser.java +++ b/Parser.java @@ -20,8 +20,7 @@ public String getContentWithoutUnicode() throws IOException { return getContent(true); } public void saveContent(String content) { - FileOutputStream o = new FileOutputStream(file); - try { + try (FileOutputStream o = new FileOutputStream(file)) { for (int i = 0; i < content.length(); i += 1) { o.write(content.charAt(i)); } @@ -29,15 +28,16 @@ public void saveContent(String content) { e.printStackTrace(); } } - private String getContent(boolean withoutUnicode) throws IOException { - FileInputStream i = new FileInputStream(file); - String output = ""; - int data; - while ((data = i.read()) > 0) { - if(!withoutUnicode || data < 0x80) { - output += (char) data; - }) + private String getContent(boolean withUnicode) throws IOException { + try (FileInputStream i = new FileInputStream(file)) { + String output = ""; + int data; + while ((data = i.read()) > 0) { + if(withUnicode || data >= 0x80) { + output += (char) data; + }) + } + return output; } - return output; } } From ee15bf2ed2c0efdb3fcaf2b60abbfc97cf881f1c Mon Sep 17 00:00:00 2001 From: Denis Zakharov Date: Fri, 25 Oct 2019 01:06:30 +0300 Subject: [PATCH 3/9] Little fix --- Parser.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Parser.java b/Parser.java index ff4d065..b6270ed 100644 --- a/Parser.java +++ b/Parser.java @@ -14,10 +14,10 @@ public synchronized File getFile() { return file; } public String getContent() throws IOException { - return getContent(false); + return getContent(true); } public String getContentWithoutUnicode() throws IOException { - return getContent(true); + return getContent(false); } public void saveContent(String content) { try (FileOutputStream o = new FileOutputStream(file)) { From d98d61273fad7320d2c71c85bbe34782e57fc2dc Mon Sep 17 00:00:00 2001 From: Denis Zakharov Date: Fri, 25 Oct 2019 01:50:41 +0300 Subject: [PATCH 4/9] Make it more object oriented --- Document.php | 16 +++++++--------- Parser.java | 9 +++------ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/Document.php b/Document.php index a99f7c8..fa17bb6 100644 --- a/Document.php +++ b/Document.php @@ -5,7 +5,7 @@ class Document { public $name; - public function init($name, User $user) { + public function __construct($name, User $user) { assert(strlen($name) > 5); $this->user = $user; $this->name = $name; @@ -25,6 +25,10 @@ private function getColumn($column) { return $row[$column]; // sixth column in a row } + public static function getUserDocuments(User $user) { + // to be implemented later + } + public static function getAllDocuments() { // to be implemented later } @@ -34,18 +38,12 @@ public static function getAllDocuments() { class User { public function makeNewDocument($name) { - $doc = new Document(); - $doc->init($name, $this); + $doc = new Document($name, $this); return $doc; } public function getMyDocuments() { - $list = array(); - foreach (Document::getAllDocuments() as $doc) { - if ($doc->user == $this) - $list[] = $doc; - } - return $list; + return Document::getUserDocuments($this); } } diff --git a/Parser.java b/Parser.java index b6270ed..2b98e7d 100644 --- a/Parser.java +++ b/Parser.java @@ -5,13 +5,10 @@ /** * This class is thread safe. */ -public class Parser { +public class EnhansedFile { private File file; - public synchronized void setFile(File f) { - file = f; - } - public synchronized File getFile() { - return file; + public EnhansedFile(File f) { + this.file = f; } public String getContent() throws IOException { return getContent(true); From aed515a95b376dde4220cee0a2a11f66354ac757 Mon Sep 17 00:00:00 2001 From: Denis Zakharov Date: Fri, 25 Oct 2019 02:01:35 +0300 Subject: [PATCH 5/9] The last iteration --- Document.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/Document.php b/Document.php index fa17bb6..1405c40 100644 --- a/Document.php +++ b/Document.php @@ -25,25 +25,17 @@ private function getColumn($column) { return $row[$column]; // sixth column in a row } - public static function getUserDocuments(User $user) { - // to be implemented later - } - - public static function getAllDocuments() { - // to be implemented later - } - } class User { public function makeNewDocument($name) { - $doc = new Document($name, $this); - return $doc; + return new Document($name, $this); } public function getMyDocuments() { - return Document::getUserDocuments($this); + $db = Database::getInstance(); + return $db->query('SELECT * FROM document WHERE user_id = ' . $this.id); } } From 3cea0a76ffd5d0f3198884a53b009514eafdf46e Mon Sep 17 00:00:00 2001 From: Denis Zakharov Date: Fri, 25 Oct 2019 02:13:50 +0300 Subject: [PATCH 6/9] Create Document correctly --- Document.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Document.php b/Document.php index 1405c40..3739828 100644 --- a/Document.php +++ b/Document.php @@ -35,7 +35,11 @@ public function makeNewDocument($name) { public function getMyDocuments() { $db = Database::getInstance(); - return $db->query('SELECT * FROM document WHERE user_id = ' . $this.id); + $list = array(); + foreach ($db->query('SELECT name FROM document WHERE user_id = ' . $this->id) as $name) { + $list[] = $this->makeNewDocument($name); + } + return $list; } } From c90b877d5f2d35d216282d82a7b07a81b3d6ae4c Mon Sep 17 00:00:00 2001 From: Denis Zakharov Date: Fri, 25 Oct 2019 09:34:20 +0300 Subject: [PATCH 7/9] Some improvements --- Document.php | 17 ----------------- Parser.java => FileWithContent.java | 8 ++++---- User.php | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 21 deletions(-) rename Parser.java => FileWithContent.java (80%) create mode 100644 User.php diff --git a/Document.php b/Document.php index 3739828..3a9a88d 100644 --- a/Document.php +++ b/Document.php @@ -26,20 +26,3 @@ private function getColumn($column) { } } - -class User { - - public function makeNewDocument($name) { - return new Document($name, $this); - } - - public function getMyDocuments() { - $db = Database::getInstance(); - $list = array(); - foreach ($db->query('SELECT name FROM document WHERE user_id = ' . $this->id) as $name) { - $list[] = $this->makeNewDocument($name); - } - return $list; - } - -} diff --git a/Parser.java b/FileWithContent.java similarity index 80% rename from Parser.java rename to FileWithContent.java index 2b98e7d..9a192bd 100644 --- a/Parser.java +++ b/FileWithContent.java @@ -5,9 +5,9 @@ /** * This class is thread safe. */ -public class EnhansedFile { +public class FileWithContent { private File file; - public EnhansedFile(File f) { + public FileWithContent(File f) { this.file = f; } public String getContent() throws IOException { @@ -16,7 +16,7 @@ public String getContent() throws IOException { public String getContentWithoutUnicode() throws IOException { return getContent(false); } - public void saveContent(String content) { + public synchronized void saveContent(String content) { try (FileOutputStream o = new FileOutputStream(file)) { for (int i = 0; i < content.length(); i += 1) { o.write(content.charAt(i)); @@ -25,7 +25,7 @@ public void saveContent(String content) { e.printStackTrace(); } } - private String getContent(boolean withUnicode) throws IOException { + private synchronized String getContent(boolean withUnicode) throws IOException { try (FileInputStream i = new FileInputStream(file)) { String output = ""; int data; diff --git a/User.php b/User.php new file mode 100644 index 0000000..3d2d05e --- /dev/null +++ b/User.php @@ -0,0 +1,17 @@ +query('SELECT name FROM document WHERE user_id = ' . $this->id) as $name) { + $list[] = $this->makeNewDocument($name); + } + return $list; + } + +} From 9b9deda8daabdbaffe0e4ec7fafa9e9f805c7d9a Mon Sep 17 00:00:00 2001 From: Denis Zakharov Date: Fri, 25 Oct 2019 09:42:52 +0300 Subject: [PATCH 8/9] Another thought --- Document.php | 4 ++-- User.php | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Document.php b/Document.php index 3a9a88d..e19e3c3 100644 --- a/Document.php +++ b/Document.php @@ -1,9 +1,9 @@ 5); diff --git a/User.php b/User.php index 3d2d05e..075c1ed 100644 --- a/User.php +++ b/User.php @@ -1,6 +1,12 @@ id = $id; + } + public function makeNewDocument($name) { return new Document($name, $this); } From fdb4e8f968dabdefb9a6c9196910350d2fe595ee Mon Sep 17 00:00:00 2001 From: Denis Zakharov Date: Fri, 25 Oct 2019 09:44:50 +0300 Subject: [PATCH 9/9] Renaming --- User.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/User.php b/User.php index 075c1ed..7e7d2d6 100644 --- a/User.php +++ b/User.php @@ -7,7 +7,7 @@ public function __construct($id) { $this->id = $id; } - public function makeNewDocument($name) { + public function createDocument($name) { return new Document($name, $this); } @@ -15,7 +15,7 @@ public function getMyDocuments() { $db = Database::getInstance(); $list = array(); foreach ($db->query('SELECT name FROM document WHERE user_id = ' . $this->id) as $name) { - $list[] = $this->makeNewDocument($name); + $list[] = $this->createDocument($name); } return $list; }