-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#11: Add "Sorted" implementation of argument for velocity resource
- Loading branch information
Showing
15 changed files
with
549 additions
and
96 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2018 Yurii Dubinka | ||
* | ||
* 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 com.github.dgroup.velocity.arg; | ||
|
||
import org.apache.velocity.VelocityContext; | ||
|
||
/** | ||
* The resource variable. | ||
* | ||
* The variable, which will be replaced in the velocity template. | ||
* Immutable single argument for {@link VelocityContext}. | ||
* | ||
* @since 0.1.0 | ||
*/ | ||
public final class ArgOf extends ArgEnvelope<Object> { | ||
|
||
/** | ||
* Ctor. | ||
* @param key The variable name specified in template file. | ||
* @param value The variable value specified in template file. | ||
*/ | ||
public ArgOf(final String key, final Object value) { | ||
super(key, () -> value); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/main/java/com/github/dgroup/velocity/arg/iterable/ArgsEnvelope.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,68 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2018 Yurii Dubinka | ||
* | ||
* 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 com.github.dgroup.velocity.arg.iterable; | ||
|
||
import com.github.dgroup.velocity.Arg; | ||
import org.cactoos.Scalar; | ||
import org.cactoos.scalar.UncheckedScalar; | ||
|
||
/** | ||
* Envelope for {@link Arg}. | ||
* | ||
* @param <V> The type of value. | ||
* @since 0.2.0 | ||
* @todo #/DEV Implement {@code Mapped} functionality. | ||
*/ | ||
public class ArgsEnvelope<V> implements Arg<Iterable<V>> { | ||
|
||
/** | ||
* The Apache Velocity variable name, specified in template file. | ||
*/ | ||
private final String key; | ||
|
||
/** | ||
* The Apache Velocity variable value, specified in template file. | ||
*/ | ||
private final UncheckedScalar<Iterable<V>> val; | ||
|
||
/** | ||
* Ctor. | ||
* @param key The variable name specified in template file. | ||
* @param val The variable value specified in template file. | ||
*/ | ||
public ArgsEnvelope(final String key, final Scalar<Iterable<V>> val) { | ||
this.key = key; | ||
this.val = new UncheckedScalar<>(val); | ||
} | ||
|
||
@Override | ||
public final String name() { | ||
return this.key; | ||
} | ||
|
||
@Override | ||
public final Iterable<V> value() { | ||
return this.val.value(); | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/main/java/com/github/dgroup/velocity/arg/iterable/Sorted.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,102 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2018 Yurii Dubinka | ||
* | ||
* 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 com.github.dgroup.velocity.arg.iterable; | ||
|
||
import java.util.Comparator; | ||
import java.util.function.Function; | ||
import org.cactoos.iterable.IterableOf; | ||
|
||
/** | ||
* The resource variable with sorted value. | ||
* | ||
* Useful when the resource value is collection or other iterable entities. | ||
* | ||
* @param <V> The type of value. | ||
* @since 0.2.0 | ||
*/ | ||
public final class Sorted<V> extends ArgsEnvelope<V> { | ||
|
||
/** | ||
* Ctor. | ||
* @param key The variable name specified in template file. | ||
* @param val The variable value specified in template file. | ||
* The value should implement the {@link Comparable}. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public Sorted(final String key, final V... val) { | ||
this(key, (Comparator<V>) Comparator.naturalOrder(), val); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param key The variable name specified in template file. | ||
* @param fnc The function to evaluate the comparator for values sorting. | ||
* @param val The variable value specified in template file. | ||
* @param <K> the type of the {@code Comparable} sort key. | ||
*/ | ||
public <K extends Comparable<? super K>> Sorted( | ||
final String key, | ||
final Function<? super V, ? extends K> fnc, | ||
final V... val | ||
) { | ||
this(key, fnc, new IterableOf<>(val)); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param key The variable name specified in template file. | ||
* @param fnc The function to evaluate the comparator for values sorting. | ||
* @param val The variable value specified in template file. | ||
* @param <K> the type of the {@code Comparable} sort key. | ||
*/ | ||
public <K extends Comparable<? super K>> Sorted( | ||
final String key, | ||
final Function<? super V, ? extends K> fnc, | ||
final Iterable<V> val | ||
) { | ||
this(key, Comparator.comparing(fnc), val); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param key The variable name specified in template file. | ||
* @param cmp The comparator in order to sort the values. | ||
* @param val The variable value specified in template file. | ||
*/ | ||
public Sorted(final String key, final Comparator<V> cmp, final V... val) { | ||
this(key, cmp, new IterableOf<>(val)); | ||
} | ||
|
||
/** | ||
* Ctor. | ||
* @param key The variable name specified in template file. | ||
* @param cmp The comparator in order to sort the values. | ||
* @param value The variable value specified in template file. | ||
*/ | ||
public Sorted(final String key, final Comparator<V> cmp, | ||
final Iterable<V> value) { | ||
super(key, () -> new org.cactoos.iterable.Sorted<>(cmp, value)); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/github/dgroup/velocity/arg/iterable/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,28 @@ | ||
/* | ||
* MIT License | ||
* | ||
* Copyright (c) 2018 Yurii Dubinka | ||
* | ||
* 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. | ||
*/ | ||
/** | ||
* The variables for velocity templates which contains | ||
* {@link java.lang.Iterable} or {@link java.util.Collection} as the value. | ||
*/ | ||
package com.github.dgroup.velocity.arg.iterable; |
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
Oops, something went wrong.