-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #351 from klum-dsl/feature/309-copyfrom-strategies
Copy Strategies
- Loading branch information
Showing
29 changed files
with
2,068 additions
and
333 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
klum-ast-annotations/src/main/java/com/blackbuild/klum/ast/util/copy/DefaultOverwrite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Stephan Pauxberger | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.blackbuild.klum.ast.util.copy; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Default overwrite strategy. Merges single values (or overwrite non DSL single values), adds collections and merges maps. | ||
*/ | ||
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.PACKAGE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Overwrite( | ||
singles = @Overwrite.Single(OverwriteStrategy.Single.MERGE), | ||
collections = @Overwrite.Collection(OverwriteStrategy.Collection.ADD), | ||
maps = @Overwrite.Map(OverwriteStrategy.Map.MERGE_VALUES) | ||
) | ||
public @interface DefaultOverwrite { | ||
} |
42 changes: 42 additions & 0 deletions
42
...ast-annotations/src/main/java/com/blackbuild/klum/ast/util/copy/FullReplaceOverwrite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Stephan Pauxberger | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.blackbuild.klum.ast.util.copy; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Overwrite strategy that completely replaces the target`s fields with those of the source, only ignoring null values. | ||
*/ | ||
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.PACKAGE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Overwrite( | ||
singles = @Overwrite.Single(OverwriteStrategy.Single.REPLACE), | ||
collections = @Overwrite.Collection(OverwriteStrategy.Collection.ALWAYS_REPLACE), | ||
maps = @Overwrite.Map(OverwriteStrategy.Map.FULL_REPLACE) | ||
) | ||
public @interface FullReplaceOverwrite { | ||
} |
43 changes: 43 additions & 0 deletions
43
klum-ast-annotations/src/main/java/com/blackbuild/klum/ast/util/copy/HelmOverwrite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Stephan Pauxberger | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.blackbuild.klum.ast.util.copy; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Helm like overwrite strategy. Merges single values (or overwrite non DSL single values), replaces collections even | ||
* if copy source is empty and merges maps. | ||
*/ | ||
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.PACKAGE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Overwrite( | ||
singles = @Overwrite.Single(OverwriteStrategy.Single.MERGE), | ||
collections = @Overwrite.Collection(OverwriteStrategy.Collection.ALWAYS_REPLACE), | ||
maps = @Overwrite.Map(OverwriteStrategy.Map.MERGE_VALUES) | ||
) | ||
public @interface HelmOverwrite { | ||
} |
43 changes: 43 additions & 0 deletions
43
klum-ast-annotations/src/main/java/com/blackbuild/klum/ast/util/copy/LegacyOverwrite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Stephan Pauxberger | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.blackbuild.klum.ast.util.copy; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Overwrite strategy of KlumAST 1.x. Merges single values (or overwrite non DSL single values), replaces collections even | ||
* if copy source is empty and merges maps. | ||
*/ | ||
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.PACKAGE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Overwrite( | ||
singles = @Overwrite.Single(OverwriteStrategy.Single.MERGE), | ||
collections = @Overwrite.Collection(OverwriteStrategy.Collection.ALWAYS_REPLACE), | ||
maps = @Overwrite.Map(OverwriteStrategy.Map.FULL_REPLACE) | ||
) | ||
public @interface LegacyOverwrite { | ||
} |
42 changes: 42 additions & 0 deletions
42
klum-ast-annotations/src/main/java/com/blackbuild/klum/ast/util/copy/LenientOverwrite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Stephan Pauxberger | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.blackbuild.klum.ast.util.copy; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Overwrite strategy that only modifies empty/null fields of the target. | ||
*/ | ||
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.PACKAGE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Overwrite( | ||
singles = @Overwrite.Single(OverwriteStrategy.Single.SET_IF_NULL), | ||
collections = @Overwrite.Collection(OverwriteStrategy.Collection.SET_IF_EMPTY), | ||
maps = @Overwrite.Map(OverwriteStrategy.Map.SET_IF_EMPTY) | ||
) | ||
public @interface LenientOverwrite { | ||
} |
78 changes: 78 additions & 0 deletions
78
klum-ast-annotations/src/main/java/com/blackbuild/klum/ast/util/copy/Overwrite.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Stephan Pauxberger | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.blackbuild.klum.ast.util.copy; | ||
|
||
import com.blackbuild.groovy.configdsl.transform.cast.NeedsDSLClass; | ||
import com.blackbuild.klum.cast.KlumCastValidated; | ||
import com.blackbuild.klum.cast.KlumCastValidator; | ||
import com.blackbuild.klum.cast.checks.NeedsOneOf; | ||
import com.blackbuild.klum.cast.checks.NeedsType; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Handles how values are copied from one object to another. | ||
*/ | ||
@Target({ElementType.FIELD, ElementType.TYPE, ElementType.PACKAGE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@KlumCastValidated | ||
@NeedsDSLClass | ||
@NeedsOneOf(value = {"singles", "collections", "maps"}) | ||
public @interface Overwrite { | ||
|
||
Overwrite.Single singles() default @Overwrite.Single(OverwriteStrategy.Single.INHERIT); | ||
Overwrite.Collection collections() default @Overwrite.Collection(OverwriteStrategy.Collection.INHERIT); | ||
Overwrite.Map maps() default @Overwrite.Map(OverwriteStrategy.Map.INHERIT); | ||
|
||
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@KlumCastValidated | ||
@NeedsDSLClass | ||
@KlumCastValidator("com.blackbuild.klum.ast.validation.OverwriteSingleCheck") | ||
@interface Single { | ||
OverwriteStrategy.Single value(); | ||
} | ||
|
||
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@KlumCastValidated | ||
@NeedsDSLClass | ||
@NeedsType(java.util.Collection.class) | ||
@interface Collection { | ||
OverwriteStrategy.Collection value(); | ||
} | ||
|
||
@Target({ElementType.FIELD, ElementType.ANNOTATION_TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@KlumCastValidated | ||
@NeedsDSLClass | ||
@NeedsType(java.util.Map.class) | ||
@KlumCastValidator("com.blackbuild.klum.ast.validation.OverwriteMapCheck") | ||
@interface Map { | ||
OverwriteStrategy.Map value(); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
klum-ast-annotations/src/main/java/com/blackbuild/klum/ast/util/copy/OverwriteStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2015-2024 Stephan Pauxberger | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.blackbuild.klum.ast.util.copy; | ||
|
||
/** | ||
* Defines the various strategies for different targets. Note that the collection and map strategies are combined | ||
* with dsl or basic object, depending on the element type | ||
*/ | ||
public interface OverwriteStrategy { | ||
|
||
/** | ||
* Defines the strategy for a single dsl object. | ||
*/ | ||
enum Single { | ||
/** No explicit overwrite strategy is set. */ | ||
INHERIT, | ||
/** The object is fully replaced. */ | ||
REPLACE, | ||
/** The object is replaced even if the copy source is null. */ | ||
ALWAYS_REPLACE, | ||
/** The object is set only when there was no previous value. */ | ||
SET_IF_NULL, | ||
/** The object is merged with the existing object. */ | ||
MERGE | ||
} | ||
|
||
/** | ||
* Defines the strategy for a collection. | ||
*/ | ||
enum Collection { | ||
/** No explicit overwrite strategy is set. */ | ||
INHERIT, | ||
/** | ||
* The members of the copy source are added to the target collection. Potential item replacements are handled | ||
* by the target collection (like {@link java.util.Set}) | ||
*/ | ||
ADD, | ||
/** The collection is fully replaced if the replacement is not empty. */ | ||
REPLACE, | ||
/** The collection is fully replaced only if target has now elements. */ | ||
SET_IF_EMPTY, | ||
/** The map is replaced even if the copy source is empty. */ | ||
ALWAYS_REPLACE, | ||
} | ||
|
||
/** | ||
* Defines the strategy for a map. | ||
*/ | ||
enum Map { | ||
/** No explicit overwrite strategy is set. */ | ||
INHERIT, | ||
/** The map is fully replaced if the replacement is not empty. */ | ||
FULL_REPLACE, | ||
/** The map is replaced even if the copy source is empty. */ | ||
ALWAYS_REPLACE, | ||
/** The map is replaced only if the target map is empty. */ | ||
SET_IF_EMPTY, | ||
/** The members of the copy source are added to the target map. Members with the same keys are replaced. */ | ||
MERGE_KEYS, | ||
/** The members of the copy source are added to the target map. Members with the same keys are merged. Only valid for DSL elements. */ | ||
MERGE_VALUES, | ||
/** The members of the copy source are added to the target map. Members with the existing keys are ignored. */ | ||
ADD_MISSING | ||
} | ||
} |
Oops, something went wrong.