Skip to content

Commit 640efed

Browse files
authoredSep 21, 2022
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

+6-5
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.`.
File renamed without changes.

‎pom.xml

+1-1
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

+4
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

+3-1
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

+4-1
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

+2-2
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

+6-5
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

+12-41
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

+2-1
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
}

‎src/main/java/net/quickwrite/fluent4j/functions/NumberFunction.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.quickwrite.fluent4j.ast.FluentElement;
44
import net.quickwrite.fluent4j.util.args.FluentArgs;
5+
import net.quickwrite.fluent4j.util.args.FunctionFluentArgs;
56
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
67
import net.quickwrite.fluent4j.ast.placeable.NumberLiteral;
78
import net.quickwrite.fluent4j.ast.placeable.base.FluentPlaceable;
@@ -73,7 +74,7 @@ public NumberFunction() {
7374
* @return The number as a {@link CustomNumberLiteral} with the parameters
7475
*/
7576
@Override
76-
public FluentPlaceable getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
77+
public FluentPlaceable getResult(final DirectFluentBundle bundle, final FunctionFluentArgs arguments) {
7778
final FluentElement number = arguments.getPositional(0);
7879

7980
CustomNumberLiteral numberLiteral;

‎src/main/java/net/quickwrite/fluent4j/util/args/FluentArgs.java

+1-25
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface FluentArgs {
1111
* so that it does not need to be created multiple
1212
* times and can be easily accessed.
1313
*/
14-
FluentArgs EMPTY_ARGS = new ResourceFluentArguments();
14+
FluentArgs EMPTY_ARGS = new FluentArguments();
1515

1616
/**
1717
* Changes the way some values point so that there are no
@@ -26,23 +26,6 @@ public interface FluentArgs {
2626
*/
2727
void sanitize(final DirectFluentBundle bundle, final FluentArgs arguments);
2828

29-
/**
30-
* Returns the {@link FluentElement} with the given
31-
* position in the positional arguments.
32-
*
33-
* @param index The position the argument has
34-
* @return The argument itself
35-
*/
36-
FluentElement getPositional(final int index);
37-
38-
/**
39-
* Returns the amount of positional
40-
* parameters the object has.
41-
*
42-
* @return The amount of positional parameters
43-
*/
44-
int getPositionalSize();
45-
4629
/**
4730
* Adds a new named argument to the named arguments.
4831
*
@@ -76,11 +59,4 @@ public interface FluentArgs {
7659
* @return if no argument exist
7760
*/
7861
boolean isEmpty();
79-
80-
/**
81-
* Adds a new positional argument at the end.
82-
*
83-
* @param argument The argument itself
84-
*/
85-
void addPositional(final FluentElement argument);
8662
}

‎src/main/java/net/quickwrite/fluent4j/util/args/ResourceNamedFluentArguments.java ‎src/main/java/net/quickwrite/fluent4j/util/args/FluentArguments.java

+3-18
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212
* A storage for the named arguments without the
1313
* positional arguments.
1414
*/
15-
public class ResourceNamedFluentArguments implements FluentArgs {
15+
public class FluentArguments implements FluentArgs {
1616
private final Map<String, FluentElement> namedArguments;
1717

1818
/**
1919
* Creates a new argument container with the given
2020
* arguments.
2121
*/
22-
public ResourceNamedFluentArguments() {
22+
public FluentArguments() {
2323
this(new HashMap<>());
2424
}
2525

@@ -29,7 +29,7 @@ public ResourceNamedFluentArguments() {
2929
*
3030
* @param namedArguments The named arguments
3131
*/
32-
public ResourceNamedFluentArguments(final Map<String, FluentElement> namedArguments) {
32+
public FluentArguments(final Map<String, FluentElement> namedArguments) {
3333
this.namedArguments = namedArguments;
3434
}
3535

@@ -44,16 +44,6 @@ public void sanitize(final DirectFluentBundle bundle, final FluentArgs arguments
4444
}
4545
}
4646

47-
@Override
48-
public FluentElement getPositional(final int index) {
49-
return null;
50-
}
51-
52-
@Override
53-
public int getPositionalSize() {
54-
return 0;
55-
}
56-
5747
@Override
5848
public void setNamed(final String key, final FluentElement argument) {
5949
this.namedArguments.put(key, argument);
@@ -74,11 +64,6 @@ public boolean isEmpty() {
7464
return this.namedArguments.isEmpty();
7565
}
7666

77-
@Override
78-
public void addPositional(final FluentElement argument) {
79-
80-
}
81-
8267
@Override
8368
public String toString() {
8469
return "ResourceNamedFluentArguments: {\n" +

0 commit comments

Comments
 (0)
Please sign in to comment.