├── LICENSE
├── pom.xml
├── README.md
└── src
├── test
└── java
│ └── io
│ └── herrmann
│ └── generator
│ └── GeneratorTest.java
└── main
└── java
└── io
└── herrmann
└── generator
└── Generator.java
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Michael Herrmann.
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 |
5 | io.herrmann
6 | generator
7 | 1.0-SNAPSHOT
8 | jar
9 |
10 | java-generator-functions
11 | An implementation of a Python-like yield(...) method in Java.
12 | https://github.com/mherrmann/java-generator-functions
13 |
14 |
15 |
16 | MIT License
17 | http://www.opensource.org/licenses/mit-license.php
18 | repo
19 |
20 |
21 |
22 |
23 |
24 | Michael Herrmann
25 | michael@herrmann.io
26 |
27 |
28 |
29 |
30 | scm:git:git@github.com:mherrmann/java-generator-functions.git
31 | scm:git:git@github.com:mherrmann/java-generator-functions.git
32 | scm:git:git@github.com:mherrmann/java-generator-functions.git
33 |
34 |
35 |
36 | UTF-8
37 |
38 |
39 |
40 |
41 | junit
42 | junit
43 | 4.11
44 | test
45 |
46 |
47 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | java-generator-functions
2 | ========================
3 |
4 | An implementation of Python-like generator functions in Java. This repository contains a single class, `Generator` with a method `yield(...)` which can be used to mimic the behaviour of the `yield` keyword in Python.
5 |
6 | Examples
7 | --------
8 | The following is a simple generator that yields `1` and then `2`:
9 |
10 | ```java
11 | Generator simpleGenerator = new Generator() {
12 | public void run() throws InterruptedException {
13 | yield(1);
14 | // Some logic here...
15 | yield(2);
16 | }
17 | };
18 | for (Integer element : simpleGenerator)
19 | System.out.println(element);
20 | // Prints "1", then "2".
21 | ```
22 |
23 | Infinite generators are also possible:
24 |
25 | ```java
26 | Generator infiniteGenerator = new Generator() {
27 | public void run() throws InterruptedException {
28 | while (true)
29 | yield(1);
30 | }
31 | };
32 | ```
33 |
34 | The `Generator` class lies in package `io.herrmann.generator`. So you need to `import io.herrmann.generator.Generator;` in order for the above examples to work.
35 |
36 | Usage
37 | -----
38 |
39 | This package is hosted as a Maven repository with the following url:
40 |
41 | http://dl.bintray.com/filipmalczak/maven
42 |
43 | To use it from Maven, add the following to your `pom.xml`:
44 |
45 | ```xml
46 |
47 | ...
48 |
49 | ...
50 |
51 | java-generator-functions
52 | http://dl.bintray.com/filipmalczak/maven
53 |
54 |
55 | ...
56 |
57 |
58 | io.herrmann
59 | java-generator-functions
60 | 1.0
61 |
62 |
63 |
64 | ```
65 |
66 | For Gradle:
67 |
68 | ```gradle
69 | compile(group: 'io.herrmann', name: 'java-generator-functions', version: '1.0')
70 | ```
71 |
72 | Caveats and Performance
73 | -----------------------
74 | The `Generator` class internally works with a Thread to produce the items. It does ensure that no Threads stay around if the corresponding Generator is no longer used. However:
75 |
76 | **If too many `Generator`s are created before the JVM gets a chance to garbage collect the old ones, you may encounter `OutOfMemoryError`s. This problem most strongly presents itself on OS X where the maximum number of Threads is significantly lower than on other OSs (around 2000).**
77 |
78 | The performance is obviously not great but not too shabby either. On my machine with a dual core i5 CPU @ 2.67 GHz, 1000 items can be produced in < 0.03s.
79 |
80 | Contributing
81 | ------------
82 | Contributions and pull requests are welcome. Please ensure that `mvn test` still passes and add any unit tests as you see fit. Please also follow the same coding conventions, in particular the line limit of 80 characters and the use of tabs instead of spaces.
83 |
--------------------------------------------------------------------------------
/src/test/java/io/herrmann/generator/GeneratorTest.java:
--------------------------------------------------------------------------------
1 | package io.herrmann.generator;
2 |
3 | import org.junit.Test;
4 |
5 | import java.util.ArrayList;
6 | import java.util.Arrays;
7 | import java.util.Iterator;
8 | import java.util.List;
9 |
10 | import static org.junit.Assert.assertEquals;
11 | import static org.junit.Assert.assertTrue;
12 |
13 | public class GeneratorTest {
14 | @Test
15 | public void testEmptyGenerator() {
16 | assertEquals(new ArrayList