• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ExpediaGroup / bull / #865

09 Jan 2024 03:40PM CUT coverage: 100.0%. Remained the same
#865

Pull #461

Fabio Borriello
Trigger notification
Pull Request #461: Bump org.slf4j:slf4j-api from 2.0.10 to 2.0.11

1198 of 1198 relevant lines covered (100.0%)

1.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/bull-bean-transformer/src/main/java/com/expediagroup/beans/populator/Populator.java
1
/**
2
 * Copyright (C) 2019-2023 Expedia, Inc.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 * http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package com.expediagroup.beans.populator;
17

18
import java.lang.reflect.Field;
19
import java.util.Optional;
20

21
import com.expediagroup.beans.BeanUtils;
22
import com.expediagroup.beans.transformer.BeanTransformer;
23
import com.expediagroup.transformer.utils.ClassUtils;
24
import com.expediagroup.transformer.utils.ReflectionUtils;
25

26
/**
27
 * Populator for collection or map objects.
28
 * @param <O> the type of the object to get populated.
29
 */
30
public abstract class Populator<O> {
31
    /**
32
     * Reflection utils instance {@link ReflectionUtils}.
33
     */
34
    final ReflectionUtils reflectionUtils;
35

36
    /**
37
     * Class reflection utils instance {@link ClassUtils}.
38
     */
39
    final ClassUtils classUtils;
40

41
    /**
42
     * Transformer class instance {@link BeanTransformer} containing the field mapping and transformation functions.
43
     */
44
    private final BeanTransformer transformer;
45

46
    /**
47
     * Default constructor.
48
     * @param beanTransformer the bean transformer containing the field name mapping and transformation functions
49
     */
50
    Populator(final BeanTransformer beanTransformer) {
2✔
51
        transformer = beanTransformer;
2✔
52
        reflectionUtils = new ReflectionUtils();
2✔
53
        classUtils = new ClassUtils();
2✔
54
    }
2✔
55

56
    /**
57
     * Populates the target object with the values into the source object.
58
     * @param field the field to be populated
59
     * @param fieldValue the source object from which extract the values
60
     * @return a populated list of elements
61
     */
62
    protected abstract O getPopulatedObject(Field field, O fieldValue);
63

64
    /**
65
     * Populates the target object with the values into the source object.
66
     * @param <K> the target object type
67
     * @param targetClass the destination object class
68
     * @param fieldName the field to be populated
69
     * @param fieldValue the source object from which extract the values
70
     * @return a populated list of elements
71
     */
72
    public final <K> O getPopulatedObject(final Class<K> targetClass, final String fieldName, final O fieldValue) {
73
        return getPopulatedObject(reflectionUtils.getDeclaredField(fieldName, targetClass), fieldValue);
2✔
74
    }
75

76
    /**
77
     * Wrapper method for {@link BeanUtils} transform method.
78
     * @param sourceObj the source object
79
     * @param targetClass the destination object class
80
     * @param <T> the Source object type
81
     * @param <K> the target object type
82
     * @return a copy of the source object into the destination object
83
     */
84
    public final <T, K> K transform(final T sourceObj, final Class<K> targetClass) {
85
        return transform(sourceObj, targetClass, null);
2✔
86
    }
87

88
    /**
89
     * Wrapper method for BeanUtil transform method.
90
     * @param sourceObj the source object
91
     * @param targetClass the destination object class
92
     * @param nestedGenericClass the nested generic object class. i.e. in case of object like this:
93
     *                           {@code List<List<String>>} the value is: String
94
     * @param <T> the Source object type
95
     * @param <K> the target object type
96
     * @return a copy of the source object into the destination object
97
     */
98
    @SuppressWarnings("unchecked")
99
    final <T, K> K transform(final T sourceObj, final Class<K> targetClass, final Class<?> nestedGenericClass) {
100
        final K res;
101
        if (classUtils.isPrimitiveOrSpecialType(sourceObj.getClass())) {
2✔
102
            res = (K) sourceObj;
2✔
103
        } else {
104
            final Optional<Populator> optPopulator = PopulatorFactory.getPopulator(targetClass, sourceObj.getClass(), transformer);
2✔
105
            res = (K) optPopulator
2✔
106
                    .map(populator -> ((ICollectionPopulator<Object>) populator).getPopulatedObject(targetClass, targetClass, sourceObj, nestedGenericClass))
2✔
107
                    .orElseGet(() -> transformer.transform(sourceObj, targetClass));
2✔
108
        }
109
        return res;
2✔
110
    }
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc