Skip to content

jschwartz73/Flapi

 
 

Repository files navigation

Flapi - A fluent API generator for Java

v0.8 Build Status

Tip with Gratipay Tip with BitcoinBitcoin

What is it?

Flapi is a code generation library for creating fluent API's in Java. Fluent builders allow developers to more easily interact with your code, using a syntax more akin to natural language. See these articles for more information.

Flapi turns this:
Descriptor builder = Flapi.builder()
	.setPackage("unquietcode.tools.flapi.examples.email.builder")
	.setStartingMethodName("composeEmail")
	.setDescriptorName("Email")

	.addMethod("subject(String subject)").atMost(1)
	.addMethod("addRecipient(String emailAddress)").atLeast(1)
	.addMethod("sender(String emailAddress)").exactly(1)
	.addMethod("body(String text)").atMost(1)
	.addMethod("send()").last(EmailMessage.class)
.build();
...or this:
interface EmailHelper {

	@AtMost(1) void subject(String subject);
	@AtLeast(1) void addRecipient(String emailAddress);
	@Exactly(1) void sender(String emailAddress);
	@Any void addCC(String emailAddress);
	@Any void addBCC(String emailAddress);
	@AtMost(1) void body(String text);
	@Any void addAttachment(File file);
	@Last EmailMessage send();
}

Flapi.create(EmailHelper.class)
	.setPackage("unquietcode.tools.flapi.examples.email.builder")
	.setStartingMethodName("compose")
.build();
...into this:
composeEmail()
    .sender("HAL9000@gmail.com")
    .addRecipient("dave@unquietcode.com")
    .subject("Just what do you think you're doing, Dave?")
    .body("I know that you and Frank were planning to disconnect me, " +
          "and I'm afraid that's something I cannot allow to happen...")
.send();

If you are using Maven (or Gradle, or Ivy) you can download and install to your local repo, or include the following repository and dependency in your build script:

Maven

<repository>
  <id>uqc</id>
  <name>UnquietCode Repository</name>
  <url>http://www.unquietcode.com/maven/releases</url>
</repository>

...

<dependency>
  <groupId>unquietcode.tools.flapi</groupId>
  <artifactId>flapi</artifactId>
  <version>0.8</version>
  <scope>test</scope>
</dependency>

Gradle

repositories {
  maven {
    url 'http://www.unquietcode.com/maven/releases'
  }
}

...

dependencies {
  testCompile 'unquietcode.tools.flapi:flapi:0.8'
}

In a test define your Descriptor object and output the generated source code. (The Pizza Builder example is a simple descriptor you can start with.) You can also make use of the gradle plugin, or the maven plugin, to perform the code generation.

At the time of writing the project builds fine in JDK 6, however please note that the automated builds are no longer being run for that release. Future versions will be built using JDK 8, exposing a JDK 7 compatible public interface, and use JDK 7 as the default target for code generation, where the current default is JDK 6.

(PSA: If you're still using JDK 6 or lower please do something about that soon.)

Additional Resources

  • Documentation
    Please visit the documentation page for a tour of Flapi's features and how to use them. (generated using the very nice tool docker)

  • Examples
    Many helpful examples are included on the wiki, corresponding to examples and tests in the src/test directory.

  • Blog Post
    The original blog post describing Flapi.

What's the project's status?

Version 0.8 has been released, and includes a new Gradle build plugin, support for block mixins, and more. See the Release Notes for the full details.

Problems?

Use the issue tracker to report problems encountered or new feature requests.

Contributing

Feel free to fork the project and fiddle around! Submit pull requests to improve the code.
Create issues to help support the project. Ask questions. (Say hello.)

Tip with Gratipay Tip with BitcoinBitcoin

If you like this software and find it useful, then please consider supporting my efforts through a donation via BitCoin or other means.

Special thanks to Concurrent, Inc. for their feedback and support as a user of Flapi, which they use in their Fluid library for Cascading.

License

Flapi is licensed under the Apache Software License (ASL) 2.0

Thanks!

Peace, love, and code.

About

Flapi is an API generator for Java, which generates chained API's for improved fluency in your code.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 98.8%
  • Other 1.2%