09 2013 档案
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...
阅读全文
HotSpot Generations
摘要:本文主要介绍HotSpot JVM的 Generations 机制,原文来自 Oracle 文档 Java SE 6 HotSpot[tm] Virtual Machine Garbage Collection Tuning 第三,四章 -- Generations & Performance Co...
阅读全文
Java Run-Time Data Areas
摘要:前言本文主要介绍JVM的运行时数据区来自Oracle文档Java Virtual Machine Specification--Chapter2.The Structure of the Java Virtual Machine 2.5Link :http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-2.html#jvms-2.5----------------------------------------------------------------------------------------------------------
阅读全文
Reloading Java Classes 201: How do ClassLoader leaks happen? Translation
摘要:The original link :http://zeroturnaround.com/rebellabs/rjc201/From ClassLoaders to Classes从ClassLoader到ClassesIf you have programmed in Java for some time you know that memory leaks do happen. Usually it’s the case of a collection somewhere with references to objects (e.g. listeners) that should hav
阅读全文
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
阅读全文
Reloading Java Classes 101: Objects, Classes and ClassLoaders Translation
摘要:The original link:http://zeroturnaround.com/rebellabs/reloading-objects-classes-classloaders/A Bird's Eye View鸟瞰The first thing to understand when talking about reloading Java code is the relation between classes and objects. All Java code is associated with methods contained in classes. Simplif
阅读全文
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检查工具. 我们强烈推荐你
阅读全文
Java Singleton Implementation
摘要:概述Java中单例模式的实现有多重方法, 要实现单例模式主要的问题是线程安全问题以及对Lazy Load的考虑,主要有如下几种双重锁定懒加载单例预加载单例枚举单例双重锁定懒加载单例模式/** * 双重锁定懒加载单例实现 * * @author zhenwei.liu created on 2013 ...
阅读全文
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
阅读全文
Java Reference
摘要:概述Java中一共有四种Reference, 其中 SoftReference, WeakReference, PhantomReference内有一个Referent和ReferenceQueueReferent: 被引用对象RefernceQueue: 当引用的Referent被回收后该引用会被enqueue到这个ReferenceQueue中一个对象可以同时拥有多种引用, 可以通过Reference.get()方法获取ReferentStrongReference (强引用)强引用时Java中使用得最多的引用, 我们使用的普通引用就是Java强应用例如Object o = new Obj
阅读全文
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...
阅读全文
Guava 源码分析之 Beta, GwtCompatible, GwtIncompatible, Charset, HashCode
摘要:com.google.common.annotations.Beta/** * 表明一个公用API的未来版本是受不兼容变更或删除限制的 * 拥有这个注释标志的API不受任何兼容性保证 * */@Retention(RetentionPolicy.CLASS)@Target({ ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})@Documented@Gwt...
阅读全文
Java IO的简单示例
摘要:File类示例public class FileUsageTest { private static void usage() { System.err.println("Usage: FileUsageTest path1 ... \n" + "Creates each path\n" + "Usage: FileUsageTest -d path1 ...\n" + "Deletes each path\n" + "Usage: FileUsageTest -r path1 path2\n"
阅读全文
从volatile说到i++的线程安全问题
摘要:简介volatile关键字保证了在多线程环境下,被修饰的变量在别修改后会马上同步到主存,这样该线程对这个变量的修改就是对所有其他线程可见的,其他线程能够马上读到这个修改后值.Thread的本地内存每个Thread都拥有自己的线程存储空间Thread何时同步本地存储空间的数据到主存是不确定的例子借用G...
阅读全文
乐观锁与悲观锁
摘要:假如有一个购买行为事务,我们更需要去跟新数据库update item set amount = amount - 1 where item_id = 1;然而当amount只有1个的时候,同时有两个顾客进入了事务进行购买行为会如何,最后amount=-1,两个顾客都获得了这个商品,这显然不合理而使用乐观锁和悲观锁的解决方案可以如下:1. 乐观锁1) 概念: 在执行修改操作时不判断是否存在冲突,而是到了操作完成后再判断是否存在冲突,如有冲突则回滚2) 适用情况: 一般适用于回滚代价低,且冲突较少的情况.3) 优点: 执行操作时不会造成阻塞4) 缺点: 如果冲突较多,将造成较多的回滚操作5) 实现
阅读全文