Mixja

Java library implementing Mixtures - Setup objects without needing private methods or using double brace initialization anti-pattern.

License

License

GroupId

GroupId

com.andyln
ArtifactId

ArtifactId

mixja
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

jar
Description

Description

Mixja
Java library implementing Mixtures - Setup objects without needing private methods or using double brace initialization anti-pattern.
Project URL

Project URL

https://github.com/NguyenAndrew/Mixja
Source Code Management

Source Code Management

http://github.com/NguyenAndrew/Mixja/tree/master

Download mixja

How to add to project

<!-- https://jarcasting.com/artifacts/com.andyln/mixja/ -->
<dependency>
    <groupId>com.andyln</groupId>
    <artifactId>mixja</artifactId>
    <version>1.0.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.andyln/mixja/
implementation 'com.andyln:mixja:1.0.0'
// https://jarcasting.com/artifacts/com.andyln/mixja/
implementation ("com.andyln:mixja:1.0.0")
'com.andyln:mixja:jar:1.0.0'
<dependency org="com.andyln" name="mixja" rev="1.0.0">
  <artifact name="mixja" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.andyln', module='mixja', version='1.0.0')
)
libraryDependencies += "com.andyln" % "mixja" % "1.0.0"
[com.andyln/mixja "1.0.0"]

Dependencies

test (2)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-engine jar 5.6.0
org.mockito : mockito-core jar 3.3.0

Project Modules

There are no modules declared in this project.

Mixja

GitHub
Maven metadata URL
javadoc

Java library implementing Mixtures (also known as IIFE or Immediately-Invoked Function Expressions) - Setup objects without needing private methods or using double brace initialization anti-pattern. Readable, efficient and safe way to construct ArrayLists, HashMaps, your own Objects, and more!

How to Install

Add the following line to your pom.xml

<dependency>
  <groupId>com.andyln</groupId>
  <artifactId>mixja</artifactId>
  <version>1.0.0</version>
</dependency>

Examples

mix - Creates an object that may require additional put/add/setters/etc. Java Lambda used to setup and return this object.

import static com.andyln.Mixja.mix;
...
private Map<String, String> map = mix(() -> {
    Map<String, String> map = new HashMap<>();
    map.put("Grace", "Hopper");
    map.put("Ada", "Lovelace");
    map.put("Alan", "Turing");
    return map;
});

eix - checked (e)xception may be thrown (m)ix. The performance of try-catch is why this method is standalone.

import static com.andyln.Mixja.eix;
...
private Set<String> brokenSet = eix(() -> {
    Set<String> set = mock(Set.class);
    when(set.add(anyString())).thenThrow(new UnsupportedOperationException());
    return set;
});

FAQ - Readings

Q: When should I use a Mixture or Immediately-Invoked Function Expression (IIFE)?

A: Avoids needing to create named functions. Cleaner code by encapsulating variables. IIFE are

  • Anonymous - You don't have to make a name for your function.
  • Brief - Code is easy to read due to its "one-time-use" nature.
  • Inline - Code is viewed exactly where it is used. No need to jump around code base to see functionalty.

Q: Are there any more resources where I can learn about IIFE?

A: Here are some of my recommended reading links (Note: These reading links are JavaScript specific, Java examples are in FAQ - Usages below):

FAQ - Usages

Q: Why use this library instead of just using Map.of(...)?

A: You should use Of(...) when possible! This library helps solves several problems not covered by that syntax.

  1. Can setup objects that are not part of Collections. Usable with your own classes or classes from your dependencies.
CustomObject customObject = mix(() -> {
    CustomObject customObject = new CustomObject();
    customObject.setName("Awesome");
    customObject.setFeature(new Feature());
    customObject.setDate("March 31, 2020");
    return customObject;
});
  1. Of(...) syntax is only available on Java 9+. Can use this library on Java 8.

Q: Can't I just use a Builder?

A: This library can deliver similar results to a Builder, but will also provide the following additional benefits:

  1. API delivers on use cases that a Builder may not inherently provide.
Toaster usedToaster = mix(() -> {
  Toaster toaster = new Toaster();
  toaster.add(new Bread());
  toaster.add(new Bagel());
  toaster.toast();
  toaster.removeAll();
  return toaster;
});
  1. Allows you to maintain fewer Builder classes and annotations

Versions

Version
1.0.0