| Conditions | 8 |
| Total Lines | 95 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | package easytests.api.v1.mappers; |
||
| 22 | public QuestionsMapper() { |
||
| 23 | super(); |
||
| 24 | this.createTypeMap(QuestionModel.class, Question.class) |
||
| 25 | .addMappings(mapper -> { |
||
| 26 | mapper.<Integer>map(questionModel -> questionModel.getQuestionType().getId(), |
||
| 27 | (question, id) -> question.setType(id)); |
||
| 28 | |||
| 29 | mapper.<Integer>map(questionModel -> questionModel.getTopic().getId(), |
||
| 30 | (question, id) -> question.getTopic().setId(id)); |
||
| 31 | |||
| 32 | mapper.<List<AdminAnswer>>map(questionModel -> questionModel.getAnswers(), |
||
| 33 | (question, list) -> question.setAnswers(list)); |
||
| 34 | } |
||
| 35 | ); |
||
| 36 | this.createTypeMap(AnswerModel.class, AdminAnswer.class) |
||
| 37 | .addMappings(mapper -> { |
||
| 38 | mapper.<String>map(answerModel -> answerModel.getTxt(), |
||
| 39 | (answer, text) -> answer.setText(text)); |
||
| 40 | |||
| 41 | mapper.<Boolean>map(answerModel -> answerModel.getRight(), |
||
| 42 | (answer, right) -> answer.setIsRight(right)); |
||
| 43 | } |
||
| 44 | ); |
||
| 45 | |||
| 46 | Converter<Identity, TopicModel> convertIdentityToTopicModel |
||
| 47 | = new Converter<Identity, TopicModel>() { |
||
| 48 | public TopicModel convert(MappingContext<Identity, TopicModel> context) { |
||
| 49 | final TopicModel topicModel = new TopicModel(); |
||
| 50 | topicModel.setId(context.getSource().getId()); |
||
| 51 | |||
| 52 | return topicModel; |
||
| 53 | } |
||
| 54 | }; |
||
| 55 | |||
| 56 | Converter<AdminAnswer, AnswerModel> convertAdminAnswerToAnswerModel |
||
|
|
|||
| 57 | = new Converter<AdminAnswer, AnswerModel>() { |
||
| 58 | public AnswerModel convert(MappingContext<AdminAnswer, AnswerModel> context) { |
||
| 59 | final AnswerModel answerModel = new AnswerModel(); |
||
| 60 | answerModel.setId(context.getSource().getId()); |
||
| 61 | |||
| 62 | return answerModel; |
||
| 63 | } |
||
| 64 | }; |
||
| 65 | |||
| 66 | Converter<AdminAnswer, AnswerModel> convertIdentityToAnswerModel |
||
| 67 | = new Converter<AdminAnswer, AnswerModel>() { |
||
| 68 | public AnswerModel convert(MappingContext<AdminAnswer, AnswerModel> context) { |
||
| 69 | final AnswerModel answerModel = new AnswerModel(); |
||
| 70 | answerModel.setId(context.getSource().getId()); |
||
| 71 | |||
| 72 | return answerModel; |
||
| 73 | } |
||
| 74 | }; |
||
| 75 | |||
| 76 | Converter<List<AdminAnswer>, List<AnswerModel>> convertAdminAnswerListToAnswerModelList |
||
| 77 | = new Converter<List<AdminAnswer>, List<AnswerModel>>() { |
||
| 78 | public List<AnswerModel> convert(MappingContext<List<AdminAnswer>, List<AnswerModel>> context) { |
||
| 79 | final List<AnswerModel> answerModels = new ArrayList<>(); |
||
| 80 | for (int i = 0; i < context.getSource().size(); ++i) { |
||
| 81 | final AdminAnswer adminAnswer = context.getSource().get(i); |
||
| 82 | |||
| 83 | final AnswerModel answerModel = new AnswerModel(); |
||
| 84 | |||
| 85 | answerModel.setId(adminAnswer.getId()); |
||
| 86 | answerModel.setRight(adminAnswer.getIsRight()); |
||
| 87 | answerModel.setTxt(adminAnswer.getText()); |
||
| 88 | answerModel.setSerialNumber(adminAnswer.getNumber()); |
||
| 89 | |||
| 90 | answerModels.add(answerModel); |
||
| 91 | } |
||
| 92 | return answerModels; |
||
| 93 | } |
||
| 94 | }; |
||
| 95 | |||
| 96 | Converter<Integer, QuestionTypeModel> convertIntegerToQuestionTypeModel |
||
| 97 | = new Converter<Integer, QuestionTypeModel>() { |
||
| 98 | public QuestionTypeModel convert(MappingContext<Integer, QuestionTypeModel> context) { |
||
| 99 | final QuestionTypeModel questionTypeModel = new QuestionTypeModel(); |
||
| 100 | questionTypeModel.setId(context.getSource()); |
||
| 101 | |||
| 102 | return questionTypeModel; |
||
| 103 | } |
||
| 104 | }; |
||
| 105 | |||
| 106 | final PropertyMap<Question, QuestionModel> mymap = new PropertyMap<Question, QuestionModel>() { |
||
| 107 | protected void configure() { |
||
| 108 | map(source.getId()).setId(null); |
||
| 109 | map(source.getText()).setText(null); |
||
| 110 | using(convertIntegerToQuestionTypeModel).map(source.getType()).setQuestionType(null); |
||
| 111 | using(convertIdentityToTopicModel).map(source.getTopic()).setTopic(null); |
||
| 112 | using(convertAdminAnswerListToAnswerModelList).map(source.getAnswers()).setAnswers(null); |
||
| 113 | } |
||
| 114 | }; |
||
| 115 | |||
| 116 | this.addMappings(mymap); |
||
| 117 | |||
| 120 |