Skip to content

Package: NamedQueryManager

NamedQueryManager

nameinstructionbranchcomplexitylinemethod
NamedQueryManager()
M: 0 C: 8
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
addNamedQuery(String, String)
M: 0 C: 32
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
getQuery(String)
M: 0 C: 25
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.query;
16:
17: import java.util.HashMap;
18: import java.util.Map;
19: import java.util.Objects;
20:
21: /**
22: * Manages named queries in the persistence unit.
23: */
24: public class NamedQueryManager {
25:
26: private final Map<String, String> queryMap = new HashMap<>();
27:
28: /**
29: * Adds a named query mapping.
30: *
31: * @param name Named of the query
32: * @param query Query string
33: * @throws IllegalArgumentException If there already exists a mapping for the specified name
34: */
35: public void addNamedQuery(String name, String query) {
36: Objects.requireNonNull(name);
37: Objects.requireNonNull(query);
38:• if (queryMap.containsKey(name)) {
39: throw new IllegalArgumentException("Query with name " + name + " already exists in this persistence unit.");
40: }
41: queryMap.put(name, query);
42: }
43:
44: /**
45: * Gets a query mapped by the specified name.
46: *
47: * @param name Query name
48: * @return Query string
49: * @throws IllegalArgumentException If a query has not been defined with the given name
50: */
51: public String getQuery(String name) {
52:• if (!queryMap.containsKey(name)) {
53: throw new IllegalArgumentException("Query with name " + name + " was not found in this persistence unit.");
54: }
55: return queryMap.get(name);
56: }
57: }