| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DataContainerProvider |
|
| 1.4;1.4 |
| 1 | package org.jtheque.core.managers.persistence.able; | |
| 2 | ||
| 3 | /* | |
| 4 | * This file is part of JTheque. | |
| 5 | * | |
| 6 | * JTheque is free software: you can redistribute it and/or modify | |
| 7 | * it under the terms of the GNU General Public License as published by | |
| 8 | * the Free Software Foundation, either version 3 of the License. | |
| 9 | * | |
| 10 | * JTheque 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 | |
| 13 | * GNU General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU General Public License | |
| 16 | * along with JTheque. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | */ | |
| 18 | ||
| 19 | import org.jtheque.core.utils.CoreUtils; | |
| 20 | ||
| 21 | import java.util.ArrayList; | |
| 22 | import java.util.Collection; | |
| 23 | ||
| 24 | /** | |
| 25 | * A provider for dao. This class give access to all DAOs or to specific dao with different | |
| 26 | * parameters of search. | |
| 27 | * | |
| 28 | * @author Baptiste Wicht | |
| 29 | */ | |
| 30 | 0 | public final class DataContainerProvider { |
| 31 | 0 | private final Collection<DataContainer<? extends Entity>> containers = new ArrayList<DataContainer<? extends Entity>>(10); |
| 32 | ||
| 33 | 0 | private static final DataContainerProvider INSTANCE = new DataContainerProvider(); |
| 34 | ||
| 35 | /** | |
| 36 | * Add a container to manage. | |
| 37 | * | |
| 38 | * @param container The container to add. | |
| 39 | */ | |
| 40 | public void addContainer(DataContainer<? extends Entity> container) { | |
| 41 | 0 | assert container != null : "Container can't be null"; |
| 42 | ||
| 43 | 0 | CoreUtils.getLogger(getClass()).debug("Add container {} for datatype {}", container, container.getDataType()); |
| 44 | ||
| 45 | 0 | containers.add(container); |
| 46 | 0 | } |
| 47 | ||
| 48 | /** | |
| 49 | * Remove a container. | |
| 50 | * | |
| 51 | * @param container The container to remove. | |
| 52 | */ | |
| 53 | public void removeContainer(DataContainer<? extends Entity> container) { | |
| 54 | 0 | containers.remove(container); |
| 55 | 0 | } |
| 56 | ||
| 57 | /** | |
| 58 | * Return the unique instance of the DaoProvider. | |
| 59 | * | |
| 60 | * @return The unique instance of the class. | |
| 61 | */ | |
| 62 | public static DataContainerProvider getInstance() { | |
| 63 | 0 | return INSTANCE; |
| 64 | } | |
| 65 | ||
| 66 | /** | |
| 67 | * Return all the DAOs. | |
| 68 | * | |
| 69 | * @return A List containing all the DAO. | |
| 70 | */ | |
| 71 | public Iterable<DataContainer<? extends Entity>> getAllContainers() { | |
| 72 | 0 | return containers; |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * Return the DAO for a specific data type. | |
| 77 | * | |
| 78 | * @param dataType The data type for which we want the DAO. | |
| 79 | * @return The dao or <code>null</code> if we don't find one. | |
| 80 | */ | |
| 81 | public DataContainer<? extends Entity> getContainerForDataType(String dataType) { | |
| 82 | 0 | DataContainer<? extends Entity> container = null; |
| 83 | ||
| 84 | 0 | for (DataContainer<? extends Entity> d : containers) { |
| 85 | 0 | if (dataType.equals(d.getDataType())) { |
| 86 | 0 | container = d; |
| 87 | ||
| 88 | 0 | break; |
| 89 | } | |
| 90 | } | |
| 91 | ||
| 92 | 0 | CoreUtils.getLogger(getClass()).debug("Get container {} : return {}", dataType, container ); |
| 93 | ||
| 94 | 0 | return container; |
| 95 | } | |
| 96 | } |