StructManager.java
/***************************************************************************
Copyright 2012 Emily Estes
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 net.metanotion.scripting;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import net.metanotion.util.reflect.GetInitializer;
import net.metanotion.util.reflect.Initializer;
import net.metanotion.util.reflect.ReflectiveFieldInitializer;
/** This class stores a collection of constructors for "structs" indexed by class name.
*/
public final class StructManager implements Iterable<Map.Entry<String,GetInitializer>> {
private final ConcurrentHashMap<String,GetInitializer> structs = new ConcurrentHashMap<>();
/** Look up the initializer for a given struct by type name.
@param name The type name of the struct to look up.
@return an Initializer instance to create an instance of the struct.
*/
public Initializer getStruct(final String name) {
final GetInitializer init = structs.get(name);
if(init != null) { return init.initializer(); }
try {
final GetInitializer gi = ReflectiveFieldInitializer.getInitializer(Class.forName(name));
structs.put(name, gi);
return gi.initializer();
} catch (ClassNotFoundException cnfe) { }
/*
Return a struct initializer
#1 - look in cache for initializer
#2 - Class.forName(name)
#2a - bean utility class (ala my IDL compiler)
#2b - Deal with final fields via constructor
#2c - "field injector"
#2d - Java Standard bean
#3 - driver look up
*/
return null;
}
/** Add a new type to the cache of structs.
@param name The type name of the struct we're adding.
@param init The GetInitializer of the struct to produce initializers.
*/
public void setStruct(final String name, final GetInitializer init) { structs.put(name, init); }
@Override public Iterator<Map.Entry<String,GetInitializer>> iterator() { return structs.entrySet().iterator(); }
}