PART3. Concurrency
加了个ListenableFuture接口:
代码实例:
PART4. String
第一. Joiner
第二.Splitter
In JAVA, ",a,,b,".split(",") result : ["", "a", "", "b"] 默认忽略最后的空字符串,但前面的不省略,行为有点奇怪。
而Guava中:
Splitter.on(','). split(" a,,b ")
result : [“ a”,””,”b ”]
Splitter.on(',').trimResults().split(" a,,b ")
result : [“a”,””,”b”]
Splitter.on(',').trimResults().omitEmptyStrings().split(",a,,b,")
result : [“a”,”b”]
System.out.println(Splitter.on(',').limit(3).split("a,b,c,d,e"));
//["a","b","c,d,e"]
第三.CharMatcher
一些代码例子:
两大组成部分:
CharMatcher有一下几种:allAscii collapse collapseControlChars collapseWhitespace indexOfChars
lastIndexNotOf numSharedChars removeChars removeCrLf replaceChars
retainAllChars strip stripAndCollapse stripNonDigits
处理机制:
collapseFrom(CharSequence, char):Replace each group of consecutive matched characters with the specified character. For example, WHITESPACE.collapseFrom(string, ' ') collapses whitespaces down to a single space.
matchesAllOf(CharSequence):Test if this matcher matches all characters in the sequence. For example, ASCII.matchesAllOf(string) tests if all characters in the string are ASCII.
removeFrom(CharSequence):Removes matching characters from the sequence.
retainFrom(CharSequence):Removes all non-matching characters from the sequence.
trimFrom(CharSequence):Removes leading and trailing matching characters.
replaceFrom(CharSequence, CharSequence):Replace matching characters with a given sequence.
第四.CharSets
PART5. Primitives
Utilities for primitives: Bytes,Shorts,Ints,Longs,Floats,Doubles,Chars,Booleans
这些原始类的util类里主要有一下几类功能:
Array相关处理:
通过的util方法:
字节转换方法:
PART6. Ranges(基于JAVA实现,在动态语言如Groovy中更简便)
创建:
(a..b) open(C, C)
[a..b] closed(C, C)
[a..b) closedOpen(C, C)
(a..b] openClosed(C, C)
(a..+∞) greaterThan(C)
[a..+∞) atLeast(C)
(-∞..b) lessThan(C)
(-∞..b] atMost(C)
(-∞..+∞) all()
以上的方式也可以通过加入BoundType(OPEN,CLOSED两种)的方式获得:
Bounded on both ends range(C, BoundType, C, BoundType)
Unbounded on top ((a..+∞) or [a..+∞)) downTo(C, BoundType)
Unbounded on bottom ((-∞..b) or (-∞..b]) upTo(C, BoundType)
实例代码:
Range关联:
encloses关系:
可否相连关系isConnected:
映射关系intersection:
跨度范围span:
PART7. Reflection
第一. 取泛型信息:TypeToken
JDK通过反射可以获取泛型信息,主要通过ParameterizedType,而TypeToken就是基于这个特性进行的封装。使用:
Note that if mapToken just returned new TypeToken<Map<K, V>>(), it could not actually reify the types assigned to K and V, so for example:
第二. Invokable,这是对java.lang.reflect.Method and java.lang.reflect.Constructor封装后的类。
创建:
Invokable invoke = Invokable.from(Method method)
Invokable invoke = Invokable.from(Constructor<T> constructor)
使用:
判断方法是否public:
JDK: Modifier.isPublic(method.getModifiers())
Invokable: invokable.isPublic()
判断方法是否可以由子类覆盖:
JDK:
Invokable:
判断方法的第一个参数是否是由@Nullable annotated:
JDK:
Invokable:
第三. 动态代理
JDK:
Guava:
加了个ListenableFuture接口:
ListenableFuture extends Future{ void addListener(Runnable paramRunnable, Executor paramExecutor); }
代码实例:
ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); ListenableFuture explosion = service.submit(new Callable() { public Explosion call() { return pushBigRedButton(); } }); Futures.addCallback(explosion, new FutureCallback() { // we want this handler to run immediately after we push the big red button! public void onSuccess(Explosion explosion) { walkAwayFrom(explosion); } public void onFailure(Throwable thrown) { battleArchNemesis(); // escaped the explosion! } });
PART4. String
第一. Joiner
Joiner joiner = Joiner.on("; ").skipNulls(); System.out.println(joiner.join("Harry", null, "Ron", "Hermione")); // Harry; Ron; Hermione Joiner joiner2 = Joiner.on("; ").useForNull("yaya"); System.out.println(joiner2.join("Harry", null, "Ron", "Hermione")); // Harry; yaya; Ron; Hermione Joiner joiner3 = Joiner.on("; ").useForNull(null); System.out.println(joiner3.join("Harry", null, "Ron", "Hermione")); // throw NullPointException Joiner joiner4 = Joiner.on("; ").useForNull(null); System.out.println(joiner4.join("Harry", null, "Ron", "Hermione")); // throw NullPointException
第二.Splitter
In JAVA, ",a,,b,".split(",") result : ["", "a", "", "b"] 默认忽略最后的空字符串,但前面的不省略,行为有点奇怪。
而Guava中:
Splitter.on(','). split(" a,,b ")
result : [“ a”,””,”b ”]
Splitter.on(',').trimResults().split(" a,,b ")
result : [“a”,””,”b”]
Splitter.on(',').trimResults().omitEmptyStrings().split(",a,,b,")
result : [“a”,”b”]
System.out.println(Splitter.on(',').limit(3).split("a,b,c,d,e"));
//["a","b","c,d,e"]
第三.CharMatcher
一些代码例子:
String noControl = CharMatcher.JAVA_ISO_CONTROL.removeFrom(string); // remove control characters String theDigits = CharMatcher.DIGIT.retainFrom(string); // only the digits String spaced = CharMatcher.WHITESPACE.trimAndCollapseFrom(string, ' '); // trim whitespace at ends, and replace/collapse whitespace into single spaces String noDigits = CharMatcher.JAVA_DIGIT.replaceFrom(string, "*"); // star out all digits String lowerAndDigit = CharMatcher.JAVA_DIGIT.or(CharMatcher.JAVA_LOWER_CASE).retainFrom(string); // eliminate all characters that aren't digits or lowercase
两大组成部分:
CharMatcher有一下几种:allAscii collapse collapseControlChars collapseWhitespace indexOfChars
lastIndexNotOf numSharedChars removeChars removeCrLf replaceChars
retainAllChars strip stripAndCollapse stripNonDigits
处理机制:
collapseFrom(CharSequence, char):Replace each group of consecutive matched characters with the specified character. For example, WHITESPACE.collapseFrom(string, ' ') collapses whitespaces down to a single space.
matchesAllOf(CharSequence):Test if this matcher matches all characters in the sequence. For example, ASCII.matchesAllOf(string) tests if all characters in the string are ASCII.
removeFrom(CharSequence):Removes matching characters from the sequence.
retainFrom(CharSequence):Removes all non-matching characters from the sequence.
trimFrom(CharSequence):Removes leading and trailing matching characters.
replaceFrom(CharSequence, CharSequence):Replace matching characters with a given sequence.
第四.CharSets
PART5. Primitives
Utilities for primitives: Bytes,Shorts,Ints,Longs,Floats,Doubles,Chars,Booleans
这些原始类的util类里主要有一下几类功能:
Array相关处理:
List<Wrapper> asList(prim... backingArray) prim[] toArray(Collection<Wrapper> collection) prim[] concat(prim[]... arrays) boolean contains(prim[] array, prim target) int indexOf(prim[] array, prim target) int lastIndexOf(prim[] array, prim target) prim min(prim... array) prim max(prim... array) String join(String separator, prim... array)
通过的util方法:
int compare(prim a, prim b) prim checkedCast(long value) prim saturatedCast(long value)
字节转换方法:
int BYTES prim fromByteArray(byte[] bytes) prim fromBytes(byte b1, ..., byte bk) byte[] toByteArray(prim value)
PART6. Ranges(基于JAVA实现,在动态语言如Groovy中更简便)
创建:
(a..b) open(C, C)
[a..b] closed(C, C)
[a..b) closedOpen(C, C)
(a..b] openClosed(C, C)
(a..+∞) greaterThan(C)
[a..+∞) atLeast(C)
(-∞..b) lessThan(C)
(-∞..b] atMost(C)
(-∞..+∞) all()
以上的方式也可以通过加入BoundType(OPEN,CLOSED两种)的方式获得:
Bounded on both ends range(C, BoundType, C, BoundType)
Unbounded on top ((a..+∞) or [a..+∞)) downTo(C, BoundType)
Unbounded on bottom ((-∞..b) or (-∞..b]) upTo(C, BoundType)
实例代码:
Range.closed(1, 3).contains(2); // returns true Range.closed(1, 3).contains(4); // returns false Range.lessThan(5).contains(5); // returns false Range.closed(1, 4).containsAll(Ints.asList(1, 2, 3)); // returns true Range.closedOpen(4, 4).isEmpty(); // returns true Range.openClosed(4, 4).isEmpty(); // returns true Range.closed(4, 4).isEmpty(); // returns false Range.open(4, 4).isEmpty(); // Range.open throws IllegalArgumentException Range.closed(3, 10).lowerEndpoint(); // returns 3 Range.open(3, 10).lowerEndpoint(); // returns 3 Range.closed(3, 10).lowerBoundType(); // returns CLOSED Range.open(3, 10).upperBoundType(); // returns OPEN
Range关联:
encloses关系:
Range r1 = Range.open(11, 19); Range r2 = Range.open(2, 14); System.out.println(r1.encloses(r2)); //false Range r3 = Range.open(12, 14); System.out.println(r1.encloses(r3)); //true Range r4 = Range.closedOpen(4, 4); System.out.println(r4.isEmpty()); //true System.out.println(r1.encloses(r4)); //false Range r5 = Range.closedOpen(14, 14); System.out.println(r5.isEmpty()); //true System.out.println(r1.encloses(r5)); //true
可否相连关系isConnected:
Range.closed(3, 5).isConnected(Range.open(5, 10)); // returns true Range.closed(0, 9).isConnected(Range.closed(3, 4)); // returns true Range.closed(0, 5).isConnected(Range.closed(3, 9)); // returns true Range.open(3, 5).isConnected(Range.open(5, 10)); // returns false Range.closed(1, 5).isConnected(Range.closed(6, 10)); // returns false
映射关系intersection:
Range.closed(3, 5).intersection(Range.open(5, 10)); // returns (5, 5] Range.closed(0, 9).intersection(Range.closed(3, 4)); // returns [3, 4] Range.closed(0, 5).intersection(Range.closed(3, 9)); // returns [3, 5] Range.open(3, 5).intersection(Range.open(5, 10)); // throws IAE Range.closed(1, 5).intersection(Range.closed(6, 10)); // throws IAE
跨度范围span:
Range.closed(3, 5).span(Range.open(5, 10)); // returns [3, 10) Range.closed(0, 9).span(Range.closed(3, 4)); // returns [0, 9] Range.closed(0, 5).span(Range.closed(3, 9)); // returns [0, 9] Range.open(3, 5).span(Range.open(5, 10)); // returns (3, 10) Range.closed(1, 5).span(Range.closed(6, 10)); // returns [1, 10]
PART7. Reflection
第一. 取泛型信息:TypeToken
JDK通过反射可以获取泛型信息,主要通过ParameterizedType,而TypeToken就是基于这个特性进行的封装。使用:
Note that if mapToken just returned new TypeToken<Map<K, V>>(), it could not actually reify the types assigned to K and V, so for example:
class Util { static <K, V> TypeToken<Map<K, V>> incorrectMapToken() { return new TypeToken<Map<K, V>>() {}; } } System.out.println(Util.<String, BigInteger>incorrectMapToken()); // just prints out "java.util.Map<K, V>" Alternately, you can capture a generic type with a (usually anonymous) subclass and resolve it against a context class that knows what the type parameters are. abstract class IKnowMyType<T> { TypeToken<T> type = new TypeToken<T>(getClass()) {}; } ... new IKnowMyType<String>() {}.type; // returns a correct TypeToken<String>
第二. Invokable,这是对java.lang.reflect.Method and java.lang.reflect.Constructor封装后的类。
创建:
Invokable invoke = Invokable.from(Method method)
Invokable invoke = Invokable.from(Constructor<T> constructor)
使用:
判断方法是否public:
JDK: Modifier.isPublic(method.getModifiers())
Invokable: invokable.isPublic()
判断方法是否可以由子类覆盖:
JDK:
!(Modifier.isFinal(method.getModifiers()) || Modifiers.isPrivate(method.getModifiers()) || Modifiers.isStatic(method.getModifiers()) || Modifiers.isFinal(method.getDeclaringClass().getModifiers()))
Invokable:
invokable.isOverridable()
判断方法的第一个参数是否是由@Nullable annotated:
JDK:
for (Annotation annotation : method.getParameterAnnotations[0]) { if (annotation instanceof Nullable) { return true; } } return false;
Invokable:
invokable.getParameters().get(0).isAnnotationPresent(Nullable.class) invokable.invoke(object, args);
第三. 动态代理
JDK:
Foo foo = (Foo) Proxy.newProxyInstance( Foo.class.getClassLoader(), new Class<?>[] {Foo.class}, invocationHandler);
Guava:
Foo foo = Reflection.newProxy(Foo.class, invocationHandler);
发表评论
-
Guava学习1
2013-05-14 15:07 1085Guava项目涵括了好几个 ... -
Spring MVC学习
2013-05-10 14:08 866DispatcherServlet主要用作 ... -
JUC AQS
2013-01-24 09:07 1055一个基于AQS的Synchr ... -
分支预测(Branch Prediction)
2012-12-24 15:36 3371在写代码的时候, ... -
货币组合问题的实现
2012-12-17 16:37 1116问题:有1元,5元,10元,50元,100元的五种货币,任给定 ... -
笔记-再读深入浅出设计模式
2012-12-14 15:23 930代码复用à经验复用 如果每次新的需求一来,都会使某方面的 ... -
CLH锁学习
2012-11-28 13:46 5695CLH锁即Craig, Landin, and Hag ... -
java语言的线程安全级别
2012-10-26 14:02 1129java中一个类可能支持的线程安全性级别: 1.非可变的 ... -
Java多线程发展简史(转)
2012-10-08 13:49 792转载自:http://www.raychase.net/698 ... -
读《分布式java应用:基础与实践》
2012-08-14 16:59 826最近将林昊大牛的《分布式java应用:基础与实践》 ... -
Java 并发和NIO的学习计划
2012-07-05 14:27 559基础还是很重要的,并且必须要扎实,同时还要适当延伸出一定的宽度 ... -
同步与异步 阻塞与非阻塞型I/O(转)
2012-07-04 10:57 766同步阻塞IO 在这个 ... -
Java 线程/内存模型的缺陷和增强(转)
2012-06-01 22:08 918Java在语言层次上实现了 ... -
读《你的灯亮着吗》
2012-05-25 20:33 1401这几天翻读了下 ... -
excel大数据量处理小结(excel07)
2012-05-17 21:46 6785前端时间因为项 ...
相关推荐
guava学习体系知识点的学习,归纳出来了知识点。根据知识点去学习可以快熟了解guava
自己整理的Guava学习笔记,可能有点简陋,见谅哈!
Guava学习入门共51页.pdf.zip
guava 学习guava 最佳实践guava 基础工具guava 集合guava 源码学习ComparisonChain 源码学习
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libraries, common annotations, string processing, I/O, 等等. 这些高质量的 API 可以使你...
这个工程是为了学习guava concurrent中的AbstractFuture而建立的,里面有可以运行的例子,再配合我的博客:https://blog.csdn.net/o1101574955/article/details/82889851,可以看明白guava concurrent的基本设计思路...
下面我们就开启优雅Java编程学习之旅! 项目相关信息: 官方首页:http://code.google.com/p/guava-libraries 官方下载:http://code.google.com/p/guava-libraries/downloads/list 官方文档:...
NULL 博文链接:https://isuifengfei.iteye.com/blog/2031770
guava学习资料整合资料,包括文档和代码
guava-18.jar .
Guava作为Java编程的助手,可以提升开发效率,对Guava设计思想的学习则极大的有益于今后的编程之路。
guava学习参考。guava是google开发的非常方便的java库
引入Lambda表达式的作用是简化代码编写;函数接口的作用是让我们可以把函数包裹成函数接口,来实现把函数当做参数一样来使用(Java 不像 C 一样支持函数指针
跟着 Guava、Spring 学习如何设计观察者模式.doc
给需要的爱学习朋友吧! 没有积分的,还下不来,就1分意思意思吧。 不是为了1分,而是方便大家。 适合英文好的朋友。
Guava学习笔记笔记的大部分中文翻译内容来源于以下译者: 沉义扬,罗立树,何一昕,武祖校对:方腾飞感谢给我们带来的这一系列好文章。引言Guava工程包含了若干被Google的Java项目广泛依赖的核心库,例如:集合...
guava 技术使用研究,主要从基础角度介绍guava技术,在学习中的心得体会
Guava-Event-Bus 学习笔记 Guava的EventBus源码学习,基于Guava的18.0版本 分析者: ###1. 背景简介 EventBus是google的一个Java工具包其中的一个工具类,类似的有多个版本其中包括移植到Android端的 和改良的,功能...
谷歌guava的jar包和源码,很值得收藏。直接通过exlipse的build path,导入jar包和源码,可供开发和学习
轻率地使用null可能会导致很多令人惊愕的问题。通过学习Google底层代码库,我们发现95%的集合类不接受null值作为元素。相比默默地接受null,使用快速失败操作拒绝null值对开发者更有帮助