org.appdapter.registry.basic.BasicRegistry Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.appdapter.lib.registry Show documentation
Show all versions of org.appdapter.lib.registry Show documentation
Pluggable object registration + query service
/*
* Copyright 2012 by The Appdapter Project (www.appdapter.org).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.appdapter.registry.basic;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.appdapter.api.registry.Description;
import org.appdapter.api.registry.Finder;
import org.appdapter.api.registry.Pattern;
import org.appdapter.api.registry.SimpleFinder;
import org.appdapter.api.registry.VerySimpleRegistry;
import org.appdapter.core.log.BasicDebugger;
/**
* @author Stu B.
*/
public class BasicRegistry extends BasicDebugger implements VerySimpleRegistry {
private Map myObjectsByDesc;
public BasicRegistry() {
myObjectsByDesc = new HashMap();
}
@Override public void registerObject(Object o, Description d) {
myObjectsByDesc.put(d, o);
}
/*
* From DeletingRegistry interface.
*
*/
@Override public Finder getFinder(Class objClaz) {
return new BasicFinder(this, objClaz);
}
protected BasicFinder getBasicFinder(Class objClaz) {
Finder f = getFinder(objClaz);
return (BasicFinder) f;
}
@Override public OT findRequiredUniqueMatch(Class objClaz, Pattern p) throws Exception {
BasicFinder bf = getBasicFinder(objClaz);
return bf.findFirstMatch(p, 1, 1);
}
@Override public OT findOptionalUniqueMatch(Class objClaz, Pattern p) throws Exception {
BasicFinder bf = getBasicFinder(objClaz);
return bf.findFirstMatch(p, 0, 1);
}
@Override public OT findOptionalFirstMatch(Class objClaz, Pattern p) {
BasicFinder bf = getBasicFinder(objClaz);
try {
return bf.findFirstMatch(p, 0, SimpleFinder.MAX_MATCHES);
} catch (Throwable t) {
t.printStackTrace();
throw new RuntimeException("Got unexpected exception of type " + t.getClass());
}
}
@Override public List findAllMatches(Class objClaz, Pattern p, int minAllowed, int maxAllowed)
throws Exception {
BasicFinder bf = getBasicFinder(objClaz);
return bf.findAllMatches(p, minAllowed, maxAllowed);
}
protected List brutishlyCollectAllMatches (Class objClz, Pattern p) {
List resultList = new ArrayList();
if (p instanceof BasicPattern) {
// Use optimized search built into the map
BasicPattern bp = (BasicPattern) p;
Description bpd = bp.getDescription();
Object candidate = myObjectsByDesc.get(bpd);
if (candidate != null) {
if (objClz.isInstance(candidate)) {
resultList.add((OT) candidate);
} else {
// TODO : print warning if so configured
}
}
} else {
throw new UnsupportedOperationException("Cannot use non-BasicPattern " + p + " with BasicRegistry.");
}
return resultList;
}
public void registerNamedObject(Object o, String objName) {
BasicDescription bd = new BasicDescription(objName);
registerObject(o, bd);
}
@Override public OT findOptionalUniqueNamedObject(Class objClaz, String objName) throws Exception {
BasicDescription bd = new BasicDescription(objName);
Pattern p = new BasicPattern(bd);
return findOptionalUniqueMatch(objClaz, p);
}
@Override public OT findRequiredUniqueNamedObject(Class objClaz, String objName) throws Exception {
BasicDescription bd = new BasicDescription(objName);
Pattern p = new BasicPattern(bd);
return findRequiredUniqueMatch(objClaz, p);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy