<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: jdk5 code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 03:01:54 GMT</pubDate>
    <description>DZone Snippets: jdk5 code</description>
    <item>
      <title> range() in Java</title>
      <link>http://snippets.dzone.com/posts/show/3867</link>
      <description>A range is a very handy feature of programing languages like Python.&lt;br /&gt;&lt;br /&gt;    * range( 10 ) -&gt; 0 1 2 3 4 5 6 7 8 9&lt;br /&gt;    * range( 5, 10 ) -&gt; 5 6 7 8 9&lt;br /&gt;    * range( 0, 10, 3 ) -&gt; 0 3 6 9&lt;br /&gt;    * range( '0', '9' ) -&gt; 012345678&lt;br /&gt;&lt;br /&gt;With an extended for loop it is possible to use such a feature too:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/*&lt;br /&gt; * This project is made available under the terms of the BSD license, more information can be found at&lt;br /&gt; * http://www.opensource.org/licenses/bsd-license.html&lt;br /&gt; *&lt;br /&gt; * Copyright (c) 2007. Christian Ullenboom (http://www.tutego.com/) and contributors. All rights reserved.&lt;br /&gt; */&lt;br /&gt;package com.tutego;&lt;br /&gt;&lt;br /&gt;import java.util.Iterator;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Class that generates immutable sequences (ranges) as Iterable&lt;Integer&gt;&lt;br /&gt; * objects. A range represents a start (0 if not given), an stop (mandatory) and&lt;br /&gt; * an optional step (1 by default). The start value is included in the range,&lt;br /&gt; * the stop value is exclusive. Every range is handled by an Iterable&lt;Integer&gt;&lt;br /&gt; * which can by used in an extended for loop.&lt;br /&gt; * &lt;br /&gt; * &lt;pre&gt;&lt;br /&gt; * for ( int i : range( 0, 10, 3 ) )&lt;br /&gt; *   System.out.print( i + " " ); // 0 3 6 9&lt;br /&gt; * &lt;/pre&gt;&lt;br /&gt; * &lt;br /&gt; * @author Christian Ullenboom (tutego)&lt;br /&gt; * @version 1.0&lt;br /&gt; */&lt;br /&gt;public class Range&lt;br /&gt;{&lt;br /&gt;  public static Iterable&lt;Integer&gt; range( final int start, final int stop, final int step )&lt;br /&gt;  {&lt;br /&gt;    if ( step &lt;= 0 )&lt;br /&gt;      throw new IllegalArgumentException( "step &gt; 0 isrequired!" );&lt;br /&gt;&lt;br /&gt;    return new Iterable&lt;Integer&gt;()&lt;br /&gt;    {&lt;br /&gt;      public Iterator&lt;Integer&gt; iterator()&lt;br /&gt;      {&lt;br /&gt;        return new Iterator&lt;Integer&gt;()&lt;br /&gt;        {&lt;br /&gt;          private int counter = start;&lt;br /&gt;&lt;br /&gt;          public boolean hasNext()&lt;br /&gt;          {&lt;br /&gt;            return counter &lt; stop;&lt;br /&gt;          }&lt;br /&gt;&lt;br /&gt;          public Integer next()&lt;br /&gt;          {&lt;br /&gt;            try&lt;br /&gt;            {&lt;br /&gt;              return counter;&lt;br /&gt;            }&lt;br /&gt;            finally { counter += step; }&lt;br /&gt;          }&lt;br /&gt;&lt;br /&gt;          public void remove() { }&lt;br /&gt;        };&lt;br /&gt;      }&lt;br /&gt;    };&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static Iterable&lt;Integer&gt; range( final int start, final int stop )&lt;br /&gt;  {&lt;br /&gt;    return range( start, stop, 1 );&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public static Iterable&lt;Integer&gt; range( final int stop )&lt;br /&gt;  {&lt;br /&gt;    return range( 0, stop, 1 );&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This is an example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;package com.tutego;&lt;br /&gt;&lt;br /&gt;import static com.tutego.Range.range;&lt;br /&gt;&lt;br /&gt;public class RangeDemo&lt;br /&gt;{&lt;br /&gt;  public static void main( String[] args )&lt;br /&gt;  {&lt;br /&gt;    for ( int i : range( 10 ) )&lt;br /&gt;      System.out.print( i + " " );&lt;br /&gt;&lt;br /&gt;    System.out.println();&lt;br /&gt;&lt;br /&gt;    for ( int i : range( 5, 10 ) )&lt;br /&gt;      System.out.print( i + " " );&lt;br /&gt;&lt;br /&gt;    System.out.println();&lt;br /&gt;&lt;br /&gt;    for ( int i : range( 0, 10, 3 ) )&lt;br /&gt;      System.out.print( i + " " );&lt;br /&gt;&lt;br /&gt;    System.out.println();&lt;br /&gt;&lt;br /&gt;    for ( int i : range( '0', '9' ) )&lt;br /&gt;      System.out.print( (char) i  );&lt;br /&gt;&lt;br /&gt;    System.out.println();&lt;br /&gt;&lt;br /&gt;    String[] a = { "Mary", "had", "a", "little", "lamb" };&lt;br /&gt;    for ( int i : range(a.length ) )&lt;br /&gt;      System.out.printf( "%d %s%n", i, a[i] );&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 24 Apr 2007 13:20:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3867</guid>
      <author>tutego (Chrsitian Ullenboom)</author>
    </item>
    <item>
      <title>Handy Iterable for a range of Integers</title>
      <link>http://snippets.dzone.com/posts/show/3792</link>
      <description>Looping a specific number of times produces code a bit too verbose in Java.  The JDK5 enhanced for statement is a handy improvement, but you still have to fall back to the traditional for statement if you want to repeat a loop, say, 10 times.&lt;br /&gt;&lt;br /&gt;Not with this implementation of Iterable.  You can use it like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;for (int i : new Range(100))&lt;br /&gt;    System.out.printf("I have said this %d times. Read dzone.com!%n", i);&lt;br /&gt;&lt;br /&gt;class Range implements Iterable&lt;Integer&gt; {&lt;br /&gt;    &lt;br /&gt;    private final Integer stop;&lt;br /&gt;    &lt;br /&gt;    public Range(int stop) {&lt;br /&gt;        this.stop = stop;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public Iterator&lt;Integer&gt; iterator() {&lt;br /&gt;        return new Iterator&lt;Integer&gt;() {&lt;br /&gt;            &lt;br /&gt;            private Integer counter = 0;&lt;br /&gt;&lt;br /&gt;            public boolean hasNext() {&lt;br /&gt;                return (counter != stop);&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            public Integer next() {&lt;br /&gt;                if (counter == stop)&lt;br /&gt;                    throw new NoSuchElementException();&lt;br /&gt;                return ++counter;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            public void remove() {&lt;br /&gt;            }};&lt;br /&gt;    }&lt;br /&gt;    &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Apr 2007 14:39:26 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3792</guid>
      <author>asgeirn (Asgeir S. Nilsen)</author>
    </item>
  </channel>
</rss>
