Source:
public static Builder from(XmlMultiConfiguration config) { return new Builder() { @Override public Builder withManager(String identity, Configuration configuration) { Map<String, Config> configurations = new HashMap<>(config.configurations); configurations.put(identity, new SingleConfig(configuration)); return from(new XmlMultiConfiguration(configurations)); } public Builder withoutManager(String identity) { Map<String, Config> configurations = config.configurations; configurations.remove(identity); return from(new XmlMultiConfiguration(configurations)); } @Override public Variant withManager(String identity) { Map<String, Configuration> variants = new HashMap<>(); Config current = config.configurations.get(identity); if (current instanceof VariantConfig) { variants.putAll(((VariantConfig) current).configs); } else if (current != null) { throw new IllegalStateException("Existing non-variant configuration cannot be replaced - it must be removed first."); } return new Variant() { @Override public Variant withoutVariant(String variant) { variants.remove(variant); return this; } @Override public Variant variant(String variant, Configuration configuration) { variants.put(variant, configuration); return this; } @Override public Builder withoutManager(String identity) { return from(build()).withoutManager(identity); } @Override public Builder withManager(String identity, Configuration configuration) { return from(build()).withManager(identity, configuration); } @Override public Variant withManager(String identity) { return from(build()).withManager(identity); } @Override public XmlMultiConfiguration build() { Map<String, Config> configurations = new HashMap<>(config.configurations); configurations.put(identity, new VariantConfig(variants)); return new XmlMultiConfiguration(configurations); } }; } @Override public XmlMultiConfiguration build() { return config; } }; }

Target:
public static Builder from(XmlMultiConfiguration config) { return new Builder() { @Override public Builder withManager(String identity, Configuration configuration) { Map<String, Config> configurations = new HashMap<>(config.configurations); configurations.put(identity, new SingleConfig(configuration)); return from(new XmlMultiConfiguration(configurations)); } @Override public Builder withoutManager(String identity) { Map<String, Config> configurations = config.configurations; configurations.remove(identity); return from(new XmlMultiConfiguration(configurations)); } @Override public Variant withManager(String identity) { Map<String, Configuration> variants = new HashMap<>(); Config current = config.configurations.get(identity); if (current instanceof VariantConfig) { variants.putAll(((VariantConfig) current).configs); } else if (current != null) { throw new IllegalStateException("Existing non-variant configuration cannot be replaced - it must be removed first."); } return new Variant() { @Override public Variant withoutVariant(String variant) { variants.remove(variant); return this; } @Override public Variant variant(String variant, Configuration configuration) { variants.put(variant, configuration); return this; } @Override public Builder withoutManager(String identity) { return from(build()).withoutManager(identity); } @Override public Builder withManager(String identity, Configuration configuration) { return from(build()).withManager(identity, configuration); } @Override public Variant withManager(String identity) { return from(build()).withManager(identity); } @Override public XmlMultiConfiguration build() { Map<String, Config> configurations = new HashMap<>(config.configurations); configurations.put(identity, new VariantConfig(variants)); return new XmlMultiConfiguration(configurations); } }; } @Override public XmlMultiConfiguration build() { return config; } }; }