-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
820a6c5
commit 1fc456e
Showing
4 changed files
with
189 additions
and
69 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
79 changes: 79 additions & 0 deletions
79
src/main/java/com/mitchellbosecke/pebble/node/fornode/LazyLength.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,79 @@ | ||
package com.mitchellbosecke.pebble.node.fornode; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.Collection; | ||
import java.util.Enumeration; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
public class LazyLength extends Number { | ||
|
||
private final Object iterableEvaluation; | ||
private int value = -1; | ||
|
||
public LazyLength(Object iterableEvaluation) { | ||
this.iterableEvaluation = iterableEvaluation; | ||
} | ||
|
||
@Override | ||
public int intValue() { | ||
return getValue(); | ||
} | ||
|
||
@Override | ||
public long longValue() { | ||
return (long) getValue(); | ||
} | ||
|
||
@Override | ||
public float floatValue() { | ||
return (float) getValue(); | ||
} | ||
|
||
@Override | ||
public double doubleValue() { | ||
return (double) getValue(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.valueOf(getValue()); | ||
} | ||
|
||
private int getValue() { | ||
if (this.value == -1) { | ||
this.value = getIteratorSize(iterableEvaluation); | ||
} | ||
return value; | ||
} | ||
|
||
private int getIteratorSize(Object iterable) { | ||
if (iterable == null) { | ||
return 0; | ||
} | ||
if (iterable instanceof Collection) { | ||
return ((Collection<?>) iterable).size(); | ||
} else if (iterable instanceof Map) { | ||
return ((Map<?, ?>) iterable).size(); | ||
} else if (iterable.getClass().isArray()) { | ||
return Array.getLength(iterable); | ||
} else if (iterable instanceof Enumeration) { | ||
Enumeration<?> enumeration = (Enumeration<?>) iterable; | ||
int size = 0; | ||
while (enumeration.hasMoreElements()) { | ||
size++; | ||
enumeration.nextElement(); | ||
} | ||
return size; | ||
} | ||
|
||
// assumed to be of type Iterator | ||
Iterator<?> it = ((Iterable<?>) iterable).iterator(); | ||
int size = 0; | ||
while (it.hasNext()) { | ||
size++; | ||
it.next(); | ||
} | ||
return size; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/mitchellbosecke/pebble/node/fornode/LazyRevIndex.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,40 @@ | ||
package com.mitchellbosecke.pebble.node.fornode; | ||
|
||
public class LazyRevIndex extends Number { | ||
private final int value; | ||
private final LazyLength lazyLength; | ||
|
||
public LazyRevIndex(int value, LazyLength lazyLength) { | ||
this.value = value; | ||
this.lazyLength = lazyLength; | ||
} | ||
|
||
@Override | ||
public int intValue() { | ||
return getValue(); | ||
} | ||
|
||
@Override | ||
public long longValue() { | ||
return (long) getValue(); | ||
} | ||
|
||
@Override | ||
public float floatValue() { | ||
return (float) getValue(); | ||
} | ||
|
||
@Override | ||
public double doubleValue() { | ||
return (double) getValue(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.valueOf(getValue()); | ||
} | ||
|
||
private int getValue() { | ||
return lazyLength.intValue() - this.value - 1; | ||
} | ||
} |
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,60 @@ | ||
package com.mitchellbosecke.pebble; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import com.mitchellbosecke.pebble.loader.StringLoader; | ||
import com.mitchellbosecke.pebble.template.PebbleTemplate; | ||
import java.io.IOException; | ||
import java.io.StringWriter; | ||
import java.io.Writer; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.Test; | ||
|
||
public class ForTest { | ||
@Test | ||
public void testForLengthWithOperation() throws IOException { | ||
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build(); | ||
|
||
String source = "{% for user in users %}{% if loop.index < ( loop.length - 1) %}{{user.username}}{% endif %}{% endfor %}"; | ||
PebbleTemplate template = pebble.getTemplate(source); | ||
Map<String, Object> context = new HashMap<>(); | ||
List<User> users = new ArrayList<>(); | ||
users.add(new User("Alex")); | ||
users.add(new User("Bob")); | ||
users.add(new User("John")); | ||
context.put("users", users); | ||
|
||
Writer writer = new StringWriter(); | ||
template.evaluate(writer, context); | ||
assertEquals("AlexBob", writer.toString()); | ||
} | ||
|
||
@Test | ||
public void testForRevIndexWithOperation() throws IOException { | ||
PebbleEngine pebble = new PebbleEngine.Builder().loader(new StringLoader()).strictVariables(false).build(); | ||
|
||
String source = "{% for user in users %}{% if (loop.revindex - 1) >= 0 %}{{user.username}}{% endif %}{% endfor %}"; | ||
PebbleTemplate template = pebble.getTemplate(source); | ||
Map<String, Object> context = new HashMap<>(); | ||
List<User> users = new ArrayList<>(); | ||
users.add(new User("Alex")); | ||
users.add(new User("Bob")); | ||
users.add(new User("John")); | ||
context.put("users", users); | ||
|
||
Writer writer = new StringWriter(); | ||
template.evaluate(writer, context); | ||
assertEquals("AlexBob", writer.toString()); | ||
} | ||
|
||
public static class User { | ||
public final String username; | ||
|
||
public User(String username) { | ||
this.username = username; | ||
} | ||
} | ||
} |