Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 0.2.0-alpha #6

Merged
merged 12 commits into from
Sep 21, 2022
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,21 @@ objects by yourself:
FluentResource resource = FluentParser.parse("emails = You have { $unreadEmails } unread emails.");
FluentBundle bundle = new ResourceFluentBundle(ULocale.ENGLISH, resource);

FluentArgs arguments = new ResourceFluentArguments();
FluentArgs arguments = new FluentArguments();
arguments.setNamed("unreadEmails", new NumberLiteral(10));

System.out.println(bundle.getMessage("emails", arguments));
System.out.println(bundle.getMessage("emails", arguments).get());
```

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

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

System.out.println(bundle.getMessage("emails", arguments));
FluentArgs arguments = new FluentArgsBuilder().set("unreadEmails", 10).build();

System.out.println(bundle.getMessage("emails", arguments).get());
```

In both cases they would print the message `You have 10 unread emails.`.
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.quickwrite</groupId>
<artifactId>fluent4j</artifactId>
<version>0.1.0-alpha</version>
<version>0.2.0-alpha</version>
<build>
<plugins>
<plugin>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/quickwrite/fluent4j/ast/FluentBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ private StringSlice getVariantIdentifier(final StringSlice content) {

@Override
public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
if (this.fluentElements.size() == 1) {
return this.fluentElements.get(0).getResult(bundle, arguments);
}

final StringBuilder builder = new StringBuilder();

for (final FluentElement element : this.fluentElements) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.quickwrite.fluent4j.ast.placeable.base.FluentSelectable;
import net.quickwrite.fluent4j.util.StringSlice;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.args.FunctionFluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;

/**
Expand All @@ -24,7 +25,8 @@ public FluentElement getArgumentResult(final DirectFluentBundle bundle, final Fl
try {
return bundle
.getFunction(this.functionName)
.getResult(bundle, this.getArguments(bundle, arguments));
.orElseThrow()
.getResult(bundle, (FunctionFluentArgs) this.getArguments(bundle, arguments));
} catch (final Exception exception) {
return new StringLiteral("{" + functionName + "()}");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;


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

@Override
public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
return bundle.getMessage(this.stringValue(), arguments);
return bundle
.getMessage(this.stringValue(), arguments)
.orElse("{" + this.stringValue() + "}");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.quickwrite.fluent4j.ast.placeable.base.FluentFunction;
import net.quickwrite.fluent4j.util.StringSlice;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.args.ResourceNamedFluentArguments;
import net.quickwrite.fluent4j.util.args.FluentArguments;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;

/**
Expand Down Expand Up @@ -62,7 +62,7 @@ public FluentElement getArgumentResult(final DirectFluentBundle bundle, final Fl

@Override
protected FluentArgs getFluentArgumentInstance() {
return new ResourceNamedFluentArguments();
return new FluentArguments();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import net.quickwrite.fluent4j.util.StringSlice;
import net.quickwrite.fluent4j.util.StringSliceUtil;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.args.ResourceFluentArguments;
import net.quickwrite.fluent4j.util.args.FunctionFluentArgs;
import net.quickwrite.fluent4j.util.args.FunctionFluentArguments;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
Expand All @@ -30,16 +31,16 @@ public FluentFunction(final String functionName, final StringSlice content) {
throw new FluentParseException("The callee has to be an upper-case identifier or a term");
}

this.arguments = (content == null) ? FluentArgs.EMPTY_ARGS : this.getArguments(content);
this.arguments = (content == null) ? FunctionFluentArgs.EMPTY_ARGS : this.getArguments(content);
}

private FluentArgs getArguments(final StringSlice content) {
StringSliceUtil.skipWhitespaceAndNL(content);
if (content.isBigger()) {
return FluentArgs.EMPTY_ARGS;
return FunctionFluentArgs.EMPTY_ARGS;
}

final FluentArgs arguments = this.getFluentArgumentInstance();
final FunctionFluentArgs arguments = (FunctionFluentArgs) this.getFluentArgumentInstance();

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

protected FluentArgs getFluentArgumentInstance() {
return new ResourceFluentArguments();
return new FunctionFluentArguments();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.quickwrite.fluent4j.ast.placeable.StringLiteral;
import net.quickwrite.fluent4j.functions.NumberFunction;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.args.ResourceFluentArguments;
import net.quickwrite.fluent4j.util.args.FluentArguments;

/**
* The builder class for the {@link FluentArgs} class.
Expand Down Expand Up @@ -45,7 +45,7 @@ public class FluentArgsBuilder extends AbstractBuilder<FluentArgs> {
* </ol>
*/
public FluentArgsBuilder() {
super(new ResourceFluentArguments());
super(new FluentArguments());
}

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

return this;
Expand All @@ -68,8 +68,8 @@ public FluentArgsBuilder setNamed(final String key, final FluentElement argument
* @param argument The argument itself with all of it's data
* @return The FluentArgsBuilder object itself
*/
public FluentArgsBuilder setNamed(final String key, final String argument) {
return this.setNamed(key, new StringLiteral(argument));
public FluentArgsBuilder set(final String key, final String argument) {
return this.set(key, new StringLiteral(argument));
}

/**
Expand All @@ -79,48 +79,19 @@ public FluentArgsBuilder setNamed(final String key, final String argument) {
* @param argument The argument itself with all of it's data
* @return The FluentArgsBuilder object itself
*/
public FluentArgsBuilder setNamed(final String key, final Number argument) {
return this.setNamed(key, new NumberLiteral(argument));
public FluentArgsBuilder set(final String key, final Number argument) {
return this.set(key, new NumberLiteral(argument));
}

/**
* Adds a positional argument to the argument list.
*
* <p>
* The argument will always be added at the end and cannot be rearranged.
*
* @param argument The argument itself with all of it's data
* @return The FluentArgsBuilder object itself
*/
public FluentArgsBuilder addPositional(final FluentElement argument) {
this.element.addPositional(argument);

return this;
}

/**
* Adds a positional {@link String} argument to the argument list.
*
* <p>
* The argument will always be added at the end and cannot be rearranged.
*
* @param argument The argument itself with all of it's data
* @return The FluentArgsBuilder object itself
*/
public FluentArgsBuilder addPositional(final String argument) {
return this.addPositional(new StringLiteral(argument));
}

/**
* Adds a positional {@link Number} argument to the argument list.
*
* <p>
* The argument will always be added at the end and cannot be rearranged.
* Adds a named {@link Boolean} to the {@link FluentArgs} that can be
* accessed.
*
* @param key The key that is used to access this argument
* @param argument The argument itself with all of it's data
* @return The FluentArgsBuilder object itself
*/
public FluentArgsBuilder addPositional(final Number argument) {
return this.addPositional(new NumberLiteral(argument));
public FluentArgsBuilder set(final String key, final Boolean argument) {
return this.set(key, argument.toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.quickwrite.fluent4j.functions;

import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.args.FunctionFluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.ast.placeable.base.FluentPlaceable;
import net.quickwrite.fluent4j.ast.placeable.NumberLiteral;
Expand Down Expand Up @@ -69,5 +70,5 @@ public String getIdentifier() {
* @param arguments The arguments the function gets
* @return The result
*/
public abstract FluentPlaceable getResult(final DirectFluentBundle bundle, final FluentArgs arguments);
public abstract FluentPlaceable getResult(final DirectFluentBundle bundle, final FunctionFluentArgs arguments);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.quickwrite.fluent4j.ast.FluentElement;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.args.FunctionFluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.ast.placeable.NumberLiteral;
import net.quickwrite.fluent4j.ast.placeable.base.FluentPlaceable;
Expand Down Expand Up @@ -73,7 +74,7 @@ public NumberFunction() {
* @return The number as a {@link CustomNumberLiteral} with the parameters
*/
@Override
public FluentPlaceable getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
public FluentPlaceable getResult(final DirectFluentBundle bundle, final FunctionFluentArgs arguments) {
final FluentElement number = arguments.getPositional(0);

CustomNumberLiteral numberLiteral;
Expand Down
26 changes: 1 addition & 25 deletions src/main/java/net/quickwrite/fluent4j/util/args/FluentArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public interface FluentArgs {
* so that it does not need to be created multiple
* times and can be easily accessed.
*/
FluentArgs EMPTY_ARGS = new ResourceFluentArguments();
FluentArgs EMPTY_ARGS = new FluentArguments();

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

/**
* Returns the {@link FluentElement} with the given
* position in the positional arguments.
*
* @param index The position the argument has
* @return The argument itself
*/
FluentElement getPositional(final int index);

/**
* Returns the amount of positional
* parameters the object has.
*
* @return The amount of positional parameters
*/
int getPositionalSize();

/**
* Adds a new named argument to the named arguments.
*
Expand Down Expand Up @@ -76,11 +59,4 @@ public interface FluentArgs {
* @return if no argument exist
*/
boolean isEmpty();

/**
* Adds a new positional argument at the end.
*
* @param argument The argument itself
*/
void addPositional(final FluentElement argument);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
* A storage for the named arguments without the
* positional arguments.
*/
public class ResourceNamedFluentArguments implements FluentArgs {
public class FluentArguments implements FluentArgs {
private final Map<String, FluentElement> namedArguments;

/**
* Creates a new argument container with the given
* arguments.
*/
public ResourceNamedFluentArguments() {
public FluentArguments() {
this(new HashMap<>());
}

Expand All @@ -29,7 +29,7 @@ public ResourceNamedFluentArguments() {
*
* @param namedArguments The named arguments
*/
public ResourceNamedFluentArguments(final Map<String, FluentElement> namedArguments) {
public FluentArguments(final Map<String, FluentElement> namedArguments) {
this.namedArguments = namedArguments;
}

Expand All @@ -44,16 +44,6 @@ public void sanitize(final DirectFluentBundle bundle, final FluentArgs arguments
}
}

@Override
public FluentElement getPositional(final int index) {
return null;
}

@Override
public int getPositionalSize() {
return 0;
}

@Override
public void setNamed(final String key, final FluentElement argument) {
this.namedArguments.put(key, argument);
Expand All @@ -74,11 +64,6 @@ public boolean isEmpty() {
return this.namedArguments.isEmpty();
}

@Override
public void addPositional(final FluentElement argument) {

}

@Override
public String toString() {
return "ResourceNamedFluentArguments: {\n" +
Expand Down
Loading