/** * Get the root node in the tree of configurables. * @return Returns the root of the configurable factories, or this factory if no parent is set. */ public ConfigurableFactory<?> getRoot() { return (_parent != null)? _parent.getRoot() : this; }
public static List<String> mergePaths (ConfigurableFactory<?> parent, List<String> childPath) { List<String> parentPath = null; if (null != parent) parentPath = parent.getRootPath(); return mergePaths(parentPath, childPath); } public static List<String> mergePaths (List<String> parentPath, List<String> childPath) {
@Override public void readConfiguration (JSONObject rootNode) throws ConfigurationException { // read the predictable super.readConfiguration(rootNode); }
private <PT> void writePropertyValue (PrintStream stream, String prefix, ConfigurationProperty<PT> property) { if (hasPropertyValue(property)) { stream.println(prefix+property.getName()+": "+property.encode(getDefaultValue(property))+" (DEFAULT)"); } else { PT value; try { value = property.unencodeJSON(new JSONNode(_configurationNode, property.getName())); stream.println(prefix+property.getName()+": "+property.encode(value)); } catch (JSONException|ConfigurationException e) { stream.println(prefix+property.getName()+": "+property.encode(getDefaultValue(property))+" (DEFAULT - read error)"); } } }
@Override protected T create () throws ConfigurationException { String subType = getPropertyValue(FACTORY_TYPE); try { return super.produce(subType, getFactoryType()); } catch (ConfigurationException e) { LOGGER.warn("Error creating product {}[{}] for {}", new Object[] {getFactoryType(), getName(), subType}); return null; } }
@Override public <GT> GT produce (String name, Class<GT> goodsType) throws ConfigurationException { // Uber-factories only produce for exact name matches. if (null == getName() && null != name) return null; if (null != getName() && !getName().equals(name)) return null; return super.produce(name, goodsType); } }
/** * Get one of the goods managed by this factory. * * This version returns a new instance each time it is called. * * @param goodsType The type of goods desired. */ public <GT> GT produce (Class<GT> goodsType) throws ConfigurationException { return produce(null, goodsType); }
@Override public void encodeJSON (JSONNode propertyNode, List<T> value) throws JSONException { JSONArray valueToPut = new JSONArray(); int i = 0; for (T v: value) { _baseProperty.encodeJSON(new JSONNode(valueToPut, i), v); ++i; } propertyNode.setAsJSONArray(valueToPut); }
public UberFactory (String name, Class<T> factoryType, ConfigurableFactory<?> parent, List<String> path, boolean isSingleton, List<ConfigurableFactory<? extends T>> children, String defaultType) { super(name, factoryType, parent, path, isSingleton); _children = children; for (ConfigurableFactory<? extends T> child: children) { addChildFactory(child); } addProperty(FACTORY_TYPE); setDefaultValue(FACTORY_TYPE, defaultType); _defaultType = defaultType; if (null == _defaultType) _defaultType = children.get(0).getName(); }
/** * Indicates if an actual value is recorded for the given property. * * @param property The property of interest. * @return True if the property is listed and non-default in the factory. */ public boolean hasPropertyValue (ConfigurationProperty<?> property) { return (_configured && null != _configurationNode && getPropertyNode( property ).has( property.getName() ) ); }
@Override protected FactoryResult create () throws ConfigurationException { FactoryResult result = new FactoryResult(); result._int = getPropertyValue(INT_PROP); result._double = getPropertyValue(DOUBLE_PROP); result._string = getPropertyValue(STRING_PROP); return result; } @Override
@Override public JSONObject getConfigurationNode () { return super.getConfigurationNode(); } }
public OuterTestFactory () { super(Integer.class, null, Arrays.asList("abc")); addChildFactory(new TestFactory(Arrays.asList("def"))); } @Override
/** * Overridden in order to make this public so others can compose trees */ @Override public void addChildFactory(ConfigurableFactory<?> child) { super.addChildFactory(child); } }
@Override public void encodeJSON (JSONNode propertyNode, JSONArray value) throws JSONException { propertyNode.setAsJSONArray(value); }
@Override public Boolean unencodeJSON (JSONNode propertyNode) throws JSONException, ConfigurationException { return propertyNode.getAsBoolean(); }
@Override public Integer unencodeJSON (JSONNode propertyNode) throws JSONException, ConfigurationException { return propertyNode.getAsInt(); }
@Override public Double unencodeJSON (JSONNode propertyNode) throws JSONException, ConfigurationException { return propertyNode.getAsDouble(); }
public TestFactory (List<String> path) { super(FactoryResult.class, null, path); addProperty(INT_PROP); addProperty(DOUBLE_PROP); addProperty(STRING_PROP); } @Override
@Override public void readConfiguration (JSONObject rootNode) throws ConfigurationException { super.readConfiguration(rootNode); _baseFactory.readConfiguration(rootNode); }