1. Introduction
1.绪论
Cactoos is a library of object-oriented Java primitive types.
Cactoos 是一个面向对象的Java原始类型库。
In this tutorial, we’ll take a look at some of the classes available as a part of this library.
在本教程中,我们将看一下作为该库一部分的一些可用的类。
2. Cactoos
2.仙人掌类
The Cactoos library’s repertoire is pretty rich, ranging from string manipulation to data structures. The primitive types and their corresponding methods offered by this library are similar to the ones provided by other libraries like Guava and Apache Commons but are more focused on object-oriented design principles.
Cactoos库的剧目相当丰富,从字符串操作到数据结构。这个库所提供的原始类型及其相应的方法与其他库所提供的类似,如Guava和Apache Commons,但更注重面向对象的设计原则。
2.1. Comparison With Apache Commons
2.1.与Apache Commons的比较
Cactoos library is equipped with classes that provide the same functionality as the static methods that are part of the Apache Commons library.
Cactoos库配备了提供与Apache Commons库中的静态方法相同功能的类。
Let’s take a look at some of these static methods that are part of the StringUtils package and their equivalent classes in Cactoos:
让我们看看这些静态方法中的一些,它们是StringUtils包的一部分,以及它们在Cactoos中的对应类。
Static method of StringUtils | Equivalent Cactoos class |
---|---|
isBlank() | IsBlank |
lowerCase() | Lowered |
upperCase() | Upper |
rotate() | Rotated |
swapCase() | SwappedCase |
stripStart() | TrimmedLeft |
stripEnd() | TrimmedRight |
More information on this can be found in the official documentation. We’ll take a look at the implementation of some of these in the subsequent section.
关于这方面的更多信息可以在官方文档中找到。我们将在随后的章节中看一下其中一些的实现。
3. The Maven Dependency
3.Maven的依赖性
Let’s start by adding the required Maven dependency. The latest version of this library can be found on Maven Central:
我们先来添加所需的Maven依赖性。该库的最新版本可以在Maven Central上找到。
<dependency>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
<version>0.43</version>
</dependency>
4. Strings
4. 弦乐
Cactoos has a wide range of classes for manipulating the String object.
Cactoos有大量的类用于操作String对象。
4.1. String Object Creation
4.1.创建字符串对象
Let’s look at how a String object can be created using the TextOf class:
让我们看看如何使用TextOf类来创建String对象。
String testString = new TextOf("Test String").asString();
4.2. Formatted String
4.2.格式化字符串
In case a formatted String needs to be created, we can use the FormattedText class:
如果需要创建一个格式化的String,我们可以使用FormattedText类。
String formattedString = new FormattedText("Hello %s", stringToFormat).asString();
Let’s verify that this method, in fact, returns the formatted String:
让我们验证一下,这个方法实际上是返回格式化的String。
StringMethods obj = new StringMethods();
String formattedString = obj.createdFormattedString("John");
assertEquals("Hello John", formattedString);
4.3. Lower/Upper Case Strings
4.3.小写/大写字符串
The Lowered class converts a String to its lower case using its TextOf object:
Lowered类使用其TextOf对象将String转换为其小写。
String lowerCaseString = new Lowered(new TextOf(testString)).asString();
Similarly, a given String can be converted to its upper case using the Upper class:
同样,一个给定的String可以使用Upper类转换为其大写。
String upperCaseString = new Upper(new TextOf(testString)).asString();
Let’s verify the outputs of these methods using a test string:
让我们用一个测试字符串来验证这些方法的输出。
StringMethods obj = new StringMethods();
String lowerCaseString = obj.toLowerCase("TeSt StrIng");
String upperCaseString = obj.toUpperCase("TeSt StrIng");
assertEquals("test string", lowerCaseString);
assertEquals("TEST STRING", upperCaseString);
4.4. Check for an Empty String
4.4.检查是否为空字符串
As discussed earlier, Cactoos library provides an IsBlank class to check for null or empty String:
如前所述,Cactoos库提供了一个IsBlank类来检查null或空String。
new IsBlank(new TextOf(testString)) != null;
5. Collections
5.收藏
This library also provides several classes for working on Collections. Let’s take a look at a few of these.
这个库还提供了几个用于处理Collections的类。让我们看一下其中的几个。
5.1. Iterating a Collection
5.1.迭代一个集合
We can iterate a list of strings, using the utility class And:
我们可以使用实用类And来迭代一个字符串的列表。
new And((String input) -> LOGGER.info(new FormattedText("%s\n", input).asString()), strings).value();
The above method is a functional way of iterating over the Strings list that writes the output to the logger.
上述方法是迭代Strings列表的一种功能方式,它将输出写入记录器。
5.2. Filtering a Collection
5.2.筛选一个集合
The Filtered class can be used to filter a collection based on specific criteria:
Filtered类可以用来根据特定的标准来过滤一个集合。
Collection<String> filteredStrings
= new ListOf<>(new Filtered<>(string -> string.length() == 5, new IterableOf<>(strings)));
Let’s test this method by passing in a few arguments, only 3 of which satisfy the criteria:
让我们通过传入一些参数来测试这个方法,其中只有3个参数满足标准。
CollectionUtils obj = new CollectionUtils();
List<String> strings = new ArrayList<String>() {
add("Hello");
add("John");
add("Smith");
add("Eric");
add("Dizzy");
};
int size = obj.getFilteredList(strings).size();
assertEquals(3, size);
Some other classes for Collections provided by this library can be found in the official documentation.
在官方文档中可以找到这个库提供的其他一些集合的类。
6. Conclusion
6.结语
In this tutorial, we have looked at Cactoos library and some of the classes it provides for string and data structure manipulation.
在本教程中,我们已经看了Cactoos库和它提供的一些用于字符串和数据结构操作的类。
In addition to these, the library also provides other utility classes for IO operations and also Date and Time.
As usual, the code samples used in this tutorial are available over on GitHub.
像往常一样,本教程中使用的代码样本可在GitHub上获得超过。