Git Product home page Git Product logo

cactoos's Introduction

Managed by Zerocracy DevOps By Rultor.com

Build Status Build status Javadoc PDD status Maven Central License

jpeek report Test Coverage SonarQube

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 August December 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

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")
  )
).value();

To read a binary file from classpath:

byte[] data = new BytesOf(
  new ResourceOf("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(
	new TextOf("Hello")
);
// To upper case
new UpperText(
	new TextOf("Hello")
);

Iterables/Collections/Lists/Sets

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 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 SplitText(
      new TextOf(
        new File("/tmp/names.txt")
      ),
      new TextOf("\\s+")
    )
  )
);

To count elements in an iterable:

int total = new LengthOf(
  "how", "are", "you"
).value();

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 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();

Our objects vs. their static methods

Cactoos Guava Apache Commons JDK 8
And Iterables.all() - -
Filtered Iterables.filter() ? -
FormattedText - - String.format()
IsBlank - StringUtils.isBlank() -
JoinedText - - String.join()
LengthOf - - String#length()
LowerText - - String#toLowerCase()
NormalizedText - StringUtils.normalize() -
Or Iterables.any() - -
RepeatedText - StringUtils.repeat() -
ReplacedText - - String#replace()
ReversedText - - StringBuilder#reverse()
RotatedText - StringUtils.rotate() -
SplitText - - String#split()
StickyList Lists.newArrayList() ? Arrays.asList()
SubText - - String#substring()
SwappedCaseText - StringUtils.swapCase() -
TextOf ? IOUtils.toString() -
TrimmedLeftText - StringUtils.stripStart() -
TrimmedRightText - StringUtils.stripEnd() -
TrimmedText - StringUtils.stripAll() String#trim()
UpperText - - String#toUpperCase()

Questions

Ask your questions related to cactoos library on Stackoverflow with cactoos tag.

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.

cactoos's People

Contributors

akryvtsun avatar alex-semenyuk avatar bedward70 avatar caarlos0 avatar cheparinv avatar dronmdf avatar dusan-rychnovsky avatar fabriciofx avatar g4s8 avatar gookv avatar ixmanuel avatar myashchenko avatar rultor avatar smallcreep avatar svendiedrichsen avatar timmeey avatar vssekorin avatar yegor256 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.