-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create a deep copy of a configuration? #132
Comments
Hi! I've created #133 to track your feature request about modification tracking. For now, you can implement it yourself by calling copy, iterating on the entries and recursively copying sub-configurations, it shouldn't be too long to implement. |
I'm still wondering what the right interface is. /**
* Puts a deep copy of the given configuration to the root of this config.
* <p>
* This methods calls {@code deepCopy} on every sub-configuration,
* manually copies lists of known type (including {@code ArrayList}),
* calls {@code clone()} on {@code Cloneable} values, and simply
* copy the reference of other values.
* <p>
* For more control over the deep copy, in particular if you have
* put nested mutable objects into the config, use {@link #putDeepCopy(Function)}.
*/
void putDeepCopy(UnmodifiableConfig configToCopy);
/**
* Puts a deep copy of the given configuration to the root of this config.
* <p>
* This methods calls {@code deepCopy} on every sub-configuration
* and uses the {@code valueCopier} for other values (including lists).
*
* @param valueCopier a function that creates a deep copy of any object
*/
void putDeepCopy(UnmodifiableConfig configToCopy, Function<Object, Object> valueCopier); But given that many options could be good to have, it may be better to have a dedicated class |
Ah that's right, I miscopied some code. The design choice here is: how do you choose the type of the new config. For simple configs that maps a HashMap and aren't thread-safe it's trivial, but for Thus, doing |
Ah, I see - that makes sense... From a usability / "discoverability" view, I'd prefer the latter, as I'd then come from an existing object and have all my methods there, with all their documentation / examples. Seems more fluent to me... But maybe that's a personal preference... :-) |
hi again,
is there any (simple) way to create a deep copy of a configuration? Then one could compare the original and copied version, if one was changed later on? (
equals
seems to work properly for this case, comparing values and recursively including sub-configurations.)This is slightly related to #118, as I was evaluating workarounds to this and to the question, if the config has been changed (to then manually / optionally save the config with user interaction). (Not only for autosave, but also for changes in config, a listener would be nice.)
But it seems, that in a copy only the 'simple' values are copied; sub-configurations remain linked and will be changed in both places:
Maybe (if this behaviour isn't considered a bug), add
Config.deepCopy(UnmodifiableConfig)
?The text was updated successfully, but these errors were encountered: