Skip to content

Instantly share code, notes, and snippets.

@ababup1192
Last active September 26, 2018 23:49
Show Gist options
  • Save ababup1192/aae8a692db1d456208c427204fbbc552 to your computer and use it in GitHub Desktop.
Save ababup1192/aae8a692db1d456208c427204fbbc552 to your computer and use it in GitHub Desktop.
class Main {
public static void main(String... args) {
assert new QuestionList().toJson().equals("[]");
}
}
class QuestionList {
public QuestionList() {
}
public String toJson() {
return "";
}
}
class Main {
public static void main(String... args) {
assert new QuestionList().toJson().equals("[]");
}
}
class QuestionList {
public QuestionList() {
}
public String toJson() {
return "[]";
}
}
import java.util.Arrays;
import java.util.List;
class Main {
public static void main(String... args) {
assert new QuestionList().toJson().equals("[]");
assert (new QuestionList(new Question("body", Arrays.asList())).toJson()
.equals("[{\"body\": \"body\", \"answers\": []}]"));
}
}
class QuestionList {
private Question question;
public QuestionList() {
}
public QuestionList(Question question) {
this.question = question;
}
public String toJson() {
return "[]";
}
}
class Question {
public Question(String body, List<String> answers) {
}
}
import java.util.Arrays;
import java.util.List;
class Main {
public static void main(String... args) {
assert new QuestionList().toJson().equals("[]");
assert (new QuestionList(new Question("body", Arrays.asList())).toJson()
.equals("[{\"body\": \"body\", \"answers\": []}]"));
}
}
class QuestionList {
private Question question;
public QuestionList() {
}
public QuestionList(Question question) {
this.question = question;
}
public String toJson() {
return question == null ? "[]" : "[{\"body\": \"body\", \"answers\": []}]";
}
}
class Question {
public Question(String body, List<String> answers) {
}
}
import java.util.Arrays;
import java.util.List;
class Main {
public static void main(String... args) {
assert new QuestionList().toJson().equals("[]");
assert (new QuestionList(new Question("body", Arrays.asList())).toJson()
.equals("[{\"body\": \"body\", \"answers\": []}]"));
assert (new QuestionList(new Question("body2", Arrays.asList())).toJson()
.equals("[{\"body\": \"body2\", \"answers\": []}]"));
}
}
class QuestionList {
private Question question;
public QuestionList() {
}
public QuestionList(Question question) {
this.question = question;
}
public String toJson() {
return question == null ? "[]" : "[{\"body\": \"body\", \"answers\": []}]";
}
}
class Question {
public Question(String body, List<String> answers) {
}
}
import java.util.Arrays;
import java.util.List;
class Main {
public static void main(String... args) {
assert new QuestionList().toJson().equals("[]");
assert (new QuestionList(new Question("body", Arrays.asList())).toJson()
.equals("[{\"body\": \"body\", \"answers\": []}]"));
assert (new QuestionList(new Question("body2", Arrays.asList())).toJson()
.equals("[{\"body\": \"body2\", \"answers\": []}]"));
}
}
class QuestionList {
private Question question;
public QuestionList() {
}
public QuestionList(Question question) {
this.question = question;
}
public String toJson() {
return question == null ? "[]" : "[{\"body\": \"" + question.getBody() + "\", \"answers\": []}]";
}
}
class Question {
private String body;
public Question(String body, List<String> answers) {
this.body = body;
}
public String getBody() {
return body;
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
class Main {
public static void main(String... args) {
assert new QuestionList().toJson().equals("[]");
assert (new QuestionList(new Question("body", Arrays.asList())).toJson()
.equals("[{\"body\": \"body\", \"answers\": []}]"));
assert (new QuestionList(new Question("body2", Arrays.asList())).toJson()
.equals("[{\"body\": \"body2\", \"answers\": []}]"));
}
}
class QuestionList {
private Optional<Question> questionOpt;
public QuestionList() {
questionOpt = Optional.empty();
}
public QuestionList(Question question) {
this.questionOpt = Optional.ofNullable(question);
}
public String toJson() {
return questionOpt.map(question -> "[{\"body\": \"" + question.getBody() + "\", \"answers\": []}]")
.orElseGet(() -> "[]");
}
}
class Question {
private String body;
public Question(String body, List<String> answers) {
this.body = body;
}
public String getBody() {
return body;
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
class Main {
public static void main(String... args) {
assert new QuestionList().toJson().equals("[]");
assert (new QuestionList(new Question("body", Arrays.asList())).toJson()
.equals("[{\"body\": \"body\", \"answers\": []}]"));
assert (new QuestionList(new Question("body2", Arrays.asList())).toJson()
.equals("[{\"body\": \"body2\", \"answers\": []}]"));
assert (new QuestionList(new Question("body", Arrays.asList("answer1"))).toJson()
.equals("[{\"body\": \"body\", \"answers\": [\"answer1\"]}]"));
}
}
class QuestionList {
private Optional<Question> questionOpt;
public QuestionList() {
questionOpt = Optional.empty();
}
public QuestionList(Question question) {
this.questionOpt = Optional.ofNullable(question);
}
public String toJson() {
return questionOpt.map(question -> "[{\"body\": \"" + question.getBody() + "\", \"answers\": []}]")
.orElseGet(() -> "[]");
}
}
class Question {
private String body;
public Question(String body, List<String> answers) {
this.body = body;
}
public String getBody() {
return body;
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
class Main {
public static void main(String... args) {
assert new QuestionList().toJson().equals("[]");
assert (new QuestionList(new Question("body", Arrays.asList())).toJson()
.equals("[{\"body\": \"body\", \"answers\": []}]"));
assert (new QuestionList(new Question("body2", Arrays.asList())).toJson()
.equals("[{\"body\": \"body2\", \"answers\": []}]"));
assert (new QuestionList(new Question("body", Arrays.asList("answer1", "answer2"))).toJson()
.equals("[{\"body\": \"body\", \"answers\": [\"answer1\", \"answer2\"]}]"));
}
}
class QuestionList {
private Optional<Question> questionOpt;
public QuestionList() {
questionOpt = Optional.empty();
}
public QuestionList(Question question) {
this.questionOpt = Optional.ofNullable(question);
}
public String toJson() {
return questionOpt.map(question -> "[{\"body\": \"" + question.getBody() + "\", \"answers\": ["
+ question.getAnswers().stream().map(answer -> "\"" + answer + "\"").collect(Collectors.joining(", ")) + "]}]")
.orElseGet(() -> "[]");
}
}
class Question {
private String body;
private List<String> answers;
public Question(String body, List<String> answers) {
this.body = body;
this.answers = answers;
}
public String getBody() {
return body;
}
public List<String> getAnswers() {
return answers;
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
class Main {
public static void main(String... args) {
assert new QuestionList().toJson().equals("[]");
assert (new QuestionList(new Question("body", Arrays.asList())).toJson()
.equals("[{\"body\": \"body\", \"answers\": []}]"));
assert (new QuestionList(new Question("body2", Arrays.asList())).toJson()
.equals("[{\"body\": \"body2\", \"answers\": []}]"));
assert (new QuestionList(new Question("body", Arrays.asList("answer1", "answer2"))).toJson()
.equals("[{\"body\": \"body\", \"answers\": [\"answer1\", \"answer2\"]}]"));
}
}
class QuestionList {
private Optional<Question> questionOpt;
public QuestionList() {
questionOpt = Optional.empty();
}
public QuestionList(Question question) {
this.questionOpt = Optional.ofNullable(question);
}
public String toJson() {
return questionOpt
.map(question -> "[{\"body\": \"" + question.getBody() + "\", " + createAnswersField(question) + "}]")
.orElseGet(() -> "[]");
}
private String createAnswersField(Question question) {
return "\"answers\": ["
+ question.getAnswers().stream().map(answer -> "\"" + answer + "\"").collect(Collectors.joining(", ")) + "]";
}
}
class Question {
private String body;
private List<String> answers;
public Question(String body, List<String> answers) {
this.body = body;
this.answers = answers;
}
public String getBody() {
return body;
}
public List<String> getAnswers() {
return answers;
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
class Main {
public static void main(String... args) {
assert new QuestionList().toJson().equals("[]");
assert (new QuestionList(new Question("body", Arrays.asList())).toJson()
.equals("[{\"body\": \"body\", \"answers\": []}]"));
assert (new QuestionList(new Question("body2", Arrays.asList())).toJson()
.equals("[{\"body\": \"body2\", \"answers\": []}]"));
assert (new QuestionList(new Question("body", Arrays.asList("answer1", "answer2"))).toJson()
.equals("[{\"body\": \"body\", \"answers\": [\"answer1\", \"answer2\"]}]"));
}
}
class QuestionList {
private Optional<Question> questionOpt;
public QuestionList() {
questionOpt = Optional.empty();
}
public QuestionList(Question question) {
this.questionOpt = Optional.ofNullable(question);
}
public String toJson() {
return questionOpt.map(question -> "[" + question.toJson() + "]").orElseGet(() -> "[]");
}
}
class Question {
private String body;
private List<String> answers;
public Question(String body, List<String> answers) {
this.body = body;
this.answers = answers;
}
public String getBody() {
return body;
}
public List<String> getAnswers() {
return answers;
}
public String toJson() {
return "{" + createBodyField() + ", " + createAnswersField() + "}";
}
private String createBodyField() {
return "\"body\": \"" + body + "\"";
}
private String createAnswersField() {
return "\"answers\": [" + answers.stream().map(answer -> "\"" + answer + "\"").collect(Collectors.joining(", "))
+ "]";
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
class Main {
public static void main(String... args) {
assert new QuestionList().toJson().equals("[]");
assert (new QuestionList(new Question("body", Arrays.asList())).toJson()
.equals("[{\"body\": \"body\", \"answers\": []}]"));
assert (new QuestionList(new Question("body2", Arrays.asList())).toJson()
.equals("[{\"body\": \"body2\", \"answers\": []}]"));
assert (new QuestionList(new Question("body", Arrays.asList("answer1", "answer2"))).toJson()
.equals("[{\"body\": \"body\", \"answers\": [\"answer1\", \"answer2\"]}]"));
assert (new QuestionList(new Question("body", Arrays.asList("answer1", "answer2")),
new Question("body2", Arrays.asList("answer3", "answer4"))).toJson().equals(
"[{\"body\": \"body\", \"answers\": [\"answer1\", \"answer2\"]}, {\"body\": \"body2\", \"answers\": [\"answer3\", \"answer4\"]}]"));
}
}
class QuestionList {
private List<Question> questions;
public QuestionList() {
questions = Arrays.asList();
}
public QuestionList(Question... questions) {
this.questions = Arrays.asList(questions);
}
public String toJson() {
return "[" + questions.stream().map(Question::toJson).collect(Collectors.joining(", ")) + "]";
}
}
class Question {
private String body;
private List<String> answers;
public Question(String body, List<String> answers) {
this.body = body;
this.answers = answers;
}
public String toJson() {
return "{" + createBodyField() + ", " + createAnswersField() + "}";
}
private String createBodyField() {
return "\"body\": \"" + body + "\"";
}
private String createAnswersField() {
return "\"answers\": [" + answers.stream().map(answer -> "\"" + answer + "\"").collect(Collectors.joining(", "))
+ "]";
}
}
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
class Main {
public static void main(String... args) {
QuestionRepository repo1 = new QuestionRepository() {
@Override
public QuestionList findAll() {
return new QuestionList();
};
};
QuestionRepository repo2 = new QuestionRepository() {
@Override
public QuestionList findAll() {
return new QuestionList(new Question("body", Arrays.asList()));
};
};
QuestionRepository repo3 = new QuestionRepository() {
@Override
public QuestionList findAll() {
return new QuestionList(new Question("body2", Arrays.asList()));
};
};
QuestionRepository repo4 = new QuestionRepository() {
@Override
public QuestionList findAll() {
return new QuestionList(new Question("body", Arrays.asList("answer1", "answer2")));
};
};
QuestionRepository repo5 = new QuestionRepository() {
@Override
public QuestionList findAll() {
return new QuestionList(new Question("body", Arrays.asList("answer1", "answer2")),
new Question("body2", Arrays.asList("answer3", "answer4")));
};
};
assert repo1.findAll().toJson().equals("[]");
assert repo2.findAll().toJson().equals("[{\"body\": \"body\", \"answers\": []}]");
assert repo3.findAll().toJson().equals("[{\"body\": \"body2\", \"answers\": []}]");
assert repo4.findAll().toJson().equals("[{\"body\": \"body\", \"answers\": [\"answer1\", \"answer2\"]}]");
assert repo5.findAll().toJson().equals(
"[{\"body\": \"body\", \"answers\": [\"answer1\", \"answer2\"]}, {\"body\": \"body2\", \"answers\": [\"answer3\", \"answer4\"]}]");
}
}
interface QuestionRepository {
QuestionList findAll();
}
class QuestionList {
private List<Question> questions;
public QuestionList() {
questions = Arrays.asList();
}
public QuestionList(Question... questions) {
this.questions = Arrays.asList(questions);
}
public String toJson() {
return "[" + questions.stream().map(Question::toJson).collect(Collectors.joining(", ")) + "]";
}
}
class Question {
private String body;
private List<String> answers;
public Question(String body, List<String> answers) {
this.body = body;
this.answers = answers;
}
public String toJson() {
return "{" + createBodyField() + ", " + createAnswersField() + "}";
}
private String createBodyField() {
return "\"body\": \"" + body + "\"";
}
private String createAnswersField() {
return "\"answers\": [" + answers.stream().map(answer -> "\"" + answer + "\"").collect(Collectors.joining(", "))
+ "]";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment