# HG changeset patch # User Jaroslav Tulach # Date 1346616013 -7200 # Node ID ebe08056c60c730c41c82e66a67541451a5ed23a # Parent e25dbfce40e98170ec77a049f2f68badef813bc5 Identifying code snippets to be used on my blog diff -r e25dbfce40e9 -r ebe08056c60c samples/effectivelist/src/org/apidesign/effectivelist/List.scala --- a/samples/effectivelist/src/org/apidesign/effectivelist/List.scala Fri Aug 31 20:16:57 2012 +0200 +++ b/samples/effectivelist/src/org/apidesign/effectivelist/List.scala Sun Sep 02 22:00:13 2012 +0200 @@ -8,6 +8,7 @@ * case class Person(name : String) extends Listable[Person] * */ +// BEGIN: effectivelist.list final class List[T <: Listable[T]] { private var item : T = _ private final var EMPTY : T = _ @@ -72,3 +73,4 @@ } } } +// END: effectivelist.list diff -r e25dbfce40e9 -r ebe08056c60c samples/effectivelist/src/org/apidesign/effectivelist/Listable.scala --- a/samples/effectivelist/src/org/apidesign/effectivelist/Listable.scala Fri Aug 31 20:16:57 2012 +0200 +++ b/samples/effectivelist/src/org/apidesign/effectivelist/Listable.scala Sun Sep 02 22:00:13 2012 +0200 @@ -3,7 +3,9 @@ /** A member of a {@link List} that makes keeping references * to previous and next items in the list effective. */ +// BEGIN: effectivelist.item trait Listable[T <: Listable[T]] { private[effectivelist] var prev : T = _ private[effectivelist] var next : T = _ } +// END: effectivelist.item diff -r e25dbfce40e9 -r ebe08056c60c samples/effectivelist/test/org/apidesign/effectivelist/ListMixinTest.scala --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/samples/effectivelist/test/org/apidesign/effectivelist/ListMixinTest.scala Sun Sep 02 22:00:13 2012 +0200 @@ -0,0 +1,26 @@ +package org.apidesign.effectivelist + +import org.junit._ +import Assert._ + +// BEGIN: effectivelist.point +class Point(val x: Int, val y : Int); +// END: effectivelist.point + +class ListMixinTest { + // BEGIN: effectivelist.convert + class ListablePoint(x: Int, y: Int) extends Point(x,y) with Listable[ListablePoint] { + def this(r : Point) = this(r.x, r.y) + } + implicit def toList(p : Point) : ListablePoint = new ListablePoint(p) + + @Test def useThePlainOldPointInList : Unit = { + val list = new List[ListablePoint] + list.add(new Point(10, 20)) + assertEquals("One element", 1, list.size) + assertEquals("X is 10", 10, list.get(0).x) + assertEquals("y is 20", 20, list.get(0).y) + } + // END: effectivelist.convert + +} diff -r e25dbfce40e9 -r ebe08056c60c samples/effectivelist/test/org/apidesign/effectivelist/ListTest.scala --- a/samples/effectivelist/test/org/apidesign/effectivelist/ListTest.scala Fri Aug 31 20:16:57 2012 +0200 +++ b/samples/effectivelist/test/org/apidesign/effectivelist/ListTest.scala Sun Sep 02 22:00:13 2012 +0200 @@ -4,6 +4,7 @@ import Assert._ class ListTest { + // BEGIN: effectivelist.person case class Person(name : String, age : Int) extends Listable[Person] var list : List[Person] = _ @@ -28,6 +29,7 @@ assertEquals("Anna was inserted 3rd", "Anna", list.get(2).name) assertEquals("Anna was inserted 4th", 6, list.get(3).age) } + // END: effectivelist.person @Test def addFewAndThenTryToRemoveHead() = { assertTrue(list.add(p1))