- 浏览: 62867 次
- 性别:
- 来自: 成都
文章分类
最新评论
guava ComparisonChain源码的实现十分精妙,用到了利用内部类实现单例设计模式以及利用状态设计模式。代码十分优雅。
上源码欣赏:
public abstract class ComparisonChain {
//构造函数私有 单例模式
private ComparisonChain() {}
/**
* Begins a new chained comparison statement. See example in the class
* documentation.
*/
public static ComparisonChain start() {
return ACTIVE;
}
//利用内部类的方式来实现单例设计模式
private static final ComparisonChain ACTIVE =
new ComparisonChain() {
@SuppressWarnings("unchecked")
@Override
public ComparisonChain compare(Comparable left, Comparable right) {
return classify(left.compareTo(right));
}
@Override
public <T> ComparisonChain compare(
@Nullable T left, @Nullable T right, Comparator<T> comparator) {
return classify(comparator.compare(left, right));
}
@Override
public ComparisonChain compare(int left, int right) {
return classify(Ints.compare(left, right));
}
@Override
public ComparisonChain compare(long left, long right) {
return classify(Longs.compare(left, right));
}
@Override
public ComparisonChain compare(float left, float right) {
return classify(Float.compare(left, right));
}
@Override
public ComparisonChain compare(double left, double right) {
return classify(Double.compare(left, right));
}
@Override
public ComparisonChain compareTrueFirst(boolean left, boolean right) {
return classify(Booleans.compare(right, left)); // reversed
}
@Override
public ComparisonChain compareFalseFirst(boolean left, boolean right) {
return classify(Booleans.compare(left, right));
}
//委托到当前状态
ComparisonChain classify(int result) {
return (result < 0) ? LESS : (result > 0) ? GREATER : ACTIVE;
}
@Override
public int result() {
return 0;
}
};
//状态设计模式的体现 实现两个值保存大于和小于两种状态
private static final ComparisonChain LESS = new InactiveComparisonChain(-1);
private static final ComparisonChain GREATER = new InactiveComparisonChain(1);
private static final class InactiveComparisonChain extends ComparisonChain {
final int result;
InactiveComparisonChain(int result) {
this.result = result;
}
//返回当前状态
@Override
public ComparisonChain compare(@Nullable Comparable left, @Nullable Comparable right) {
return this;
}
@Override
public <T> ComparisonChain compare(
@Nullable T left, @Nullable T right, @Nullable Comparator<T> comparator) {
return this;
}
@Override
public ComparisonChain compare(int left, int right) {
return this;
}
@Override
public ComparisonChain compare(long left, long right) {
return this;
}
@Override
public ComparisonChain compare(float left, float right) {
return this;
}
@Override
public ComparisonChain compare(double left, double right) {
return this;
}
@Override
public ComparisonChain compareTrueFirst(boolean left, boolean right) {
return this;
}
@Override
public ComparisonChain compareFalseFirst(boolean left, boolean right) {
return this;
}
@Override
public int result() {
return result;
}
}
/**
* Compares two comparable objects as specified by {@link
* Comparable#compareTo}, <i>if</i> the result of this comparison chain
* has not already been determined.
*/
public abstract ComparisonChain compare(Comparable<?> left, Comparable<?> right);
/**
* Compares two objects using a comparator, <i>if</i> the result of this
* comparison chain has not already been determined.
*/
public abstract <T> ComparisonChain compare(
@Nullable T left, @Nullable T right, Comparator<T> comparator);
/**
* Compares two {@code int} values as specified by {@link Ints#compare},
* <i>if</i> the result of this comparison chain has not already been
* determined.
*/
public abstract ComparisonChain compare(int left, int right);
/**
* Compares two {@code long} values as specified by {@link Longs#compare},
* <i>if</i> the result of this comparison chain has not already been
* determined.
*/
public abstract ComparisonChain compare(long left, long right);
/**
* Compares two {@code float} values as specified by {@link
* Float#compare}, <i>if</i> the result of this comparison chain has not
* already been determined.
*/
public abstract ComparisonChain compare(float left, float right);
/**
* Compares two {@code double} values as specified by {@link
* Double#compare}, <i>if</i> the result of this comparison chain has not
* already been determined.
*/
public abstract ComparisonChain compare(double left, double right);
/**
* Discouraged synonym for {@link #compareFalseFirst}.
*
* @deprecated Use {@link #compareFalseFirst}; or, if the parameters passed
* are being either negated or reversed, undo the negation or reversal and
* use {@link #compareTrueFirst}.
* @since 19.0
*/
@Deprecated
public final ComparisonChain compare(Boolean left, Boolean right) {
return compareFalseFirst(left, right);
}
/**
* Compares two {@code boolean} values, considering {@code true} to be less
* than {@code false}, <i>if</i> the result of this comparison chain has not
* already been determined.
*
* @since 12.0
*/
public abstract ComparisonChain compareTrueFirst(boolean left, boolean right);
/**
* Compares two {@code boolean} values, considering {@code false} to be less
* than {@code true}, <i>if</i> the result of this comparison chain has not
* already been determined.
*
* @since 12.0 (present as {@code compare} since 2.0)
*/
public abstract ComparisonChain compareFalseFirst(boolean left, boolean right);
/**
* Ends this comparison chain and returns its result: a value having the
* same sign as the first nonzero comparison result in the chain, or zero if
* every result was zero.
*/
public abstract int result();
}
上源码欣赏:
public abstract class ComparisonChain {
//构造函数私有 单例模式
private ComparisonChain() {}
/**
* Begins a new chained comparison statement. See example in the class
* documentation.
*/
public static ComparisonChain start() {
return ACTIVE;
}
//利用内部类的方式来实现单例设计模式
private static final ComparisonChain ACTIVE =
new ComparisonChain() {
@SuppressWarnings("unchecked")
@Override
public ComparisonChain compare(Comparable left, Comparable right) {
return classify(left.compareTo(right));
}
@Override
public <T> ComparisonChain compare(
@Nullable T left, @Nullable T right, Comparator<T> comparator) {
return classify(comparator.compare(left, right));
}
@Override
public ComparisonChain compare(int left, int right) {
return classify(Ints.compare(left, right));
}
@Override
public ComparisonChain compare(long left, long right) {
return classify(Longs.compare(left, right));
}
@Override
public ComparisonChain compare(float left, float right) {
return classify(Float.compare(left, right));
}
@Override
public ComparisonChain compare(double left, double right) {
return classify(Double.compare(left, right));
}
@Override
public ComparisonChain compareTrueFirst(boolean left, boolean right) {
return classify(Booleans.compare(right, left)); // reversed
}
@Override
public ComparisonChain compareFalseFirst(boolean left, boolean right) {
return classify(Booleans.compare(left, right));
}
//委托到当前状态
ComparisonChain classify(int result) {
return (result < 0) ? LESS : (result > 0) ? GREATER : ACTIVE;
}
@Override
public int result() {
return 0;
}
};
//状态设计模式的体现 实现两个值保存大于和小于两种状态
private static final ComparisonChain LESS = new InactiveComparisonChain(-1);
private static final ComparisonChain GREATER = new InactiveComparisonChain(1);
private static final class InactiveComparisonChain extends ComparisonChain {
final int result;
InactiveComparisonChain(int result) {
this.result = result;
}
//返回当前状态
@Override
public ComparisonChain compare(@Nullable Comparable left, @Nullable Comparable right) {
return this;
}
@Override
public <T> ComparisonChain compare(
@Nullable T left, @Nullable T right, @Nullable Comparator<T> comparator) {
return this;
}
@Override
public ComparisonChain compare(int left, int right) {
return this;
}
@Override
public ComparisonChain compare(long left, long right) {
return this;
}
@Override
public ComparisonChain compare(float left, float right) {
return this;
}
@Override
public ComparisonChain compare(double left, double right) {
return this;
}
@Override
public ComparisonChain compareTrueFirst(boolean left, boolean right) {
return this;
}
@Override
public ComparisonChain compareFalseFirst(boolean left, boolean right) {
return this;
}
@Override
public int result() {
return result;
}
}
/**
* Compares two comparable objects as specified by {@link
* Comparable#compareTo}, <i>if</i> the result of this comparison chain
* has not already been determined.
*/
public abstract ComparisonChain compare(Comparable<?> left, Comparable<?> right);
/**
* Compares two objects using a comparator, <i>if</i> the result of this
* comparison chain has not already been determined.
*/
public abstract <T> ComparisonChain compare(
@Nullable T left, @Nullable T right, Comparator<T> comparator);
/**
* Compares two {@code int} values as specified by {@link Ints#compare},
* <i>if</i> the result of this comparison chain has not already been
* determined.
*/
public abstract ComparisonChain compare(int left, int right);
/**
* Compares two {@code long} values as specified by {@link Longs#compare},
* <i>if</i> the result of this comparison chain has not already been
* determined.
*/
public abstract ComparisonChain compare(long left, long right);
/**
* Compares two {@code float} values as specified by {@link
* Float#compare}, <i>if</i> the result of this comparison chain has not
* already been determined.
*/
public abstract ComparisonChain compare(float left, float right);
/**
* Compares two {@code double} values as specified by {@link
* Double#compare}, <i>if</i> the result of this comparison chain has not
* already been determined.
*/
public abstract ComparisonChain compare(double left, double right);
/**
* Discouraged synonym for {@link #compareFalseFirst}.
*
* @deprecated Use {@link #compareFalseFirst}; or, if the parameters passed
* are being either negated or reversed, undo the negation or reversal and
* use {@link #compareTrueFirst}.
* @since 19.0
*/
@Deprecated
public final ComparisonChain compare(Boolean left, Boolean right) {
return compareFalseFirst(left, right);
}
/**
* Compares two {@code boolean} values, considering {@code true} to be less
* than {@code false}, <i>if</i> the result of this comparison chain has not
* already been determined.
*
* @since 12.0
*/
public abstract ComparisonChain compareTrueFirst(boolean left, boolean right);
/**
* Compares two {@code boolean} values, considering {@code false} to be less
* than {@code true}, <i>if</i> the result of this comparison chain has not
* already been determined.
*
* @since 12.0 (present as {@code compare} since 2.0)
*/
public abstract ComparisonChain compareFalseFirst(boolean left, boolean right);
/**
* Ends this comparison chain and returns its result: a value having the
* same sign as the first nonzero comparison result in the chain, or zero if
* every result was zero.
*/
public abstract int result();
}
发表评论
-
guava不可变集合ImmutableList体系架构
2017-04-11 17:46 265guava不可变集合ImmutableList: -
guava 集合工具类FluentIterable
2017-04-06 14:07 520这个类专门针对iterable封装了许多方法。主要用于过滤集合 ... -
guava 函数式编程三兄弟之Supplier的用法
2017-04-05 16:37 2986Supplier用于创建对象。 public interfac ... -
guava 函数式编程三兄弟之Predicate的用法
2017-04-05 16:30 928predicate主要用于过滤数据下面是predicate接口 ... -
guava 函数式编程三兄弟之Function的用法
2017-04-05 16:24 894先上guavaFunction的接口: ... -
guava Objects的常用用法以及实现
2017-04-01 17:05 382Objects主要用来重写toString和HashCode方 ... -
Splitter和MapSplitter的用法
2017-03-31 17:22 1433Spliter和MapSpliter跟Joiner以及MapJ ... -
guava Joiner以及MapJoiner的用法
2017-03-31 15:10 2450guava joiner主要用于字符串的拼接:传统的假如我们想 ...
相关推荐
Guava is a set of core Java libraries from Google that includes new collection types (such as multimap and multiset), immutable collections, a graph library, and utilities for concurrency, I/O, ...
《深入解析Guava源码:探索Java编程的高级实践》 Guava,源自Google的开源Java库,以其丰富的集合...在实际开发中,关联Guava源码,可以实时查阅相关实现,加深对功能原理的理解,进而更好地利用Guava解决实际问题。
Java开发案例-springboot-64-整合Guava-Retry实现重试-源代码+文档.rar Java开发案例-springboot-64-整合Guava-Retry实现重试-源代码+文档.rar Java开发案例-springboot-64-整合Guava-Retry实现重试-源代码+文档.rar...
本篇文章将详细探讨如何利用Guava库实现定时缓存功能,以提高应用的性能和效率。 首先,Guava的Cache组件是其核心特性之一,它提供了一种高效、自动化的缓存机制。定时缓存功能允许我们存储计算结果或从远程服务...
├─Google Guava 第13讲-Guava之CharStreams和ByteStreams源码剖析(比较简单).wmv ├─Google Guava 第14讲-Guava之Closer使用和原理剖析,非常重要.wmv ├─Google Guava 第15讲-Base64原理详解,手动实现base...
这个压缩包包含了Guava 1.9版本的jar包和源码,这对于理解和学习Guava的内部实现非常有帮助。下面我们将深入探讨Guava的一些主要特性、功能以及如何使用。 1. **集合框架扩展** Guava对Java集合框架进行了扩展,...
通过查看源码,开发者可以深入理解Guava内部的实现原理,便于学习、调试和定制。源码分析可以帮助开发者更好地应用Guava库,理解其设计模式和最佳实践,同时也能提高代码质量。 总之,Guava库是Java开发者的强大...
本文将深入探讨如何利用Zookeeper和Guava来实现动态限流,并基于提供的"基于Zookeeper和guava动态限流 源码"进行解析。我们将首先理解Zookeeper和Guava的基本概念,然后介绍它们如何协同工作以实现动态限流,最后会...
本文重点讨论了三种限流算法:令牌桶算法、漏桶算法和计数器算法,并深入解析了Google Guava库中的RateLimiter类,它是基于令牌桶算法的实现。 1. **令牌桶算法**: - 令牌桶算法的核心思想是系统以恒定速率向桶中...
这个"Guava 工程项目包"包含了一个完整的 Guava 源码库,名为 "guava-master",对于学习和理解 Guava 的内部实现非常有帮助。 1. **核心库功能** - **集合框架**:Guava 提供了丰富的集合类,如 Multiset(多集)...
本篇文章将深入探讨Guava的学习,包括最佳实践、基础工具、集合以及源码分析,特别是`ComparisonChain`的源码学习。 首先,了解Guava的最佳实践是非常重要的。Guava提供了一些设计模式和编程技巧,可以帮助开发者写...
通过导入Guava的jar包和源码,开发者可以在Eclipse等IDE中直接查看和学习源代码,加深对Guava库的理解,提升开发效率。无论是开发新项目还是优化现有代码,Guava都是一个值得信赖的Java工具库。
- **Objects**:提供了更简洁的 `hashCode()` 和 `toString()` 实现,以及其它实用的 Object 工具方法。 - **Sorting**:提供强大的 fluent Comparators,支持多关键字排序。 - **Throwables**:简化异常处理和...
通过源码,开发者可以看到Guava是如何实现各种高级数据结构、集合操作、并发工具、缓存策略等复杂功能的。 Guava 21.0版本引入了许多新特性与改进,包括但不限于: - **新功能**: 添加了新的数据结构和算法,例如...
描述中提到的"guava-19.0-sources.jar"则是Guava库19.0版本的源码文件,它包含了所有Guava类的原始Java源代码。开发者可以通过查看这些源代码来理解Guava内部的工作原理,方便进行学习、调试和扩展。源码文件对于...
在本文中,我们将介绍如何在 SpringBoot 项目中使用 Guava Cache 实现本地缓存。Guava Cache 是一个高性能的缓存框架,由 Google 开发维护。它提供了一个灵活的缓存机制,可以根据实际需求进行配置。 首先,我们...
2. **缓存机制**:Guava提供了LruCache(最近最少使用)缓存实现,可以方便地在应用程序中构建高效的缓存系统。 3. **函数式编程**:Guava支持函数式编程,包括Function、Predicate、Transformer等接口,便于编写...
Guava的EventBus源码学习,基于Guava的18.0版本 分析者: ###1. 背景简介 EventBus是google的一个Java工具包其中的一个工具类,类似的有多个版本其中包括移植到Android端的 和改良的,功能基本都是一样的。此处主要...
3. guava-19.0.jar:此版本发布于2015年,主要更新包括对已有的类和接口进行优化和增强,例如改进了Multiset和Multimap的实现,提升了性能。同时,Guava 19开始支持Java 8的新特性,如默认方法和lambda表达式。在...
赠送jar包:guava-23.0.jar; 赠送原API文档:guava-23.0-javadoc.jar; 赠送源代码:guava-23.0-sources.jar; 赠送Maven依赖信息文件:guava-23.0.pom; 包含翻译后的API文档:guava-23.0-javadoc-API文档-中文...