fork download
  1. // Ignore the magic object, it is redis internal stuff implementation
  2. object RedisMagic {
  3. protected val map: collection.mutable.Map[String, Any] = collection.mutable.Map.empty
  4. def set[T](name: String, value: T): Boolean = { map(name) = value; true }
  5. def get[T](name: String): Option[T] = map.get(name).map(_.asInstanceOf[T])
  6. }
  7.  
  8. object RedisObjectValue {
  9. implicit def objectValueToValue[T](ov: RedisObjectValue[T]): T = {
  10. ov.get
  11. }
  12. }
  13.  
  14. case class RedisObjectValue[T](name: String, default: T) {
  15. def :=(value: T): Boolean = {
  16. RedisMagic.set[T](name, value)
  17. }
  18.  
  19. def get: T = {
  20. RedisMagic.get[T](name).getOrElse(default)
  21. }
  22.  
  23. override def equals(o: Any) = o match {
  24. case ov: RedisObjectValue[T] =>
  25. if (this.hashCode() == ov.hashCode()) true // Same reference
  26. else this.get == ov.get
  27. case v =>
  28. // If we don't compare to another RedisObjectValue, let the equals method of data type handle it
  29. this.get.equals(v)
  30. }
  31. }
  32.  
  33. class Player(_id: Long) {
  34. protected def saveName(valName: String): String = _id + ":" + valName
  35. val userId = new RedisObjectValue[Long](saveName("userId"), 0l)
  36. val name = new RedisObjectValue[String](saveName("name"), "Unknown")
  37. }
  38.  
  39. object Main extends App {
  40. val player1 = new Player(1)
  41. player1.userId := 42
  42. player1.name := "God"
  43. val player2 = new Player(2)
  44. player2.userId := 2
  45. player2.name := "Unknown"
  46.  
  47. val list = Vector(player1, player2)
  48. val filtered = list.filter(_.userId == 42)
  49. System.out.println("Count of filtered: " + filtered.length)
  50. }
Success #stdin #stdout 0.42s 322432KB
stdin
Standard input is empty
stdout
Count of filtered: 0