Skip to content

Commit 1fbef60

Browse files
committed
Update README.md
1 parent b77a05f commit 1fbef60

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

fluentinterface/README.md

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,35 @@ tags:
99
---
1010

1111
## Intent
12-
A fluent interface provides an easy-readable, flowing interface, that often mimics a domain specific language. Using
13-
this pattern results in code that can be read nearly as human language.
12+
13+
A fluent interface provides an easy-readable, flowing interface, that often mimics a domain specific
14+
language. Using this pattern results in code that can be read nearly as human language.
1415

1516
## Explanation
1617

17-
The Fluent Interface pattern is useful when you want to provide an easy readable, flowing API. Those interfaces tend
18-
to mimic domain specific languages, so they can nearly be read as human languages.
18+
The Fluent Interface pattern is useful when you want to provide an easy readable, flowing API. Those
19+
interfaces tend to mimic domain specific languages, so they can nearly be read as human languages.
1920

2021
A fluent interface can be implemented using any of
2122

22-
* Method Chaining - calling a method returns some object on which further methods can be called.
23-
* Static Factory Methods and Imports
23+
* Method chaining - calling a method returns some object on which further methods can be called.
24+
* Static factory methods and imports.
2425
* Named parameters - can be simulated in Java using static factory methods.
2526

2627
Real world example
2728

28-
> We need to select numbers based on different criteria from the list. It's a great chance to utilize fluent interface pattern to provide readable easy-to-use developer experience.
29+
> We need to select numbers based on different criteria from the list. It's a great chance to
30+
> utilize fluent interface pattern to provide readable easy-to-use developer experience.
2931
3032
In plain words
3133

3234
> Fluent Interface pattern provides easily readable flowing interface to code.
3335
3436
Wikipedia says
3537

36-
> In software engineering, a fluent interface is an object-oriented API whose design relies extensively on method chaining. Its goal is to increase code legibility by creating a domain-specific language (DSL).
38+
> In software engineering, a fluent interface is an object-oriented API whose design relies
39+
> extensively on method chaining. Its goal is to increase code legibility by creating a
40+
> domain-specific language (DSL).
3741
3842
**Programmatic Example**
3943

@@ -134,29 +138,35 @@ result is printed afterwards.
134138
.first(2)
135139
.last()
136140
.ifPresent(number -> LOGGER.info("Last amongst first two negatives: {}", number));
137-
138-
// The initial list contains: 1, -61, 14, -22, 18, -87, 6, 64, -82, 26, -98, 97, 45, 23, 2, -68.
139-
// The first three negative values are: -61, -22, -87.
140-
// The last two positive values are: 23, 2.
141-
// The first even number is: 14
142-
// A string-mapped list of negative numbers contains: String[-61], String[-22], String[-87], String[-82], String[-98], String[-68].
143-
// The lazy list contains the last two of the first four positive numbers mapped to Strings: String[18], String[6].
144-
// Last amongst first two negatives: -22
141+
```
142+
143+
Program output:
144+
145+
```java
146+
The initial list contains: 1, -61, 14, -22, 18, -87, 6, 64, -82, 26, -98, 97, 45, 23, 2, -68.
147+
The first three negative values are: -61, -22, -87.
148+
The last two positive values are: 23, 2.
149+
The first even number is: 14
150+
A string-mapped list of negative numbers contains: String[-61], String[-22], String[-87], String[-82], String[-98], String[-68].
151+
The lazy list contains the last two of the first four positive numbers mapped to Strings: String[18], String[6].
152+
Last amongst first two negatives: -22
145153
```
146154

147155
## Class diagram
156+
148157
![Fluent Interface](./etc/fluentinterface.png "Fluent Interface")
149158

150159
## Applicability
160+
151161
Use the Fluent Interface pattern when
152162

153-
* You provide an API that would benefit from a DSL-like usage
154-
* You have objects that are difficult to configure or use
163+
* You provide an API that would benefit from a DSL-like usage.
164+
* You have objects that are difficult to configure or use.
155165

156166
## Known uses
157167

158168
* [Java 8 Stream API](http://www.oracle.com/technetwork/articles/java/ma14-java-se-8-streams-2177646.html)
159-
* [Google Guava FluentInterable](https://github.com/google/guava/wiki/FunctionalExplained)
169+
* [Google Guava FluentIterable](https://github.com/google/guava/wiki/FunctionalExplained)
160170
* [JOOQ](http://www.jooq.org/doc/3.0/manual/getting-started/use-cases/jooq-as-a-standalone-sql-builder/)
161171
* [Mockito](http://mockito.org/)
162172
* [Java Hamcrest](http://code.google.com/p/hamcrest/wiki/Tutorial)

fluentinterface/src/main/java/com/iluwatar/fluentinterface/app/App.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323

2424
package com.iluwatar.fluentinterface.app;
2525

26-
import static java.lang.String.valueOf;
27-
2826
import com.iluwatar.fluentinterface.fluentiterable.FluentIterable;
2927
import com.iluwatar.fluentinterface.fluentiterable.lazy.LazyFluentIterable;
3028
import com.iluwatar.fluentinterface.fluentiterable.simple.SimpleFluentIterable;

fluentinterface/src/main/java/com/iluwatar/fluentinterface/fluentiterable/lazy/LazyFluentIterable.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public FluentIterable<E> filter(Predicate<? super E> predicate) {
7070
return new LazyFluentIterable<>() {
7171
@Override
7272
public Iterator<E> iterator() {
73-
return new DecoratingIterator<E>(iterable.iterator()) {
73+
return new DecoratingIterator<>(iterable.iterator()) {
7474
@Override
7575
public E computeNext() {
7676
while (fromIterator.hasNext()) {
@@ -107,10 +107,10 @@ public Optional<E> first() {
107107
*/
108108
@Override
109109
public FluentIterable<E> first(int count) {
110-
return new LazyFluentIterable<E>() {
110+
return new LazyFluentIterable<>() {
111111
@Override
112112
public Iterator<E> iterator() {
113-
return new DecoratingIterator<E>(iterable.iterator()) {
113+
return new DecoratingIterator<>(iterable.iterator()) {
114114
int currentIndex;
115115

116116
@Override
@@ -149,10 +149,10 @@ public Optional<E> last() {
149149
*/
150150
@Override
151151
public FluentIterable<E> last(int count) {
152-
return new LazyFluentIterable<E>() {
152+
return new LazyFluentIterable<>() {
153153
@Override
154154
public Iterator<E> iterator() {
155-
return new DecoratingIterator<E>(iterable.iterator()) {
155+
return new DecoratingIterator<>(iterable.iterator()) {
156156
private int stopIndex;
157157
private int totalElementsCount;
158158
private List<E> list;
@@ -194,10 +194,10 @@ private void initialize() {
194194
*/
195195
@Override
196196
public <T> FluentIterable<T> map(Function<? super E, T> function) {
197-
return new LazyFluentIterable<T>() {
197+
return new LazyFluentIterable<>() {
198198
@Override
199199
public Iterator<T> iterator() {
200-
return new DecoratingIterator<T>(null) {
200+
return new DecoratingIterator<>(null) {
201201
final Iterator<E> oldTypeIterator = iterable.iterator();
202202

203203
@Override
@@ -226,7 +226,7 @@ public List<E> asList() {
226226

227227
@Override
228228
public Iterator<E> iterator() {
229-
return new DecoratingIterator<E>(iterable.iterator()) {
229+
return new DecoratingIterator<>(iterable.iterator()) {
230230
@Override
231231
public E computeNext() {
232232
return fromIterator.hasNext() ? fromIterator.next() : null;

0 commit comments

Comments
 (0)