Skip to content
This repository has been archived by the owner on May 22, 2019. It is now read-only.

Commit

Permalink
neuter some of our custom sequence conversion methods
Browse files Browse the repository at this point in the history
  • Loading branch information
freels committed Feb 22, 2011
1 parent 54778c9 commit 1f4f2f9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 38 deletions.
10 changes: 5 additions & 5 deletions src/main/scala/com/twitter/gizzard/thrift/ManagerService.scala
Expand Up @@ -67,18 +67,18 @@ class ManagerService[S <: shards.Shard, J <: JsonJob](nameServer: NameServer[S],
wrapEx(nameServer.getShard(id.fromThrift).toThrift)
}
def shards_for_hostname(hostname: String): JList[ShardInfo] = {
wrapEx(nameServer.shardsForHostname(hostname).map(_.toThrift).toJavaList)
wrapEx(nameServer.shardsForHostname(hostname).map(_.toThrift))
}
def get_busy_shards(): JList[ShardInfo] = {
wrapEx(nameServer.getBusyShards().map(_.toThrift).toJavaList)
wrapEx(nameServer.getBusyShards().map(_.toThrift))
}


def list_upward_links(id: ShardId): JList[LinkInfo] = {
wrapEx(nameServer.listUpwardLinks(id.fromThrift).map(_.toThrift).toJavaList)
wrapEx(nameServer.listUpwardLinks(id.fromThrift).map(_.toThrift))
}
def list_downward_links(id: ShardId): JList[LinkInfo] = {
wrapEx(nameServer.listDownwardLinks(id.fromThrift).map(_.toThrift).toJavaList)
wrapEx(nameServer.listDownwardLinks(id.fromThrift).map(_.toThrift))
}


Expand All @@ -91,7 +91,7 @@ class ManagerService[S <: shards.Shard, J <: JsonJob](nameServer: NameServer[S],
wrapEx(nameServer.getForwardingForShard(id.fromThrift).toThrift)
}
def get_forwardings(): JList[Forwarding] = {
wrapEx(nameServer.getForwardings().map(_.toThrift).toJavaList)
wrapEx(nameServer.getForwardings().map(_.toThrift))
}

def list_hostnames() = wrapEx(nameServer.listHostnames.toJavaList)
Expand Down
@@ -1,48 +1,22 @@
package com.twitter.gizzard
package thrift.conversions

import scala.collection.JavaConversions._
import java.nio.{BufferUnderflowException, ByteBuffer, ByteOrder}
import java.util.{AbstractList => JAbstractList, List => JList}


class ScalaSeqAdapter[A, B](protected val list: JList[A])(f: A => B) extends Seq[B] {
def length = list.size

def apply(i: Int) = f(list.get(i))

def iterator = new Iterator[B] {
val iterator = list.iterator

def next = f(iterator.next)
def hasNext = iterator.hasNext
}

override def equals(other: Any) = {
other match {
case other: ScalaSeqAdapter[_, _] =>
list == other.list
case _ =>
false
}
}
}

class JavaListAdapter[A, B](seq: Seq[A])(f: A => B) extends JAbstractList[B] {
def size = seq.size
def get(i: Int) = f(seq(i))
}

object Sequences {
class RichSeq[A <: AnyRef](seq: Seq[A]) {
def parallel(future: Future) = new ParallelSeq(seq, future)
def toJavaList = new JavaListAdapter(seq)(x => x)
def toJavaList: JList[A] = seq
def double = for (i <- seq) yield (i, i)
}
implicit def seqToRichSeq[A <: AnyRef](seq: Seq[A]) = new RichSeq(seq)

class RichIntSeq(seq: Seq[Int]) {
def parallel(future: Future) = new ParallelSeq(seq, future)
def toJavaList = new JavaListAdapter(seq)(_.asInstanceOf[java.lang.Integer])
def toJavaList: JList[java.lang.Integer] = seq.map(_.asInstanceOf[java.lang.Integer])
def double = for (i <- seq) yield (i, i)

def pack: ByteBuffer = {
Expand All @@ -58,7 +32,7 @@ object Sequences {

class RichLongSeq(seq: Seq[Long]) {
def parallel(future: Future) = new ParallelSeq(seq, future)
def toJavaList = new JavaListAdapter(seq)(_.asInstanceOf[java.lang.Long])
def toJavaList: JList[java.lang.Long] = seq.map(_.asInstanceOf[java.lang.Long])
def double = for (i <- seq) yield (i, i)

def pack: ByteBuffer = {
Expand All @@ -73,19 +47,19 @@ object Sequences {
implicit def seqToRichLongSeq(seq: Seq[Long]) = new RichLongSeq(seq)

class RichJavaList[T <: AnyRef](list: JList[T]) {
def toSeq = new ScalaSeqAdapter(list)(id => id)
def toSeq: Seq[T] = list
def toList = toSeq.toList
}
implicit def javaListToRichSeq[T <: AnyRef](list: JList[T]) = new RichJavaList(list)

class RichJavaIntList(list: JList[java.lang.Integer]) {
def toSeq = new ScalaSeqAdapter(list)(_.asInstanceOf[Int])
def toSeq: Seq[Int] = list.map(_.asInstanceOf[Int])
def toList = toSeq.toList
}
implicit def javaIntListToRichSeq(list: JList[java.lang.Integer]) = new RichJavaIntList(list)

class RichJavaLongList(list: JList[java.lang.Long]) {
def toSeq = new ScalaSeqAdapter(list)(_.asInstanceOf[Long])
def toSeq: Seq[Long] = list.map(_.asInstanceOf[Long])
def toList = toSeq.toList
}
implicit def javaLongListToRichSeq(list: JList[java.lang.Long]) = new RichJavaLongList(list)
Expand Down

0 comments on commit 1f4f2f9

Please sign in to comment.