1. Introduction

Cactoos is a library of object-oriented Java primitive types.

In this tutorial, we’ll take a look at some of the classes available as a part of this library.

2. Cactoos

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.

2.1. Comparison With 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.

Let’s take a look at some of these static methods that are part of the StringUtils package and their equivalent classes in 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

Let’s start by adding the required Maven dependency. The latest version of this library can be found on Maven Central:

<dependency>
    <groupId>org.cactoos</groupId>
    <artifactId>cactoos</artifactId>
    <version>0.55.0</version>
</dependency>

4. Strings

Cactoos has a wide range of classes for manipulating the String object.

4.1. String Object Creation

Let’s look at how a String object can be created using the TextOf class:

String testString = new TextOf("Test String").asString();

4.2. Formatted String

In case a formatted String needs to be created, we can use the FormattedText class:

String formattedString = new FormattedText("Hello %s", stringToFormat).asString();

Let’s verify that this method, in fact, returns the formatted String:

StringMethods obj = new StringMethods();

String formattedString = obj.createdFormattedString("John");
assertEquals("Hello John", formattedString);

4.3. Lower/Upper Case Strings

The Lowered class converts a String to its lower case using its TextOf object:

String lowerCaseString = new Lowered(new TextOf(testString)).asString();

Similarly, a given String can be converted to its upper case using the Upper class:

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

As discussed earlier, Cactoos library provides an IsBlank class to check for null or empty String:

new IsBlank(new TextOf(testString)) != null;

5. Collections

This library also provides several classes for working on Collections. Let’s take a look at a few of these.

5.1. Iterating a Collection

We can iterate a list of strings, using the utility class And:

new And(
  new Mapped<>(
    new FuncOf<>(
      input -> System.out.printf("Item: %s\n", input),
      new True()
    ),
    strings
  )
).value();

The above method is a functional way of iterating over the Strings list that writes the output to the logger.

5.2. Filtering a Collection

The Filtered class can be used to filter a collection based on specific criteria:

public Collection<String> getFilteredList(List<String> strings) {
    return new ListOf<>(
      new Filtered<>(
        s -> s.length() == 5,
        strings
      )
    );
}

Let’s test this method by passing in a few arguments, only 3 of which satisfy the criteria:

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

In this tutorial, we have looked at Cactoos library and some of the classes it provides for string and data structure manipulation.

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.