Skip to content

Commit

Permalink
#11: Add "Sorted" implementation of argument for velocity resource
Browse files Browse the repository at this point in the history
  • Loading branch information
dgroup committed Oct 20, 2018
1 parent 5090c39 commit 7f0fb74
Show file tree
Hide file tree
Showing 15 changed files with 549 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
* @param <V> Type of item.
* @since 0.1.0
*/
public interface Variable<V> {
public interface Arg<V> {

/**
* Label of variable in Apache Velocity template.
Expand Down
20 changes: 2 additions & 18 deletions src/main/java/com/github/dgroup/velocity/Resource.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
*/
package com.github.dgroup.velocity;

import com.github.dgroup.velocity.rs.RsEnvelope;
import com.github.dgroup.velocity.rs.RsException;
import org.apache.velocity.VelocityContext;
import org.cactoos.Scalar;
Expand All @@ -45,7 +44,7 @@ public interface Resource<T> {
* @return HTML/SQL/XML/etc
* @throws RsException in case template format error.
*/
T compose(Variable... args) throws RsException;
T compose(Arg... args) throws RsException;

/**
* Transform the velocity template to HTML/SQL/etc using velocity variables.
Expand All @@ -54,7 +53,7 @@ public interface Resource<T> {
* @return HTML/SQL/XML/etc
* @throws RsException in case template format error.
*/
T compose(Iterable<Variable> args) throws RsException;
T compose(Iterable<Arg> args) throws RsException;

/**
* Transform the velocity template to HTML/SQL/etc using velocity variables.
Expand All @@ -65,19 +64,4 @@ public interface Resource<T> {
*/
T compose(Scalar<VelocityContext> ctx) throws RsException;

/**
* Fake implementation for unit testing purposes.
* @param <T> Type of fake resource.
*/
final class Fake<T> extends RsEnvelope<T> {

/**
* Ctor.
* @param rsrc The fake resource.
*/
public Fake(final T rsrc) {
super(ctx -> rsrc);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@
* 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;

package com.github.dgroup.velocity.vrb;

import com.github.dgroup.velocity.Variable;
import org.apache.velocity.VelocityContext;
import com.github.dgroup.velocity.Arg;
import org.cactoos.Scalar;
import org.cactoos.scalar.UncheckedScalar;

/**
* The resource variable.
*
* The variable, which will be replaced in the velocity template.
* Immutable single argument for {@link VelocityContext}.
* Envelope for {@link Arg}.
*
* @since 0.1.0
* @param <V> The type of value.
* @since 0.2.0
*/
public final class RsVariable implements Variable<Object> {
public class ArgEnvelope<V> implements Arg<V> {

/**
* The Apache Velocity variable name, specified in template file.
Expand All @@ -45,26 +43,25 @@ public final class RsVariable implements Variable<Object> {
/**
* The Apache Velocity variable value, specified in template file.
*/
private final Object val;
private final UncheckedScalar<V> val;

/**
* Ctor.
* @param key The variable name specified in template file.
* @param value The variable value specified in template file.
* @param val The variable value specified in template file.
*/
public RsVariable(final String key, final Object value) {
public ArgEnvelope(final String key, final Scalar<V> val) {
this.key = key;
this.val = value;
this.val = new UncheckedScalar<>(val);
}

@Override
public String name() {
public final String name() {
return this.key;
}

@Override
public Object value() {
return this.val;
public final V value() {
return this.val.value();
}

}
47 changes: 47 additions & 0 deletions src/main/java/com/github/dgroup/velocity/arg/ArgOf.java
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);
}
}
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 src/main/java/com/github/dgroup/velocity/arg/iterable/Sorted.java
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));
}

}
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;
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
/**
* The variables for velocity templates.
*/
package com.github.dgroup.velocity.vrb;
package com.github.dgroup.velocity.arg;
Loading

0 comments on commit 7f0fb74

Please sign in to comment.