| Conditions | 6 |
| Total Lines | 72 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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; |
||
| 25 | public QuestionsMapper() { |
||
| 26 | super(); |
||
| 27 | this.createTypeMap(QuestionModel.class, Question.class) |
||
| 28 | .addMappings(mapper -> { |
||
| 29 | mapper.<Integer>map(questionModel -> |
||
| 30 | questionModel.getQuestionType().getId(), (question, id) -> question.setType(id)); |
||
| 31 | |||
| 32 | mapper.<Integer>map(questionModel -> |
||
| 33 | questionModel.getTopic().getId(), (question, id) -> question.getTopic().setId(id)); |
||
| 34 | |||
| 35 | mapper.<List<AdminAnswer>>map(questionModel -> |
||
| 36 | questionModel.getAnswers(), (question, list) -> question.setAnswers(list)); |
||
| 37 | } |
||
| 38 | ); |
||
| 39 | this.createTypeMap(AnswerModel.class, AdminAnswer.class) |
||
| 40 | .addMappings(mapper -> { |
||
| 41 | mapper.<String>map(answerModel -> |
||
| 42 | answerModel.getTxt(), (answer, text) -> answer.setText(text)); |
||
| 43 | |||
| 44 | mapper.<Boolean>map(answerModel -> |
||
| 45 | answerModel.getRight(), (answer, right) -> answer.setIsRight(right)); |
||
| 46 | } |
||
| 47 | ); |
||
| 48 | |||
| 49 | Converter<Identity, TopicModel> convertIdentityToTopicModel = |
||
| 50 | new Converter<Identity, TopicModel>() { |
||
| 51 | public TopicModel convert(MappingContext<Identity, TopicModel> context) { |
||
| 52 | final TopicModel topicModel = new TopicModel(); |
||
| 53 | topicModel.setId(context.getSource().getId()); |
||
| 54 | |||
| 55 | return topicModel; |
||
| 56 | } |
||
| 57 | }; |
||
| 58 | |||
| 59 | Converter<List<AdminAnswer>, List<AnswerModel>> convertAdminAnswerListToAnswerModelList |
||
| 60 | = new Converter<List<AdminAnswer>, List<AnswerModel>>() { |
||
| 61 | public List<AnswerModel> convert(MappingContext<List<AdminAnswer>, List<AnswerModel>> context) { |
||
| 62 | final List<AnswerModel> answerModels = new ArrayList<>(); |
||
| 63 | for (int i = 0; i < context.getSource().size(); ++i) { |
||
| 64 | final AdminAnswer adminAnswer = context.getSource().get(i); |
||
| 65 | |||
| 66 | final AnswerModel answerModel = new AnswerModel(); |
||
| 67 | |||
| 68 | adminAnswersMapper.map(adminAnswer, answerModel); |
||
| 69 | |||
| 70 | answerModels.add(answerModel); |
||
| 71 | } |
||
| 72 | return answerModels; |
||
| 73 | } |
||
| 74 | }; |
||
| 75 | |||
| 76 | Converter<Integer, QuestionTypeModel> convertIntegerToQuestionTypeModel |
||
| 77 | = new Converter<Integer, QuestionTypeModel>() { |
||
| 78 | public QuestionTypeModel convert(MappingContext<Integer, QuestionTypeModel> context) { |
||
| 79 | final QuestionTypeModel questionTypeModel = new QuestionTypeModel(); |
||
| 80 | questionTypeModel.setId(context.getSource()); |
||
| 81 | |||
| 82 | return questionTypeModel; |
||
| 83 | } |
||
| 84 | }; |
||
| 85 | |||
| 86 | final PropertyMap<Question, QuestionModel> mymap = new PropertyMap<Question, QuestionModel>() { |
||
| 87 | protected void configure() { |
||
| 88 | map(source.getId()).setId(null); |
||
| 89 | map(source.getText()).setText(null); |
||
| 90 | using(convertIntegerToQuestionTypeModel).map(source.getType()).setQuestionType(null); |
||
| 91 | using(convertIdentityToTopicModel).map(source.getTopic()).setTopic(null); |
||
| 92 | using(convertAdminAnswerListToAnswerModelList).map(source.getAnswers()).setAnswers(null); |
||
| 93 | } |
||
| 94 | }; |
||
| 95 | |||
| 96 | this.addMappings(mymap); |
||
| 97 | |||
| 100 |