Skip to content
peichhorn edited this page Jul 3, 2012 · 1 revision

Yield.yield(...)

Overview

Allows for the creation of a generator which is presented as an Iterable interface. Iteration over elements is done via a state machine, rather than creating all elements at once, so client code can discontinue looping when required, without sacrificing memory.

Example

With Lombok

import static lombok.Yield.yield;

import java.io.BufferedReader;
import java.io.Reader;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class Lines {

  public static Iterable<String> in(final Reader reader) {
    return in(new BufferedReader(reader));
  }

  private static Iterable<String> in(final BufferedReader in) {
    try {
      for (String next = in.readLine(); next != null; next = in.readLine()) yield(next);
    } finally {
      in.close();
    }
  }
}

Vanilla Java

import java.io.BufferedReader;
import java.io.Reader;

public final class Lines {
  
  public static Iterable<String> in(final Reader reader) {
    return in(new BufferedReader(reader));
  }
  
  @java.lang.SuppressWarnings("all")
  private static Iterable<String> in(final BufferedReader in) {
    
    class $YielderIn implements java.util.Iterator<java.lang.String>, java.lang.Iterable<java.lang.String>, java.io.Closeable {
      private String next;
      private java.lang.Throwable $yieldException1;
      private int $state1;
      private int $state;
      private boolean $hasNext;
      private boolean $nextDefined;
      private java.lang.String $next;
      
      private $YielderIn() {
        super();
      }
      
      public java.util.Iterator<java.lang.String> iterator() {
        if ($state == 0) {
          $state = 1;
          return this;
        } else return new $YielderIn();
      }
      
      public boolean hasNext() {
        if (!$nextDefined) {
          $hasNext = getNext();
          $nextDefined = true;
        }
        return $hasNext;
      }
      
      public java.lang.String next() {
        if (!hasNext()) {
          throw new java.util.NoSuchElementException();
        }
        $nextDefined = false;
        return $next;
      }
      
      public void remove() {
        throw new java.lang.UnsupportedOperationException();
      }
      
      public void close() {
        do switch ($state) {
        case 4: 
          $state1 = 6;
          $state = 5;
          continue;
        
        default: 
          $state = 6;
          return;
        
        }         while (getNext());
      }
      
      private boolean getNext() {
        java.lang.Throwable $yieldException;
        while (true) {
          try {
            switch ($state) {
            case 0: 
              $state = 1;
            
            case 1: 
              $yieldException1 = null;
              $state1 = 6;
              $state = 2;
            
            case 2: 
              next = in.readLine();
            
            case 3: 
              if (!(next != null)) {
                $state = 5;
                continue;
              }
              $next = next;
              $state = 4;
              return true;
            
            case 4: 
              next = in.readLine();
              $state = 3;
              continue;
            
            case 5: 
              {
                in.close();
              }
              if ($yieldException1 != null) {
                $yieldException = $yieldException1;
                break;
              }
              $state = $state1;
              continue;
            
            case 6: 
            
            default: 
              return false;
            
            }
          } catch (final java.lang.Throwable $yieldExceptionCaught) {
            $yieldException = $yieldExceptionCaught;
          }
          switch ($state) {
          case 2: 
          
          case 3: 
          
          case 4: 
            $yieldException1 = $yieldException;
            $state = 5;
            continue;
          
          default: 
            $state = 6;
            java.util.ConcurrentModificationException $yieldExceptionUnhandled = new java.util.ConcurrentModificationException();
            $yieldExceptionUnhandled.initCause($yieldException);
            throw $yieldExceptionUnhandled;
          
          }
        }
      }
    }
    return new $YielderIn();
  }
  
  @java.lang.SuppressWarnings("all")
  private Lines() {
  }
}

Behind the Scenes

(Documentation pending)

Configuration

Nothing to configure yet.


Credit where credit is due:

Yield is based on the idea, algorithms and sources of Arthur and Vladimir Nesterovsky, who generously allowed me to use them.