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

#141 : Let's use cactoos'es primitives #142

Merged
merged 2 commits into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package eo;

import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -67,7 +66,7 @@ public void run() {
}
)
public static void main(final String[] args) {
final Thread thread = new Thread(new App(Arrays.asList(args)));
final Thread thread = new Thread(new App(new Iterable<>(args)));
thread.start();
try {
thread.join();
Expand Down
13 changes: 10 additions & 3 deletions eo-compiler/src/main/java/org/eolang/compiler/java/JavaClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.Collection;
import org.cactoos.Input;
import org.cactoos.io.BytesAsInput;
import org.cactoos.text.FormattedText;
import org.cactoos.text.JoinedText;
import org.cactoos.text.UncheckedText;
import org.eolang.compiler.syntax.ObjectBody;

/**
Expand Down Expand Up @@ -69,16 +72,20 @@ public JavaClass(final String name, final Collection<String> ifaces,

@Override
public String path() {
return String.format("%s.java", this.name);
return new UncheckedText(
new FormattedText("%s.java", this.name)
).asString();
}

@Override
public Input code() {
return new BytesAsInput(
String.format(
new FormattedText(
"package eo;\n\npublic final class %s implements %s {\n%s\n}",
this.name,
String.join(", ", this.ifaces),
new UncheckedText(
new JoinedText(", ", this.ifaces)
).asString(),
this.body.java(this.name)
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
import org.cactoos.Input;
import org.cactoos.io.BytesAsInput;
import org.cactoos.list.MappedIterable;
import org.cactoos.text.FormattedText;
import org.cactoos.text.JoinedText;
import org.cactoos.text.UncheckedText;
import org.eolang.compiler.syntax.Method;

/**
Expand Down Expand Up @@ -60,22 +63,28 @@ public JavaInterface(final String name, final Collection<Method> methods) {

@Override
public String path() {
return String.format("%s.java", this.name);
return new UncheckedText(
new FormattedText("%s.java", this.name)
).asString();
}

@Override
public Input code() {
return new BytesAsInput(
String.format(
new FormattedText(
"package eo;\n\npublic interface %s {\n %s\n}",
this.name,
String.join(
"\n ",
new MappedIterable<>(
this.methods,
mtd -> String.format("%s;", mtd.java())
new UncheckedText(
new JoinedText(
"\n ",
new MappedIterable<>(
this.methods,
mtd -> new UncheckedText(
new FormattedText("%s;", mtd.java())
).asString()
)
)
)
).asString()
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import java.util.Collection;
import java.util.stream.Collectors;
import org.cactoos.text.FormattedText;
import org.cactoos.text.JoinedText;
import org.cactoos.text.UncheckedText;
import org.eolang.compiler.syntax.AttrCtorInitFormat;
import org.eolang.compiler.syntax.AttrCtorParamFormat;
import org.eolang.compiler.syntax.Attribute;
Expand Down Expand Up @@ -81,31 +84,37 @@ public PrimaryConstructor(
* @return Java code for constructor.
*/
public String code() {
return String.format(
"public %s(%s) {\n %s \n}",
this.name,
String.join(
", ",
this.attributes
.stream()
.map(
attr -> attr.java(
PrimaryConstructor.CTOR_PARAM_FORMAT
)
return new UncheckedText(
new FormattedText(
"public %s(%s) {\n %s \n}",
this.name,
new UncheckedText(
new JoinedText(
", ",
this.attributes
.stream()
.map(
attr -> attr.java(
PrimaryConstructor.CTOR_PARAM_FORMAT
)
)
.collect(Collectors.toList())
)
.collect(Collectors.toList())
),
String.join(
"\n",
this.attributes
.stream()
.map(
attr -> attr.java(
PrimaryConstructor.CTOR_INIT_FORMAT
)
).asString(),
new UncheckedText(
new JoinedText(
"\n",
this.attributes
.stream()
.map(
attr -> attr.java(
PrimaryConstructor.CTOR_INIT_FORMAT
)
)
.collect(Collectors.toList())
)
.collect(Collectors.toList())
).asString()
)
);
).asString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package org.eolang.compiler.syntax;

import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Attribute argument.
*
Expand All @@ -48,6 +51,8 @@ public ArgAttribute(final String name) {

@Override
public String java() {
return String.format("this.%s", this.name);
return new UncheckedText(
new FormattedText("this.%s", this.name)
).asString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package org.eolang.compiler.syntax;

import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Attribute as java constructor initializer.
*
Expand All @@ -34,6 +37,8 @@ public final class AttrCtorInitFormat implements AttributeFormat {

@Override
public String code(final String type, final String name) {
return String.format("this.%s = %s;", name, name);
return new UncheckedText(
new FormattedText("this.%s = %s;", name, name)
).asString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package org.eolang.compiler.syntax;

import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Attribute as java constructor parameter.
*
Expand All @@ -34,6 +37,8 @@ public final class AttrCtorParamFormat implements AttributeFormat {

@Override
public String code(final String type, final String name) {
return String.format("final %s %s", type, name);
return new UncheckedText(
new FormattedText("final %s %s", type, name)
).asString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
*/
package org.eolang.compiler.syntax;

import org.cactoos.text.FormattedText;
import org.cactoos.text.UncheckedText;

/**
* Attribute as java class field.
*
Expand All @@ -34,6 +37,8 @@ public final class AttrFieldFormat implements AttributeFormat {

@Override
public String code(final String type, final String name) {
return String.format("private final %s %s;", type, name);
return new UncheckedText(
new FormattedText("private final %s %s;", type, name)
).asString();
}
}
25 changes: 16 additions & 9 deletions eo-compiler/src/main/java/org/eolang/compiler/syntax/CpObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import java.util.Collection;
import java.util.stream.Collectors;
import org.cactoos.text.FormattedText;
import org.cactoos.text.JoinedText;
import org.cactoos.text.UncheckedText;

/**
* Object copying.
Expand Down Expand Up @@ -62,15 +65,19 @@ public CpObject(final String name, final Collection<Argument> arguments) {
* @return Java code
*/
public String java() {
return String.format(
"new %s(%s)",
this.name,
String.join(
", ",
this.arguments.stream()
.map(Argument::java)
.collect(Collectors.toList())
return new UncheckedText(
new FormattedText(
"new %s(%s)",
this.name,
new UncheckedText(
new JoinedText(
", ",
this.arguments.stream()
.map(Argument::java)
.collect(Collectors.toList())
)
).asString()
)
);
).asString();
}
}
41 changes: 26 additions & 15 deletions eo-compiler/src/main/java/org/eolang/compiler/syntax/Ctor.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.cactoos.text.FormattedText;
import org.cactoos.text.JoinedText;
import org.cactoos.text.UncheckedText;

/**
* Object secondary constructor.
Expand All @@ -51,21 +54,27 @@ public Ctor(
final List<Parameter> parameters,
final Collection<Argument> arguments
) {
this.template = String.format(
"public %%s(%s) {\n this(%s);\n}",
String.join(
", ",
parameters.stream()
.map(Parameter::java)
.collect(Collectors.toList())
),
String.join(
", ",
arguments.stream()
.map(Argument::java)
.collect(Collectors.toList())
this.template = new UncheckedText(
new FormattedText(
"public %%s(%s) {\n this(%s);\n}",
new UncheckedText(
new JoinedText(
", ",
parameters.stream()
.map(Parameter::java)
.collect(Collectors.toList())
)
).asString(),
new UncheckedText(
new JoinedText(
", ",
arguments.stream()
.map(Argument::java)
.collect(Collectors.toList())
)
).asString()
)
);
).asString();
}

/**
Expand All @@ -75,6 +84,8 @@ public Ctor(
* @return Java code
*/
public String java(final String name) {
return String.format(this.template, name);
return new UncheckedText(
new FormattedText(this.template, name)
).asString();
}
}
29 changes: 18 additions & 11 deletions eo-compiler/src/main/java/org/eolang/compiler/syntax/Method.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import java.util.Collection;
import org.cactoos.list.MappedIterable;
import org.cactoos.text.FormattedText;
import org.cactoos.text.JoinedText;
import org.cactoos.text.UncheckedText;

/**
* Method.
Expand Down Expand Up @@ -68,18 +71,22 @@ public Method(final String mtd, final Collection<Parameter> params,
* @return Java code
*/
public String java() {
return String.format(
"%s %s(%s)",
this.type,
this.name,
String.join(
", ",
new MappedIterable<>(
this.parameters,
Parameter::java
)
return new UncheckedText(
new FormattedText(
"%s %s(%s)",
this.type,
this.name,
new UncheckedText(
new JoinedText(
", ",
new MappedIterable<>(
this.parameters,
Parameter::java
)
)
).asString()
)
);
).asString();
}

}