Java tutorial
/** * "Mag Tured" library is a set of base components to build wargame * softwares. * Copyright (C) 2015 Gwen * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package org.magtured.common.component; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import static org.apache.commons.lang3.Validate.*; import org.magtured.common.lang.IdConfiguration; import org.magtured.common.lang.WithId; import org.magtured.common.util.PackageUtil; import org.magtured.common.util.TypeFilterSet; import org.springframework.core.type.filter.AnnotationTypeFilter; import org.springframework.core.type.filter.AssignableTypeFilter; public class MemoryIdManager implements IdManager { private Map<Class<? extends WithId>, MemoryTypeIdManager> typeToManager = new HashMap<>(); private Set<String> prefixInUses = new HashSet<>(); @Override public void registerType(Class<? extends WithId> type, String prefix) { notNull(type); notEmpty(prefix); isTrue(isRegisteredType(type) == false, "type already registered"); isTrue(isRegisteredPrefix(prefix) == false, "Prefix already registered : " + prefix); doRegisterType(type, prefix); } private boolean isRegisteredType(Class<? extends WithId> clazz) { return typeToManager.containsKey(clazz); } private boolean isRegisteredPrefix(String prefix) { return prefixInUses.contains(prefix); } private void doRegisterType(Class<? extends WithId> type, String prefix) { MemoryTypeIdManager manager = new MemoryTypeIdManager(prefix); typeToManager.put(type, manager); prefixInUses.add(prefix); } @Override public void registerType(Class<? extends WithId> type) throws NullPointerException, IllegalArgumentException { notNull(type); IdConfiguration idConf = type.getAnnotation(IdConfiguration.class); if (idConf == null) throw new IllegalArgumentException( "Missing annotation " + IdConfiguration.class.getName() + " on " + type.getName()); String prefix = idConf.prefix(); registerType(type, prefix); } @Override public void registerPackage(Class<?> packageClass) throws NullPointerException, IllegalArgumentException, ClassNotFoundException { notNull(packageClass); findAutonomousWithIdClassesInPackage(packageClass).forEach((type) -> registerType(type)); } private static Set<Class<WithId>> findAutonomousWithIdClassesInPackage(Class<?> packageClass) throws ClassNotFoundException { String basePackage = packageClass.getPackage().getName(); TypeFilterSet filters = new TypeFilterSet(new AssignableTypeFilter(WithId.class), new AnnotationTypeFilter(IdConfiguration.class)); return PackageUtil.findClassesInPackage(basePackage, filters); } @Override public boolean isUsed(String id) { if (id == null) return false; for (MemoryTypeIdManager manager : typeToManager.values()) { if (manager.isInUsed(id)) return true; } return false; } @Override public String createId(Class<? extends WithId> clazz) throws IllegalArgumentException, NullPointerException { notNull(clazz); MemoryTypeIdManager manager = typeToManager.get(clazz); if (manager == null) throw new IllegalArgumentException("unregistered type"); return manager.generateId(); } @Override public void addIdInUsed(String objectId) throws NullPointerException { notNull(objectId); for (MemoryTypeIdManager manager : typeToManager.values()) { manager.addIdInUsed(objectId); } } @Override public void setIdInUsed(Set<String> objectIdSet) throws NullPointerException { notNull(objectIdSet); for (MemoryTypeIdManager manager : typeToManager.values()) { manager.setIdInUsed(objectIdSet); } } @Override public boolean unuseId(String id) { if (id == null) return false; for (MemoryTypeIdManager manager : typeToManager.values()) { if (manager.deleteId(id)) return true; } return false; } @Override public void clear() { for (MemoryTypeIdManager manager : typeToManager.values()) manager.deleteAll(); } }