跳到主要內容

發表文章

目前顯示的是有「Guava」標籤的文章

Guava相關文章列表

Introduction Guava提供許多常見問題的實做,節省了我們開發時間。 Reflection.newProxy Dynamic Proxy Guava Cache Hello World Map Cache Cache with Suppliers Object Cache File Changed Cache Migration Problems EventBus Basic Usage Resource Guava wiki GitBook - Getting.Started.with.Google.Guava Preconditions 讓code更乾淨

Guava - EventBus Basic Usage

Introduction 最近在看Domain Event的實作方式,剛好看到Teddy分享使用Guava的EventBus來處理事件的註冊與分派,於是花了一些時間實驗API的用法。以下內容分享給大家。Source code可以從link下載。 How to? Event 我的範例事件為MJGotGodGuyCardEvent: public class MJGotGodGuyCardEvent {   private final String girlName ;   public MJGotGodGuyCardEvent ( String girlName ) { this . girlName = girlName ; }   public String getGirlName ( ) { return girlName ; }   } Event Handler 我的事件處理者有兩個,第一個為MJGoToPronhub。需注意的是: @Subscribe: 用以宣告處理函式,函式只允許一個參數,且要與Event物件相同。 @AllowConcurrentEvents: 用以告知EventBus此函式可以接受Concurrent存取,預設會使用循序方式存取此函式。 public class MJGoToPronhub { private static Logger logger = LoggerFactory. getLogger ( MJGoToPronhub. class ) ; private List < MJGotGodGuyCardEvent > receivedEvents = new CopyOnWriteArrayList <> ( ) ;   @AllowConcurrentEvents @Subscribe public void handle ( MJGotGodGuyCardEvent event ) { receivedEvents. add ( event )...

Guava Migration Problems

Introduction 本篇文章分享我們升級guava所遇到的問題。 TypeToken的isAssignableFrom被移除 這是我們從18.0升級到21.0遇到的問題。從19.0的 javadoc 得知,這個method在此版本設為Deprecated,將於20.0版本移除。 只要改用isSupertypeOf就可以解決問題。

Guava - File Changed Cache

Problem 讀取某個檔案建立model,可能因為成本昂貴而建立Cache;此外,程式可能會因為檔案有修改而重新建立Cache。本篇教學,將告訴你如何透過Guava達到這個需求。 How to? Guava本身有提供ExpiringMemoizingSupplier讓你以時間為條件,去重讀cache;ExpiredFileMemorizeSupplier使用Proxy的概念,去控管何時該真的去讀實際資料。我參考這個做法,建立了ExpiredFileMemorizeSupplier物件,會根據檔案是否修改,去決定是否讀真實資料: package org.tonylin.practice.guava.cache ;   import java.io.File ;   import com.google.common.base.Supplier ;   public class ExpiredFileMemorizeSupplier < T > implements Supplier < T > {   private File mFile ; private Supplier < T > mDelegate ; private long mLastModified = 0 ; private transient volatile T value ;   public ExpiredFileMemorizeSupplier ( IFileDataSupplier < T > delegate ) { mFile = delegate. getFile ( ) ; mDelegate = delegate ; }   public ExpiredFileMemorizeSupplier ( Supplier < T > delegate, File file ) { mFile = file ; mDelegate = delegate ; }   @Override public T get ( ) {...

Guava - Cache with Suppliers

 Introduction 先前 曾使用Guava cache建立Map cache,而單一值的cache也被常常使用。 How to? 以下兩個範例,testSuppliersCache示範了使用方式,而testSuppliersExpiredCache增加了時間限制: public class TestSuppliers { private int count = 0 ; @Test public void testSuppliersCache ( ) { Supplier < String > s = Suppliers. memoize ( this :: getMemoize ) ; Assert . assertEquals ( 0 , count ) ; Assert . assertEquals ( "testMemoize" , s. get ( ) ) ; Assert . assertEquals ( 1 , count ) ; Assert . assertEquals ( "testMemoize" , s. get ( ) ) ; Assert . assertEquals ( 1 , count ) ; }   @Test public void testSuppliersExpiredCache ( ) throws Exception { Supplier < String > s = Suppliers. memoizeWithExpiration ( this :: getMemoize, 3 , TimeUnit. SECONDS ) ; Assert . assertEquals ( 0 , count ) ; Assert . assertEquals ( "testMemoize" , s. get ( ) ) ; Assert . assertEquals ( 1 , count ) ; Thread . sleep ( 3200 ) ; Assert . assertEqu...

Guava Cache Hello World

Introduction 大部分應用程式都會透過Cache機制,去增加效能或暫時性的備援。Guava提供了簡單的Cache實做,讓你不用替資源回收或同步問題煩惱。 How to? 我們可以透過CacheBuilder去設定Cache的條件。在build時,你可以選擇要預先提供服務實做(CacheLoader),或是在get時提供。以下是在build時,就提供了服務實做: @Test public void test ( ) throws Exception { LoadingCache < String , String > cache = CacheBuilder. newBuilder ( ) . maximumSize ( 10 ) . expireAfterAccess ( 5 , TimeUnit. SECONDS ) . expireAfterWrite ( 5 , TimeUnit. SECONDS ) . softValues ( ) . build ( new CacheLoader < String , String > ( ) { private int count = 0 ; @Override public String load ( String key ) throws Exception { return key + ( count ++ ) ; } } ) ;   Assert . assertEquals ( "test10" , cache. get ( "test1" ) ) ; Thread . sleep ( 4000 ) ; Assert . assertEquals ( "test10" , cache. get ( "test1" ) ) ; Thread . sleep ( 2000 ) ; Assert . assertNull ( cache. getIfPresent ( "test1" ) ) ; Assert . assertEquals (...