Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
QuickWrite committed Oct 16, 2022
2 parents ad2942b + a439d64 commit fdca48a
Show file tree
Hide file tree
Showing 32 changed files with 392 additions and 148 deletions.
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.2.1-alpha</version>
<version>0.2.2-alpha</version>
<build>
<plugins>
<plugin>
Expand Down
24 changes: 4 additions & 20 deletions src/main/java/net/quickwrite/fluent4j/ast/FluentBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import net.quickwrite.fluent4j.exception.FluentParseException;
import net.quickwrite.fluent4j.util.StringSlice;
import net.quickwrite.fluent4j.util.StringSliceUtil;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.util.bundle.args.AccessorBundle;

import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -88,32 +88,16 @@ public String getIdentifier() {
return this.identifier;
}

private StringSlice getVariantIdentifier(final StringSlice content) {
char character = content.getChar();
final int start = content.getPosition();

while (character != ' '
&& character != '\n'
&& character != ']'
&& character != '\0'
) {
content.increment();
character = content.getChar();
}

return content.substring(start, content.getPosition());
}

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

final StringBuilder builder = new StringBuilder();

for (final FluentElement element : this.fluentElements) {
builder.append(element.getResult(bundle, arguments));
builder.append(element.getResult(bundle));
}

return builder;
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/net/quickwrite/fluent4j/ast/FluentElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import net.quickwrite.fluent4j.ast.placeable.NumberLiteral;
import net.quickwrite.fluent4j.ast.placeable.SelectExpression;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.util.bundle.args.AccessorBundle;

/**
* The base interface for the AST.
Expand Down Expand Up @@ -41,9 +41,8 @@ public interface FluentElement {
* the different arguments and the base bundle that
* it has been called from.
*
* @param bundle The base bundle
* @param arguments The arguments that are being passed on the scope
* @param bundle The bundle that is getting passed down
* @return The {@link CharSequence} value of the argument with the parameters
*/
CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments);
CharSequence getResult(final AccessorBundle bundle);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package net.quickwrite.fluent4j.ast;

import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.util.StringSlice;
import net.quickwrite.fluent4j.util.bundle.args.AccessorBundle;

/**
* The TextElement is just storing a text that does
Expand Down Expand Up @@ -82,7 +82,7 @@ public String stringValue() {
}

@Override
public CharSequence getResult(DirectFluentBundle bundle, FluentArgs arguments) {
public CharSequence getResult(AccessorBundle bundle) {
return this.text;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/net/quickwrite/fluent4j/ast/FluentVariant.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package net.quickwrite.fluent4j.ast;

import net.quickwrite.fluent4j.exception.FluentParseException;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.ast.placeable.NumberLiteral;
import net.quickwrite.fluent4j.ast.placeable.StringLiteral;
import net.quickwrite.fluent4j.ast.placeable.base.FluentPlaceable;
import net.quickwrite.fluent4j.util.bundle.args.AccessorBundle;

/**
* A variant stores a single variant of a
Expand Down Expand Up @@ -53,8 +53,8 @@ public String stringValue() {
return identifier.stringValue();
}

public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
return this.content.getResult(bundle, arguments);
public CharSequence getResult(AccessorBundle bundle) {
return this.content.getResult(bundle);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import net.quickwrite.fluent4j.util.StringSlice;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.util.bundle.args.AccessorBundle;

import java.util.List;

Expand Down Expand Up @@ -41,8 +42,8 @@ public String stringValue() {
}

@Override
public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
final FluentMessage fluentMessage = this.getMessage(bundle, reference.stringValue());
public CharSequence getResult(final AccessorBundle bundle) {
final FluentMessage fluentMessage = this.getMessage(bundle.getBundle(), reference.stringValue());
if (fluentMessage == null) {
return getErrorString();
}
Expand All @@ -53,12 +54,12 @@ public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs
return getErrorString();
}

return attribute.getResult(bundle, getArguments(arguments));
return attribute.getResult(bundle);
}

@Override
public FluentElement getArgumentResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
final FluentMessage fluentMessage = this.getMessage(bundle, reference.stringValue());
public FluentElement getArgumentResult(final AccessorBundle bundle) {
final FluentMessage fluentMessage = this.getMessage(bundle.getBundle(), reference.stringValue());
if (fluentMessage == null) {
return this;
}
Expand All @@ -69,6 +70,12 @@ public FluentElement getArgumentResult(final DirectFluentBundle bundle, final Fl
return this;
}

if (bundle.getAccessedStorage().alreadyAccessed(attribute)) {
return this;
}

bundle.getAccessedStorage().addElement(attribute);

final List<FluentElement> elementList = attribute.getElements();

if (elementList.size() != 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import net.quickwrite.fluent4j.ast.FluentElement;
import net.quickwrite.fluent4j.ast.placeable.base.FluentFunction;
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;
import net.quickwrite.fluent4j.util.bundle.args.AccessorBundle;

/**
* Functions provide additional functionality available to the localizers.
Expand All @@ -21,12 +20,13 @@ public FunctionReference(final String functionName, final FluentArgs arguments)
}

@Override
public FluentElement getArgumentResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
public FluentElement getArgumentResult(final AccessorBundle bundle) {
try {
return bundle
.getBundle()
.getFunction(this.functionName)
.orElseThrow()
.getResult(bundle, (FunctionFluentArgs) this.getArguments(bundle, arguments));
.getResult(bundle, (FunctionFluentArgs) this.getArguments(bundle));
} catch (final Exception exception) {
return new StringLiteral("{" + functionName + "()}");
}
Expand Down Expand Up @@ -69,13 +69,12 @@ protected boolean check(String string) {
* This means that the {@code NUMBER}-function gets no arguments
* this function will return <code>{NUMBER()}</code>.
*
* @param bundle The base bundle
* @param arguments The arguments that are being passed on the scope
* @return The result of the function with the specific parameters
*
* @param bundle@return The result of the function with the specific parameters
*/
@Override
public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
return this.getArgumentResult(bundle, arguments).getResult(bundle, arguments);
public CharSequence getResult(AccessorBundle bundle) {
return this.getArgumentResult(bundle).getResult(bundle);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import net.quickwrite.fluent4j.ast.FluentElement;
import net.quickwrite.fluent4j.ast.placeable.base.FluentPlaceable;
import net.quickwrite.fluent4j.util.StringSlice;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.util.bundle.args.AccessorBundle;


/**
Expand Down Expand Up @@ -36,9 +36,10 @@ public String stringValue() {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import net.quickwrite.fluent4j.ast.placeable.base.FluentPlaceable;
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.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.util.bundle.args.AccessorBundle;

import java.math.BigDecimal;

Expand Down Expand Up @@ -51,8 +51,8 @@ public static NumberLiteral getNumberLiteral(final String value) {
}

@Override
public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
return NumberFormat.getInstance(bundle.getLocale()).format(number);
public CharSequence getResult(final AccessorBundle bundle) {
return NumberFormat.getInstance(bundle.getBundle().getLocale()).format(number);
}

private static BigDecimal convertToBigDecimal(final Number number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import net.quickwrite.fluent4j.ast.FluentVariant;
import net.quickwrite.fluent4j.ast.placeable.base.FluentArgumentResult;
import net.quickwrite.fluent4j.ast.placeable.base.FluentPlaceable;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.util.bundle.args.AccessorBundle;

import java.util.List;

Expand Down Expand Up @@ -50,17 +50,17 @@ public String stringValue() {
}

@Override
public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
public CharSequence getResult(final AccessorBundle bundle) {
final FluentElement argument = (identifier instanceof FluentArgumentResult) ?
((FluentArgumentResult) identifier).getArgumentResult(bundle, arguments) : identifier;
((FluentArgumentResult) identifier).getArgumentResult(bundle) : identifier;

for (final FluentVariant variant : variants) {
if (argument.matches(bundle, variant.getIdentifier())) {
return variant.getResult(bundle, arguments);
if (argument.matches(bundle.getBundle(), variant.getIdentifier())) {
return variant.getResult(bundle);
}
}

return defaultVariant.getResult(bundle, arguments);
return defaultVariant.getResult(bundle);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import net.quickwrite.fluent4j.ast.placeable.base.FluentSelectable;
import net.quickwrite.fluent4j.ast.placeable.base.FluentUnicodeTranslator;
import net.quickwrite.fluent4j.util.StringSlice;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.util.bundle.args.AccessorBundle;
import org.apache.commons.text.translate.AggregateTranslator;
import org.apache.commons.text.translate.CharSequenceTranslator;
import org.apache.commons.text.translate.LookupTranslator;
Expand Down Expand Up @@ -55,7 +55,7 @@ public StringLiteral(final String content) {
}

@Override
public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
public CharSequence getResult(AccessorBundle bundle) {
return this.literal;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
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.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.util.bundle.args.AccessorBundle;
import net.quickwrite.fluent4j.util.bundle.args.AccessorElementsBundle;

/**
* Terms are similar to regular messages but they can
Expand Down Expand Up @@ -39,24 +40,19 @@ protected boolean check(final String string) {
}

@Override
public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
return this.getArgumentResult(bundle, arguments).getResult(bundle, this.getArguments(bundle, arguments));
public CharSequence getResult(final AccessorBundle bundle) {
return bundle.getBundle()
.getTerm(this.functionName, new AccessorElementsBundle(bundle.getBundle(), this.getArguments(bundle), bundle.getAccessedStorage()))
.orElse("{-" + this.functionName + "}");
}

/**
* @param bundle The bundle that this is being called from
* @param arguments The arguments that are passed into this function
* @return
* @return The result of the term that is being called
*/
@Override
public FluentElement getArgumentResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
final FluentTerm term = bundle.getTerm(this.functionName);

if (term == null) {
return new StringLiteral("{-" + this.functionName + "}");
}

return term;
public FluentElement getArgumentResult(final AccessorBundle bundle) {
return new StringLiteral(getResult(bundle).toString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.quickwrite.fluent4j.util.StringSlice;
import net.quickwrite.fluent4j.util.args.FluentArgs;
import net.quickwrite.fluent4j.util.bundle.DirectFluentBundle;
import net.quickwrite.fluent4j.util.bundle.args.AccessorBundle;

/**
* Variables are pieces of data received from the app.
Expand Down Expand Up @@ -51,25 +52,26 @@ public String stringValue() {
}

@Override
public FluentElement getArgumentResult(DirectFluentBundle bundle, final FluentArgs arguments) {
public FluentElement getArgumentResult(final AccessorBundle bundle) {
final FluentArgs arguments = bundle.getArguments();
final FluentElement argument = arguments.getNamed(content);

if (argument == null) {
return new StringLiteral("{$" + content + "}");
}

return arguments.getNamed(content);
return argument;
}

@Override
public CharSequence getResult(final DirectFluentBundle bundle, final FluentArgs arguments) {
final FluentElement argument = arguments.getNamed(content);
public CharSequence getResult(final AccessorBundle bundle) {
final FluentElement argument = bundle.getArguments().getNamed(content);

if (argument == null) {
return "{$" + content + "}";
}

return argument.getResult(bundle, arguments);
return argument.getResult(bundle);
}

@Override
Expand Down
Loading

0 comments on commit fdca48a

Please sign in to comment.