All Projects → mintern-java → Functions

mintern-java / Functions

Licence: other
Every Java @FunctionalInterface you want

Projects that are alternatives of or similar to Functions

Springbootcodegenerator
又名大狼狗代码生成器,基于SpringBoot2+Freemarker的JAVA代码生成器,以释放双手为目的,支持mysql/oracle/pgsql三大数据库, 用DDL-SQL语句生成JPA/JdbcTemplate/Mybatis/MybatisPlus/BeetlSQL等相关代码.
Stars: ✭ 810 (+610.53%)
Mutual labels:  freemarker
Blogmanageplatform
一个springboot项目的脚手架,追求简洁高速可扩展。
Stars: ✭ 34 (-70.18%)
Mutual labels:  freemarker
Molgenis
MOLGENIS - for scientific data: management, exploration, integration and analysis.
Stars: ✭ 88 (-22.81%)
Mutual labels:  freemarker
Loginandroidstudiotemplate
This template generates code snippets for Login with Presenter, View, Model and Activity classes. Following are the different styles of login fields that are generated from this template.
Stars: ✭ 18 (-84.21%)
Mutual labels:  freemarker
Android Studio Templates
Android Studio Templates
Stars: ✭ 10 (-91.23%)
Mutual labels:  freemarker
Freemarker Java 8
Library adding java.time support to FreeMarker
Stars: ✭ 37 (-67.54%)
Mutual labels:  freemarker
Fs Blog
个人博客,Spring Boot 开山之作,采用 Spring Boot + MyBatis,前端 Bootstrap + LayUI,支持程序员非常青睐的轻量化的 Markdown 编辑器 Editor.md,支持标签分类检索
Stars: ✭ 700 (+514.04%)
Mutual labels:  freemarker
Blog
蓝眼系列软件之《蓝眼博客》开发中...
Stars: ✭ 108 (-5.26%)
Mutual labels:  freemarker
V5cmsjava
Spring+SpringMVC+Spring Data JPA+FreeMarker+Bootstarp(master分支),mybatis分支ORM使用的是MyBatis。
Stars: ✭ 29 (-74.56%)
Mutual labels:  freemarker
Bootplus
基于SpringBoot + Shiro + MyBatisPlus的权限管理框架
Stars: ✭ 88 (-22.81%)
Mutual labels:  freemarker
Alfresco Datalist Constraints
Use datalists to maintain Alfresco model constraints
Stars: ✭ 7 (-93.86%)
Mutual labels:  freemarker
Arc42 Gradle
Gradle project template for architecture documentation based on the arc42 template (s. http://www.arc42.de/)
Stars: ✭ 8 (-92.98%)
Mutual labels:  freemarker
Skeleton
🔨 Nepxion Skeleton is a generic codes and files generator based on freemaker for any text formats, and provides skeleton of Spring Cloud with docker deployment 基于Docker & Spring Cloud的代码和文件的脚手架生成平台
Stars: ✭ 61 (-46.49%)
Mutual labels:  freemarker
Vs Freemarker
FreeMarker language colorization extension for Visual Studio Code
Stars: ✭ 17 (-85.09%)
Mutual labels:  freemarker
Jeeplatform
一款企业信息化开发基础平台,拟集成OA(办公自动化)、CMS(内容管理系统)等企业系统的通用业务功能 JeePlatform项目是一款以SpringBoot为核心框架,集ORM框架Mybatis,Web层框架SpringMVC和多种开源组件框架而成的一款通用基础平台,代码已经捐赠给开源中国社区
Stars: ✭ 1,285 (+1027.19%)
Mutual labels:  freemarker
Springboot Learning
🚕 spring boot学习案例,方便spring boot 初学者快速掌握相关知识
Stars: ✭ 724 (+535.09%)
Mutual labels:  freemarker
Android Studio Templates
A set of templates for your Android Studio
Stars: ✭ 35 (-69.3%)
Mutual labels:  freemarker
Android Studio Mvp Template Google Architecture
Stars: ✭ 111 (-2.63%)
Mutual labels:  freemarker
Materialtabstemplate
MaterialTabsTemplate is solely created to reduce the burden of writing same boiler plate codes for Tab creation in Android.
Stars: ✭ 92 (-19.3%)
Mutual labels:  freemarker
Android Studio Group Templates Mvp
Example of template for Android Studio, to create a set of files for MVP functionality.
Stars: ✭ 62 (-45.61%)
Mutual labels:  freemarker

Java Functions

Maven Central Javadoc

To include the full library in your project (not recommended!), add the following to your POM:

<project>
...
    <dependencies>
        ...
        <dependency>
            <groupId>net.mintern</groupId>
            <artifactId>functions-ternary-all</artifactId>
            <version>2.0</version>
        </dependency>
        ...
    </dependencies>
...
</project>

You almost certainly do not want or need all of the 16,000+ (!) classes, so replace ternary-all with the subset you need.

Every Java @FunctionalInterface you want!

This project provides @FunctionalInterfaces for every possible function of 0 to 3 arguments. It enumerates all combinations of parameters and return values for all Java types:

  • void
  • boolean
  • byte
  • short
  • char
  • int
  • long
  • float
  • double
  • Object

The functions are named in a logical way that is well-suited for automatic code generation but remains readable. For example:

  • NilToLong: long call()
  • ObjIntToNil<T>: void call(T t, int i)
  • BoolToShort: short call(boolean bool)
  • DblFloatByteToChar: char call(double d, float fl, byte b)
  • ObjObjObjToObj<T, U, V, R>: R call(T t, U u, V v)

Checked interfaces

As with Java 8's java.util.function package, none of the above methods throw exceptions. Especially in the case of IOException this can lead to lambdas that are much uglier than necessary. To solve this problem, this project also provides a checked version for every interface, indicated by its presence in a .checked package and an E suffix:

  • NilToLongE<E extends Exception>: long call() throws E
  • ObjIntToNilE<T, E extends Exception>: void call(T t, int i) throws E
  • BoolToShortE<E extends Exception>: short call(boolean bool) throws E

...and so on. This means that you can now write a method like this:

void withReader(Path p, ObjToNilE<BufferedReader, IOException> f) throws IOException {
    try (BufferedReader r = Files.newBufferedReader(p, Charset.forName("UTF-8"))) {
        f.call(r);
    }
}

and call it as follows:

withReader(path, reader -> {
    String line;
    while ((line = reader.readLine()) != null) { // <-- may throw!
        // do something with line
    }
});

This works! In other words, there's no need to catch and wrap the exception when you use these checked interfaces in obvious places.

Static methods

It gets better than that! Even when a method expects an unchecked functional interface, you can still avoid the exception handling boilerplate:

Stream<Path> paths = ...;
String[] firstLines = paths
        .map(ObjToObj.uncheckedIO(path -> Files.newBufferedReader(path, utf8)))
        .map(ObjToObj.uncheckedIO(BufferedReader::readLine))
        .toArray(String[]::new);

Both newBufferedReader and getLine can throw exceptions, but the uncheckedIO method transparently converts an ObjToObjE<T, U, IOException> to an ObjToObj<T, U>, wrapping any thrown IOException with an UncheckedIOException!

A more general unchecked(Function<? super E, RuntimeException> wrap, f) is also provided that allows you to wrap your exception however you wish, or you can use unchecked(f) to simply wrap it in a RuntimeException.

But Stream.map takes a Function!

I'm glad you noticed that! Any net.mintern.functions interface that has the same signature as a Java interface extends the Java one, meaning that you can just plug it right in! For example:

  • NilToNil extends Runnable
  • ObjIntToNil<T> extends ObjIntConsumer<T>
  • ObjObjToObj<T, U, R> extends BiFunction<T, U, R>

and so on. There is one exception: as of 2.0, NilToObjE<V> doesn't extends Callable<V>, as this breaks lambda type inference. See #2.

bind

In case that's not enough, every non-nullary function provides both static and instance bind and rbind methods. If you are tired of this pattern:

hexStrings.mapToInt(hexString -> Integer.valueOf(hexString, 16))

then you can replace it with:

hexStrings.mapToInt(ObjIntToInt.rbind(Integer::valueOf, 16))

It's not a huge win in this case, but perhaps you'll find places where it is!

Sensible packaging

As you might imagine, providing all of these type combinations results in an explosion in the number of classes. In order to avoid pulling in so many functions that you are unlikely to use, I've split them up as follows:

  • nullary (20 classes): checked and unchecked functions to produce every type (a la Java's Suppliers)
  • unary-core (48 classes): 1-argument functions that accept int, long, double, or Object, returning void, boolean, int, long, double, or Object. The unchecked unary-core functions correspond to Java's Function and Predicate types.
  • unary-extended (96 classes): unary-core, but with boolean, byte, char, and float arguments and return values
  • unary-all (36 classes): all of unary-extended, plus short types (because who uses short?)
  • binary-core (192 classes): like unary-core, but for 2-argument functions (includes replacements for BiFunction, ObjIntConsumer, BiPredicate, etc.)
  • binary-extended (960 classes): you get the idea
  • binary-all (468 classes)
  • ternary-core (768 classes): exactly what you think it is
  • ternary-extended (8448 classes): not a typo!
  • ternary-all (5364 classes)

Only pull in what you need!

Note that every extended package pulls in its corresponding core package, in addition to the other extended packages that take fewer arguments. Likewise, all pulls in extended. This means that a dependency on binary-extended will also pull in binary-core, unary-extended, unary-core, and nullary.

Contributing

I will happily accept Pull Requests. If you have any questions, ask away.

Building

In the root directory, run mvn install. That will build everything.

Where are all the source files?

For this project, I used an ancient-but-solid (by modern standards) template language called FreeMarker to provide a template that generates all of the source. A handy FreeMarker PreProcessor (FMPP)—in conjunction with an FMPP Maven plugin that I came across on Stack Overflow—turns that template into thousands of Java source files.

Some data definitions are in semicolon-delimited (scsv!) files in the FreeMarker configuration, but most of the logic for generating everything is in a single template file: Functions.java.ft.

What's next?

I'll be using this library to build out modern Pair and Triple libraries, Either types, Collection and Stream (and BiStream and TriStream) enhancements, and more. If you like this library, keep an eye on the mintern-java organization for new ones!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].