May 27, 2012

[Java] Type Erasure: Implement the Interface with Two Generic Types

在 Java 實作擁有 Generic Type 的 Interface 時,可能會希望同時 implement 兩種不同 Type 的 Interface。

狀況如下,有個 class 需要接收兩種不同的 Future Task 結果,

- 要被 implement 的 interface
 
public interface FutureListener<T> {
    public void onFutureDone(T future);
}

- 要實作 interface 的 class
 
public class Service implements 
    FutureListener<int> futureA, FutureListener<String> futureB {     /// type erasure

    public void onFutureDone(Future<int> future) { }
    public void onFutureDone(<String> future) {}
}

可惜的是,因為 type erasure,這樣的實作方式是不被接受的。


  • 那...... 什麼是 type erasure 呢?
Oracle 的 Java Tutorials 內可以找到討論 type erasure 的條目,摘錄如下,

When a generic type is instantiated, the compiler translates those types by a technique called type erasure — a process where the compiler removes all information related to type parameters and type arguments within a class or method. Type erasure enables Java applications that use generics to maintain binary compatibility with Java libraries and applications that were created before generics.

在 compile-time 時,Compiler 會移除 type parameter 及 type argument,藉此達成 Generic 目的,又

For instance, Box<String> is translated to type Box, which is called the raw type — a raw type is a generic class or interface name without any type arguments. This means that you can't find out what type of Object a generic class is using at runtime.

由此可知,在 run-time 時,會因為 compiler 把 type parameters & arguments 去除掉,使得 List<int> 及 List<String> 沒辦法被表現出差異,如此會導致 JVM 無法認清不同 type 的 object。

  • 如果有 type erasure,那 T type 如何被操作?
Compiler 會幫忙作 casting,因而達到 Generic。各位可以參考 Angelika Langer 的網站,針對 Type Erasure 有詳盡的描述及範例。

  • 該怎麼解決這個問題?
可以透過實作兩個 inner class 來解決這個問題,

 
public class Service {
    @Override
    private class FutureA implements FutureListener<int> {
        public void onFutureDone(Future<int> future) { }
    }

    @Override
    private class FutureB implements FutureListener<String> {
        public void onFutureDone(Future<String> future) { }
    }
}
  • 結論
在使用 Java 的 Generic 特性時,看來還是得先理解 Type Erasure,才能避免一些實作上的問題。透過這樣的 Survey 後,發現 Java 其他有趣的部份,一時半刻要弄熟也不是簡單的事情,還有很多需要學習的部份,一想到這邊就感受到渾身的熱情,有幸能夠接觸這些技術真是太好了。

Reference:

Apr 21, 2012

[Web] Dictionary & Translate API 試用

最近打算做些翻譯應用,本來 Google Dictionary API 是首選,但去年關閉了,取而代之的是Google Translate API,可惜的是,一點免費的使用幅度也沒有,開口就是要 $20 per 1M characters of text,對我來說,如果只是要初期嘗試,卻要特別花這樣的費用,除了麻煩之外,也是另一筆開銷,所以我就不考慮了。

而後在網路上找了許多API,包含以下這些,

    Google Translate API
    Dictionary.com
    Pearson Longman Dictionary
    Abbreviations.com
    Glosbe API
    Microsoft Translator API

Mar 27, 2012

[Git] 用 repo 來管理多個 Repository

雖然很久之前就因為下載 Google Android Source Code,因此使用 repo 來下載及管理多個 Git Repository,雖然對這個方便的 tool 很好奇,但一直沒有去多加探究。而且可惜的是,在網路上的資料,多半只提到如何使用 repo 來下載 Android Open Source Project (AOSP)  的 source code 及 upload change,卻沒有提及如何利用這個 tool 來管理多個 Repository。這邊透過網路上零星的資訊及參考 AOSP 的作法,整理在下面供其他有興趣的人參考。

這邊我利用 github 來做操作範例,各位也可以自行到我的 Picker's github 去參考看看細節,接下來我們來看看,要同時能夠管理多個 Repository,需要做些什麼事情。

Mar 6, 2012

[Git] 統一口徑的 .gitignore

每個接觸過版本控制系統的開發者,想必都遇到過一個問題「揪竟~~要不要把這個檔案加入版本控管呢?」,會提出這樣的問題,通常是當時還不了解程式的建置過程 (build process),不了解建置前需要哪些額外的文件供建置工具 (build tool) 使用,也不知道過程中會產生哪些附屬品,所以才會有這樣的疑問。但有疑惑不可恥,只要進一步去弄清楚「程式建置過程」,相信那個完美的解答便呼之欲出了。

Feb 17, 2012

[Git] "Unable to find remote helper for 'https'" Problem

前陣子我的 Git 出了點問題,使用 git log 時,畫面上都是些沒有 color 的字串,而且還會包含色彩碼,一時間找不到發生原因,索性就順便更新 Git 到新版本。

從官網上看到的版本是 1.7.9,直接下載 Source Code 來編譯,編譯程序就是照一般的流程,如下,

    $ git-1.7.9> ./configure
    $ git-1.7.9> make
    $ git-1.7.9> sudo make install

但安裝完後,發現在下載 AOSP 的 Source Code 時,會出現如下的訊息,

    "Unable to find remote helper for 'https'"

經過搜尋後,發現問題應該出在 openssl 及 curl 安裝套件有問題,應該是因為我沒有安裝 他們的他們的 development 套件,所以也順便看一下 Git 有沒有什麼相依性套件需要先安裝,從 Git Community Book - Installing Git 可以看到,

    You will need the expatcurlzlib, and openssl libraries installed - though with the possible exception of expat, these will normally already be there.

確認套件都有安裝後,重新編譯並安裝 Git,就可以下載 AOSP 的 Source Code 了。

Feb 11, 2012

[Git] How to Update the Old Commit with Git and to Push a New Patch Set on the Gerrit?

簡述:
Version Control 及 Code Review 在軟體開發流程上,是非常重要的兩個部份。因為在 Android Open Source Project 上,是使用 Git 並搭配 Gerrit Code Review 來進行開發,所以這邊也來談談該怎麼善用這兩個工具的功能,方便做 Code Review。

既然是 Code Review,便有可能出現不被接受的 commit,此時可知,如果是因為同樣目的而修改的部份,卻是每次都要在 Gerrit 上產生一個新的 change,顯然是很麻煩,而且容易造成 Gerrit 及 Git 的歷史紀錄混亂,所以,Gerrit 提供了 Change-Id 的使用,讓 Gerrit 協助你管理這些同樣的修改目標,使得這些修改都可以歸類於一個 change  紀錄中,存放成一個一個的 Patch Set。

Feb 1, 2012

[Linux] 使用 tmux 取代老舊的 Terminal

耳聞 tmux 非常好用,所以來看看該怎麼開始使用這個 tool。

安裝方式:

很簡單,在 Ubuntu 下只要透過 aptitude 即可。

  $> sudo aptitude install tmux


操作方式:

直接在 command line 執行 tmux,即可運行 tmux tool。

在進入 tmux 之後,可以輸入下列組合鍵來取得說明 [Ctrl] + [b] + [?],這邊以[]包含起來,表示鍵盤上的按鍵,以下是擷取一部份的說明內容,


要使用任何 tmux 操作指令,需要先鍵入組合鍵 [Ctrl] + [b],再搭配指定的功能符號。


實際使用:

實際試用後發現,切割式的 Terminal 畫面呈現方式,對 command line 重度使用者來說,實在是很方便,而且還能調整成自己的 layout 形式,搭配 Vim 時,可以讓我有非常高的使用效率。


使用左右切割的視窗,然後下方的視窗可以作為編譯時使用,就不用切換 terminal,也不需要另外開個 terminal 作編譯,非常讚!

各位可以在 github 裡找到我的 tmux 設定檔


官方網站:
tmux 

參考來源:
Tmux 教學 + Screen 到 Tmux 的無痛轉換 by Tsung's Blog
推薦使用tmux – a "terminal multiplexer" by 查理布朗的倒楣世界

Jan 25, 2012

[Android] An Android Application for Getting the System Property

Sometimes, I have to check whether the system property has been changes or not. The requirement usually comes form customer for providing the preview device quickly. So, we have to change the system property manually. 
Because sometimes the non-technical operator need to check the property by himself, I create the project helps people to obtain the system property that do not need through the adb shell command. 

You can get the source code form the github: SystemProperty

[Android] The Size of Application Icon

We can design the icon for android application. When we want to put our icon, we should follow the rule of design on the Android. In general, we have to provide four icons in the different densities. The four types of icon are such as,
  1. Low Density: 36x36 px
  2. Medium Density: 48x48 px
  3. High Density: 72x72 px
  4. Extra High Density: 96x96 px