随笔分类 - Guava
Chapter 7 -- Functional
摘要:Caveats说明As of Java 7, functional programming in Java can only be approximated through awkward and verbose use of anonymous classes. This is expected to change in Java 8, but Guava is currently aimed at users of Java 5 and above.在Java7中, 只能通过笨拙且啰嗦的使用匿名类来模拟函数式编程.在Java 8中这些亟待改变, 但Guava现在的目标是Java5及以上用户
阅读全文
Chapter 6 -- Caches
摘要:CachesExplainedExplanation for how to use Guava caches.explainedUpdatedJun 4, 2013bylowas...@google.comExampleLoadingCache graphs =CacheBuilder.newBuilder() .maximumSize(1000) .expireAfterWrite(10,TimeUnit.MINUTES) .removalListener(MY_LISTENER) .build( newCacheLoader(){ publicGraph ...
阅读全文
Chapter 5 -- ImmutableCollections
摘要:Examplepublicstatic final ImmutableSet COLOR_NAMES =ImmutableSet.of( "red", "orange", "yellow", "green", "blue", "purple");classFoo{ Set bars; Foo(Set bars){ this.bars =ImmutableSet.copyOf(bars);// defensive copy! }}Why?Immutable objects ha
阅读全文
Guava Enums
摘要:概述Enums提供了几个操作Enum的便利方法常用方法Field getField(Enum enumValue):返回变量名为enumValue变量值的Field> Function valueOfFunction(Class enumClass): 返回一个可以将enum名字字符串转换成指定类型的enum的,如果enum不存在时,Function将返回nullOptional getIfPresent(Class enumClass, String value):使用Enum.valueOf()来返回指定名称和class的Enum的Optional,如果不存在则返回Absent, 常
阅读全文
Guava CharMatcher
摘要:概述CharMatcher提供了多种对字符串处理的方法, 它的主要意图有:1. 找到匹配的字符2. 处理匹配的字符CharMatcher内部主要实现包括两部分:1. 实现了大量公用内部类, 用来方便用户对字符串做匹配: 例如 JAVA_DIGIT 匹配数字, JAVA_LETTER 匹配字母等等.2. 实现了大量处理字符串的方法, 使用特定的CharMatcher可以对匹配到的字符串做出多种处理, 例如 remove(), replace(), trim(), retain()等等CharMatcher本身是一个抽象类, 其中一些操作方法是抽象方法, 他主要依靠内部继承CharMatcher的
阅读全文
Guava CaseFormat
摘要:概述CaseFormat用来转换各种不同的编程语言间的变量名命名格式, 主要用到的方法只有一个 CaseFormat.to(CaseFormat from, String s) CaseFormat fromFormat = CaseFormat.LOWER_CAMEL; CaseFormat toFormat = CaseFormat.UPPER_CAMEL; String s = "lowerCamel"; System.out.println(fromFormat.to(toFormat, s));输出lowerCamelLower...
阅读全文
Guava BiMap AbstractBiMap
摘要:BiMapBiMap是一个结构,他定义了一个Map结构,代表这个Map的key和value都具有唯一性, 并且可以生成相互联系的反向视图, 反向视图的数据会随着本体BiMap的变更而变更/* * Copyright (C) 2007 The Guava Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a c
阅读全文
Guava CompoundOrdering
摘要:概述CompoundOrdering是Ordering的子类,它用来存储比较器链, Ordering的compound()内部实现就是使用CompoundOrdering(Comparator primary, Comparator secondary)方法来实现的代码/** An ordering that tries several comparators in order. */@GwtCompatible(serializable = true)final class CompoundOrdering extends Ordering imple...
阅读全文
Chapter 4 -- Throwables
摘要:TODO: rewrite with more examplesGuava'sThrowablesutility can frequently simplify dealing with exceptions.Propagation 传播Sometimes, when you catch an exception, you want to throw it back up to the next try/catch block. This is frequently the case forRuntimeExceptionorErrorinstances, which do not r
阅读全文
Guava ClassToInstanceMap
摘要:概述ClassToInstanceMap提供了一种是用Class作为Key, 对应实例作为Value的途径.他定义了T getInstance(Class)和T putInstance(Class T)两个方法, 这两个方法消除了元素类型转换的过程并保证了元素在Map中是类型安全的.ClassToInstanceMap有一个独立的类型参数, 一般命名为B. 它对应着Map的元素的类型的最大上界.例如ClassToInstanceMap numberDefaults = MutableClassToInstanceMap.create();numberDefaults.putInstance(I
阅读全文
Chapter 3 -- Ordering
摘要:Guava's fluent comparator class, Ordering, explained.explainedUpdatedJun 27, 2013bycpov...@google.comExample 例子assertTrue(byLengthOrdering.reverse().isOrdered(list));Overview 总览Orderingis Guava's "fluent"Comparatorclass, which can be used to build complex comparators and apply them
阅读全文
Chapter 2 -- Preconditions
摘要:Using Guava's precondition checking utilities, explained.explainedUpdatedApr 23, 2012bywasserman.louisPreconditionsGuava provides a number of precondition checking utilities. We strongly recommend importing these statically. (How to do this easily in Eclipse.)Guava提供了一系列precondition检查工具. 我们强烈推荐你
阅读全文
Chapter 1 -- UsingAndAvoidingNull
摘要:"Null sucks."-Doug Lea"Null 很恶心!""I call it my billion-dollar mistake."-Sir C. A. R. Hoare, on his invention of the null reference"我称它为我的十亿美元错误"Using and avoiding null 使用和避免nullCareless use ofnullcan cause a staggering variety of bugs. Studying the Google code
阅读全文
FinalizableReference, FinalizablePhantomReference, FinalizableReferenceQueue
摘要:FinalizableReference/* * Copyright (C) 2007 The Guava Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * *
阅读全文
Guava Finalizer
摘要:/* * Copyright (C) 2008 The Guava Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use * this file except in compliance with the License. You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable la
阅读全文
Reader 与 Guava MultiReader
摘要:Reader是Java IO体系里字符处理读取流的基本类,代码如下/* * %W% %E% * * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package java.io;/** * Abstract class for reading character streams. The only methods that a * subclass must .
阅读全文
Java Type Inference (类型推断)
摘要:public class Test2 { public static void main(String[] args) { ArrayList list = newArrayList(); list.add("k"); list.add("b"); System.out.println(list.getClass()); // 这一句编译是不会通过的,因为编译器对pick()返回值的推断是Serializable或Comparable类型 // 亦即 String "a", list 这两个参数共同继承的...
阅读全文
Guava Files 源码分析(二)
摘要:createTempDir()之后就没有什么有意思的函数了,基本上都是对Java IO函数操作的聚合,只看一个simplifyPath() /** * Returns the lexically cleaned form of the path name, usually (but * not always) equivalent to the original. The following heuristics are used: * * * empty string becomes . * . stays as . * fol...
阅读全文
Guava Files 源码分析(一)
摘要:Files中的工厂Files类中对InputStream, OutputStream以及Reader,Writer的操作封装了抽象工厂模式,抽象工厂是InputSupplier与OutputSupplier,具体工厂是Files中的newInputStreamSupplier(),newOutputStreamSupplier()等方法而InputStream, OutputStream以及Reader,Writer则是抽象产品, 他们的各种实现和装饰器包装则为具体产品Input与Output工厂Files中将Input与Output(包括InputStream,OutputStream和Re
阅读全文
观察者模式与Guava EventBus
摘要:观察者模式结构图代码实现public abstract class Subject { private List observerList = new ArrayList(); /** * 注册观察者 * @param observer */ public void register(Observer observer) { observerList.add(observer); } /** * 注销观察者 * * @param observer */ public void unregi...
阅读全文