Github user neykov commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/462#discussion_r94980522
--- Diff:
core/src/main/java/org/apache/brooklyn/core/config/BasicConfigInheritance.java
---
@@ -28,51 +32,188 @@
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.config.ConfigValueAtContainer;
import org.apache.brooklyn.util.collections.CollectionMerger;
+import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.exceptions.ReferenceWithError;
import org.apache.brooklyn.util.guava.Maybe;
+import org.apache.brooklyn.util.text.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+@SuppressWarnings("serial")
public class BasicConfigInheritance implements ConfigInheritance {
+ private static final Logger log =
LoggerFactory.getLogger(BasicConfigInheritance.class);
+
private static final long serialVersionUID = -5916548049057961051L;
public static final String CONFLICT_RESOLUTION_STRATEGY_DEEP_MERGE =
"deep_merge";
public static final String CONFLICT_RESOLUTION_STRATEGY_OVERWRITE =
"overwrite";
+ public static abstract class DelegatingConfigInheritance implements
ConfigInheritance {
+ protected abstract ConfigInheritance getDelegate();
+
+ @Override @Deprecated public InheritanceMode
isInherited(ConfigKey<?> key, Object from, Object to) {
+ return getDelegate().isInherited(key, from, to);
+ }
+ @Override public <TContainer, TValue> boolean
isReinheritable(ConfigValueAtContainer<TContainer, TValue> parent,
ConfigInheritanceContext context) {
+ return getDelegate().isReinheritable(parent, context);
+ }
+ @Override public <TContainer, TValue> boolean
considerParent(ConfigValueAtContainer<TContainer, TValue> local,
ConfigValueAtContainer<TContainer, TValue> parent, ConfigInheritanceContext
context) {
+ return getDelegate().considerParent(local, parent, context);
+ }
+ @Override
+ public <TContainer, TValue>
ReferenceWithError<ConfigValueAtContainer<TContainer, TValue>>
resolveWithParent(ConfigValueAtContainer<TContainer, TValue> local,
ConfigValueAtContainer<TContainer, TValue> resolvedParent,
ConfigInheritanceContext context) {
+ return getDelegate().resolveWithParent(local, resolvedParent,
context);
+ }
+ @Override
+ public boolean equals(Object obj) {
+ return super.equals(obj) || getDelegate().equals(obj);
+ }
+
+ // standard deserialization method
+ protected ConfigInheritance readResolve() {
+ return returnEquivalentConstant(this);
+ }
+ }
+
+ private static ConfigInheritance
returnEquivalentConstant(ConfigInheritance candidate) {
+ for (ConfigInheritance knownMode: Arrays.asList(
+ NOT_REINHERITED, NOT_REINHERITED_ELSE_DEEP_MERGE,
NEVER_INHERITED, OVERWRITE, BasicConfigInheritance.DEEP_MERGE)) {
+ if (candidate.equals(knownMode)) return knownMode;
+ }
+ if (candidate.equals(new BasicConfigInheritance(false,
CONFLICT_RESOLUTION_STRATEGY_OVERWRITE, true, true))) {
+ // ignore the ancestor flag for this mode
+ return NEVER_INHERITED;
+ }
+ return candidate;
+ }
+
+ /*
+ * use of delegate is so that stateless classes can be defined to make
the serialization nice,
+ * both the name and hiding the implementation detail (also making it
easier for that detail to change);
+ * with aliased type names the field names here could even be aliases
for the type names.
+ * (we could alternatively have an "alias-for-final-instance" mode in
serialization,
+ * to optimize where we know that a java instance is final.)
+ */
+
+ private static class NotReinherited extends
DelegatingConfigInheritance {
+ final static BasicConfigInheritance DELEGATE = new
BasicConfigInheritance(false, CONFLICT_RESOLUTION_STRATEGY_OVERWRITE, false,
true);
+ @Override protected ConfigInheritance getDelegate() { return
DELEGATE; }
+ }
/** Indicates that a config key value should not be passed down from a
container where it is defined.
* Unlike {@link #NEVER_INHERITED} these values can be passed down if
set as anonymous keys at a container
* (ie the container does not expect it) to a container which does
expect it, but it will not be passed down further.
* If the inheritor also defines a value the parent's value is ignored
irrespective
* (as in {@link #OVERWRITE}; see {@link
#NOT_REINHERITED_ELSE_DEEP_MERGE} if merging is desired). */
- public static BasicConfigInheritance NOT_REINHERITED = new
BasicConfigInheritance(false,CONFLICT_RESOLUTION_STRATEGY_OVERWRITE,false);
+ public static ConfigInheritance NOT_REINHERITED = new NotReinherited();
--- End diff --
Suggestion for a less mind-bending, delegate-free implementation:
```
private static class NotReinherited extends BasicConfigInheritance {
public NotReinherited() {super(false,
CONFLICT_RESOLUTION_STRATEGY_OVERWRITE, false, true);}
@SuppressWarnings("unused") private ConfigInheritance readResolve()
{return NOT_REINHERITED;};
}
public static ConfigInheritance NOT_REINHERITED = new NotReinherited();
```
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---