Commits

Stepan Koltsov  committed 63fad38

toString for Either classes

  • Participants
  • Parent commits e069756

Comments (0)

Files changed (2)

File src/main/java/ru/yandex/bolts/collection/Either.java

             return value;
         }
 
+        @Override
+        public String toString() {
+            return "Either.Left(" + value + ")";
+        }
+
     }
 
     private static class Right<A, B> extends Either<A, B> {
         public B getRight() {
             return value;
         }
+
+        @Override
+        public String toString() {
+            return "Either.Right(" + value + ")";
+        }
     }
 
     /** Base class for left and right projection */
             if (isLeft()) return Either.left(f.apply(get()));
             else return either.uncheckedCast();
         }
+
+        @Override
+        public String toString() {
+            return "Either.LeftProjection(" + getO() + ")";
+        }
     }
 
     public static class RightProjection<A, B> extends Projection<A, B, B> {
             if (isRight()) return Either.right(f.apply(get()));
             else return either.uncheckedCast();
         }
+
+        @Override
+        public String toString() {
+            return "Either.RightProjection(" + getO() + ")";
+        }
+
     }
 
     public static <A, B> Either<A, B> left(A a) { return new Left<A, B>(a); }

File src/main/java/ru/yandex/bolts/collection/EitherTest.java

+package ru.yandex.bolts.collection;
+
+import ru.yandex.bolts.collection.Either.Projection;
+import junit.framework.TestCase;
+
+/**
+ * @author ssytnik
+ */
+public class EitherTest extends TestCase {
+
+    public void testToString() {
+        Either<Long, String> either;
+        Projection<Long, String, ?> projection;
+
+        either = Either.left(123L);
+        assertEquals("Either.Left(123)", either.toString());
+        projection = either.left();
+        assertEquals("Either.LeftProjection(Some(123))", projection.toString());
+
+        either = Either.right("str");
+        assertEquals("Either.Right(str)", either.toString());
+        projection = either.right();
+        assertEquals("Either.RightProjection(Some(str))", projection.toString());
+
+        projection = either.left();
+        assertEquals("Either.LeftProjection(None)", projection.toString());
+    }
+
+}