1 /* 2 * This file is part of *** M y C o R e *** 3 * See http://www.mycore.de/ for details. 4 * 5 * MyCoRe is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * 10 * MyCoRe 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 MyCoRe. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 package org.mycore.datamodel.ifs2; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 import java.util.Objects; 24 import java.util.stream.Stream; 25 26 public class MCRStoreCenter { 27 private Map<String, MCRStore> storeHeap; 28 29 private static MCRStoreCenter instance = new MCRStoreCenter(); 30 31 private MCRStoreCenter() { 32 this.storeHeap = new HashMap<>(); 33 } 34 35 public static MCRStoreCenter instance() { 36 return instance; 37 } 38 39 /** 40 * Add a store to the store center 41 * 42 * @param store - Add this store to store center 43 * @throws MCRStoreAlreadyExistsException If with the same id already exists in the store center 44 */ 45 public void addStore(String id, MCRStore store) throws MCRStoreAlreadyExistsException { 46 if (storeHeap.putIfAbsent(id, store) != null) { 47 throw new MCRStoreAlreadyExistsException("Could not add store with ID " + id + ", store allready exists"); 48 } 49 } 50 51 /** 52 * Get the MyCoRe Store with the given ID from store center. 53 * 54 * @param id - The id of the to retrieved store 55 * @param storeClass - The class type of the retrieved store 56 * @return The retrieved store or null if not exists 57 * @deprecated use {@link #getStore(String)} instead 58 */ 59 @Deprecated 60 @SuppressWarnings("unchecked") 61 public <T extends MCRStore> T getStore(String id, Class<T> storeClass) { 62 return (T) storeHeap.get(id); 63 } 64 65 /** 66 * Get the MyCoRe Store with the given ID from store center. 67 * 68 * @param id - The id of the to retrieved store 69 * @return The retrieved store or null if not exists 70 */ 71 public <T extends MCRStore> T getStore(String id) { 72 return (T) storeHeap.get(id); 73 } 74 75 /** 76 * @return a Stream of all {@link MCRStore}s that are an instance of <code><T></code> 77 */ 78 public <T extends MCRStore> Stream<T> getCurrentStores(Class<T> sClass) { 79 return storeHeap.values() 80 .stream() 81 .filter(sClass::isInstance) 82 .map(s -> (T) s) 83 .filter(Objects::nonNull); 84 } 85 86 /** 87 * Remove the store from store center 88 * 89 * @param id - Removed this store from store center 90 * @return true if successfully removed or false 91 */ 92 public boolean removeStore(String id) { 93 return storeHeap.remove(id) != null; 94 } 95 96 /** 97 * Remove all store from the store center 98 */ 99 public void clear() { 100 storeHeap.clear(); 101 } 102 }