{"action":"created","comment":{"url":"https://api.github.com/repos/java-course-au/assignments-2/pulls/comments/53595078","id":53595078,"diff_hunk":"@@ -0,0 +1,136 @@\n+package ru.spbau.mit;\n+\n+import org.junit.Test;\n+import static org.junit.Assert.*;\n+\n+import java.util.*;\n+import java.util.function.Function;\n+import java.util.function.Supplier;\n+\n+public class LazyFactoryTest {\n+    private abstract static class CounterSupplier<T> implements Supplier<T> {\n+        protected T result;\n+        protected int counter = 0;\n+\n+        public CounterSupplier(T result) {\n+            this.result = result;\n+        }\n+\n+        public int getCounter() {\n+            return counter;\n+        }\n+    }\n+\n+    private static class IntegerCounterSupplier extends CounterSupplier<Integer> {\n+        private static final int RESULT = 10;\n+        public IntegerCounterSupplier() {\n+            super(RESULT);\n+        }\n+\n+        @Override\n+        public Integer get() {\n+            counter++;\n+            return result;\n+        }\n+    }\n+\n+    private static class NullSupplier <T> extends CounterSupplier<T> {\n+        public NullSupplier() {\n+            super(null);\n+        }\n+\n+        @Override\n+        public T get() {\n+            counter++;\n+            return null;\n+        }\n+    }\n+\n+    public <T> void checkCounterAndSingleResult(Function<CounterSupplier<T>, Lazy<T>> getLazyFactory,\n+                                                CounterSupplier<T> supplier,\n+                                                boolean areMultipleCalculationsAllowed) {\n+        final int ITERATION_NUMBER = 100;\n+\n+        Lazy<T> lazy = getLazyFactory.apply(supplier);\n+        assertTrue(supplier.getCounter() == 0);\n+        List<T> results = new ArrayList<>();\n+        for (int i = 0; i < ITERATION_NUMBER; i++) {\n+            results.add(lazy.get());\n+        }\n+        for (int i = 0; i < ITERATION_NUMBER; i++) {\n+            assertTrue(results.get(0) == results.get(i));\n+        }\n+        if (!areMultipleCalculationsAllowed) {\n+            assertEquals(supplier.getCounter(), 1);\n+        }\n+        assertEquals(results.get(0), supplier.get());\n+    }\n+\n+    public <T> void checkMultiThreading(Function<CounterSupplier<T>, Lazy<T>> getLazyFactory,\n+                                        CounterSupplier<T> supplier, boolean areMultipleCalculationsAllowed) {\n+        final int THREAD_NUMBER = 100;\n+\n+        Lazy<T> lazy = getLazyFactory.apply(supplier);\n+        assertTrue(supplier.getCounter() == 0);\n+        List<Thread> threads = new ArrayList<>();\n+        List<T> results = Collections.synchronizedList(new ArrayList<>());\n+\n+        for (int i = 0; i < THREAD_NUMBER; i++) {\n+            threads.add(new Thread(new Runnable() {\n+                @Override\n+                public void run() {\n+                    results.add(lazy.get());\n+                }\n+            }));\n+        }\n+        for (Thread thread : threads) {\n+            thread.start();\n+        }\n+        for (Thread thread : threads) {\n+            try {\n+                thread.join();\n+            } catch (InterruptedException e) {\n+                e.printStackTrace();\n+            }\n+        }\n+\n+        for (int i = 0; i < THREAD_NUMBER; i++) {\n+            if (results.get(0) != results.get(i)) {\n+                System.out.println(results.get(0));\n+                System.out.println(results.get(i));\n+            }\n+            assertTrue(results.get(0) == results.get(i));\n+        }\n+        if (!areMultipleCalculationsAllowed) {\n+            assertEquals(supplier.getCounter(), 1);\n+        }\n+    }\n+\n+    public <T> void checkNullSupplier(Function<CounterSupplier<T>, Lazy<T>> getLazyFactory,\n+                                      NullSupplier supplier, boolean areMultipleCalculationsAllowed) {","path":"src/test/java/ru/spbau/mit/LazyFactoryTest.java","position":110,"original_position":110,"commit_id":"ce368ed227f9c3a9621e39e95cf80abc00d8d236","original_commit_id":"ce368ed227f9c3a9621e39e95cf80abc00d8d236","user":{"login":"dzharkov","id":863207,"avatar_url":"https://avatars.githubusercontent.com/u/863207?v=3","gravatar_id":"","url":"https://api.github.com/users/dzharkov","html_url":"https://github.com/dzharkov","followers_url":"https://api.github.com/users/dzharkov/followers","following_url":"https://api.github.com/users/dzharkov/following{/other_user}","gists_url":"https://api.github.com/users/dzharkov/gists{/gist_id}","starred_url":"https://api.github.com/users/dzharkov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dzharkov/subscriptions","organizations_url":"https://api.github.com/users/dzharkov/orgs","repos_url":"https://api.github.com/users/dzharkov/repos","events_url":"https://api.github.com/users/dzharkov/events{/privacy}","received_events_url":"https://api.github.com/users/dzharkov/received_events","type":"User","site_admin":false},"body":"Зачем здесь параметр `supplier`, если его значение это одно и то же выражение?","created_at":"2016-02-22T08:06:52Z","updated_at":"2016-02-22T08:06:52Z","html_url":"https://github.com/java-course-au/assignments-2/pull/6#discussion_r53595078","pull_request_url":"https://api.github.com/repos/java-course-au/assignments-2/pulls/6","_links":{"self":{"href":"https://api.github.com/repos/java-course-au/assignments-2/pulls/comments/53595078"},"html":{"href":"https://github.com/java-course-au/assignments-2/pull/6#discussion_r53595078"},"pull_request":{"href":"https://api.github.com/repos/java-course-au/assignments-2/pulls/6"}}},"pull_request":{"url":"https://api.github.com/repos/java-course-au/assignments-2/pulls/6","id":59694028,"html_url":"https://github.com/java-course-au/assignments-2/pull/6","diff_url":"https://github.com/java-course-au/assignments-2/pull/6.diff","patch_url":"https://github.com/java-course-au/assignments-2/pull/6.patch","issue_url":"https://api.github.com/repos/java-course-au/assignments-2/issues/6","number":6,"state":"open","locked":false,"title":"Java02. ДЗ 01, Подгузов Никита, подгруппа 1","user":{"login":"Nikitosh","id":11414730,"avatar_url":"https://avatars.githubusercontent.com/u/11414730?v=3","gravatar_id":"","url":"https://api.github.com/users/Nikitosh","html_url":"https://github.com/Nikitosh","followers_url":"https://api.github.com/users/Nikitosh/followers","following_url":"https://api.github.com/users/Nikitosh/following{/other_user}","gists_url":"https://api.github.com/users/Nikitosh/gists{/gist_id}","starred_url":"https://api.github.com/users/Nikitosh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Nikitosh/subscriptions","organizations_url":"https://api.github.com/users/Nikitosh/orgs","repos_url":"https://api.github.com/users/Nikitosh/repos","events_url":"https://api.github.com/users/Nikitosh/events{/privacy}","received_events_url":"https://api.github.com/users/Nikitosh/received_events","type":"User","site_admin":false},"body":"@dzharkov \r\nHW01 modified","created_at":"2016-02-17T19:18:57Z","updated_at":"2016-02-22T08:06:52Z","closed_at":null,"merged_at":null,"merge_commit_sha":"9cdb3121309c72a4714cfb7d6fe8bb0f7c5489ac","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/java-course-au/assignments-2/pulls/6/commits","review_comments_url":"https://api.github.com/repos/java-course-au/assignments-2/pulls/6/comments","review_comment_url":"https://api.github.com/repos/java-course-au/assignments-2/pulls/comments{/number}","comments_url":"https://api.github.com/repos/java-course-au/assignments-2/issues/6/comments","statuses_url":"https://api.github.com/repos/java-course-au/assignments-2/statuses/ce368ed227f9c3a9621e39e95cf80abc00d8d236","head":{"label":"Nikitosh:master","ref":"master","sha":"ce368ed227f9c3a9621e39e95cf80abc00d8d236","user":{"login":"Nikitosh","id":11414730,"avatar_url":"https://avatars.githubusercontent.com/u/11414730?v=3","gravatar_id":"","url":"https://api.github.com/users/Nikitosh","html_url":"https://github.com/Nikitosh","followers_url":"https://api.github.com/users/Nikitosh/followers","following_url":"https://api.github.com/users/Nikitosh/following{/other_user}","gists_url":"https://api.github.com/users/Nikitosh/gists{/gist_id}","starred_url":"https://api.github.com/users/Nikitosh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Nikitosh/subscriptions","organizations_url":"https://api.github.com/users/Nikitosh/orgs","repos_url":"https://api.github.com/users/Nikitosh/repos","events_url":"https://api.github.com/users/Nikitosh/events{/privacy}","received_events_url":"https://api.github.com/users/Nikitosh/received_events","type":"User","site_admin":false},"repo":{"id":51816626,"name":"assignments-2","full_name":"Nikitosh/assignments-2","owner":{"login":"Nikitosh","id":11414730,"avatar_url":"https://avatars.githubusercontent.com/u/11414730?v=3","gravatar_id":"","url":"https://api.github.com/users/Nikitosh","html_url":"https://github.com/Nikitosh","followers_url":"https://api.github.com/users/Nikitosh/followers","following_url":"https://api.github.com/users/Nikitosh/following{/other_user}","gists_url":"https://api.github.com/users/Nikitosh/gists{/gist_id}","starred_url":"https://api.github.com/users/Nikitosh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Nikitosh/subscriptions","organizations_url":"https://api.github.com/users/Nikitosh/orgs","repos_url":"https://api.github.com/users/Nikitosh/repos","events_url":"https://api.github.com/users/Nikitosh/events{/privacy}","received_events_url":"https://api.github.com/users/Nikitosh/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Nikitosh/assignments-2","description":"","fork":true,"url":"https://api.github.com/repos/Nikitosh/assignments-2","forks_url":"https://api.github.com/repos/Nikitosh/assignments-2/forks","keys_url":"https://api.github.com/repos/Nikitosh/assignments-2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Nikitosh/assignments-2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Nikitosh/assignments-2/teams","hooks_url":"https://api.github.com/repos/Nikitosh/assignments-2/hooks","issue_events_url":"https://api.github.com/repos/Nikitosh/assignments-2/issues/events{/number}","events_url":"https://api.github.com/repos/Nikitosh/assignments-2/events","assignees_url":"https://api.github.com/repos/Nikitosh/assignments-2/assignees{/user}","branches_url":"https://api.github.com/repos/Nikitosh/assignments-2/branches{/branch}","tags_url":"https://api.github.com/repos/Nikitosh/assignments-2/tags","blobs_url":"https://api.github.com/repos/Nikitosh/assignments-2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Nikitosh/assignments-2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Nikitosh/assignments-2/git/refs{/sha}","trees_url":"https://api.github.com/repos/Nikitosh/assignments-2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Nikitosh/assignments-2/statuses/{sha}","languages_url":"https://api.github.com/repos/Nikitosh/assignments-2/languages","stargazers_url":"https://api.github.com/repos/Nikitosh/assignments-2/stargazers","contributors_url":"https://api.github.com/repos/Nikitosh/assignments-2/contributors","subscribers_url":"https://api.github.com/repos/Nikitosh/assignments-2/subscribers","subscription_url":"https://api.github.com/repos/Nikitosh/assignments-2/subscription","commits_url":"https://api.github.com/repos/Nikitosh/assignments-2/commits{/sha}","git_commits_url":"https://api.github.com/repos/Nikitosh/assignments-2/git/commits{/sha}","comments_url":"https://api.github.com/repos/Nikitosh/assignments-2/comments{/number}","issue_comment_url":"https://api.github.com/repos/Nikitosh/assignments-2/issues/comments{/number}","contents_url":"https://api.github.com/repos/Nikitosh/assignments-2/contents/{+path}","compare_url":"https://api.github.com/repos/Nikitosh/assignments-2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Nikitosh/assignments-2/merges","archive_url":"https://api.github.com/repos/Nikitosh/assignments-2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Nikitosh/assignments-2/downloads","issues_url":"https://api.github.com/repos/Nikitosh/assignments-2/issues{/number}","pulls_url":"https://api.github.com/repos/Nikitosh/assignments-2/pulls{/number}","milestones_url":"https://api.github.com/repos/Nikitosh/assignments-2/milestones{/number}","notifications_url":"https://api.github.com/repos/Nikitosh/assignments-2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Nikitosh/assignments-2/labels{/name}","releases_url":"https://api.github.com/repos/Nikitosh/assignments-2/releases{/id}","deployments_url":"https://api.github.com/repos/Nikitosh/assignments-2/deployments","created_at":"2016-02-16T07:25:59Z","updated_at":"2016-02-16T07:26:00Z","pushed_at":"2016-02-17T23:26:19Z","git_url":"git://github.com/Nikitosh/assignments-2.git","ssh_url":"git@github.com:Nikitosh/assignments-2.git","clone_url":"https://github.com/Nikitosh/assignments-2.git","svn_url":"https://github.com/Nikitosh/assignments-2","homepage":null,"size":23,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"java-course-au:master","ref":"master","sha":"106ee086c7eb27c84b9ad1359a228971c1fe19dc","user":{"login":"java-course-au","id":14248092,"avatar_url":"https://avatars.githubusercontent.com/u/14248092?v=3","gravatar_id":"","url":"https://api.github.com/users/java-course-au","html_url":"https://github.com/java-course-au","followers_url":"https://api.github.com/users/java-course-au/followers","following_url":"https://api.github.com/users/java-course-au/following{/other_user}","gists_url":"https://api.github.com/users/java-course-au/gists{/gist_id}","starred_url":"https://api.github.com/users/java-course-au/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/java-course-au/subscriptions","organizations_url":"https://api.github.com/users/java-course-au/orgs","repos_url":"https://api.github.com/users/java-course-au/repos","events_url":"https://api.github.com/users/java-course-au/events{/privacy}","received_events_url":"https://api.github.com/users/java-course-au/received_events","type":"Organization","site_admin":false},"repo":{"id":51697606,"name":"assignments-2","full_name":"java-course-au/assignments-2","owner":{"login":"java-course-au","id":14248092,"avatar_url":"https://avatars.githubusercontent.com/u/14248092?v=3","gravatar_id":"","url":"https://api.github.com/users/java-course-au","html_url":"https://github.com/java-course-au","followers_url":"https://api.github.com/users/java-course-au/followers","following_url":"https://api.github.com/users/java-course-au/following{/other_user}","gists_url":"https://api.github.com/users/java-course-au/gists{/gist_id}","starred_url":"https://api.github.com/users/java-course-au/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/java-course-au/subscriptions","organizations_url":"https://api.github.com/users/java-course-au/orgs","repos_url":"https://api.github.com/users/java-course-au/repos","events_url":"https://api.github.com/users/java-course-au/events{/privacy}","received_events_url":"https://api.github.com/users/java-course-au/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/java-course-au/assignments-2","description":"","fork":false,"url":"https://api.github.com/repos/java-course-au/assignments-2","forks_url":"https://api.github.com/repos/java-course-au/assignments-2/forks","keys_url":"https://api.github.com/repos/java-course-au/assignments-2/keys{/key_id}","collaborators_url":"https://api.github.com/repos/java-course-au/assignments-2/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/java-course-au/assignments-2/teams","hooks_url":"https://api.github.com/repos/java-course-au/assignments-2/hooks","issue_events_url":"https://api.github.com/repos/java-course-au/assignments-2/issues/events{/number}","events_url":"https://api.github.com/repos/java-course-au/assignments-2/events","assignees_url":"https://api.github.com/repos/java-course-au/assignments-2/assignees{/user}","branches_url":"https://api.github.com/repos/java-course-au/assignments-2/branches{/branch}","tags_url":"https://api.github.com/repos/java-course-au/assignments-2/tags","blobs_url":"https://api.github.com/repos/java-course-au/assignments-2/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/java-course-au/assignments-2/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/java-course-au/assignments-2/git/refs{/sha}","trees_url":"https://api.github.com/repos/java-course-au/assignments-2/git/trees{/sha}","statuses_url":"https://api.github.com/repos/java-course-au/assignments-2/statuses/{sha}","languages_url":"https://api.github.com/repos/java-course-au/assignments-2/languages","stargazers_url":"https://api.github.com/repos/java-course-au/assignments-2/stargazers","contributors_url":"https://api.github.com/repos/java-course-au/assignments-2/contributors","subscribers_url":"https://api.github.com/repos/java-course-au/assignments-2/subscribers","subscription_url":"https://api.github.com/repos/java-course-au/assignments-2/subscription","commits_url":"https://api.github.com/repos/java-course-au/assignments-2/commits{/sha}","git_commits_url":"https://api.github.com/repos/java-course-au/assignments-2/git/commits{/sha}","comments_url":"https://api.github.com/repos/java-course-au/assignments-2/comments{/number}","issue_comment_url":"https://api.github.com/repos/java-course-au/assignments-2/issues/comments{/number}","contents_url":"https://api.github.com/repos/java-course-au/assignments-2/contents/{+path}","compare_url":"https://api.github.com/repos/java-course-au/assignments-2/compare/{base}...{head}","merges_url":"https://api.github.com/repos/java-course-au/assignments-2/merges","archive_url":"https://api.github.com/repos/java-course-au/assignments-2/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/java-course-au/assignments-2/downloads","issues_url":"https://api.github.com/repos/java-course-au/assignments-2/issues{/number}","pulls_url":"https://api.github.com/repos/java-course-au/assignments-2/pulls{/number}","milestones_url":"https://api.github.com/repos/java-course-au/assignments-2/milestones{/number}","notifications_url":"https://api.github.com/repos/java-course-au/assignments-2/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/java-course-au/assignments-2/labels{/name}","releases_url":"https://api.github.com/repos/java-course-au/assignments-2/releases{/id}","deployments_url":"https://api.github.com/repos/java-course-au/assignments-2/deployments","created_at":"2016-02-14T14:22:46Z","updated_at":"2016-02-14T14:52:34Z","pushed_at":"2016-02-22T06:39:26Z","git_url":"git://github.com/java-course-au/assignments-2.git","ssh_url":"git@github.com:java-course-au/assignments-2.git","clone_url":"https://github.com/java-course-au/assignments-2.git","svn_url":"https://github.com/java-course-au/assignments-2","homepage":null,"size":133,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":13,"mirror_url":null,"open_issues_count":20,"forks":13,"open_issues":20,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/java-course-au/assignments-2/pulls/6"},"html":{"href":"https://github.com/java-course-au/assignments-2/pull/6"},"issue":{"href":"https://api.github.com/repos/java-course-au/assignments-2/issues/6"},"comments":{"href":"https://api.github.com/repos/java-course-au/assignments-2/issues/6/comments"},"review_comments":{"href":"https://api.github.com/repos/java-course-au/assignments-2/pulls/6/comments"},"review_comment":{"href":"https://api.github.com/repos/java-course-au/assignments-2/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/java-course-au/assignments-2/pulls/6/commits"},"statuses":{"href":"https://api.github.com/repos/java-course-au/assignments-2/statuses/ce368ed227f9c3a9621e39e95cf80abc00d8d236"}}}}