[Google Guava] 7-原生类型

简介:

原文链接 译文链接 译者:沈义扬,校对:丁一

概述

Java的原生类型就是指基本类型:byte、short、int、long、float、double、char和boolean。

在从Guava查找原生类型方法之前,可以先查查Arrays类,或者对应的基础类型包装类,如Integer

原生类型不能当作对象或泛型的类型参数使用,这意味着许多通用方法都不能应用于它们。Guava提供了若干通用工具,包括原生类型数组与集合API的交互,原生类型和字节数组的相互转换,以及对某些原生类型的无符号形式的支持。

原生类型 Guava工具类(都在com.google.common.primitives
byte BytesSignedBytesUnsignedBytes
short Shorts
int IntsUnsignedIntegerUnsignedInts
long LongsUnsignedLongUnsignedLongs
float Floats
double Doubles
char Chars
boolean Booleans

Bytes工具类没有定义任何区分有符号和无符号字节的方法,而是把它们都放到了SignedBytes和UnsignedBytes工具类中,因为字节类型的符号性比起其它类型要略微含糊一些。

int和long的无符号形式方法在UnsignedInts和UnsignedLongs类中,但由于这两个类型的大多数用法都是有符号的,Ints和Longs类按照有符号形式处理方法的输入参数。

此外,Guava为int和long的无符号形式提供了包装类,即UnsignedInteger和UnsignedLong,以帮助你使用类型系统,以极小的性能消耗对有符号和无符号值进行强制转换。

在本章下面描述的方法签名中,我们用Wrapper表示JDK包装类,prim表示原生类型。(Prims表示相应的Guava工具类。)

原生类型数组工具

原生类型数组是处理原生类型集合的最有效方式(从内存和性能双方面考虑)。Guava为此提供了许多工具方法。

方法签名 描述 类似方法 可用性
List<Wrapper> asList(prim… backingArray) 把数组转为相应包装类的List Arrays.asList 符号无关*
prim[] toArray(Collection<Wrapper> collection) 把集合拷贝为数组,和collection.toArray()一样线程安全 Collection.toArray() 符号无关
prim[] concat(prim[]… arrays) 串联多个原生类型数组 Iterables.concat 符号无关
boolean contains(prim[] array, prim target) 判断原生类型数组是否包含给定值 Collection.contains 符号无关
int indexOf(prim[] array, prim target) 给定值在数组中首次出现处的索引,若不包含此值返回-1 List.indexOf 符号无关
int lastIndexOf(prim[] array, prim target) 给定值在数组最后出现的索引,若不包含此值返回-1 List.lastIndexOf 符号无关
prim min(prim… array) 数组中最小的值 Collections.min 符号相关*
prim max(prim… array) 数组中最大的值 Collections.max 符号相关
String join(String separator, prim… array) 把数组用给定分隔符连接为字符串 Joiner.on(separator).join 符号相关
Comparator<prim[]>   lexicographicalComparator() 按字典序比较原生类型数组的Comparator Ordering.natural().lexicographical() 符号相关

*符号无关方法存在于Bytes, Shorts, Ints, Longs, Floats, Doubles, Chars, Booleans。而UnsignedInts, UnsignedLongs, SignedBytes, 或UnsignedBytes不存在。

*符号相关方法存在于SignedBytes, UnsignedBytes, Shorts, Ints, Longs, Floats, Doubles, Chars, Booleans, UnsignedInts, UnsignedLongs。而Bytes不存在。

通用工具方法

Guava为原生类型提供了若干JDK6没有的工具方法。但请注意,其中某些方法已经存在于JDK7中。

方法签名 描述 可用性
int compare(prim a, prim b) 传统的Comparator.compare方法,但针对原生类型。JDK7的原生类型包装类也提供这样的方法 符号相关
prim checkedCast(long value) 把给定long值转为某一原生类型,若给定值不符合该原生类型,则抛出IllegalArgumentException 仅适用于符号相关的整型*
prim saturatedCast(long value) 把给定long值转为某一原生类型,若给定值不符合则使用最接近的原生类型值 仅适用于符号相关的整型

*这里的整型包括byte, short, int, long。不包括char, boolean, float, 或double。

**译者注:不符合主要是指long值超出prim类型的范围,比如过大的long超出int范围。

注:com.google.common.math.DoubleMath提供了舍入double的方法,支持多种舍入模式。相见第12章的”浮点数运算”。

字节转换方法

Guava提供了若干方法,用来把原生类型按大字节序与字节数组相互转换。所有这些方法都是符号无关的,此外Booleans没有提供任何下面的方法。

方法或字段签名 描述
int BYTES 常量:表示该原生类型需要的字节数
prim fromByteArray(byte[] bytes) 使用字节数组的前Prims.BYTES个字节,按大字节序返回原生类型值;如果bytes.length <= Prims.BYTES,抛出IAE
prim fromBytes(byte b1, …, byte bk) 接受Prims.BYTES个字节参数,按大字节序返回原生类型值
byte[] toByteArray(prim value) 大字节序返回value的字节数组

无符号支持

JDK原生类型包装类提供了针对有符号类型的方法,而UnsignedInts和UnsignedLongs工具类提供了相应的无符号通用方法。UnsignedInts和UnsignedLongs直接处理原生类型:使用时,由你自己保证只传入了无符号类型的值。

此外,对int和long,Guava提供了无符号包装类(UnsignedIntegerUnsignedLong),来帮助你以极小的性能消耗,对有符号和无符号类型进行强制转换。

无符号通用工具方法

JDK的原生类型包装类提供了有符号形式的类似方法。

方法签名 说明
int UnsignedInts.parseUnsignedInt(String)long UnsignedLongs.parseUnsignedLong(String) 按无符号十进制解析字符串
int UnsignedInts.parseUnsignedInt(String string, int radix)long UnsignedLongs.parseUnsignedLong(String string, int radix) 按无符号的特定进制解析字符串
String UnsignedInts.toString(int)String UnsignedLongs.toString(long) 数字按无符号十进制转为字符串
String UnsignedInts.toString(int   value, int radix)String UnsignedLongs.toString(long value, int radix) 数字按无符号特定进制转为字符串

无符号包装类

无符号包装类包含了若干方法,让使用和转换更容易。

方法签名 说明
UnsignedPrim add(UnsignedPrim), subtract, multiply, divide, remainder 简单算术运算
UnsignedPrim valueOf(BigInteger) 按给定BigInteger返回无符号对象,若BigInteger为负或不匹配,抛出IAE
UnsignedPrim valueOf(long) 按给定long返回无符号对象,若long为负或不匹配,抛出IAE
UnsignedPrim asUnsigned(prim value) 把给定的值当作无符号类型。例如,UnsignedInteger.asUnsigned(1<<31)的值为231,尽管1<<31当作int时是负的
BigInteger bigIntegerValue() 用BigInteger返回该无符号对象的值
toString(),  toString(int radix) 返回无符号值的字符串表示

译者注:UnsignedPrim指各种无符号包装类,如UnsignedInteger、UnsignedLong

文章转自 并发编程网-ifeve.com


目录
相关文章
|
存储 缓存 算法
Google Guava之RateLimiter
在日常开发中,限流是高并发系统的三把守护利器之一,它的另外两个好兄弟缓存、降级下次再说。而限流在绝大多数场景中用来限制并发和请求量,像秒杀之类的高流量业务的场景,都能见到它的身影,所以它就是保护系统和下游的业务系统不被流量冲垮的利器。
237 6
Google Guava之RateLimiter
|
1月前
|
缓存 Java Maven
Google guava工具类库的介绍和使用
Google guava工具类库的介绍和使用
|
1月前
|
缓存 安全 Java
Google guava工具类的介绍和使用
Google guava工具类的介绍和使用
35 1
|
5月前
|
存储 缓存 Java
Google Guava EventBus使用
Google Guava EventBus使用
40 0
|
6月前
|
设计模式 消息中间件 缓存
JAVA设计模式第五讲:设计模式在 Google Guava 的应用
JAVA设计模式第五讲:设计模式在 Google Guava 的应用
|
6月前
|
存储 JavaScript Java
Cocos Creator Android 平台 Google 原生登录(二)
Cocos Creator Android 平台 Google 原生登录
123 0
|
6月前
|
API 开发工具 Android开发
Cocos Creator Android 平台 Google 原生登录(一)
Cocos Creator Android 平台 Google 原生登录
182 0
|
12月前
|
缓存 安全 JavaScript
别再造轮子了,Google 开源的 Guava 工具库真心强大!
别再造轮子了,Google 开源的 Guava 工具库真心强大!
|
Java API Maven
Guava:google公司开发的一款Java类库扩展工具包
Guava:google公司开发的一款Java类库扩展工具包
334 0
|
API
Google Guava之Splitter
我们上篇文章讲述了Joiner相关API使用,其中提到了Splitter,它与Joiner操作相反,它是根据给定的分隔符,把一个字符串分隔成若个子字符串,那么本篇我们来看看Splitter都有哪些操作的方式。
170 0
Google Guava之Splitter

热门文章

最新文章