Skip to content

Timmeey/cactoos

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DevOps By Rultor.com

Build Status Build status Javadoc PDD status Test Coverage Maven Central License

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 (July 2017).

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. There are a few design principles behind Cactoos:

  • No null (why?)
  • No code in constructors (why?)
  • No getters and setters (why?)
  • No mutable objects (why?)
  • No static methods, not even private ones (why?)
  • No instanceof, type casting, or reflection (why?)
  • No implementation inheritance (why?)
  • No public methods without @Override
  • No statements in test methods except assertThat (why?)

How to use. The library has no dependencies. All you need is this (get the latest version here):

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

Java version required: 1.8+.

Input/Output

To read a text file in UTF-8:

String text = new BytesAsText(
  new InputAsBytes(
    new FileAsInput(
      new File("/code/a.txt")
    )
  )
).asString();

To write a text into a file:

new LengthOfInput(
  new TeeInput(
    new BytesAsInput(
      new TextAsBytes(
        new StringAsText("Hello, world!")
      )
    ),
    new FileAsOutput(
      new File("/code/a.txt")
    )
  )
).asValue();

To read a binary file from classpath:

byte[] data = new InputAsBytes(
  new ResourceAsInput("foo/img.jpg")
).asBytes();

Text/Strings

To format a text:

String text = new FormattedText(
  "How are you, %s?",
  name
).asString();

To manipulate with a text:

// To lower case
new LowerText("Hello");
// To upper case
new UpperText("Hello");

Iterables/Collections/Lists/Sets

To filter a collection:

Collection<String> filtered = new IterableAsCollection<>(
  new FilteredIterable<>(
    new ArrayAsIterable<>("hello", "world", "dude"),
    new Func<String, Boolean>() {
      @Override
      public boolean apply(String i) {
        return i.length() > 4;
      }
    }
  )
);

With Lambda:

new IterableAsCollection<>(
  new FilteredIterable<>(
    new ArrayAsIterable<>("hello", "world", "dude"),
    i -> i.length() > 4
  )
);

To iterate a collection:

new AllOf(
  new TransformedIterable<>(
    new ArrayAsIterable<>("how", "are", "you"),
    new ProcAsFunc<>(
      input -> {
        System.out.printf("Item: %s\n", input);
      }
    )
  )
).asValue();

Or even more compact:

new IterableAsBoolean(
  new ArrayAsIterable<>("how", "are", "you"),
  new ProcAsFunc<>(
    input -> System.out.printf("Item: %s\n", i)
  )
).asValue();

To sort a list of words in the file:

List<String> sorted = new SortedList<>(
  new IterableAsList<>(
    new TextAsLines(
      new InputAsText(
        new FileAsInput(
          new File("/tmp/names.txt")
        )
      )
    )
  )
);

To count elements in an iterable:

int total = new LengthOfIterable(
  new ArrayAsIterable<>("how", "are", "you")
).asValue();

Funcs and Procs

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 IterableAsBoolean<>(
  names,
  new ProcAsFunc<>(
    n -> {
      System.out.printf("Hello, %s!\n", n);
    }
  )
).asValue();

This is an endless while/do loop:

while (!ready) {
  System.out.prinln("Still waiting...");
}

Here is its object-oriented alternative:

new IterableAsBoolean<>(
  new EndlessIterable<>(ready),
  r -> {
    System.out.prinln("Still waiting...");
    return !ready;
  }
).asValue();

How to contribute?

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.

Contributors

License (MIT)

Copyright (c) 2017 Yegor Bugayenko

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

Object-Oriented Java Primitives

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 100.0%