偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Kestrel.scala中的PersistentQueue

開發(fā) 后端
本文介紹Kestrel中的PersistentQueue,后面還重點(diǎn)介紹了一下Scala中的match和case的用法。

上一篇講到Kestrel.scala中的QueueCollection,下面將介紹PersistentQueue。

繼續(xù)走讀QueueCollection.scala的代碼,因?yàn)楹竺娣庋b的大量方法,都是對(duì)queues和fanout_queues的操作,根據(jù)定義,這兩個(gè)變量都是mutable.HashMap[String, XXXX]類型的,所以我們先介紹一下mutable.HashMap的幾個(gè)在Java中陌生的方法:( scala 的 apidoc 在 http://www.scala-lang.org/docu/files/api/index.html 可以查到)

◆apply (key : A) : B

◆Retrieve the value which is associated with the given key. This method throws an exception if there is no mapping from the given key to a value.

◆get (key : A) : Option[B]

◆Check if this map maps key to a value and return the value if it exists.

◆getOrElse [B2 >: B](key : A, default : => B2) : B2

◆Check if this map maps key to a value. Return that value if it exists, otherwise return default.

◆getOrElseUpdate (key : A, default : => B) : B

◆Check if this map maps key to a value. Return that value if it exists, otherwise put default as that key’s value and return it.

#t#我們發(fā)現(xiàn)get和apply在Scala中,是完全相同的功能,但是在get返回值里面的Option究竟是什么意思呢?這個(gè)問題從剛開始閱讀Scala代碼的時(shí)候就已經(jīng)困惑我們很久了。其實(shí)查詢一下Scala的手冊(cè),我們不難發(fā)現(xiàn),這是一個(gè)對(duì)于NULL的改造,因?yàn)樵贘ava里面,有些是面向?qū)ο蟮淖兞?,而有些不是,如果需要在Scala的語言內(nèi),保證所有對(duì)空的判斷是一致的,那么就需要做一點(diǎn)什么。所以Scala設(shè)計(jì)了Option這個(gè)抽象類,以及兩個(gè)子類Some和None。Option的實(shí)例,要么是Some類型,要么是None類型。所以把Option[類型]作為參數(shù)傳遞,也就是把這種類型的空值一并處理了,如果不存在,返回的是None[類型],不需要象apply一樣拋出一個(gè)異常。

讓我們重新讀下面這段代碼:

  1. private[kestrel] def queue(name: String): Option[PersistentQueue] = synchronized {  
  2.     ……  
  3.       Some(queues.get(name) getOrElse {  
  4.         // only happens when creating a queue for the first time.  
  5.         val q = if (name contains '+') {  
  6.           val master = name.split('+')(0)  
  7.           fanout_queues.getOrElseUpdate(master, new mutable.HashSet[String]) += name  
  8.           log.info("Fanout queue %s added to %s", name, master)  
  9.           new PersistentQueue(path.getPath, name, queueConfigs.configMap(master))  
  10.         } else {  
  11.           new PersistentQueue(path.getPath, name, queueConfigs.configMap(name))  
  12.         }  
  13.         q.setup  
  14.         queues(name) = q  
  15.         q  
  16.       })  
  17.   ……  
  18.   }  

先不要暈,根據(jù)之前對(duì)Option的理解,我們知道這是一個(gè)被Option封裝了的PersistentQueue類。我們也知道了所有的Scala方法都不需要return,***一條執(zhí)行命令的返回值就是這個(gè)方法的返回值,所以,在這里,Some(……)就是整個(gè)方法的返回值,很高興,因?yàn)榉椒▽?duì)返回值的定義是PersistentQueue,所以我們知道Some括號(hào)里面的一定也是PersistentQueue。

Some(queues.get(name) ……),很好,因?yàn)閝ueues的定義是mutable.HashMap[String, PersistentQueue],所以get返回的就是Option[PersistentQueue]。這個(gè)方法貌似已經(jīng)寫完了,后面的到底是在做什么呢?getOrElse,按照定義,就是如果值不存在,那么就做后面{}里面的事情,這樣的寫法,其實(shí)就是對(duì)空值的處理。用QueueCollection角度來看,就是當(dāng)查詢queues的時(shí)候,這個(gè)隊(duì)列如果不存在,那么就做{}里面的處理,創(chuàng)建一個(gè)隊(duì)列。這里需要注意的是——這個(gè)getOrElse不是HashMap的getOrElse,而是Option的getOrElse。

然后讀起來就比較順利了,創(chuàng)建一個(gè)q,是PersistentQueue類型的,把它賦值給queues(name)中,加入HashMap表中。***不要忘記把q作為整個(gè)函數(shù)的返回,也就是Some()的返回。和get(name)存在的時(shí)候一樣。

有了queue這個(gè)函數(shù)作為基礎(chǔ),后面讀起來就容易很多了,我們就重點(diǎn)介紹一下match和case的用法,在add方法里面有這么一段代碼:

  1. queue(key) match {  
  2.   case None => false 
  3.   case Some(q) =>  
  4.     ……  
  5.     val result = q.add(item, normalizedExpiry)  
  6.     if (result) totalAdded.incr()  
  7.     result  
  8. }  

之前我們知道 queue(key)返回的是Option[PersistentQueue],match就是做匹配,根據(jù)不同的匹配來執(zhí)行不同的操作,None,如果這個(gè)queue沒有查詢到,那么就返回false。Some(q),如果queue返回的是一個(gè)Some類型,也就是Option有值的時(shí)候的返回,那么這個(gè)q就是返回的PersistentQueue類型的那個(gè)實(shí)例!就像函數(shù)的參數(shù)一樣,可以直接使用。

很驚奇吧,剛接觸Scala的時(shí)候,我?guī)缀鯚o法相信case可以這樣做判斷。后來我們發(fā)現(xiàn),之所以能夠做這種判斷,是因?yàn)樗械腟cala都是被類封裝的,并且基于Scala的基類,實(shí)現(xiàn)了一個(gè)所謂的case class和case object的抽象類,并且實(shí)現(xiàn)了基于類的統(tǒng)一的==操作符。這一連串的改變?cè)炀土水惓?qiáng)大的Scala的case語法。

至于match…case還能怎么用,參考這個(gè)鏈接 http://programming-scala.labs.oreilly.com/ch03.html#PatternMatching

【編輯推薦】

  1. 走讀Kestrel,了解Scala
  2. Kestrel.scala中的QueueCollection
  3. 從Kestrel看Scala的核心程序模塊
  4. Scala實(shí)例教程:Kestrel
  5. Scala編程語言
責(zé)任編輯:yangsai 來源: dingsding.com
相關(guān)推薦

2009-09-28 11:25:17

PersistentQKestrelScala

2009-09-22 09:59:40

QueueCollecScala

2009-09-28 11:37:03

Journal.scaKestrel

2009-09-18 11:44:05

Scala實(shí)例教程Kestrel

2009-09-28 11:42:21

KestrelScala

2009-09-28 10:26:12

Scala代碼實(shí)例Kestrel

2009-09-22 09:42:24

Scala的核心

2009-07-22 07:53:00

Scala擴(kuò)展類

2009-07-08 15:35:18

Case類Scala

2009-07-22 07:45:00

Scala代碼重復(fù)

2009-07-08 12:43:59

Scala ServlScala語言

2023-06-12 15:33:52

Scalafor循環(huán)語句

2009-07-21 17:21:57

Scala定義函數(shù)

2020-10-31 17:33:18

Scala語言函數(shù)

2010-09-14 15:34:41

Scala

2009-07-22 08:57:49

Scalafinal

2009-07-21 11:25:03

ScalaRational類

2009-07-20 18:03:26

Scala程序Singleton對(duì)象

2017-03-07 15:13:28

Scala偏函數(shù)函數(shù)

2009-07-21 14:03:00

Scalaif表達(dá)式while循環(huán)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)