Juha Komulainen avatar Juha Komulainen committed 1a059f6

Simplified code by delegating to libraries.

Comments (0)

Files changed (2)

src/main/scala/solitarius/general/Utils.scala

  */
 package solitarius.general
 
-import java.util.Random
+import java.util.{ Arrays, Collections }
 
 object Utils {
-  
-  private val random = new Random
 
-  /** Poor man's profiling */
-  def time[T](name: String) (thunk: => T) {
-    val start = currentTime
-    try {
-      thunk
-    } finally {
-      val total = currentTime - start
-      println(name + ": " + total + "ms")
-    }
-  }
-  
-  def sum(xs: Seq[Int]) = xs.foldLeft(0)(_+_)
-  
-  def currentTime = System.currentTimeMillis
-  
   /** Returns a shuffled array containing elements of given collection */
-  def shuffled[T](items: Collection[T]) (implicit manifest: scala.reflect.ClassManifest[T]): Array[T] = {
+  def shuffled[T](items: Iterable[T]) (implicit manifest: scala.reflect.ClassManifest[T]): Array[T] = {
     val array = items.toArray
-    shuffle(array)
+    Collections.shuffle(Arrays.asList(array))
     array
   }
-  
+
   def replicate[T](count: Int, items: List[T]): List[T] =
-    if (count == 0) Nil else items ::: replicate(count-1, items)
-
-  /** Shuffles given array in-place */
-  def shuffle[T](array: Array[T]) =
-    for (i <- Iterator.range(array.size, 1, -1))
-      swap(array, i - 1, random.nextInt(i))
-
-  private def swap[T](array: Array[T], i: Int, j: Int) = {
-    val tmp = array(i)
-    array(i) = array(j)
-    array(j) = tmp
-  }
+    List.fill(count)(items).flatten
 }

src/main/scala/solitarius/ui/TableauView.scala

 import javax.swing.JComponent
 
 import solitarius.general.{ Card, Pile, Sequence, Tableau, Utils }
-import Utils.sum
 
 import CardImages.{ cardImages, backside, cardWidth, cardHeight }
 
   addMouseListener(myMouseListener)
   addMouseMotionListener(myMouseMotionListener)
   setPreferredSize(new Dimension(marginLeft + marginRight + model.columnCount * (preferredPadding + cardWidth),
-                                 marginTop + marginBottom + sum(preferredRowHeights)))
+                                 marginTop + marginBottom + preferredRowHeights.sum))
   setMinimumSize(getPreferredSize)
   
   def preferredRowHeights = model.rowHeights.map(cards => preferredPadding + cards * cardHeight)
   private def verticalPadding = {
     val height = getHeight
     val contentHeight = height - marginTop - marginTop
-    val cardHeights = sum(model.rowHeights) * cardHeight
+    val cardHeights = model.rowHeights.sum * cardHeight
     (contentHeight - cardHeights) / (model.rowCount-1)
   }
   
   private def gridCoordinates(x: Int, y: Int) = {
-    val prev = cardHeight * sum(model.rowHeights.take(y))
+    val prev = cardHeight * model.rowHeights.take(y).sum
     (marginLeft + (cardWidth + horizontalPadding) * x, 
      marginTop + prev + (verticalPadding * y))
   }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.