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
@GwtCompatible
public @interface Beta {}
com.google.common.annotations.GwtCompatible
/**
* 表明一个类型可能会与 Google Web Toolkit 一起使用.
* 如果一个方法使用这个注释,说明这个方法的返回值是 GWT 兼容的
*
*/
@Retention(RetentionPolicy.CLASS)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@GwtCompatible
public @interface GwtCompatible { /**
* 说明一个类型或者方法的返回值是否支持 GWT 序列化
*
*/
boolean serializable() default false; /**
* 说明一个类型是否在 GWT 被模拟.
* 被模拟的源(父源)和JVM的实现不一样
*
*/
boolean emulated() default false;
}
com.google.common.annotations.GwtIncompatible
/**
* 说明一个方法可能无法与 GWT 一起使用
* 他只能用于被 @GwtCompatible 标志的类的字段,方法和内部类
*
*/
@Retention(RetentionPolicy.CLASS)
@Target({
ElementType.TYPE, ElementType.METHOD,
ElementType.CONSTRUCTOR, ElementType.FIELD })
@Documented
@GwtCompatible
public @interface GwtIncompatible {
/**
* 用于表示不兼容 GWT 的原因
*
*/
String value();
}
com.google.common.base.Charsets
/**
* 定义了一些字符编码常量
*
*/
@GwtCompatible(emulated = true)
public final class Charsets {
private Charsets() {} /**
* US-ASCII: seven-bit ASCII, the Basic Latin block of the Unicode character set (ISO646-US).
*/
@GwtIncompatible("Non-UTF-8 Charset")
public static final Charset US_ASCII = Charset.forName("US-ASCII"); /**
* ISO-8859-1: ISO Latin Alphabet Number 1 (ISO-LATIN-1).
*/
@GwtIncompatible("Non-UTF-8 Charset")
public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1"); /**
* UTF-8: eight-bit UCS Transformation Format.
*/
public static final Charset UTF_8 = Charset.forName("UTF-8"); /**
* UTF-16BE: sixteen-bit UCS Transformation Format, big-endian byte order.
*/
@GwtIncompatible("Non-UTF-8 Charset")
public static final Charset UTF_16BE = Charset.forName("UTF-16BE"); /**
* UTF-16LE: sixteen-bit UCS Transformation Format, little-endian byte order.
*/
@GwtIncompatible("Non-UTF-8 Charset")
public static final Charset UTF_16LE = Charset.forName("UTF-16LE"); /**
* UTF-16: sixteen-bit UCS Transformation Format, byte order identified by an optional byte-order
* mark.
*/
@GwtIncompatible("Non-UTF-8 Charset")
public static final Charset UTF_16 = Charset.forName("UTF-16"); /*
* Please do not add new Charset references to this class, unless those character encodings are
* part of the set required to be supported by all Java platform implementations! Any Charsets
* initialized here may cause unexpected delays when this class is loaded. See the Charset
* Javadocs for the list of built-in character encodings.
*/
}
com.google.common.hash.HashCode
/**
* 任意长度的不可变 HashCode*/
@Beta
public abstract class HashCode {
HashCode() {} /**
* 返回 hashCode 的前4个字节并以小端形式存储
* 他的实现有Byte, Int, Long转化为Int的形式
* 实现方式很简单,Byte转Int的小端的方法:
* 1) 先对hashCode前4个字节分别与0xFF(1111 1111)做&,目的是固定每个字节的code长度吧
* 2) 将各个字节做<<运算,使得hashCode以低位字节开头,转为小端
* 3) 将各个字节做|,合成最后的int返回
* 而Int, Long转Int都是直接返回原值
*/
public abstract int asInt(); /**
* 实现方法同asInt
*/
public abstract long asLong(); /**
* 返回hashCode的Byte数组形式,以小端方式返回
* 实现方法是对hashCode做>>运算后将低位字节强制转型为byte
*/
public abstract byte[] asBytes(); /**
* 将hashCode写入指定的byte[]数组
* 写入位置从目标byte[]的offset开始
* 写入长度为maxLength
*/
public int writeBytesTo(byte[] dest, int offset, int maxLength) {
byte[] hash = asBytes();
maxLength = Ints.min(maxLength, hash.length);
Preconditions.checkPositionIndexes(offset, offset + maxLength, dest.length);
System.arraycopy(hash, 0, dest, offset, maxLength);
return maxLength;
} /**
* 返回hashCode的位数长度
*/
public abstract int bits(); /**
* 使用hashCode的字节数组形式来比较两个HashCode是否相等
*/
@Override public boolean equals(Object object) {
if (object instanceof HashCode) {
HashCode that = (HashCode) object;
// Undocumented: this is a non-short-circuiting equals(), in case this is a cryptographic
// hash code, in which case we don't want to leak timing information
return MessageDigest.isEqual(this.asBytes(), that.asBytes());
}
return false;
} /**
* Returns a "Java hash code" for this {@code HashCode} instance; this is well-defined
* (so, for example, you can safely put {@code HashCode} instances into a {@code
* HashSet}) but is otherwise probably not what you want to use.
*/
@Override public int hashCode() {
/*
* As long as the hash function that produced this isn't of horrible quality, this
* won't be of horrible quality either.
*/
return asInt();
} /**
* 将hashCode按照byte[](小端)的形式转为16进制数字符串
* 其中每个byte转为两个16进制数,这个byte按照大端存储,而整个字符串还是按照小端存储
*/
@Override public String toString() {
byte[] bytes = asBytes();
// TODO(user): Use c.g.common.base.ByteArrays once it is open sourced.
StringBuilder sb = new StringBuilder(2 * bytes.length);
for (byte b : bytes) {
sb.append(hexDigits[(b >> 4) & 0xf]).append(hexDigits[b & 0xf]);
}
return sb.toString();
} private static final char[] hexDigits = "0123456789abcdef".toCharArray();
}
Guava 源码分析之 Beta, GwtCompatible, GwtIncompatible, Charset, HashCode的更多相关文章
- Guava 源码分析(Cache 原理 对象引用、事件回调)
前言 在上文「Guava 源码分析(Cache 原理)」中分析了 Guava Cache 的相关原理. 文末提到了回收机制.移除时间通知等内容,许多朋友也挺感兴趣,这次就这两个内容再来分析分析. 在开 ...
- Guava 源码分析之Cache的实现原理
Guava 源码分析之Cache的实现原理 前言 Google 出的 Guava 是 Java 核心增强的库,应用非常广泛. 我平时用的也挺频繁,这次就借助日常使用的 Cache 组件来看看 Goog ...
- [Guava源码分析]ImmutableCollection:不可变集合
摘要: 我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3888557.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的 ...
- [Guava源码分析]Ordering:排序
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3876466.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- Guava 源码分析(Cache 原理)
前言 Google 出的 Guava 是 Java 核心增强的库,应用非常广泛. 我平时用的也挺频繁,这次就借助日常使用的 Cache 组件来看看 Google 大牛们是如何设计的. 缓存 本次主要讨 ...
- [Guava源码分析]Objects 和 ComparisonChain:帮助重写Object方法
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3874194.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- [Guava源码分析] Preconditions 前置条件
我的技术博客经常被流氓网站恶意爬取转载.请移步原文:http://www.cnblogs.com/hamhog/p/3874170.html,享受整齐的排版.有效的链接.正确的代码缩进.更好的阅读体验 ...
- Guava源码分析——ServiceManager
ServiceManager类: 用于监控服务集的管理器,该类提供了诸如startAsync.stopAsync.servicesByState方法来运行.结束和检查服务集,而且,通过监听器 ...
- Guava cacha 机制及源码分析
1.ehcahce 什么时候用比较好:2.问题:当有个消息的key不在guava里面的话,如果大量的消息过来,会同时请求数据库吗?还是只有一个请求数据库,其他的等待第一个把数据从DB加载到Guava中 ...
随机推荐
- 如何解决谷歌Chrome浏览器空白页的问题
如何解决谷歌Chrome浏览器空白页的问题 谷歌Chrome浏览器突然不打开任何网页,无论是任何站点(如http://www.baidu.com), 还是Chrome浏览器的设置页面(chrome ...
- 编写一个简单的 JDBC 程序
连接数据库的步骤: 1.注册驱动(只做一次) 2.建立连接(Connection) 3.创建执行SQL的语句(Statement) 4.执行语句 5.处理执行结果(ResultSet) 6.释放资源 ...
- R语言编程艺术(2)R中的数据结构
本文对应<R语言编程艺术>第2章:向量:第3章:矩阵和数组:第4章:列表:第5章:数据框:第6章:因子和表 ======================================== ...
- 预备作业02:体会做中学(Learning By Doing)
1.很惭愧,我并没有什么技能能强过大家. 2...... 3.我觉得培养一个技能,必须要通过勤勉的练习,认真的学习,还有不断地结合实践. 4.我觉得我学习<程序设计与数据结构>之后应该对程 ...
- 机器学习之路: python nltk 文本特征提取
git: https://github.com/linyi0604/MachineLearning 分别使用词袋法和nltk自然预言处理包提供的文本特征提取 from sklearn.feature_ ...
- collection 和 collections
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha collection 是集合的意思. 集合 是 集合类的上级接口, 比如 set 和 l ...
- PHP 笔记——面向对象编程知识点
类是属性和方法的集合,是面向对象编程方式的核心和基础,通过类可以将零散的用于实现某项功能的代码进行有效管理. 类是由class关键字.类名和成员组成的,类名不区分大小写. 在类中定义常量使用关键字 c ...
- [BZOJ4771]七彩树(主席树)
https://blog.csdn.net/KsCla/article/details/78249148 用类似经典的链上区间颜色计数问题的做法,这个题可以看成是询问DFS在[L[x],R[x]]中, ...
- 【2016NOIP十连测】【test4】【状压DP】【容斥原理】巨神兵
题目大意: 给一个n个点(n<=17),m条边的有向图(无自环.无重边),求其无环子图的方案数. 题解: 看到n<=17,显然是用状压dp. 用f[i]表示点集i的满足条件的方案数. 状态 ...
- C++之lambda理解
简介 在C++ Primer中,是这样定义的-一个lambda表达式表示一个可调用的代码单元,可以将其理解为一个未命名的内联函数:与任何函数类似,一个lambda具有一个返回类型,一个参数列表和一个函 ...