Skip to content

Commit

Permalink
Clean up code in src/main/java
Browse files Browse the repository at this point in the history
Infer generic parameters for Map, List, etc.
Cease use of deprecated Java API.
Suppress "unchecked", "hiding" warnings where appropriate.
Add missing serialVersionUID where necessary.
Use getRenderSettings()/getParseSettings() instead of deprecated access.
Remove unused class members.
Remove unused imports.
  • Loading branch information
kohlschuetter committed Jun 27, 2023
1 parent 4a4be2f commit 949b1f8
Show file tree
Hide file tree
Showing 40 changed files with 225 additions and 173 deletions.
2 changes: 0 additions & 2 deletions src/main/java/liqp/Insertion.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package liqp;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import liqp.nodes.LNode;

Expand Down
12 changes: 6 additions & 6 deletions src/main/java/liqp/LValue.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package liqp;

import static java.math.BigDecimal.ROUND_UNNECESSARY;
import static liqp.filters.date.Parser.getZonedDateTimeFromTemporalAccessor;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
Expand Down Expand Up @@ -219,9 +219,9 @@ public static boolean isTemporal(Object value){
* @param value
* @return
*/
protected Object[] mapAsArray(Map value) {
protected Object[] mapAsArray(Map<?,?> value) {
List<Object[]> keyValuePairs = new ArrayList<>();
for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) value).entrySet()) {
for (Map.Entry<?, ?> entry : value.entrySet()) {
keyValuePairs.add(new Object[]{entry.getKey(), entry.getValue()});
}
return keyValuePairs.toArray();
Expand Down Expand Up @@ -279,7 +279,7 @@ public Number asNumber(Object value) throws NumberFormatException {
// mimic ruby's `BigDecimal.to_f` with standard java capabilities
// same time provide expected out for java.math.BigDecimal
public static String asFormattedNumber(BigDecimal bd) {
return bd.setScale(Math.max(1, bd.stripTrailingZeros().scale()), ROUND_UNNECESSARY).toPlainString();
return bd.setScale(Math.max(1, bd.stripTrailingZeros().scale()), RoundingMode.UNNECESSARY).toPlainString();
}

/**
Expand Down Expand Up @@ -438,7 +438,7 @@ public boolean isFalsy(Object value, TemplateContext context) {
if (this.isArray(value) && this.asArray(value, context).length == 0)
return true;

if ((value instanceof Map) && ((Map) value).isEmpty())
if ((value instanceof Map) && ((Map<?,?>) value).isEmpty())
return true;

return false;
Expand All @@ -456,8 +456,8 @@ public boolean isMap(Object value) {
return value != null && (value instanceof Map);
}

@SuppressWarnings("unchecked")
public Map<String, Object> asMap(Object value) {

return (Map<String, Object>)value;
}
}
1 change: 1 addition & 0 deletions src/main/java/liqp/ParseSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import liqp.filters.Filters;
import liqp.parser.Flavor;

@SuppressWarnings("hiding")
public class ParseSettings {
public static final Flavor DEFAULT_FLAVOR = Flavor.LIQUID;
public static final ParseSettings DEFAULT = DEFAULT_FLAVOR.defaultParseSettings();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/liqp/ProtectionSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import liqp.exceptions.ExceededMaxIterationsException;

@SuppressWarnings("hiding")
public class ProtectionSettings {
public static final ProtectionSettings DEFAULT = new ProtectionSettings.Builder().build();

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/liqp/RenderSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import liqp.parser.LiquidSupport;

@SuppressWarnings("hiding")
public class RenderSettings {
public static final Locale DEFAULT_LOCALE = Locale.ENGLISH;

Expand Down Expand Up @@ -102,7 +103,7 @@ public Builder withLocale(Locale locale){
* that does not have own timezone information.
* May be null, so the timezone pattern will be omitted in formatted strings.
* @param defaultTimeZone - value or <code>null<code/>
* @return
* @return This builder.
*/
public Builder withDefaultTimeZone(ZoneId defaultTimeZone) {
this.defaultTimeZone = defaultTimeZone;
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/liqp/TemplateContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

import liqp.RenderTransformer.ObjectAppender;
import liqp.parser.LiquidSupport;
import liqp.filters.Filters;
import liqp.parser.Flavor;
import liqp.parser.LiquidSupport;

public class TemplateContext {

Expand Down Expand Up @@ -180,9 +180,7 @@ public Map<String, Object> getEnvironmentMap() {
return environmentMap;
}

/**
* The registry is
*/
@SuppressWarnings("unchecked")
public <T extends Map<String, ?>> T getRegistry(String registryName) {
if (parent != null) {
return parent.getRegistry(registryName);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/liqp/TemplateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public Builder withProtectionSettings(ProtectionSettings s) {
return this;
}

@SuppressWarnings("hiding")
public Builder withErrorMode(ErrorMode errorMode) {
this.errorMode = errorMode;
return this;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/liqp/blocks/Block.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package liqp.blocks;

import liqp.Insertion;
import liqp.ParseSettings;

public abstract class Block extends Insertion {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package liqp.exceptions;

public class ExceededMaxIterationsException extends RuntimeException {
private static final long serialVersionUID = -2177965025182952056L;

public ExceededMaxIterationsException(int maxIterations) {
super("exceeded maxIterations: " + maxIterations);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/liqp/exceptions/LiquidException.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.List;

public class LiquidException extends RuntimeException {
private static final long serialVersionUID = -7720091243157735323L;

public final int line;
public final int charPositionInLine;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package liqp.exceptions;


public class VariableNotExistException extends RuntimeException {
private static final long serialVersionUID = -2726673309959828365L;
private final String variableName;

public VariableNotExistException(String variableName) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/liqp/filters/Absolute_Url.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public Object apply(Object value, TemplateContext context, Object... params) {
}
}

public static String convertUnicodeURLToAscii(String url) throws URISyntaxException {
public static String convertUnicodeURLToAscii(@SuppressWarnings("hiding") String url) throws URISyntaxException {
if(url != null) {
url = url.trim();
URI uri = new URI(url);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/liqp/filters/Date.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import ua.co.k.strftime.StrftimeFormatter;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Locale;

Expand All @@ -22,7 +21,7 @@ protected Date() {
super();
}

protected Date(CustomDateFormatSupport typeSupport) {
protected Date(CustomDateFormatSupport<?> typeSupport) {
super();
CustomDateFormatRegistry.add(typeSupport);
}
Expand Down Expand Up @@ -89,7 +88,7 @@ public static void removeDatePattern(String pattern) {
datePatterns.remove(pattern);
}

public static Filter withCustomDateType(CustomDateFormatSupport typeSupport) {
public static Filter withCustomDateType(CustomDateFormatSupport<?> typeSupport) {
return new Date(typeSupport);
}
}
7 changes: 0 additions & 7 deletions src/main/java/liqp/filters/Filter.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package liqp.filters;

import static liqp.ParseSettings.DEFAULT_FLAVOR;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import liqp.LValue;
import liqp.ParseSettings;
import liqp.TemplateContext;
import liqp.TemplateParser;
import liqp.parser.Flavor;

/**
* Output markup takes filters. Filters are simple methods. The first parameter is always the output of
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/liqp/filters/Map.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package liqp.filters;

import java.util.ArrayList;
import java.util.List;

import liqp.TemplateContext;
import liqp.parser.Inspectable;
import liqp.parser.LiquidSupport;

import java.util.ArrayList;
import java.util.List;

public class Map extends Filter {

/*
Expand All @@ -29,12 +29,12 @@ public Object apply(Object value, TemplateContext context, Object... params) {

for (Object obj : array) {

java.util.Map map;
java.util.Map<?,?> map;
if (value instanceof Inspectable) {
LiquidSupport evaluated = context.evaluate(value);
map = evaluated.toLiquid();
} else {
map = (java.util.Map) obj;
map = (java.util.Map<?,?>) obj;
}

Object val = map.get(key);
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/liqp/filters/Modulo.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package liqp.filters;

import liqp.Template;
import liqp.TemplateContext;

import java.math.BigDecimal;

import liqp.TemplateContext;

public class Modulo extends Filter {

/*
Expand Down
31 changes: 18 additions & 13 deletions src/main/java/liqp/filters/Sort.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public Object apply(Object value, TemplateContext context, Object... params) {

array = super.asArray(value, context);

List<Comparable> list = asComparableList(context, array, property);
List<Comparable<Object>> list = asComparableList(context, array, property);

Collections.sort(list);

Expand Down Expand Up @@ -107,9 +107,10 @@ public String toString() {
}
}

private List<Comparable> asComparableList(TemplateContext context, Object[] array, String property) {
@SuppressWarnings("unchecked")
private List<Comparable<Object>> asComparableList(TemplateContext context, Object[] array, String property) {

List<Comparable> list = new ArrayList<Comparable>();
List<Comparable<Object>> list = new ArrayList<Comparable<Object>>();

for (Object obj : array) {

Expand All @@ -119,29 +120,33 @@ private List<Comparable> asComparableList(TemplateContext context, Object[] arra
obj = evaluated.toLiquid();
}
if (obj instanceof java.util.Map && property != null) {
list.add(new SortableMap((java.util.Map<String, Comparable>) obj, property));
list.add(new SortableMap((java.util.Map<String, Comparable<Object>>) obj, property));
} else {
list.add((Comparable) obj);
list.add((Comparable<Object>) obj);
}
}

return list;
}

static class SortableMap extends HashMap<String, Comparable> implements Comparable<SortableMap> {

static class SortableMap extends HashMap<String, Comparable<Object>> implements Comparable<Object> {
private static final long serialVersionUID = -6480701533195197967L;
final String property;

SortableMap(java.util.Map<String, Comparable> map, String property) {
SortableMap(java.util.Map<String, Comparable<Object>> map, String property) {
super.putAll(map);
this.property = property;
}

@Override
public int compareTo(SortableMap that) {

Comparable thisValue = this.get(property);
Comparable thatValue = that.get(property);
public int compareTo(Object that) {
Comparable<Object> thisValue = this.get(property);
Object thatValue;
if(that instanceof SortableMap) {
thatValue = ((SortableMap)that).get(property);
} else {
thatValue = that;
}

if (thisValue == null || thatValue == null) {
throw new RuntimeException("Liquid error: comparison of Hash with Hash failed");
Expand All @@ -154,7 +159,7 @@ public int compareTo(SortableMap that) {
public String toString() {
StringBuilder builder = new StringBuilder();

for (java.util.Map.Entry entry : super.entrySet()) {
for (java.util.Map.Entry<?,?> entry : super.entrySet()) {
builder.append(entry.getKey()).append(entry.getValue());
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/liqp/filters/Sort_Natural.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Object apply(Object value, TemplateContext context, Object... params) {
Object[] array = super.asArray(value, context);
List<Object> list = new ArrayList<Object>(Arrays.asList(array));

Collections.sort(list, new Comparator() {
Collections.sort(list, new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
return String.valueOf(o1).compareToIgnoreCase(String.valueOf(o2));
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/liqp/filters/Times.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import java.math.BigDecimal;

import static java.math.BigDecimal.ROUND_UNNECESSARY;

public class Times extends Filter {

/*
Expand Down
1 change: 0 additions & 1 deletion src/main/java/liqp/filters/Where.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import liqp.filters.where.LiquidWhereImpl;
import liqp.filters.where.PropertyResolverHelper;
import liqp.filters.where.WhereImpl;
import liqp.parser.Flavor;

/**
* There are two different implementations of this filter in ruby.
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/liqp/filters/date/CustomDateFormatRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ public class CustomDateFormatRegistry {
// might be better storage for this will be tree,
// so the subtypes will be properly handled
// and parent type will not override child's one
private static final List<CustomDateFormatSupport> supportedTypes = new ArrayList<>();
private static final List<CustomDateFormatSupport<Object>> supportedTypes = new ArrayList<>();

public static void add(CustomDateFormatSupport supportThis) {
supportedTypes.add(0, supportThis);
@SuppressWarnings("unchecked")
public static void add(CustomDateFormatSupport<?> supportThis) {
supportedTypes.add(0, (CustomDateFormatSupport<Object>)supportThis);
}


Expand All @@ -25,7 +26,7 @@ public static boolean isRegistered(CustomDateFormatSupport<?> typeSupport) {
}

public static boolean isCustomDateType(Object value) {
for (CustomDateFormatSupport el: supportedTypes) {
for (CustomDateFormatSupport<?> el: supportedTypes) {
if (el.support(value)) {
return true;
}
Expand All @@ -34,7 +35,7 @@ public static boolean isCustomDateType(Object value) {
}

public static ZonedDateTime getFromCustomType(Object value) {
for (CustomDateFormatSupport el: supportedTypes) {
for (CustomDateFormatSupport<Object> el: supportedTypes) {
if (el.support(value)) {
return el.getValue(value);
}
Expand Down
Loading

0 comments on commit 949b1f8

Please sign in to comment.