Skip to content

Commit 640efed

Browse files
authored
Merge pull request #6 from QuickWrite/develop
Version `0.2.0-alpha`
2 parents 9f9c751 + 18f5dcd commit 640efed

22 files changed

+141
-163
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,21 @@ objects by yourself:
5252
FluentResource resource = FluentParser.parse("emails = You have { $unreadEmails } unread emails.");
5353
FluentBundle bundle = new ResourceFluentBundle(ULocale.ENGLISH, resource);
5454

55-
FluentArgs arguments = new ResourceFluentArguments();
55+
FluentArgs arguments = new FluentArguments();
5656
arguments.setNamed("unreadEmails", new NumberLiteral(10));
5757

58-
System.out.println(bundle.getMessage("emails", arguments));
58+
System.out.println(bundle.getMessage("emails", arguments).get());
5959
```
6060

6161
or you could use the builders that the library provides for this:
6262

6363
```java
6464
FluentBundle bundle = new FluentBundleBuilder(ULocale.ENGLISH, "emails = You have { $unreadEmails } unread emails.")
65-
.build();
66-
FluentArgs arguments = new FluentArgsBuilder().setNamed("unreadEmails", 10).build();
65+
.build();
6766

68-
System.out.println(bundle.getMessage("emails", arguments));
67+
FluentArgs arguments = new FluentArgsBuilder().set("unreadEmails", 10).build();
68+
69+
System.out.println(bundle.getMessage("emails", arguments).get());
6970
```
7071

7172
In both cases they would print the message `You have 10 unread emails.`.

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>net.quickwrite</groupId>
88
<artifactId>fluent4j</artifactId>
9-
<version>0.1.0-alpha</version>
9+
<version>0.2.0-alpha</version>
1010
<build>
1111
<plugins>
1212
<plugin>

src/main/java/net/quickwrite/fluent4j/ast/FluentBase.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ private StringSlice getVariantIdentifier(final StringSlice content) {
207207

208208
@Override
209209
public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
210+
if (this.fluentElements.size() == 1) {
211+
return this.fluentElements.get(0).getResult(bundle, arguments);
212+
}
213+
210214
final StringBuilder builder = new StringBuilder();
211215

212216
for (final FluentElement element : this.fluentElements) {

src/main/java/net/quickwrite/fluent4j/ast/placeable/FunctionReference.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.quickwrite.fluent4j.ast.placeable.base.FluentSelectable;
66
import net.quickwrite.fluent4j.util.StringSlice;
77
import net.quickwrite.fluent4j.util.args.FluentArgs;
8+
import net.quickwrite.fluent4j.util.args.FunctionFluentArgs;
89
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
910

1011
/**
@@ -24,7 +25,8 @@ public FluentElement getArgumentResult(final DirectFluentBundle bundle, final Fl
2425
try {
2526
return bundle
2627
.getFunction(this.functionName)
27-
.getResult(bundle, this.getArguments(bundle, arguments));
28+
.orElseThrow()
29+
.getResult(bundle, (FunctionFluentArgs) this.getArguments(bundle, arguments));
2830
} catch (final Exception exception) {
2931
return new StringLiteral("{" + functionName + "()}");
3032
}

src/main/java/net/quickwrite/fluent4j/ast/placeable/MessageReference.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.quickwrite.fluent4j.util.args.FluentArgs;
77
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
88

9+
910
/**
1011
* A use-case for placeables is referencing one message in another one.
1112
*
@@ -36,7 +37,9 @@ public String stringValue() {
3637

3738
@Override
3839
public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
39-
return bundle.getMessage(this.stringValue(), arguments);
40+
return bundle
41+
.getMessage(this.stringValue(), arguments)
42+
.orElse("{" + this.stringValue() + "}");
4043
}
4144

4245
@Override

src/main/java/net/quickwrite/fluent4j/ast/placeable/TermReference.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import net.quickwrite.fluent4j.ast.placeable.base.FluentFunction;
66
import net.quickwrite.fluent4j.util.StringSlice;
77
import net.quickwrite.fluent4j.util.args.FluentArgs;
8-
import net.quickwrite.fluent4j.util.args.ResourceNamedFluentArguments;
8+
import net.quickwrite.fluent4j.util.args.FluentArguments;
99
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
1010

1111
/**
@@ -62,7 +62,7 @@ public FluentElement getArgumentResult(final DirectFluentBundle bundle, final Fl
6262

6363
@Override
6464
protected FluentArgs getFluentArgumentInstance() {
65-
return new ResourceNamedFluentArguments();
65+
return new FluentArguments();
6666
}
6767

6868
@Override

src/main/java/net/quickwrite/fluent4j/ast/placeable/base/FluentFunction.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
import net.quickwrite.fluent4j.util.StringSlice;
66
import net.quickwrite.fluent4j.util.StringSliceUtil;
77
import net.quickwrite.fluent4j.util.args.FluentArgs;
8-
import net.quickwrite.fluent4j.util.args.ResourceFluentArguments;
8+
import net.quickwrite.fluent4j.util.args.FunctionFluentArgs;
9+
import net.quickwrite.fluent4j.util.args.FunctionFluentArguments;
910
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
1011
import org.apache.commons.lang3.tuple.ImmutablePair;
1112
import org.apache.commons.lang3.tuple.Pair;
@@ -30,16 +31,16 @@ public FluentFunction(final String functionName, final StringSlice content) {
3031
throw new FluentParseException("The callee has to be an upper-case identifier or a term");
3132
}
3233

33-
this.arguments = (content == null) ? FluentArgs.EMPTY_ARGS : this.getArguments(content);
34+
this.arguments = (content == null) ? FunctionFluentArgs.EMPTY_ARGS : this.getArguments(content);
3435
}
3536

3637
private FluentArgs getArguments(final StringSlice content) {
3738
StringSliceUtil.skipWhitespaceAndNL(content);
3839
if (content.isBigger()) {
39-
return FluentArgs.EMPTY_ARGS;
40+
return FunctionFluentArgs.EMPTY_ARGS;
4041
}
4142

42-
final FluentArgs arguments = this.getFluentArgumentInstance();
43+
final FunctionFluentArgs arguments = (FunctionFluentArgs) this.getFluentArgumentInstance();
4344

4445
while (!content.isBigger()) {
4546
Pair<String, FluentElement> argument = getArgument(content);
@@ -97,7 +98,7 @@ protected FluentArgs getArguments(final DirectFluentBundle bundle, final FluentA
9798
}
9899

99100
protected FluentArgs getFluentArgumentInstance() {
100-
return new ResourceFluentArguments();
101+
return new FunctionFluentArguments();
101102
}
102103

103104
/**

src/main/java/net/quickwrite/fluent4j/builder/FluentArgsBuilder.java

Lines changed: 12 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import net.quickwrite.fluent4j.ast.placeable.StringLiteral;
66
import net.quickwrite.fluent4j.functions.NumberFunction;
77
import net.quickwrite.fluent4j.util.args.FluentArgs;
8-
import net.quickwrite.fluent4j.util.args.ResourceFluentArguments;
8+
import net.quickwrite.fluent4j.util.args.FluentArguments;
99

1010
/**
1111
* The builder class for the {@link FluentArgs} class.
@@ -45,7 +45,7 @@ public class FluentArgsBuilder extends AbstractBuilder<FluentArgs> {
4545
* </ol>
4646
*/
4747
public FluentArgsBuilder() {
48-
super(new ResourceFluentArguments());
48+
super(new FluentArguments());
4949
}
5050

5151
/**
@@ -55,7 +55,7 @@ public FluentArgsBuilder() {
5555
* @param argument The argument itself with all of it's data
5656
* @return The FluentArgsBuilder object itself
5757
*/
58-
public FluentArgsBuilder setNamed(final String key, final FluentElement argument) {
58+
public FluentArgsBuilder set(final String key, final FluentElement argument) {
5959
this.element.setNamed(key, argument);
6060

6161
return this;
@@ -68,8 +68,8 @@ public FluentArgsBuilder setNamed(final String key, final FluentElement argument
6868
* @param argument The argument itself with all of it's data
6969
* @return The FluentArgsBuilder object itself
7070
*/
71-
public FluentArgsBuilder setNamed(final String key, final String argument) {
72-
return this.setNamed(key, new StringLiteral(argument));
71+
public FluentArgsBuilder set(final String key, final String argument) {
72+
return this.set(key, new StringLiteral(argument));
7373
}
7474

7575
/**
@@ -79,48 +79,19 @@ public FluentArgsBuilder setNamed(final String key, final String argument) {
7979
* @param argument The argument itself with all of it's data
8080
* @return The FluentArgsBuilder object itself
8181
*/
82-
public FluentArgsBuilder setNamed(final String key, final Number argument) {
83-
return this.setNamed(key, new NumberLiteral(argument));
82+
public FluentArgsBuilder set(final String key, final Number argument) {
83+
return this.set(key, new NumberLiteral(argument));
8484
}
8585

8686
/**
87-
* Adds a positional argument to the argument list.
88-
*
89-
* <p>
90-
* The argument will always be added at the end and cannot be rearranged.
91-
*
92-
* @param argument The argument itself with all of it's data
93-
* @return The FluentArgsBuilder object itself
94-
*/
95-
public FluentArgsBuilder addPositional(final FluentElement argument) {
96-
this.element.addPositional(argument);
97-
98-
return this;
99-
}
100-
101-
/**
102-
* Adds a positional {@link String} argument to the argument list.
103-
*
104-
* <p>
105-
* The argument will always be added at the end and cannot be rearranged.
106-
*
107-
* @param argument The argument itself with all of it's data
108-
* @return The FluentArgsBuilder object itself
109-
*/
110-
public FluentArgsBuilder addPositional(final String argument) {
111-
return this.addPositional(new StringLiteral(argument));
112-
}
113-
114-
/**
115-
* Adds a positional {@link Number} argument to the argument list.
116-
*
117-
* <p>
118-
* The argument will always be added at the end and cannot be rearranged.
87+
* Adds a named {@link Boolean} to the {@link FluentArgs} that can be
88+
* accessed.
11989
*
90+
* @param key The key that is used to access this argument
12091
* @param argument The argument itself with all of it's data
12192
* @return The FluentArgsBuilder object itself
12293
*/
123-
public FluentArgsBuilder addPositional(final Number argument) {
124-
return this.addPositional(new NumberLiteral(argument));
94+
public FluentArgsBuilder set(final String key, final Boolean argument) {
95+
return this.set(key, argument.toString());
12596
}
12697
}

src/main/java/net/quickwrite/fluent4j/functions/AbstractFunction.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.quickwrite.fluent4j.functions;
22

33
import net.quickwrite.fluent4j.util.args.FluentArgs;
4+
import net.quickwrite.fluent4j.util.args.FunctionFluentArgs;
45
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
56
import net.quickwrite.fluent4j.ast.placeable.base.FluentPlaceable;
67
import net.quickwrite.fluent4j.ast.placeable.NumberLiteral;
@@ -69,5 +70,5 @@ public String getIdentifier() {
6970
* @param arguments The arguments the function gets
7071
* @return The result
7172
*/
72-
public abstract FluentPlaceable getResult(final DirectFluentBundle bundle, final FluentArgs arguments);
73+
public abstract FluentPlaceable getResult(final DirectFluentBundle bundle, final FunctionFluentArgs arguments);
7374
}

0 commit comments

Comments
 (0)