Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit a96a0afc81eacb398752d1b551853c7e8c5a2d9e Gareth committed Feb 15, 2012
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
+ <component name="NewModuleRootManager" inherit-compiler-output="false">
+ <output url="file://$MODULE_DIR$/target/classes" />
+ <output-test url="file://$MODULE_DIR$/target/test-classes" />
+ <content url="file://$MODULE_DIR$">
+ <sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
+ <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
+ <excludeFolder url="file://$MODULE_DIR$/target" />
+ </content>
+ <orderEntry type="inheritedJdk" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.8.2" level="project" />
+ <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-library:1.3.RC2" level="project" />
+ <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3.RC2" level="project" />
+ </component>
+</module>
+
1 README
@@ -0,0 +1 @@
+This file was created by IntelliJ IDEA 11.0.2 for binding GitHub repository
26 pom.xml
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>com.logicalpractice</groupId>
+ <artifactId>persistentcollections</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+
+ <dependencies>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.8.2</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.hamcrest</groupId>
+ <artifactId>hamcrest-library</artifactId>
+ <version>1.3.RC2</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) Logical Practice Systems. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+ * which can be found in the file epl-v10.html at the root of this distribution.
+ * By using this software in any fashion, you are agreeing to be bound by
+ * the terms of this license.
+ * You must not remove this notice, or any other, from this software.
+ */
+
+package com.logicalpractice.persistentcollections;
+
+/**
+ *
+ */
+public class RangeSequence implements Sequence<Integer> {
+ private final int from;
+ private final int to;
+ private int step = 1;
+
+ public RangeSequence(int from, int to) {
+ this.from = from;
+ this.to = to;
+ }
+
+ @Override
+ public Integer first() {
+ return from;
+ }
+
+ @Override
+ public Sequence<Integer> rest() {
+ if( from >= to ) return null;
+ return new RangeSequence(from + step, to);
+ }
+}
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) Logical Practice Systems. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+ * which can be found in the file epl-v10.html at the root of this distribution.
+ * By using this software in any fashion, you are agreeing to be bound by
+ * the terms of this license.
+ * You must not remove this notice, or any other, from this software.
+ */
+
+package com.logicalpractice.persistentcollections;
+
+/**
+ */
+public interface Sequence<T> {
+
+ T first();
+
+ Sequence<T> rest();
+}
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) Logical Practice Systems. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+ * which can be found in the file epl-v10.html at the root of this distribution.
+ * By using this software in any fashion, you are agreeing to be bound by
+ * the terms of this license.
+ * You must not remove this notice, or any other, from this software.
+ */
+
+package com.logicalpractice.persistentcollections;
+
+import java.util.Iterator;
+
+/**
+ */
+public class Sequences {
+
+ public static <T> Iterable<T> iterable(Sequence<T> sequence){
+ return new IterableSequence<T>(sequence);
+ }
+
+ public static <T> Iterable<T> headlessIterable(Sequence<T> sequence){
+ return new HeadlessIterable<T>(sequence);
+ }
+
+ private static class IterableSequence<T> implements Iterable<T> {
+
+ private final Sequence<T> sequence;
+
+ public IterableSequence(Sequence<T> sequence) {
+ this.sequence = sequence;
+ }
+
+ @Override
+ public Iterator<T> iterator() {
+ return new SequenceIterator<T>(sequence);
+ }
+ }
+
+ private static class HeadlessIterable<T> implements Iterable<T> {
+
+ private Sequence<T> sequence;
+
+ public HeadlessIterable(Sequence<T> sequence) {
+ this.sequence = sequence;
+ }
+
+ @Override
+ public Iterator<T> iterator() {
+ if( sequence == null ) throw new IllegalStateException("iterator() has been called more than once");
+
+ Iterator<T> iter = new SequenceIterator<T>(sequence);
+ sequence = null; // dereference the sequence
+ return iter;
+ }
+ }
+
+ private static class SequenceIterator<T> implements Iterator<T> {
+
+ private Sequence<T> sequence;
+
+ public SequenceIterator(Sequence<T> sequence) {
+ this.sequence = sequence;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return sequence != null;
+ }
+
+ @Override
+ public T next() {
+ T next = sequence.first();
+ sequence = sequence.rest();
+ return next;
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+ }
+}
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) Logical Practice Systems. All rights reserved.
+ * The use and distribution terms for this software are covered by the
+ * Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
+ * which can be found in the file epl-v10.html at the root of this distribution.
+ * By using this software in any fashion, you are agreeing to be bound by
+ * the terms of this license.
+ * You must not remove this notice, or any other, from this software.
+ */
+
+package com.logicalpractice.persistentcollections;
+
+import org.junit.Test;
+import sun.swing.text.CountingPrintable;
+
+import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThat;
+
+/**
+ *
+ */
+public class SequencesTest {
+
+ @Test
+ public void testIterable() throws Exception {
+ Iterable<Integer> iterable = Sequences.iterable(new RangeSequence(1,10));
+
+ int count = 0;
+ for (Integer i : iterable) {
+ count ++;
+ }
+
+ assertThat(count, equalTo(10));
+
+ // should be repeatable
+ count = 0;
+ for (Integer i : iterable) {
+ count ++;
+ }
+
+ assertThat(count, equalTo(10));
+ }
+}

0 comments on commit a96a0af

Please sign in to comment.