001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tang; 020 021import org.apache.reef.tang.types.ClassNode; 022import org.apache.reef.tang.types.ConstructorDef; 023import org.apache.reef.tang.types.NamedParameterNode; 024 025import java.util.List; 026import java.util.Map.Entry; 027import java.util.Set; 028 029/** 030 * Immutable, type-checked configuration data. 031 * <p/> 032 * Tang Configuration objects are constructed via 033 * ConfigurationBuilders, and most applications interact with the 034 * Configuration API much more than the one described here. See 035 * the ConfigurationBuilder documentation for a discussion of the 036 * semantics of configuration options. The documentation provided 037 * here is primarily for people that wish to extend Tang or implement 038 * formats that export data from Configuration objects to other systems. 039 * <p/> 040 * Conceptually, a configuration contains a set of key 041 * value pairs. Each pair either maps from an interface to an 042 * implementation (a class) or from a configuration option to a 043 * value (e.g., an integer or string). 044 * <p/> 045 * Under the hood, Configuration objects carry much richer type 046 * information than this, and also refer to the ClassHierarchy 047 * object they were checked against. Configurations can be 048 * merged into each other by creating a new ConfigurationBuilder 049 * object, and passing in multiple configurations. In such situations, 050 * Tang automatically merges the reflection data from the underlying 051 * ClassHierarchy objects, and re-validates the merged configuration 052 * data against the merged classpath. 053 * <p/> 054 * Note that the left hand side of each configuration object (the 055 * "key" in the key value pair) is unique. Although there are many 056 * APIs that take NamedParameterNode objects in this API, a given 057 * NamedParameterNode represents a unique type of binding, and is only 058 * applicable to one of the getters below. These APIs use Java generic 059 * types to make it easier to map from NamedParameterNode to the appropriate 060 * getter. 061 */ 062public interface Configuration { 063 064 /** 065 * Create a new ConfigurationBuilder object based on the same classpath 066 * as this Configuration, and populate it with the configuration options 067 * of this object. 068 * <p/> 069 * This API is unstable and should be considered private. Use the methods 070 * in org.apache.reef.Tang instead. 071 */ 072 public ConfigurationBuilder newBuilder(); 073 074 /** 075 * Return the value of the given named parameter as an unparsed string. 076 * <p/> 077 * If nothing was explicitly bound, this method returns null (it does not 078 * return default values). 079 * 080 * @param np A NamedParameter object from this Configuration's class hierarchy. 081 * @return The validated string that this parameter is bound to, or null. 082 * @see getClassHierarchy() 083 */ 084 public String getNamedParameter(NamedParameterNode<?> np); 085 086 /** 087 * Obtain the set of class hierarchy nodes or strings that were bound to a given NamedParameterNode. 088 * If nothing was explicitly bound, the set will be empty (it will not reflect any default values). 089 * 090 * @param np A NamedParameterNode from this Configuration's class hierarchy. 091 * @return A set of ClassHierarchy Node objects or a set of strings, depending on whether the NamedParameterNode refers to an interface or configuration options, respectively. 092 * @see getClassHierarchy() 093 */ 094 public Set<Object> getBoundSet(NamedParameterNode<Set<?>> np); 095 096 /** 097 * Get the list bound to a given NamedParameterNode. The list will be empty if nothing was bound. 098 * 099 * @param np Target NamedParameter 100 * @return A list bound to np 101 */ 102 public List<Object> getBoundList(NamedParameterNode<List<?>> np); 103 104 /** 105 * @return the external constructor that cn has been explicitly bound to, or null. Defaults are not returned. 106 */ 107 public <T> ClassNode<ExternalConstructor<T>> getBoundConstructor(ClassNode<T> cn); 108 109 /** 110 * @return the implementation that cn has been explicitly bound to, or null. Defaults are not returned. 111 */ 112 public <T> ClassNode<T> getBoundImplementation(ClassNode<T> cn); 113 114 /** 115 * Return the LegacyConstructor that has been bound to this Class. Such constructors are defined in the class, but missing their @Inject annotation. 116 * <p/> 117 * For now, only one legacy constructor can be bound per class. 118 * <p/> 119 * TODO: Should this return Set<ConstructorDef<T>> instead? 120 */ 121 public <T> ConstructorDef<T> getLegacyConstructor(ClassNode<T> cn); 122 123 /** 124 * @return the set of all interfaces (or super-classes) that have been explicitly 125 * bound to an implementation sub-class. 126 */ 127 Set<ClassNode<?>> getBoundImplementations(); 128 129 /** 130 * @return the set of all the interfaces that have had an external constructor bound to them. 131 */ 132 Set<ClassNode<?>> getBoundConstructors(); 133 134 /** 135 * @return the set of all the named parameters that have been explicitly bound to something. 136 */ 137 Set<NamedParameterNode<?>> getNamedParameters(); 138 139 /** 140 * @return the set of all interfaces that have a legacy constructor binding. 141 */ 142 Set<ClassNode<?>> getLegacyConstructors(); 143 144 /** 145 * Configuration objects are associated with the ClassHierarchy objects that were used during validation. 146 * 147 * @return the ClassHierarchy that backs this Configuration. 148 */ 149 public ClassHierarchy getClassHierarchy(); 150 151 /** 152 * Get the set bindings from set-valued NamedParameters to the values they were bound to. 153 * <p/> 154 * TODO: This API seems wonky. Why not return a Set<NamedParameterNode<Set<?>>> instead, and let the caller invoke getBoundSet() above? 155 * 156 * @return a flattened set with one entry for each binding (the same NamedParameterNode may be a key for multiple bindings. 157 * @deprecated 158 */ 159 @Deprecated 160 Iterable<Entry<NamedParameterNode<Set<?>>, Object>> getBoundSets(); 161 162 /** 163 * @return the set of all NamedParameterNodes explicitly bound to lists. 164 */ 165 Set<NamedParameterNode<List<?>>> getBoundLists(); 166 167}