Scitter庫的增強(qiáng)方法是什么

本篇內(nèi)容介紹了“Scitter庫的增強(qiáng)方法是什么”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

成都創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比鏡湖網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式鏡湖網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋鏡湖地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。

現(xiàn)在對于Scala而言,Twitter是一個很好的學(xué)習(xí)對象。在之前一篇文章中,Ted已經(jīng)談到了 Twitter,這個微博客站點(diǎn)目前正引起社會性網(wǎng)絡(luò)的極大興趣,我們還談到它的基于 XML-/REST 的 API 如何使它成為開發(fā)人員進(jìn)行研究和探索的一個有趣平臺。為此,我們首先充實(shí)了“Scitter” 的基本結(jié)構(gòu),Scitter 是用于訪問 Twitter 的一個 Scala 庫。

我們對于 Scitter 有幾個目標(biāo):

  1. 簡化 Twitter 訪問,比過去打開 HTTP 連接然后 “手動” 執(zhí)行操作更容易。

  2. 可以從 Java 客戶機(jī)輕松訪問它。

  3. 輕松模擬以便進(jìn)行測試。

在這一期,我們不必完成整個 Twitter API,但是我們將完成一些核心部分,目的是讓這個庫達(dá)到公共源代碼控制庫的程度,便于其他人來完成這項(xiàng)工作。

到目前為止:Scitter 0.1

首先我們簡單回顧一下到目前為止我們所處的階段:

清單 1. Scitter v0.1

package com.tedneward.scitter
{
  import org.apache.commons.httpclient._, auth._, methods._, params._
  import scala.xml._

  /**
   * Status message type. This will typically be the most common message type
   * sent back from Twitter (usually in some kind of collection form). Note
   * that all optional elements in the Status type are represented by the
   * Scala Option[T] type, since that's what it's there for.
   */
  abstract class Status
  {
    /**
     * Nested User type. This could be combined with the top-level User type,
     * if we decide later that it's OK for this to have a boatload of optional
     * elements, including the most-recently-posted status update (which is a
     * tad circular).
     */
    abstract class User
    {
      val id : Long
      val name : String
      val screenName : String
      val description : String
      val location : String
      val profileImageUrl : String
      val url : String
      val protectedUpdates : Boolean
      val followersCount : Int
    }
    /**
     * Object wrapper for transforming (format) into User instances.
     */
    object User
    {
      /*
      def fromAtom(node : Node) : Status =
      {
      
      }
      */
      /*
      def fromRss(node : Node) : Status =
      {
      
      }
      */
      def fromXml(node : Node) : User =
      {
        new User {
          val id = (node \ "id").text.toLong
          val name = (node \ "name").text
          val screenName = (node \ "screen_name").text
          val description = (node \ "description").text
          val location = (node \ "location").text
          val profileImageUrl = (node \ "profile_image_url").text
          val url = (node \ "url").text
          val protectedUpdates = (node \ "protected").text.toBoolean
          val followersCount = (node \ "followers_count").text.toInt
        }
      }
    }
  
    val createdAt : String
    val id : Long
    val text : String
    val source : String
    val truncated : Boolean
    val inReplyToStatusId : Option[Long]
    val inReplyToUserId : Option[Long]
    val favorited : Boolean
    val user : User
  }
  /**
   * Object wrapper for transforming (format) into Status instances.
   */
  object Status
  {
    /*
    def fromAtom(node : Node) : Status =
    {
    
    }
    */
    /*
    def fromRss(node : Node) : Status =
    {
    
    }
    */
    def fromXml(node : Node) : Status =
    {
      new Status {
        val createdAt = (node \ "created_at").text
        val id = (node \ "id").text.toLong
        val text = (node \ "text").text
        val source = (node \ "source").text
        val truncated = (node \ "truncated").text.toBoolean
        val inReplyToStatusId =
          if ((node \ "in_reply_to_status_id").text != "")
            Some((node \"in_reply_to_status_id").text.toLong)
          else
            None
        val inReplyToUserId = 
          if ((node \ "in_reply_to_user_id").text != "")
            Some((node \"in_reply_to_user_id").text.toLong)
          else
            None
        val favorited = (node \ "favorited").text.toBoolean
        val user = User.fromXml((node \ "user")(0))
      }
    }
  }


  /**
   * Object for consuming "non-specific" Twitter feeds, such as the public timeline.
   * Use this to do non-authenticated requests of Twitter feeds.
   */
  object Scitter
  {
    /**
     * Ping the server to see if it's up and running.
     *
     * Twitter docs say:
     * test
     * Returns the string "ok" in the requested format with a 200 OK HTTP status code.
     * URL: http://twitter.com/help/test.format
     * Formats: xml, json
     * Method(s): GET
     */
    def test : Boolean =
    {
      val client = new HttpClient()

      val method = new GetMethod("http://twitter.com/help/test.xml")

      method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
        new DefaultHttpMethodRetryHandler(3, false))

      client.executeMethod(method)
      
      val statusLine = method.getStatusLine()
      statusLine.getStatusCode() == 200
    }
    /**
     * Query the public timeline for the most recent statuses.
     *
     * Twitter docs say:
     * public_timeline
     * Returns the 20 most recent statuses from non-protected users who have set
     * a custom user icon.  Does not require authentication.  Note that the
     * public timeline is cached for 60 seconds so requesting it more often than
     * that is a waste of resources.
     * URL: http://twitter.com/statuses/public_timeline.format
     * Formats: xml, json, rss, atom
     * Method(s): GET
     * API limit: Not applicable
     * Returns: list of status elements     
     */
    def publicTimeline : List[Status] =
    {
      import scala.collection.mutable.ListBuffer
    
      val client = new HttpClient()

      val method = new GetMethod("http://twitter.com/statuses/public_timeline.xml")

      method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
        new DefaultHttpMethodRetryHandler(3, false))

      client.executeMethod(method)
      
      val statusLine = method.getStatusLine()
      if (statusLine.getStatusCode() == 200)
      {
        val responseXML =
          XML.loadString(method.getResponseBodyAsString())

        val statusListBuffer = new ListBuffer[Status]

        for (n <- (responseXML \\ "status").elements)
          statusListBuffer += (Status.fromXml(n))
        
        statusListBuffer.toList
      }
      else
      {
        Nil
      }
    }
  }
  /**
   * Class for consuming "authenticated user" Twitter APIs. Each instance is
   * thus "tied" to a particular authenticated user on Twitter, and will
   * behave accordingly (according to the Twitter API documentation).
   */
  class Scitter(username : String, password : String)
  {
    /**
     * Verify the user credentials against Twitter.
     *
     * Twitter docs say:
     * verify_credentials
     * Returns an HTTP 200 OK response code and a representation of the
     * requesting user if authentication was successful; returns a 401 status
     * code and an error message if not.  Use this method to test if supplied
     * user credentials are valid.
     * URL: http://twitter.com/account/verify_credentials.format
     * Formats: xml, json
     * Method(s): GET
     */
    def verifyCredentials : Boolean =
    {
      val client = new HttpClient()

      val method = new GetMethod("http://twitter.com/help/test.xml")

      method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
        new DefaultHttpMethodRetryHandler(3, false))
        
      client.getParams().setAuthenticationPreemptive(true)
      val creds = new UsernamePasswordCredentials(username, password)
      client.getState().setCredentials(
        new AuthScope("twitter.com", 80, AuthScope.ANY_REALM), creds)

      client.executeMethod(method)
      
      val statusLine = method.getStatusLine()
      statusLine.getStatusCode() == 200
    }
  }
}

代碼有點(diǎn)長,但是很容易分為幾個基本部分:

  1. case 類 UserStatus,表示 Twitter 在對 API 調(diào)用的響應(yīng)中發(fā)回給客戶機(jī)的基本類型,包括用于構(gòu)造或提取 XML 的一些方法。

  2. 一個 Scitter 獨(dú)立對象,處理那些不需要對用戶進(jìn)行驗(yàn)證的操作。

  3. 一個 Scitter 實(shí)例(用 username 和 password 參數(shù)化),用于那些需要對用戶執(zhí)行驗(yàn)證的操作。

到目前為止,對于這兩種 Scitter 類型,我們只談到了測試、verifyCredentials 和 public_timeline API。雖然這些有助于確定 HTTP 訪問的基礎(chǔ)(使用 Apache HttpClient 庫)可以工作,并且我們將 XML 響應(yīng)轉(zhuǎn)換成 Status 對象的基本方式也是可行的,但是現(xiàn)在我們甚至不能進(jìn)行基本的 “我的朋友在說什么” 的公共時間線查詢,也沒有采取過基本的措施來防止代碼庫中出現(xiàn) “重復(fù)” 問題,更不用說尋找一些方法來模擬用于測試的網(wǎng)絡(luò)訪問代碼。

顯然,在這一期我們有很多事情要做。

連接

對于代碼,***件讓我煩惱的事就是,我在 Scitter 對象和類的每個方法中都重復(fù)這樣的操作序列:創(chuàng)建 HttpClient 實(shí)例,對它進(jìn)行初始化,用必要的驗(yàn)證參數(shù)對它進(jìn)行參數(shù)化,等等。當(dāng)它們只有 3 個方法時,可以進(jìn)行管理,但是顯然不易于伸縮,而且以后還會增加很多方法。此外,以后重新在那些方法中引入模擬和/或本地/離線測試功能將十分困難。所以我們要解決這個問題。

實(shí)際上,我們這里介紹的并不是 Scala 本身,而是不要重復(fù)自己(Don't-Repeat-Yourself)的思想。因此,我將從基本的面向?qū)ο蠓椒ㄩ_始:創(chuàng)建一個 helper 方法,用于做實(shí)際工作:

清單 2. 對代碼庫執(zhí)行 DRY 原則

package com.tedneward.scitter
{
  // ...
  object Scitter
  {
    // ...
    private[scitter] def exec ute(url : String) =
    {
      val client = new HttpClient()
      val method = new GetMethod(url)
      
      method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
        new DefaultHttpMethodRetryHandler(3, false))
        
      client.executeMethod(method)
      
      (method.getStatusLine().getStatusCode(), method.getResponseBodyAsString())
    }
  }
}

注意兩點(diǎn):首先,我從 execute() 方法返回一個元組,其中包含狀態(tài)碼和響應(yīng)主體。這正是讓元組成為語言中固有的一部分的一個強(qiáng)大之處,因?yàn)閷?shí)際上很容易從一個方法調(diào)用返回多個返回值。當(dāng)然,在 Java 代碼中,也可以通過創(chuàng)建包含元組元素的***或嵌套類來實(shí)現(xiàn)這一點(diǎn),但是這需要一整套專用于這一個方法的代碼。此外,本來也可以返回一個包含 String 鍵和 Object 值的 Map,但是那樣就在很大程度上喪失了類型安全性。元組并不是一個非常具有變革性的特性,它只不過是又一個使 Scala 成為強(qiáng)大語言的優(yōu)秀特性。

由于使用元組,我需要使用 Scala 的另一個特色語法將兩個結(jié)果都捕捉到本地變量中,就像下面這個重寫后的 Scitter.test 那樣:

清單 3. 這符合 DRY 原則嗎?

package com.tedneward.scitter
{
  // ...
  object Scitter
  {
    /**
     * Ping the server to see if it's up and running.
     *
     * Twitter docs say:
     * test
     * Returns the string "ok" in the requested format with a 200 OK HTTP status code.
     * URL: http://twitter.com/help/test.format
     * Formats: xml, json
     * Method(s): GET
     */
    def test : Boolean =
    {
      val (statusCode, statusBody) =
        execute("http://twitter.com/statuses/public_timeline.xml")
      statusCode == 200
    }
  }
}

實(shí)際上,我可以輕松地將 statusBody 全部去掉,并用 _ 替代它,因?yàn)槲覜]有用過第二個參數(shù)(test 沒有返回 statusBody),但是對于其他調(diào)用將需要這個 statusBody,所以出于演示的目的,我保留了該參數(shù)。

注意,execute() 沒有泄露任何與實(shí)際 HTTP 通信相關(guān)的細(xì)節(jié) — 這是 Encapsulation 101。這樣便于以后用其他實(shí)現(xiàn)替換 execute()(以后的確要這么做),或者便于通過重用 HttpClient 對象來優(yōu)化代碼,而不是每次重新實(shí)例化新的對象。

接下來,注意到 execute() 方法在 Scitter 對象上嗎?這意味著我將可以從不同的 Scitter 實(shí)例中使用它(至少現(xiàn)在可以這樣做,如果以后在 execute() 內(nèi)部執(zhí)行的操作不允許這樣做,則另當(dāng)別論)— 這就是我將 execute() 標(biāo)記為 private[scitter] 的原因,這意味著 com.tedneward.scitter 包中的所有內(nèi)容都可以看到它。

(順便說一句,如果還沒有運(yùn)行測試的話,那么請運(yùn)行測試,確保一切運(yùn)行良好。我將假設(shè)我們在討論代碼時您會運(yùn)行測試,所以如果我忘了提醒您,并不意味著您也忘記這么做。)

順便說一句,對于經(jīng)過驗(yàn)證的訪問,為了支持 Scitter 類,需要一個用戶名和密碼,所以我將創(chuàng)建一個重載的 execute() 方法,該方法新增兩個 String 參數(shù):

清單 4. 更加 DRY 化的版本

package com.tedneward.scitter
{
  // ...
  object Scitter
  {
    // ...
    private[scitter] def execute(url : String, username : String, password : String) =
    {
      val client = new HttpClient()
      val method = new GetMethod(url)
      
      method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
        new DefaultHttpMethodRetryHandler(3, false))
        
  client.getParams().setAuthenticationPreemptive(true)
  client.getState().setCredentials(
new AuthScope("twitter.com", 80, AuthScope.ANY_REALM),
  new UsernamePasswordCredentials(username, password))
      
      client.executeMethod(method)
      
      (method.getStatusLine().getStatusCode(), method.getResponseBodyAsString())
    }
  }
}

實(shí)際上,除了驗(yàn)證部分,這兩個 execute() 基本上是做相同的事情,我們可以按照第二個版本完全重寫***個 execute(),但是要注意,Scala 要求顯式表明重載的 execute() 的返回類型:

清單 5. 放棄 DRY

package com.tedneward.scitter
{
  // ...
  object Scitter
  {
    // ...
    private[scitter] def execute(url : String) : (Int, String) =
  execute(url, "", "")
    private[scitter] def execute(url : String, username : String, password : String) =
    {
      val client = new HttpClient()
      val method = new GetMethod(url)
      
      method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, 
        new DefaultHttpMethodRetryHandler(3, false))
        
      if ((username != "") && (password != ""))
      {
        client.getParams().setAuthenticationPreemptive(true)
        client.getState().setCredentials(
          new AuthScope("twitter.com", 80, AuthScope.ANY_REALM),
            new UsernamePasswordCredentials(username, password))
      }
      
      client.executeMethod(method)
      
      (method.getStatusLine().getStatusCode(), method.getResponseBodyAsString())
    }
  }
}

到目前為止,一切良好。我們對 Scitter 的通信部分進(jìn)行了 DRY 化處理,接下來我們轉(zhuǎn)移到下一件事情:獲得朋友的 tweet 的列表。

連接(到朋友)

Twitter API 表明,friends_timeline API 調(diào)用 “返回認(rèn)證用戶和該用戶的朋友發(fā)表的最近 20 條狀態(tài)”。(它還指出,對于直接從 Twitter Web 站點(diǎn)使用 Twitter 的用戶,“這相當(dāng)于 Web 上的 ‘/home’”。)對于任何 Twitter API 來說,這是非常基本的要求,所以讓我們將它添加到 Scitter 類中。之所以將它添加到類而不是對象中,是因?yàn)檎缥臋n中指出的那樣,這是以驗(yàn)證用戶的身份做的事情,而我已經(jīng)決定歸入 Scitter 類,而不是 Scitter 對象。

但是,這里我們碰到一塊絆腳石:friends_timeline 調(diào)用接受一組 “可選參數(shù)”,包括 since_idmax_idcountpage,以控制返回的結(jié)果。這是一項(xiàng)比較復(fù)雜的操作,因?yàn)?Scala 不像其他語言(例如 Groovy、JRuby 或 JavaScript)那樣原生地支持 “可選參數(shù)” 的思想,但是我們首先來處理簡單的東西 — 我們來創(chuàng)建一個 friendsTimeline 方法,該方法只執(zhí)行一般的、非參數(shù)化的調(diào)用:

清單 6.“告訴我你身邊的朋友是怎樣的...”

package com.tedneward.scitter
{
  class Scitter
  {
    def friendsTimeline : List[Status] =
    {
      val (statusCode, statusBody) =
       Scitter.execute("http://twitter.com/statuses/friends_timeline.xml",
                        username, password)

      if (statusCode == 200)
      {
        val responseXML = XML.loadString(statusBody)

        val statusListBuffer = new ListBuffer[Status]

        for (n <- (responseXML \\ "status").elements)
          statusListBuffer += (Status.fromXml(n))
        
        statusListBuffer.toList
      }
      else
      {
        Nil
      }
    }
  }
}

到目前為止,一切良好。用于測試的相應(yīng)方法看上去如下所示:

清單 7. “我能判斷您是怎樣的人 ”(Miguel de Cervantes)

package com.tedneward.scitter.test
{
  class ScitterTests
  {
    // ...

    @Test def scitterFriendsTimeline =
    {
      val scitter = new Scitter(testUser, testPassword)
      val result = scitter.friendsTimeline
      assertTrue(result.length > 0)
    }
  }
}

好極了。看上去就像 Scitter 對象中的 publicTimeline() 方法,并且行為也幾乎完全相同。

對于我們來說,那些可選參數(shù)仍然有問題。因?yàn)?Scala 并沒有可選參數(shù)這樣的語言特性,乍一看來,惟一的選擇就是完整地創(chuàng)建重載的 friendsTimeline() 方法,讓該方法帶有一定數(shù)量的參數(shù)。

幸運(yùn)的是,還有一種更好的方式,即通過一種有趣的方式將 Scala 的兩個語言特性(有一個特性我還沒有提到過) — case 類和 “重復(fù)參數(shù)” 結(jié)合起來(見清單 8):

清單 8. “我有多愛你?……”

package com.tedneward.scitter
{
  // ...
  
  abstract class OptionalParam
  case class Id(id : String) extends OptionalParam
  case class UserId(id : Long) extends OptionalParam
  case class Since(since_id : Long) extends OptionalParam
  case class Max(max_id : Long) extends OptionalParam
  case class Count(count : Int) extends OptionalParam
  case class Page(page : Int) extends OptionalParam
  
  class Scitter(username : String, password : String)
  {
    // ...

    def friendsTimeline(options : OptionalParam*) : List[Status] =
    {
      val optionsStr =
        new StringBuffer("http://twitter.com/statuses/friends_timeline.xml?")
      for (option <- options)
      {
        option match
        {
          case Since(since_id) =>
            optionsStr.append("since_id=" + since_id.toString() + "&")
          case Max(max_id) =>
            optionsStr.append("max_id=" + max_id.toString() + "&")
          case Count(count) =>
            optionsStr.append("count=" + count.toString() + "&")
          case Page(page) =>
            optionsStr.append("page=" + page.toString() + "&")
        }
      }
      
      val (statusCode, statusBody) =
        Scitter.execute(optionsStr.toString(), username, password)
      if (statusCode == 200)
      {
        val responseXML = XML.loadString(statusBody)

        val statusListBuffer = new ListBuffer[Status]

        for (n <- (responseXML \\ "status").elements)
          statusListBuffer += (Status.fromXml(n))
        
        statusListBuffer.toList
      }
      else
      {
        Nil
      }
    }
  }
}

看到標(biāo)在選項(xiàng)參數(shù)后面的 * 嗎?這表明該參數(shù)實(shí)際上是一個參數(shù)序列,這類似于 Java 5 中的 varargs 結(jié)構(gòu)。和 varargs 一樣,傳遞的參數(shù)數(shù)量可以像前面那樣為 0(不過,我們將需要回到測試代碼,向 friendsTimeline 調(diào)用增加一對括號,否則編譯器無法作出判斷:是調(diào)用不帶參數(shù)的方法,還是出于部分應(yīng)用之類的目的而調(diào)用該方法);我們還可以開始傳遞那些 case 類型,如下面的清單所示:

清單 9. “……聽我細(xì)細(xì)說”(William Shakespeare)

package com.tedneward.scitter.test
{
  class ScitterTests
  {
    // ...

    @Test def scitterFriendsTimelineWithCount =
    {
      val scitter = new Scitter(testUser, testPassword)
      val result = scitter.friendsTimeline(Count(5))
      assertTrue(result.length == 5)
    }
  }
}

當(dāng)然,總是存在這樣的可能性:客戶機(jī)傳入古怪的參數(shù)序列,例如 friendsTimeline(Count(5), Count(6), Count(7)),但是在這里,我們只是將列表傳遞給 Twitter(希望它們的錯誤處理足夠強(qiáng)大,能夠只采用指定的***那個參數(shù))。當(dāng)然,如果真的擔(dān)心這一點(diǎn),也很容易在構(gòu)造發(fā)送到 Twitter 的 URL 之前,從頭至尾檢查重復(fù)參數(shù)列表,并采用指定的每種參數(shù)的***一個參數(shù)。不過,后果自負(fù)

兼容性

但是,這又產(chǎn)生一個有趣的問題:從 Java 代碼調(diào)用這個方法有多容易?畢竟,如果這個庫的主要目標(biāo)之一是維護(hù)與 Java 代碼的兼容性,那么我們需要確保 Java 代碼在使用它時不至于太麻煩。

我們首先通過我們的好朋友 javap 檢驗(yàn)一下 Scitter 類:

清單 10. 哦,沒錯,Java 代碼……我現(xiàn)在想起來了……

C:\>javap -classpath classes com.tedneward.scitter.Scitter
Compiled from "scitter.scala"
public class com.tedneward.scitter.Scitter extends java.lang.Object implements s
cala.ScalaObject{
    public com.tedneward.scitter.Scitter(java.lang.String, java.lang.String);
    public scala.List friendsTimeline(scala.Seq);
    public boolean verifyCredentials();
    public int $tag()       throws java.rmi.RemoteException;
}

這時我心中有兩點(diǎn)擔(dān)心。首先,friendsTimeline() 帶有一個 scala.Seq 參數(shù)(這是我們剛才用過的重復(fù)參數(shù)特性)。其次,friendsTimeline() 方法和 Scitter 對象中的 publicTimeline() 方法一樣(如果不信,可以運(yùn)行 javap 查證),返回一個元素列表 scala.List。這兩種類型在 Java 代碼中有多好用?

最簡單的方法是用 Java 代碼而不是 Scala 編寫一組小型的 JUnit 測試,所以接下來我們就這樣做。雖然可以測試 Scitter 實(shí)例的構(gòu)造,并調(diào)用它的 verifyCredentials() 方法,但這些并不是特別有用 — 記住,我們不是要驗(yàn)證 Scitter 類的正確性,而是要看看從 Java 代碼中使用它有多容易。為此,我們直接編寫一個測試,該測試將獲取 “friends timeline” — 換句話說,我們要實(shí)例化一個 Scitter 實(shí)例,并且不使用任何參數(shù)來調(diào)用它的 friendsTimeline() 方法。

這有點(diǎn)復(fù)雜,因?yàn)樾枰獋魅搿?code>scala.Seq 參數(shù) — scala.Seq 是一個 Scala 特性,它將映射到底層 JVM 中的一個接口,所以不能直接實(shí)例化。我們可以嘗試典型的 Java null 參數(shù),但是這樣做會在運(yùn)行時拋出異常。我們需要的是一個 scala.Seq 類,以便從 Java 代碼中輕松地實(shí)例化這個類。

最終,我們還是在 mutable.ListBuffer 類型中找到一個這樣的類,這正是在 Scitter 實(shí)現(xiàn)本身中使用的類型:

清單 11. 現(xiàn)在我明白了自己為什么喜歡 Scala……

package com.tedneward.scitter.test;

import org.junit.*;
import com.tedneward.scitter.*;

public class JavaScitterTests
{
  public static final String testUser = "TESTUSER";
  public static final String testPassword = "TESTPASSWORD";
  
  @Test public void getFriendsStatuses()
  {
    Scitter scitter = new Scitter(testUser, testPassword);
    if (scitter.verifyCredentials())
    {
      scala.List statuses =
        scitter.friendsTimeline(new scala.collection.mutable.ListBuffer());
      Assert.assertTrue(statuses.length() > 0);
    }
    else
      Assert.assertTrue(false);
  }
}

使用返回的 scala.List 不是問題,因?yàn)槲覀兛梢韵駥Υ渌?Collection 類一樣對待它(不過我們的確懷念 Collection 的一些優(yōu)點(diǎn),因?yàn)?List 上基于 Scala 的方法都假設(shè)您將從 Scala 中與它們交互),所以,遍歷結(jié)果并不難,只要用上一點(diǎn) “舊式” Java 代碼(大約 1995 年時候的風(fēng)格):

清單 12. 重回 1995,又見 Vector……

package com.tedneward.scitter.test;

import org.junit.*;
import com.tedneward.scitter.*;

public class JavaScitterTests
{
  public static final String testUser = "TESTUSER";
  public static final String testPassword = "TESTPASSWORD";

  @Test public void getFriendsStatuses()
  {
    Scitter scitter = new Scitter(testUser, testPassword);
    if (scitter.verifyCredentials())
    {
      scala.List statuses =
        scitter.friendsTimeline(new scala.collection.mutable.ListBuffer());
      Assert.assertTrue(statuses.length() > 0);
      
      for (int i=0; i<STATUSES.LENGTH(); PRE < } Assert.assertTrue(false); else stat.text()); + ? said System.out.println(stat.user().screenName() stat="(Status)statuses.apply(i);" Status { i++)>

這將我們引向另一個部分,即將參數(shù)傳遞到 friendsTimeline() 方法。不幸的是,ListBuffer 類型不是將一個集合作為構(gòu)造函數(shù)參數(shù),所以我們必須構(gòu)造參數(shù)列表,然后將集合傳遞到方法調(diào)用。這樣有些單調(diào)乏味,但還可以承受:

清單 13. 現(xiàn)在可以回到 Scala 嗎?

package com.tedneward.scitter.test;

import org.junit.*;
import com.tedneward.scitter.*;

public class JavaScitterTests
{
  public static final String testUser = "TESTUSER";
  public static final String testPassword = "TESTPASSWORD";
  
  // ...

  @Test public void getFriendsStatusesWithCount()
  {
    Scitter scitter = new Scitter(testUser, testPassword);
    if (scitter.verifyCredentials())
    {
      scala.collection.mutable.ListBuffer params =
        new scala.collection.mutable.ListBuffer();
      params.$plus$eq(new Count(5));
      
      scala.List statuses = scitter.friendsTimeline(params);

      Assert.assertTrue(statuses.length() > 0);
      Assert.assertTrue(statuses.length() == 5);
      
      for (int i=0; i<STATUSES.LENGTH(); PRE < } Assert.assertTrue(false); else stat.text()); + ? said System.out.println(stat.user().screenName() stat="(Status)statuses.apply(i);" Status { i++)>

所以,雖然 Java 版本比對應(yīng)的 Scala 版本要冗長一點(diǎn),但是到目前為止,從任何要使用 Scitter 庫的 Java 客戶機(jī)中調(diào)用該庫仍然非常簡單。好極了。

“Scitter庫的增強(qiáng)方法是什么”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

網(wǎng)頁標(biāo)題:Scitter庫的增強(qiáng)方法是什么
文章網(wǎng)址:http://m.kartarina.com/article16/jeohgg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)ChatGPT自適應(yīng)網(wǎng)站企業(yè)建站微信公眾號定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司
主站蜘蛛池模板: 成人免费无码H在线观看不卡| 精品无码一区二区三区电影| 久久久久久精品无码人妻| 国产成人亚洲综合无码精品 | 亚洲一区爱区精品无码| 国产成人无码18禁午夜福利p| 中文无码亚洲精品字幕| 国精品无码一区二区三区在线蜜臀 | 亚洲国产精品无码一线岛国| 无码熟妇人妻在线视频| 国产亚洲精品无码专区 | 亚洲中文字幕无码久久2020| 亚洲伊人成无码综合网| 无码专区天天躁天天躁在线| 国产成年无码久久久久下载| 无码内射中文字幕岛国片| 国产成人无码专区| 国模无码视频一区| 亚洲国产成人精品无码区花野真一| 无码国产成人午夜电影在线观看| 亚洲va成无码人在线观看| 免费无码又爽又刺激网站直播| 亚洲av无码无线在线观看| 无码国产精成人午夜视频一区二区| 内射无码午夜多人| 亚洲AV无码AV日韩AV网站| 日韩精品无码久久久久久 | 亚洲av永久无码制服河南实里 | 人妻系列无码专区久久五月天| 无码毛片AAA在线| 精品无码国产自产拍在线观看| 色综合99久久久无码国产精品| 无码超乳爆乳中文字幕久久| 一区二区三区无码视频免费福利| 无码人妻精品一区二区蜜桃| 亚洲AV无码国产一区二区三区 | 免费无码一区二区| 四虎成人精品国产永久免费无码| 亚洲大尺度无码无码专线一区| 亚洲精品9999久久久久无码| 久久亚洲精品成人无码网站|