Skip to content

Package: CollectionFactory

CollectionFactory

nameinstructionbranchcomplexitylinemethod
CollectionFactory()
M: 6 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
createDefaultCollection(CollectionType)
M: 0 C: 20
100%
M: 0 C: 3
100%
M: 0 C: 3
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
createDefaultMap()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
createInstance(Collection)
M: 6 C: 21
78%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 5
83%
M: 0 C: 1
100%
resolveCollectionType(Class)
M: 0 C: 28
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 9
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * JOPA
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jopa.utils;
19:
20: import cz.cvut.kbss.jopa.model.metamodel.CollectionType;
21:
22: import java.util.*;
23:
24: public final class CollectionFactory {
25:
26: private CollectionFactory() {
27: throw new AssertionError();
28: }
29:
30: /**
31: * Creates an instance of a {@link Collection} implementation best matching the specified instance.
32: * <p>
33: * E.g. for any kind of {@link List}, an {@link ArrayList} is returned.
34: *
35: * @param collection Create matching instance for this collection
36: * @return Best matching collection instance
37: */
38: public static Collection<?> createInstance(Collection<?> collection) {
39: Objects.requireNonNull(collection);
40:
41:• if (collection instanceof List) {
42: return new ArrayList<>(collection.size());
43:• } else if (collection instanceof Set) {
44: return new HashSet<>(collection.size());
45: }
46: throw new IllegalArgumentException("Unsupported collection type: " + collection);
47: }
48:
49: /**
50: * Creates default collection for the specified collection type.
51: *
52: * @param collectionType Type of the collection to create
53: * @return Collection implementation instance
54: */
55: public static <T> Collection<T> createDefaultCollection(CollectionType collectionType) {
56:• return switch (collectionType) {
57: case LIST -> new ArrayList<>();
58: case SET, COLLECTION -> new HashSet<>();
59: default -> throw new IllegalArgumentException("Collection type " + collectionType + " is not supported.");
60: };
61: }
62:
63: /**
64: * Creates default instance of {@link Map}.
65: *
66: * @return Default Map implementation instance
67: */
68: public static Map<Object, Object> createDefaultMap() {
69: return new HashMap<>();
70: }
71:
72: /**
73: * Resolves {@link CollectionType} from the specified Java class.
74: *
75: * @param javaType Java type whose collection type to resolve
76: * @return {@code CollectionType} value
77: * @throws IllegalArgumentException When unsupported java type is provided
78: */
79: public static CollectionType resolveCollectionType(Class<?> javaType) {
80:• if (List.class.isAssignableFrom(javaType)) {
81: return CollectionType.LIST;
82:• } else if (Set.class.isAssignableFrom(javaType)) {
83: return CollectionType.SET;
84:• } else if (Collection.class.isAssignableFrom(javaType)) {
85: return CollectionType.COLLECTION;
86:• } else if (Map.class.isAssignableFrom(javaType)) {
87: return CollectionType.MAP;
88: } else {
89: throw new IllegalArgumentException();
90: }
91: }
92: }