{"action":"created","comment":{"url":"https://api.github.com/repos/nizshee/java2016/pulls/comments/56889761","id":56889761,"diff_hunk":"@@ -0,0 +1,181 @@\n+package sp;\n+\n+import java.io.*;\n+import java.lang.Character;\n+import java.lang.ref.WeakReference;\n+import java.util.HashMap;\n+import java.util.Map;\n+\n+import org.json.simple.JSONObject;\n+import org.json.simple.JSONValue;\n+\n+public class TrieImpl implements Trie, StreamSerializable {\n+\n+    private final Node root = new Node();\n+\n+    @Override\n+    public boolean add(String element) {\n+        Node node = root;\n+        for(char c : element.toCharArray()) {\n+            node = node.next(c);\n+        }\n+        return changeLeafState(node, true);\n+    }\n+\n+    @Override\n+    public boolean contains(String element) {\n+        Node node = findNode(element);\n+        return node != null && node.isLeaf();\n+    }\n+\n+    @Override\n+    public boolean remove(String element) {\n+        Node node = findNode(element);\n+        return node != null && changeLeafState(node, false);\n+    }\n+\n+    @Override\n+    public int size() {\n+        return root.getSize();\n+    }\n+\n+    @Override\n+    public int howManyStartsWithPrefix(String prefix) {\n+        Node node = findNode(prefix);\n+        return node == null ? 0 : node.getSize();\n+    }\n+\n+    private Node findNode(String element) {\n+        Node node = root;\n+        for(char c : element.toCharArray()) {\n+            if (!node.containsKey(c)) return null;\n+            node = node.get(c);\n+        }\n+        return node;\n+    }\n+\n+    @Override\n+    public boolean equals(Object obj) {\n+        return obj instanceof TrieImpl && root.equals(((TrieImpl) obj).root);\n+    }\n+\n+    @Override\n+    public void serialize(OutputStream out) throws IOException {\n+        JSONObject obj = root.toJson();\n+        out.write(obj.toJSONString().getBytes());\n+    }\n+\n+    @Override\n+    public void deserialize(InputStream in) throws IOException {\n+        try {\n+            JSONObject obj = (JSONObject)JSONValue.parseWithException(new InputStreamReader(in));\n+            root.fromJson(obj, null);\n+        } catch (Exception e) {\n+            e.printStackTrace();\n+            throw new IOException(\"can't parse json\");\n+        }\n+    }\n+\n+    private boolean changeLeafState(Node node, boolean value) {\n+        if (node.isLeaf() == value) return false;\n+        node.setLeaf(value);\n+\n+        int delta = value ? 1 : -1;\n+        for (Node prev = null; node != null; node = node.getParent()) {\n+            if (prev != null && prev.getSize() == 0) {\n+                node.remove(prev.character);\n+            }\n+            node.wordCount += delta;\n+            prev = node;\n+        }\n+\n+        return true;\n+    }\n+\n+    private static class Node {\n+\n+        private int wordCount = 0;\n+        private boolean isLeaf = false;\n+        private Character character = null;\n+        private WeakReference<Node> parent = null;\n+        private Map<Character, Node> map = new HashMap<>();\n+\n+        public static final String WORD_COUNT = \"wordCount\";\n+        public static final String CHARACTER = \"character\";\n+        public static final String IS_LEAF = \"isLeaf\";\n+        public static final String OTHER = \"other\";","path":"src/main/java/sp/TrieImpl.java","position":110,"original_position":106,"commit_id":"cfe1aa95c4db2253ba09276e3189137daba525bc","original_commit_id":"751481e3c578b1455811561e380cc3f41e92c0da","user":{"login":"sproshev","id":4990763,"avatar_url":"https://avatars.githubusercontent.com/u/4990763?v=3","gravatar_id":"","url":"https://api.github.com/users/sproshev","html_url":"https://github.com/sproshev","followers_url":"https://api.github.com/users/sproshev/followers","following_url":"https://api.github.com/users/sproshev/following{/other_user}","gists_url":"https://api.github.com/users/sproshev/gists{/gist_id}","starred_url":"https://api.github.com/users/sproshev/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sproshev/subscriptions","organizations_url":"https://api.github.com/users/sproshev/orgs","repos_url":"https://api.github.com/users/sproshev/repos","events_url":"https://api.github.com/users/sproshev/events{/privacy}","received_events_url":"https://api.github.com/users/sproshev/received_events","type":"User","site_admin":false},"body":"public идут до private","created_at":"2016-03-21T20:21:21Z","updated_at":"2016-03-21T20:21:21Z","html_url":"https://github.com/nizshee/java2016/pull/2#discussion_r56889761","pull_request_url":"https://api.github.com/repos/nizshee/java2016/pulls/2","_links":{"self":{"href":"https://api.github.com/repos/nizshee/java2016/pulls/comments/56889761"},"html":{"href":"https://github.com/nizshee/java2016/pull/2#discussion_r56889761"},"pull_request":{"href":"https://api.github.com/repos/nizshee/java2016/pulls/2"}}},"pull_request":{"url":"https://api.github.com/repos/nizshee/java2016/pulls/2","id":61847939,"html_url":"https://github.com/nizshee/java2016/pull/2","diff_url":"https://github.com/nizshee/java2016/pull/2.diff","patch_url":"https://github.com/nizshee/java2016/pull/2.patch","issue_url":"https://api.github.com/repos/nizshee/java2016/issues/2","number":2,"state":"open","locked":false,"title":"Java05. ДЗ 02, Васильев Роман","user":{"login":"nizshee","id":8837119,"avatar_url":"https://avatars.githubusercontent.com/u/8837119?v=3","gravatar_id":"","url":"https://api.github.com/users/nizshee","html_url":"https://github.com/nizshee","followers_url":"https://api.github.com/users/nizshee/followers","following_url":"https://api.github.com/users/nizshee/following{/other_user}","gists_url":"https://api.github.com/users/nizshee/gists{/gist_id}","starred_url":"https://api.github.com/users/nizshee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nizshee/subscriptions","organizations_url":"https://api.github.com/users/nizshee/orgs","repos_url":"https://api.github.com/users/nizshee/repos","events_url":"https://api.github.com/users/nizshee/events{/privacy}","received_events_url":"https://api.github.com/users/nizshee/received_events","type":"User","site_admin":false},"body":"@sproshev\r\nВроде исправил все замечания с предыдущего дз.","created_at":"2016-03-06T17:00:32Z","updated_at":"2016-03-21T20:21:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"5b78f170e7a88d26591df09c9c92bd16109ae81d","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/nizshee/java2016/pulls/2/commits","review_comments_url":"https://api.github.com/repos/nizshee/java2016/pulls/2/comments","review_comment_url":"https://api.github.com/repos/nizshee/java2016/pulls/comments{/number}","comments_url":"https://api.github.com/repos/nizshee/java2016/issues/2/comments","statuses_url":"https://api.github.com/repos/nizshee/java2016/statuses/cfe1aa95c4db2253ba09276e3189137daba525bc","head":{"label":"nizshee:task2","ref":"task2","sha":"cfe1aa95c4db2253ba09276e3189137daba525bc","user":{"login":"nizshee","id":8837119,"avatar_url":"https://avatars.githubusercontent.com/u/8837119?v=3","gravatar_id":"","url":"https://api.github.com/users/nizshee","html_url":"https://github.com/nizshee","followers_url":"https://api.github.com/users/nizshee/followers","following_url":"https://api.github.com/users/nizshee/following{/other_user}","gists_url":"https://api.github.com/users/nizshee/gists{/gist_id}","starred_url":"https://api.github.com/users/nizshee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nizshee/subscriptions","organizations_url":"https://api.github.com/users/nizshee/orgs","repos_url":"https://api.github.com/users/nizshee/repos","events_url":"https://api.github.com/users/nizshee/events{/privacy}","received_events_url":"https://api.github.com/users/nizshee/received_events","type":"User","site_admin":false},"repo":{"id":52625582,"name":"java2016","full_name":"nizshee/java2016","owner":{"login":"nizshee","id":8837119,"avatar_url":"https://avatars.githubusercontent.com/u/8837119?v=3","gravatar_id":"","url":"https://api.github.com/users/nizshee","html_url":"https://github.com/nizshee","followers_url":"https://api.github.com/users/nizshee/followers","following_url":"https://api.github.com/users/nizshee/following{/other_user}","gists_url":"https://api.github.com/users/nizshee/gists{/gist_id}","starred_url":"https://api.github.com/users/nizshee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nizshee/subscriptions","organizations_url":"https://api.github.com/users/nizshee/orgs","repos_url":"https://api.github.com/users/nizshee/repos","events_url":"https://api.github.com/users/nizshee/events{/privacy}","received_events_url":"https://api.github.com/users/nizshee/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nizshee/java2016","description":"","fork":false,"url":"https://api.github.com/repos/nizshee/java2016","forks_url":"https://api.github.com/repos/nizshee/java2016/forks","keys_url":"https://api.github.com/repos/nizshee/java2016/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nizshee/java2016/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nizshee/java2016/teams","hooks_url":"https://api.github.com/repos/nizshee/java2016/hooks","issue_events_url":"https://api.github.com/repos/nizshee/java2016/issues/events{/number}","events_url":"https://api.github.com/repos/nizshee/java2016/events","assignees_url":"https://api.github.com/repos/nizshee/java2016/assignees{/user}","branches_url":"https://api.github.com/repos/nizshee/java2016/branches{/branch}","tags_url":"https://api.github.com/repos/nizshee/java2016/tags","blobs_url":"https://api.github.com/repos/nizshee/java2016/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nizshee/java2016/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nizshee/java2016/git/refs{/sha}","trees_url":"https://api.github.com/repos/nizshee/java2016/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nizshee/java2016/statuses/{sha}","languages_url":"https://api.github.com/repos/nizshee/java2016/languages","stargazers_url":"https://api.github.com/repos/nizshee/java2016/stargazers","contributors_url":"https://api.github.com/repos/nizshee/java2016/contributors","subscribers_url":"https://api.github.com/repos/nizshee/java2016/subscribers","subscription_url":"https://api.github.com/repos/nizshee/java2016/subscription","commits_url":"https://api.github.com/repos/nizshee/java2016/commits{/sha}","git_commits_url":"https://api.github.com/repos/nizshee/java2016/git/commits{/sha}","comments_url":"https://api.github.com/repos/nizshee/java2016/comments{/number}","issue_comment_url":"https://api.github.com/repos/nizshee/java2016/issues/comments{/number}","contents_url":"https://api.github.com/repos/nizshee/java2016/contents/{+path}","compare_url":"https://api.github.com/repos/nizshee/java2016/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nizshee/java2016/merges","archive_url":"https://api.github.com/repos/nizshee/java2016/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nizshee/java2016/downloads","issues_url":"https://api.github.com/repos/nizshee/java2016/issues{/number}","pulls_url":"https://api.github.com/repos/nizshee/java2016/pulls{/number}","milestones_url":"https://api.github.com/repos/nizshee/java2016/milestones{/number}","notifications_url":"https://api.github.com/repos/nizshee/java2016/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nizshee/java2016/labels{/name}","releases_url":"https://api.github.com/repos/nizshee/java2016/releases{/id}","deployments_url":"https://api.github.com/repos/nizshee/java2016/deployments","created_at":"2016-02-26T18:52:50Z","updated_at":"2016-02-26T18:52:50Z","pushed_at":"2016-03-20T17:19:11Z","git_url":"git://github.com/nizshee/java2016.git","ssh_url":"git@github.com:nizshee/java2016.git","clone_url":"https://github.com/nizshee/java2016.git","svn_url":"https://github.com/nizshee/java2016","homepage":null,"size":18,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":3,"forks":0,"open_issues":3,"watchers":0,"default_branch":"master"}},"base":{"label":"nizshee:master","ref":"master","sha":"42e417cae3224bcffb779b982cd4a2df013636de","user":{"login":"nizshee","id":8837119,"avatar_url":"https://avatars.githubusercontent.com/u/8837119?v=3","gravatar_id":"","url":"https://api.github.com/users/nizshee","html_url":"https://github.com/nizshee","followers_url":"https://api.github.com/users/nizshee/followers","following_url":"https://api.github.com/users/nizshee/following{/other_user}","gists_url":"https://api.github.com/users/nizshee/gists{/gist_id}","starred_url":"https://api.github.com/users/nizshee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nizshee/subscriptions","organizations_url":"https://api.github.com/users/nizshee/orgs","repos_url":"https://api.github.com/users/nizshee/repos","events_url":"https://api.github.com/users/nizshee/events{/privacy}","received_events_url":"https://api.github.com/users/nizshee/received_events","type":"User","site_admin":false},"repo":{"id":52625582,"name":"java2016","full_name":"nizshee/java2016","owner":{"login":"nizshee","id":8837119,"avatar_url":"https://avatars.githubusercontent.com/u/8837119?v=3","gravatar_id":"","url":"https://api.github.com/users/nizshee","html_url":"https://github.com/nizshee","followers_url":"https://api.github.com/users/nizshee/followers","following_url":"https://api.github.com/users/nizshee/following{/other_user}","gists_url":"https://api.github.com/users/nizshee/gists{/gist_id}","starred_url":"https://api.github.com/users/nizshee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nizshee/subscriptions","organizations_url":"https://api.github.com/users/nizshee/orgs","repos_url":"https://api.github.com/users/nizshee/repos","events_url":"https://api.github.com/users/nizshee/events{/privacy}","received_events_url":"https://api.github.com/users/nizshee/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/nizshee/java2016","description":"","fork":false,"url":"https://api.github.com/repos/nizshee/java2016","forks_url":"https://api.github.com/repos/nizshee/java2016/forks","keys_url":"https://api.github.com/repos/nizshee/java2016/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nizshee/java2016/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nizshee/java2016/teams","hooks_url":"https://api.github.com/repos/nizshee/java2016/hooks","issue_events_url":"https://api.github.com/repos/nizshee/java2016/issues/events{/number}","events_url":"https://api.github.com/repos/nizshee/java2016/events","assignees_url":"https://api.github.com/repos/nizshee/java2016/assignees{/user}","branches_url":"https://api.github.com/repos/nizshee/java2016/branches{/branch}","tags_url":"https://api.github.com/repos/nizshee/java2016/tags","blobs_url":"https://api.github.com/repos/nizshee/java2016/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nizshee/java2016/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nizshee/java2016/git/refs{/sha}","trees_url":"https://api.github.com/repos/nizshee/java2016/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nizshee/java2016/statuses/{sha}","languages_url":"https://api.github.com/repos/nizshee/java2016/languages","stargazers_url":"https://api.github.com/repos/nizshee/java2016/stargazers","contributors_url":"https://api.github.com/repos/nizshee/java2016/contributors","subscribers_url":"https://api.github.com/repos/nizshee/java2016/subscribers","subscription_url":"https://api.github.com/repos/nizshee/java2016/subscription","commits_url":"https://api.github.com/repos/nizshee/java2016/commits{/sha}","git_commits_url":"https://api.github.com/repos/nizshee/java2016/git/commits{/sha}","comments_url":"https://api.github.com/repos/nizshee/java2016/comments{/number}","issue_comment_url":"https://api.github.com/repos/nizshee/java2016/issues/comments{/number}","contents_url":"https://api.github.com/repos/nizshee/java2016/contents/{+path}","compare_url":"https://api.github.com/repos/nizshee/java2016/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nizshee/java2016/merges","archive_url":"https://api.github.com/repos/nizshee/java2016/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nizshee/java2016/downloads","issues_url":"https://api.github.com/repos/nizshee/java2016/issues{/number}","pulls_url":"https://api.github.com/repos/nizshee/java2016/pulls{/number}","milestones_url":"https://api.github.com/repos/nizshee/java2016/milestones{/number}","notifications_url":"https://api.github.com/repos/nizshee/java2016/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nizshee/java2016/labels{/name}","releases_url":"https://api.github.com/repos/nizshee/java2016/releases{/id}","deployments_url":"https://api.github.com/repos/nizshee/java2016/deployments","created_at":"2016-02-26T18:52:50Z","updated_at":"2016-02-26T18:52:50Z","pushed_at":"2016-03-20T17:19:11Z","git_url":"git://github.com/nizshee/java2016.git","ssh_url":"git@github.com:nizshee/java2016.git","clone_url":"https://github.com/nizshee/java2016.git","svn_url":"https://github.com/nizshee/java2016","homepage":null,"size":18,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":3,"forks":0,"open_issues":3,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/nizshee/java2016/pulls/2"},"html":{"href":"https://github.com/nizshee/java2016/pull/2"},"issue":{"href":"https://api.github.com/repos/nizshee/java2016/issues/2"},"comments":{"href":"https://api.github.com/repos/nizshee/java2016/issues/2/comments"},"review_comments":{"href":"https://api.github.com/repos/nizshee/java2016/pulls/2/comments"},"review_comment":{"href":"https://api.github.com/repos/nizshee/java2016/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/nizshee/java2016/pulls/2/commits"},"statuses":{"href":"https://api.github.com/repos/nizshee/java2016/statuses/cfe1aa95c4db2253ba09276e3189137daba525bc"}}}}