Identifying code snippets to be used on my blog
authorJaroslav Tulach <jtulach@netbeans.org>
Sun, 02 Sep 2012 22:00:13 +0200
changeset 403ebe08056c60c
parent 402 e25dbfce40e9
child 404 dc0d785c5b5d
Identifying code snippets to be used on my blog
samples/effectivelist/src/org/apidesign/effectivelist/List.scala
samples/effectivelist/src/org/apidesign/effectivelist/Listable.scala
samples/effectivelist/test/org/apidesign/effectivelist/ListMixinTest.scala
samples/effectivelist/test/org/apidesign/effectivelist/ListTest.scala
     1.1 --- a/samples/effectivelist/src/org/apidesign/effectivelist/List.scala	Fri Aug 31 20:16:57 2012 +0200
     1.2 +++ b/samples/effectivelist/src/org/apidesign/effectivelist/List.scala	Sun Sep 02 22:00:13 2012 +0200
     1.3 @@ -8,6 +8,7 @@
     1.4   * <b>case class</b> Person(name : String) <b>extends</b> Listable[Person]
     1.5   * </pre>
     1.6   */
     1.7 +// BEGIN: effectivelist.list
     1.8  final class List[T <: Listable[T]] {
     1.9    private var item : T = _
    1.10    private final var EMPTY : T = _
    1.11 @@ -72,3 +73,4 @@
    1.12      }
    1.13    }
    1.14  }
    1.15 +// END: effectivelist.list
     2.1 --- a/samples/effectivelist/src/org/apidesign/effectivelist/Listable.scala	Fri Aug 31 20:16:57 2012 +0200
     2.2 +++ b/samples/effectivelist/src/org/apidesign/effectivelist/Listable.scala	Sun Sep 02 22:00:13 2012 +0200
     2.3 @@ -3,7 +3,9 @@
     2.4  /** A member of a {@link List} that makes keeping references
     2.5   * to previous and next items in the list effective.
     2.6   */
     2.7 +// BEGIN: effectivelist.item
     2.8  trait Listable[T <: Listable[T]] {
     2.9    private[effectivelist] var prev : T = _
    2.10    private[effectivelist] var next : T = _
    2.11  }
    2.12 +// END: effectivelist.item
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/samples/effectivelist/test/org/apidesign/effectivelist/ListMixinTest.scala	Sun Sep 02 22:00:13 2012 +0200
     3.3 @@ -0,0 +1,26 @@
     3.4 +package org.apidesign.effectivelist
     3.5 +
     3.6 +import org.junit._
     3.7 +import Assert._
     3.8 +
     3.9 +// BEGIN: effectivelist.point
    3.10 +class Point(val x: Int, val y : Int);
    3.11 +// END: effectivelist.point
    3.12 +
    3.13 +class ListMixinTest {
    3.14 +  // BEGIN: effectivelist.convert
    3.15 +  class ListablePoint(x: Int, y: Int) extends Point(x,y) with Listable[ListablePoint] {
    3.16 +    def this(r : Point) = this(r.x, r.y)
    3.17 +  }
    3.18 +  implicit def toList(p : Point) : ListablePoint = new ListablePoint(p)
    3.19 +
    3.20 +  @Test def useThePlainOldPointInList : Unit = {
    3.21 +    val list = new List[ListablePoint]
    3.22 +    list.add(new Point(10, 20))
    3.23 +    assertEquals("One element", 1, list.size)
    3.24 +    assertEquals("X is 10", 10, list.get(0).x)
    3.25 +    assertEquals("y is 20", 20, list.get(0).y)
    3.26 +  }
    3.27 +  // END: effectivelist.convert
    3.28 +
    3.29 +}
     4.1 --- a/samples/effectivelist/test/org/apidesign/effectivelist/ListTest.scala	Fri Aug 31 20:16:57 2012 +0200
     4.2 +++ b/samples/effectivelist/test/org/apidesign/effectivelist/ListTest.scala	Sun Sep 02 22:00:13 2012 +0200
     4.3 @@ -4,6 +4,7 @@
     4.4  import Assert._
     4.5  
     4.6  class ListTest {
     4.7 +  // BEGIN: effectivelist.person
     4.8    case class Person(name : String, age : Int) extends Listable[Person]
     4.9    
    4.10    var list : List[Person] = _
    4.11 @@ -28,6 +29,7 @@
    4.12      assertEquals("Anna was inserted 3rd", "Anna", list.get(2).name)
    4.13      assertEquals("Anna was inserted 4th", 6, list.get(3).age)
    4.14    }
    4.15 +  // END: effectivelist.person
    4.16  
    4.17    @Test def addFewAndThenTryToRemoveHead() = {
    4.18      assertTrue(list.add(p1))