-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
ShapeList.scala
74 lines (59 loc) · 2.3 KB
/
ShapeList.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// (C) Uri Wilensky. https://github.com/NetLogo/NetLogo
package org.nlogo.api
import java.util.{ Collection => JCollection, List => JList, Set => JSet }
import collection.JavaConverters._
object ShapeList {
val DefaultShapeName = "default"
def isDefaultShapeName(name: String) =
name == DefaultShapeName
def sortShapes(unsortedShapes: JList[Shape]): JList[Shape] =
collection.mutable.ArrayBuffer(unsortedShapes.asScala: _*)
.sortBy(_.getName)
.asJava
}
class ShapeList(_shapes: Shape*) {
def this() = this(Nil: _*)
private val shapes = new collection.mutable.HashMap[String, Shape]
_shapes.foreach(add)
import ShapeList._
def shape(name: String): Shape =
shapes.get(name).getOrElse(shapes(DefaultShapeName))
/** Returns vector of the list of shapes available to the current model */
def getShapes: JList[Shape] = {
// leave out the default shape for now; we will add it later so that it is at the top of the list
val currentShapes =
shapes.values.toSeq.filterNot(s => isDefaultShapeName(s.getName))
val sortedShapes = new java.util.ArrayList[Shape]
sortedShapes.addAll(sortShapes(currentShapes.asJava))
// make sure that the shape with the name DefaultShapeName is at the top of the list.
sortedShapes.add(0, shapes(DefaultShapeName))
sortedShapes
}
/** Returns a set of the names of all available shapes */
def getNames: JSet[String] =
shapes.keySet.asJava
/** Returns true when a shape with the given name is already available to the current model */
def exists(name: String) =
shapes.contains(name)
/** Clears the list of shapes currently available */
def replaceShapes(newShapes: JCollection[Shape]) {
shapes.clear()
addAll(newShapes)
}
/** Adds a new shape to the ones currently available for use */
def add(newShape: Shape): Shape = {
val replaced = shapes.get(newShape.getName).orNull
shapes(newShape.getName) = newShape
replaced
}
/** Adds a collection of shapes to the ones currently available for use */
def addAll(collection: JCollection[Shape]) {
collection.asScala.foreach(add)
}
/** Removes a shape from those currently in use */
def removeShape(shapeToRemove: Shape) = {
val removed = shapes.get(shapeToRemove.getName).orNull
shapes -= shapeToRemove.getName
removed
}
}