-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
253 additions
and
10 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
91 changes: 91 additions & 0 deletions
91
eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.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,91 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016-2022 Objectionary.com | ||
* | ||
* 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 NON-INFRINGEMENT. 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 org.eolang.maven.optimization; | ||
|
||
import com.jcabi.xml.XML; | ||
import com.jcabi.xml.XMLDocument; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import org.eolang.maven.AssembleMojo; | ||
import org.eolang.maven.Place; | ||
|
||
/** | ||
* The cached optimization. | ||
* Returns already optimized XML if it's found in the cache. | ||
* | ||
* @since 0.28.11 | ||
* @todo #1431:90min Unit test are required. We have to test different cases like: | ||
* - if XML file is already in cache | ||
* - if XML file is absent | ||
* - if some {@link java.io.IOException} happens | ||
*/ | ||
public final class OptCached implements Optimization { | ||
|
||
/** | ||
* Real optimization. | ||
*/ | ||
private final Optimization delegate; | ||
|
||
/** | ||
* Cache folder. | ||
*/ | ||
private final Path folder; | ||
|
||
/** | ||
* The main constructor. | ||
* | ||
* @param delegate Real optimization. | ||
* @param folder Cache folder. | ||
*/ | ||
public OptCached( | ||
final Optimization delegate, | ||
final Path folder | ||
) { | ||
this.delegate = delegate; | ||
this.folder = folder; | ||
} | ||
|
||
@Override | ||
public XML apply(final Path xml) { | ||
try { | ||
final Path path = new Place( | ||
new XMLDocument(xml).xpath("/program/@name").get(0) | ||
).make(this.folder, AssembleMojo.ATTR_XMIR); | ||
final XML optimized; | ||
if (Files.exists(path)) { | ||
optimized = new XMLDocument(path); | ||
} else { | ||
optimized = this.delegate.apply(xml); | ||
Files.createDirectories(path.getParent()); | ||
Files.createFile(path); | ||
Files.write(path, optimized.toString().getBytes(StandardCharsets.UTF_8)); | ||
} | ||
return optimized; | ||
} catch (final IOException ex) { | ||
throw new IllegalStateException(String.format("Can't optimize '%s'", xml), ex); | ||
} | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
eo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptLambda.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,57 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016-2022 Objectionary.com | ||
* | ||
* 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 NON-INFRINGEMENT. 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 org.eolang.maven.optimization; | ||
|
||
import com.jcabi.xml.XML; | ||
import java.nio.file.Path; | ||
import org.cactoos.Func; | ||
import org.cactoos.func.UncheckedFunc; | ||
|
||
/** | ||
* Lambda optimization is adapter for custom optimization. | ||
* Optimization provided by external function. | ||
* | ||
* @since 0.28.11 | ||
*/ | ||
public final class OptLambda implements Optimization { | ||
|
||
/** | ||
* Custom foreign optimisation. | ||
*/ | ||
private final UncheckedFunc<Path, XML> delegate; | ||
|
||
/** | ||
* The main constructor. | ||
* | ||
* @param delegate Custom optimization. | ||
*/ | ||
public OptLambda(final Func<Path, XML> delegate) { | ||
this.delegate = new UncheckedFunc<>(delegate); | ||
} | ||
|
||
@Override | ||
public XML apply(final Path xml) { | ||
return this.delegate.apply(xml); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
eo-maven-plugin/src/main/java/org/eolang/maven/optimization/Optimization.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,37 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016-2022 Objectionary.com | ||
* | ||
* 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 NON-INFRINGEMENT. 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 org.eolang.maven.optimization; | ||
|
||
import com.jcabi.xml.XML; | ||
import java.nio.file.Path; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Abstraction for XML optimizations. | ||
* | ||
* @since 0.28.11 | ||
*/ | ||
@FunctionalInterface | ||
public interface Optimization extends Function<Path, XML> { | ||
} |
31 changes: 31 additions & 0 deletions
31
eo-maven-plugin/src/main/java/org/eolang/maven/optimization/package-info.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,31 @@ | ||
/* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2016-2022 Objectionary.com | ||
* | ||
* 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 NON-INFRINGEMENT. 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. | ||
*/ | ||
/** | ||
* Optimizations package. | ||
* The main purpose of classes under the package is to provide different implementations | ||
* of xml optimizations: | ||
* - {@link org.eolang.maven.optimization.OptLambda} - makes an optimization using foreign function | ||
* - {@link org.eolang.maven.optimization.OptCached} - looks for optimization in cache first | ||
*/ | ||
package org.eolang.maven.optimization; |
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
1d30f50
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
1223-c08eee6b
disappeared fromeo-maven-plugin/src/test/java/org/eolang/maven/OptimizeMojoTest.java
), that's why I closed #1431. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.1d30f50
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
1431-4de3d363
discovered ineo-maven-plugin/src/main/java/org/eolang/maven/optimization/OptCached.java
) and submitted as #1442. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.1d30f50
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Puzzle
1431-d4218ffb
discovered ineo-maven-plugin/src/main/java/org/eolang/maven/OptimizeMojo.java
) and submitted as #1443. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.