ATTENTION: We're still in a very early alpha version, the API may and will change frequently. Please, use it at your own risk, until we release version 1.0. You can view our progress towards this release here.
Cactoos is a collection of object-oriented Java primitives.
Motivation. We are not happy with JDK, Guava, and Apache Commons because they are procedural and not object-oriented. They do their job, but mostly through static methods. Cactoos is suggesting to do almost exactly the same, but through objects.
Principles. These are the design principles behind Cactoos.
How to use. The library has no dependencies. All you need is this (get the latest version here):
Maven:
<dependency>
<groupId>org.cactoos</groupId>
<artifactId>cactoos</artifactId>
</dependency>
Gradle:
dependencies {
compile 'org.cactoos:cactoos:<version>'
}
Java version required: 1.8+.
StackOverflow tag is cactoos.
More about it here: Object-Oriented Declarative Input/Output in Cactoos.
To read a text file in UTF-8:
String text = new TextOf(
new File("/code/a.txt")
).asString();
To write a text into a file:
new LengthOf(
new TeeInput(
"Hello, world!",
new File("/code/a.txt")
)
).intValue();
To read a binary file from classpath:
byte[] data = new BytesOf(
new ResourceOf("foo/img.jpg")
).asBytes();
To format a text:
String text = new FormattedText(
"How are you, %s?",
name
).asString();
To manipulate with a text:
// To lower case
new Lowered(
new TextOf("Hello")
);
// To upper case
new Upper(
new TextOf("Hello")
);
More about it here: Lazy Loading and Caching via Sticky Cactoos Primitives.
To filter a collection:
Collection<String> filtered = new ListOf<>(
new Filtered<>(
s -> s.length() > 4,
new IterableOf<>("hello", "world", "dude")
)
);
To flatten one iterable:
new Joined<>(
new Mapped<>(
iter -> new IterableOf<>(
new CollectionOf<>(iter).toArray(new Integer[]{})
),
new IterableOf<>(new IterableOf<>(1, 2, 3, 4, 5, 6))
)
); // Iterable<Integer>
To flatten and join several iterables:
new Joined<>(
new Mapped<>(
iter -> new IterableOf<>(
new CollectionOf<>(iter).toArray(new Integer[]{})
),
new Joined<>(
new IterableOf<>(new IterableOf<>(1, 2, 3)),
new IterableOf<>(new IterableOf<>(4, 5, 6))
)
)
); // Iterable<Integer>
To iterate a collection:
new And(
new Mapped<>(
new FuncOf<>(
input -> {
System.out.printf("Item: %s\n", input);
}
),
new IterableOf<>("how", "are", "you")
)
).value();
Or even more compact:
new And(
(String input) -> System.out.printf("Item: %s\n", input),
"how", "are", "you"
).value();
To sort a list of words in the file:
List<String> sorted = new ListOf<>(
new Sorted<>(
new Split(
new TextOf(
new File("/tmp/names.txt")
),
new TextOf("\\s+")
)
)
);
To count elements in an iterable:
int total = new LengthOf(
"how", "are", "you"
).intValue();
This is a traditional foreach
loop:
for (String name : names) {
System.out.printf("Hello, %s!\n", name);
}
This is its object-oriented alternative (no streams!):
new And(
names,
n -> {
System.out.printf("Hello, %s!\n", n);
}
).value();
This is an endless while/do
loop:
while (!ready) {
System.out.prinln("Still waiting...");
}
Here is its object-oriented alternative:
new And(
new Endless<>(ready),
ready -> {
System.out.println("Still waiting...");
return !ready;
}
).value();
From our org.cactoos.time
package.
Our classes are divided in two groups: those that parse strings into date/time objects, and those that format those objects into strings.
For example, this is the traditional way of parsing a string into an OffsetDateTime:
final OffsetDateTime date = OffsetDateTime.parse("2007-12-03T10:15:30+01:00");
Here is its object-oriented alternative (no static method calls!) using OffsetDateTimeOf
, which is a Scalar
:
final OffsetDateTime date = new OffsetDateTimeOf("2007-12-03T10:15:30+01:00").value();
To format an OffsetDateTime
into a Text
:
final OffsetDateTime date = ...;
final OffsetDateTimeAsText text = new OffsetDateTimeAsText(date);
Cactoos | Guava | Apache Commons | JDK 8 |
---|---|---|---|
And |
Iterables.all() |
- | - |
Filtered |
Iterables.filter() |
? | - |
FormattedText |
- | - | String.format() |
IsBlank |
- | StringUtils.isBlank() |
- |
Joined |
- | - | String.join() |
LengthOf |
- | - | String#length() |
Lowered |
- | - | String#toLowerCase() |
Normalized |
- | StringUtils.normalize() |
- |
Or |
Iterables.any() |
- | - |
Repeated |
- | StringUtils.repeat() |
- |
Replaced |
- | - | String#replace() |
Reversed |
- | - | StringBuilder#reverse() |
Rotated |
- | StringUtils.rotate() |
- |
Split |
- | - | String#split() |
StickyList |
Lists.newArrayList() |
? | Arrays.asList() |
Sub |
- | - | String#substring() |
SwappedCase |
- | StringUtils.swapCase() |
- |
TextOf |
? | IOUtils.toString() |
- |
TrimmedLeft |
- | StringUtils.stripStart() |
- |
TrimmedRight |
- | StringUtils.stripEnd() |
- |
Trimmed |
- | StringUtils.stripAll() |
String#trim() |
Upper |
- | - | String#toUpperCase() |
Ask your questions related to cactoos library on Stackoverflow with cactoos tag.
Just fork the repo and send us a pull request.
Make sure your branch builds without any warnings/issues:
mvn clean install -Pqulice
Note: Checkstyle is used as a static code analyze tool with checks list in GitHub precommits.
- @yegor256 as Yegor Bugayenko (Blog)
- @g4s8 as Kirill Che. (g4s8.public@gmail.com)
- @fabriciofx as Fabrício Cabral
- @englishman as Andriy Kryvtsun
- @VsSekorin as Vseslav Sekorin
- @DronMDF as Andrey Valyaev
- @dusan-rychnovsky as Dušan Rychnovský (Blog)
- @timmeey as Tim Hinkes (Blog)
- @alex-semenyuk as Alexey Semenyuk
- @smallcreep as Ilia Rogozhin
- @memoyil as Mehmet Yildirim
- @llorllale as George Aristy
- @driver733 as Mikhail Yakushin
- @izrik as Richard Sartor
- @Vatavuk as Vedran Grgo Vatavuk
- @dgroup as Yurii Dubinka