{"id":288,"date":"2019-05-11T19:45:20","date_gmt":"2019-05-12T00:45:20","guid":{"rendered":"http:\/\/horazmakes.com\/blog\/?p=288"},"modified":"2019-05-11T19:45:20","modified_gmt":"2019-05-12T00:45:20","slug":"nextvalcyclicfetcher-v1-0","status":"publish","type":"post","link":"https:\/\/horazmakes.com\/blog\/2019\/05\/11\/nextvalcyclicfetcher-v1-0\/","title":{"rendered":"NextValCyclicFetcher v1.0"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">Fetch your java.util.List&#8217;s next element and restart from the first element automatically, when no more elements left available.<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Code<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">package util;\n\nimport java.util.Iterator;\nimport java.util.List;\nimport java.util.stream.Collectors;\n\n\npublic class NextValCyclicFetcher&lt;T> {\n\n  private List&lt;T> list;\n  private Iterator&lt;T> iterator;\n\n  public NextValCyclicFetcher(List&lt;T> list) {\n    if (null == list) {\n      throw new IllegalArgumentException(\"null\");\n    }\n\n    if (list.isEmpty()) {\n      throw new IllegalArgumentException(\"empty\");\n    }\n\n    this.list = list.stream().collect(Collectors.toList());\n    this.iterator = this.list.iterator();\n  }\n\n  public T next() {\n    if (iterator.hasNext()) {\n      return iterator.next();\n    }\n\n    iterator = list.iterator();\n    return next();\n  }\n}<\/pre>\n\n\n\n<p>It uses standard Java 8 imports.<\/p>\n\n\n\n<p>The passed in List is copied so that if the original list is modified outside the cyclic fetcher, the cyclic fetcher will continue working as expected.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Example<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public void exampleMethod() {\n  List&lt;String> list = Arrays.asList(\"first\", \"second\", \"third\");\n  NextValCyclicFetcher&lt;String> listCyclicFetcher = new NextValCyclicFetcher&lt;>(list);\n\n  \/\/ more code\n\n  while (condition) {    \n    String string = listCyclicFetcher.next();\n    System.out.println(string);\n  }\n}<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Output<\/h4>\n\n\n\n<pre class=\"wp-block-verse\">first<br>second<br>third<br>first<br>second<br>third<br>first<br>...<br>(Until <strong>condition<\/strong> is not satisfied anymore)<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fetch your java.util.List&#8217;s next element and restart from the first element automatically, when no more elements left available.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,86],"tags":[115,116,117,114],"class_list":["post-288","post","type-post","status-publish","format-standard","hentry","category-java","category-programming","tag-cyclic","tag-fetcher","tag-java-util-list","tag-list"],"_links":{"self":[{"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/posts\/288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/comments?post=288"}],"version-history":[{"count":8,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/posts\/288\/revisions"}],"predecessor-version":[{"id":296,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/posts\/288\/revisions\/296"}],"wp:attachment":[{"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/media?parent=288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/categories?post=288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/horazmakes.com\/blog\/wp-json\/wp\/v2\/tags?post=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}