Skip to content

Method: addMapper(SparqlResultMapper)

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.query.mapper;
19:
20: import cz.cvut.kbss.jopa.sessions.UnitOfWork;
21: import cz.cvut.kbss.ontodriver.iteration.ResultRow;
22:
23: import java.util.ArrayList;
24: import java.util.Collections;
25: import java.util.List;
26:
27: /**
28: * Represents a single {@link cz.cvut.kbss.jopa.model.annotations.SparqlResultSetMapping} instance.
29: * <p>
30: * This instance can contain multiple {@link SparqlResultMapper} instances, representing the individual mappings
31: * specified by the {@link cz.cvut.kbss.jopa.model.annotations.SparqlResultSetMapping}.
32: */
33: public class ResultRowMapper implements SparqlResultMapper {
34:
35: private final String name;
36:
37: private final List<SparqlResultMapper> rowMappers = new ArrayList<>();
38:
39: public ResultRowMapper(String name) {
40: this.name = name;
41: }
42:
43: /**
44: * Gets the name of the result set mapping represented by this mapper.
45: *
46: * @return Mapping name
47: */
48: public String getName() {
49: return name;
50: }
51:
52: List<SparqlResultMapper> getRowMappers() {
53: return Collections.unmodifiableList(rowMappers);
54: }
55:
56: void addMapper(SparqlResultMapper mapper) {
57: rowMappers.add(mapper);
58: }
59:
60: @Override
61: public Object map(ResultRow resultRow, UnitOfWork uow) {
62: if (rowMappers.size() == 1) {
63: return rowMappers.get(0).map(resultRow, uow);
64: }
65: final Object[] result = new Object[rowMappers.size()];
66: int i = 0;
67: for (SparqlResultMapper m : rowMappers) {
68: result[i++] = m.map(resultRow, uow);
69: }
70: return result;
71: }
72: }