Google開源的Java公共類庫,Guava 19.0 RC1 發布
Guava 是一個 Google 的基于java1.6的類庫集合的擴展項目,包括 collections, caching,
primitives support, concurrency libraries, common annotations, string
processing, I/O, 等等. 這些高質量的
API 可以使你的JAVa代碼更加優雅,更加簡潔,讓你工作更加輕松愉悅。
源碼包的簡單說明:
com.google.common.annotations:普通注解類型。
com.google.common.base:基本工具類庫和接口。
com.google.common.cache:緩存工具包,非常簡單易用且功能強大的JVM內緩存。
com.google.common.collect:帶泛型的集合接口擴展和實現,以及工具類,這里你會發現很多好玩的集合。
com.google.common.eventbus:發布訂閱風格的事件總線。
com.google.common.hash: 哈希工具包。
com.google.common.io:I/O工具包。
com.google.common.math:原始算術類型和超大數的運算工具包。
com.google.common.net:網絡工具包。
com.google.common.primitives:八種原始類型和無符號類型的靜態工具包。
com.google.common.reflect:反射工具包。
com.google.common.util.concurrent:多線程工具包。
類庫使用手冊:
一. 基本工具類:讓使用Java語言更令人愉悅。
1. 使用和避免 null:null 有語言歧義, 會產生令人費解的錯誤, 反正他總是讓人不爽。很多 Guava 的工具類在遇到 null 時會直接拒絕或出錯,而不是默默地接受他們。
2. 前提條件:更容易的對你的方法進行前提條件的測試。
3. 常見的對象方法: 簡化了Object常用方法的實現, 如 hashCode() 和 toString()。
4. 排序: Guava 強大的 "fluent Comparator"比較器, 提供多關鍵字排序。
5. Throwable類: 簡化了異常檢查和錯誤傳播。
二. 集合類:集合類庫是 Guava 對 JDK 集合類的擴展, 這是 Guava 項目最完善和為人所知的部分。
1. Immutable collections(不變的集合): 防御性編程, 不可修改的集合,并且提高了效率。
2. New collection types(新集合類型):JDK collections 沒有的一些集合類型,主要有:multisets,multimaps,tables, bidirectional maps等等
3. Powerful collection utilities(強大的集合工具類): java.util.Collections 中未包含的常用操作工具類
4. Extension utilities(擴展工具類): 給 Collection 對象添加一個裝飾器? 實現迭代器? 我們可以更容易使用這些方法。
三. 緩存: 本地緩存,可以很方便的操作緩存對象,并且支持各種緩存失效行為模式。
四. Functional idioms(函數式): 簡潔, Guava實現了Java的函數式編程,可以顯著簡化代碼。
五. Concurrency(并發):強大,簡單的抽象,讓我們更容易實現簡單正確的并發性代碼。
1. ListenableFuture(可監聽的Future): Futures,用于異步完成的回調。
2. Service: 控制事件的啟動和關閉,為你管理復雜的狀態邏輯。
六. Strings: 一個非常非常有用的字符串工具類: 提供 splitting,joining, padding 等操作。
七. Primitives: 擴展 JDK 中未提供的對原生類型(如int、char等)的操作, 包括某些類型的無符號的變量。
八. Ranges: Guava 一個強大的 API,提供 Comparable 類型的范圍處理, 包括連續和離散的情況。
九. I/O: 簡化 I/O 操作, 特別是對 I/O 流和文件的操作, for Java 5 and 6.
十. Hashing: 提供比 Object.hashCode() 更復雜的 hash 方法, 提供 Bloom filters.
十一. EventBus: 基于發布-訂閱模式的組件通信,但是不需要明確地注冊在委托對象中。
十二. Math: 優化的 math 工具類,經過完整測試。
十三. Reflection: Guava 的 Java 反射機制工具類。
Guava 19.0 RC1 發布,主要改進:
common.base
-
Added
CharMatcher
static factory methods equivalent to theCharMatcher
constants. For example, addedCharMatcher.whitespace()
which is equivalent toCharMatcher.WHITESPACE
. Eventually, the constants will be deprecated and removed. -
This is being done because using constants requires a large number of classes to be initialized when anything from
CharMatcher
is used; switching to static factory methods allows classes to be initialized only as needed for the type ofCharMatcher
actually being used. -
Added
Throwables.lazyStackTrace(Throwable)
- Returns aList<StackTraceElement>
that may load the stack trace elements lazily. Useful if you want to get only the first N elements of the stack trace efficiently. -
Added
lazyStackTraceIsLazy()
- Returns whether or not the above method is able to use the special implementation that makes it lazy on the current platform. -
Added
VerifyException
constructor overloads taking aThrowable
cause.
common.cache
This package has graduated from @Beta
, making it safe to use in library code.
-
Added visibility of
CacheLoader.UnsupportedLoadingOperationException
-
Added
RemovalNotification.create
-
These should only be needed if creating a custom cache implementation
common.collect
Added factory and builder methods for various ImmutableMap
s and ImmutableMultimap
s that take Iterable<Map.Entry>
.
-
Added
FluentIterable.toMultiset()
-
Added
RangeSet.asDescendingSetOfRanges()
andRangeMap.asDescendingMapOfRanges()
-
Added
Lists.cartesianProduct(List...)
andLists.cartesianProduct(List<List>>)
-
Added
Maps.newLinkedHashMapWithExpectedSize(int)
-
Re-added
Multisets.removeOccurrences(Multiset, Multiset)
which was (binary incompatibly) missing in 18.0 because it was replaced withMultisets.removeOccurences(Multiset, Iterable)
-
Deprecated
MapConstraint
andMapConstraints
-
Deprecated
Sets.newSetFromMap(Map)
- Java 6 providesCollections.newSetFromMap(Map)
-
Removed
MapMaker.softValues()
common.eventbus
-
Added
EventBus.identifier()
-
Removed protected method
AsyncEventBus.dispatchQueuedEvents()
(made package-private)
common.hash
-
Added
BloomFilter.create
overloads taking along
for theexpectedInsertions
-
Added
Hashing.sha384()
-
Added
Hashing.concatenating(HashFunction, HashFunction, HashFunction...)
andHashing.concatenating(Iterable<HashFunction>)
common.io
-
Added
ByteSource.sizeIfKnown()
-
Added
CharSource.length()
-
Added
CharSource.lengthIfKnown()
common.net
-
Added a couple new constants to
HttpHeaders
andMediaType
-
Updated public suffix list for
InternetDomainName
common.reflect
-
Added
TypeToken.isSubtypeOf(TypeToken)
,TypeToken.isSupertypeOf(TypeToken)
and overloads of both that take aType
-
Deprecated
TypeToken.isAssignableFrom(TypeToken)
andTypeToken.isAssignableFrom(Type)
-isSupertypeOf
provides equivalent behavior with a less confusing name
common.util.concurrent
-
Added
AbstractFuture.newCancellationCause()
-
Added
AbstractFuture.setFuture(ListenableFuture)
-
Added
Futures.getChecked
-
Added
Futures.catching
andFutures.catchingAsync
-
Added
Futures.transformAsync
-
Added
Futures.withTimeout
-
Deprecated
FutureFallback
andFutures.withFallback
methods - these are replaced withFutures.catching
-
Deprecated
Futures.get
methods taking aClass<X extends Exception>
- these are replaced withFutures.getChecked
-
Deprecated
Futures.transform
methods taking anAsyncFunction
- these are replaced withFutures.transformAsync
詳細改進請看發行說明。
下載:https://github.com/google/guava/archive/v19.0-rc1.zip。