8000 GitHub - NguyenAndrew/Mixja: Java library implementing Mixtures (also known as 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!
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Java library implementing Mixtures (also known as 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!

License

Notifications You must be signed in to change notification settings

NguyenAndrew/Mixja

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Java library implementing Mixtures (also known as 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!

Resources

License

Stars

Watchers

Forks

Packages

No packages published
2DD2

Languages

0