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

ExpediaGroup / bull / #926

03 Apr 2024 02:09PM CUT coverage: 100.0%. Remained the same
#926

Pull #486

Fabio Borriello
Trigger notification
Pull Request #486: Bump org.jacoco:jacoco-maven-plugin from 0.8.11 to 0.8.12

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/MapPopulator.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 static java.util.stream.Collectors.toMap;
19

20
import java.lang.reflect.Field;
21
import java.util.Map;
22

23
import com.expediagroup.beans.transformer.BeanTransformer;
24
import com.expediagroup.transformer.model.ItemType;
25
import com.expediagroup.transformer.model.MapElemType;
26
import com.expediagroup.transformer.model.MapType;
27

28
/**
29
 * Populator for Map types object {@link Map}.
30
 */
31
class MapPopulator extends Populator<Map<?, ?>> {
32

33
    /**
34
     * Default constructor.
35
     * @param beanTransformer the bean transformer containing the field name mapping and transformation functions
36
     */
37
    MapPopulator(final BeanTransformer beanTransformer) {
38
        super(beanTransformer);
2✔
39
    }
2✔
40

41
    /**
42
     * {@inheritDoc}
43
     */
44
    @Override
45
    public Map<?, ?> getPopulatedObject(final Field field, final Map<?, ?> fieldValue) {
46
        final MapType mapGenericType = reflectionUtils.getMapGenericType(field.getGenericType(), field.getDeclaringClass().getName(), field.getName());
2✔
47
        return getPopulatedObject(fieldValue, mapGenericType);
2✔
48
    }
49

50
    /**
51
     * Populates the Map objects.
52
     * @param fieldValue the source Map.
53
     * @param mapType the map key, elem types {@link MapType}
54
     * @return the populated map
55
     */
56
    private Map<?, ?> getPopulatedObject(final Map<?, ?> fieldValue, final MapType mapType) {
57
        final MapElemType keyType = mapType.getKeyType();
2✔
58
        final MapElemType elemType = mapType.getElemType();
2✔
59
        final boolean keyIsPrimitive = isPrimitive(keyType);
2✔
60
        final boolean elemIsPrimitive = isPrimitive(elemType);
2✔
61
        Map<?, ?> populatedObject;
62
        if (keyIsPrimitive && elemIsPrimitive) {
2✔
63
            populatedObject = fieldValue;
2✔
64
        } else {
65
            populatedObject = fieldValue.keySet()
2✔
66
                    .stream()
2✔
67
//                    .parallelStream()
68
                    .collect(toMap(
2✔
69
                        key -> getElemValue(keyType, keyIsPrimitive, key),
2✔
70
                        key -> getElemValue(elemType, elemIsPrimitive, fieldValue.get(key))));
2✔
71
        }
72
        return populatedObject;
2✔
73
    }
74

75
    /**
76
     * Checks if the map element type is primitive or not.
77
     * @param mapElemType the map element to check
78
     * @return true if it's primitive, false otherwise
79
     */
80
    private boolean isPrimitive(final MapElemType mapElemType) {
81
        return mapElemType.getClass().equals(ItemType.class) && classUtils.isPrimitiveOrSpecialType(((ItemType) mapElemType).getObjectClass());
2✔
82
    }
83

84
    /**
85
     * Gets the value of a given key/elem (including complex types).
86
     * @param <T> the object type
87
     * @param mapElemType the map key, elem types {@link MapElemType}
88
     * @param elemIsPrimitiveType true if the elem map is primitive type, false otherwise.
89
     * @param value the elem value
90
     * @return the element value
91
     */
92
    @SuppressWarnings("unchecked")
93
    private <T> T getElemValue(final MapElemType mapElemType, final boolean elemIsPrimitiveType, final T value) {
94
        final T elemValue;
95
        if (elemIsPrimitiveType || classUtils.isPrimitiveOrSpecialType(value.getClass())) {
2✔
96
            elemValue = value;
2✔
97
        } else {
98
            if (mapElemType.getClass().equals(ItemType.class)) {
2✔
99
                elemValue = (T) transform(value, ((ItemType) mapElemType).getObjectClass(), ((ItemType) mapElemType).getGenericClass());
2✔
100
            } else {
101
                elemValue = (T) getPopulatedObject((Map) value, (MapType) mapElemType);
2✔
102
            }
103
        }
104
        return elemValue;
2✔
105
    }
106
}
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