Skip to content

Commit e437ab9

Browse files
author
gustavnavar
committed
Add column support for Short
1 parent f0ba4b4 commit e437ab9

3 files changed

Lines changed: 47 additions & 4 deletions

File tree

gridcore/src/main/java/me/agno/gridcore/filtering/types/FilterTypeResolver.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public FilterTypeResolver()
2828
filterCollection.add(new BigDecimalFilterType());
2929
filterCollection.add(new BigIntegerFilterType());
3030
filterCollection.add(new IntegerFilterType());
31+
filterCollection.add(new ShortFilterType());
3132
filterCollection.add(new DoubleFilterType());
3233
filterCollection.add(new LongFilterType());
3334
filterCollection.add(new FloatFilterType());
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package me.agno.gridcore.filtering.types;
2+
3+
import jakarta.persistence.criteria.CriteriaBuilder;
4+
import jakarta.persistence.criteria.Predicate;
5+
import jakarta.persistence.criteria.Root;
6+
import lombok.Getter;
7+
import me.agno.gridcore.filtering.GridFilterType;
8+
9+
@Getter
10+
public final class ShortFilterType<T> extends FilterTypeBase<T, Short> {
11+
12+
private final Class<Short> targetType = Short.class;
13+
14+
public GridFilterType getValidType(GridFilterType type) {
15+
return switch (type) {
16+
case EQUALS, NOT_EQUALS, GREATER_THAN, GREATER_THAN_OR_EQUALS, LESS_THAN, LESS_THAN_OR_EQUALS -> type;
17+
default -> GridFilterType.EQUALS;
18+
};
19+
}
20+
21+
public Short getTypedValue(String value) { return Short.valueOf(value); }
22+
23+
public Predicate getFilterExpression(CriteriaBuilder cb, Root<T> root, String expression, String value,
24+
GridFilterType filterType, String removeDiacritics) {
25+
//base implementation of building filter expressions
26+
filterType = getValidType(filterType);
27+
28+
Short typedValue = getTypedValue(value);
29+
30+
var path = getPath(root, expression);
31+
32+
return switch (filterType) {
33+
case EQUALS -> cb.equal(path, typedValue);
34+
case NOT_EQUALS -> cb.notEqual(path, typedValue);
35+
case LESS_THAN -> cb.lt(path, typedValue);
36+
case LESS_THAN_OR_EQUALS -> cb.le(path, typedValue);
37+
case GREATER_THAN -> cb.gt(path, typedValue);
38+
case GREATER_THAN_OR_EQUALS -> cb.ge(path, typedValue);
39+
default -> throw new IllegalArgumentException();
40+
};
41+
}
42+
}

gridcore/src/main/java/me/agno/gridcore/totals/TotalsProcessor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ public void process(Predicate predicate) {
6767
// LocalDate, LocalDateTime and ZonedDateTime are not Comparable
6868

6969
if (type == Byte.class || type == BigDecimal.class || type == BigInteger.class ||
70-
type == Integer.class || type == Double.class || type == Long.class ||
71-
type == Float.class) {
70+
type == Integer.class || type == Short.class || type == Double.class ||
71+
type == Long.class || type == Float.class) {
7272

7373
if (gridColumn.isSumEnabled())
7474
gridColumn.setSumValue(new Total(getSum(expression, this.grid)));
@@ -170,8 +170,8 @@ else if (type == String.class) {
170170
Class<?> type = value.getClass();
171171

172172
if (type == Byte.class || type == BigDecimal.class || type == BigInteger.class ||
173-
type == Integer.class || type == Double.class || type == Long.class ||
174-
type == Float.class) {
173+
type == Integer.class || type == Short.class || type == Double.class ||
174+
type == Long.class || type == Float.class) {
175175
gridColumn.getCalculationValues().put(key, new Total((Number) value));
176176
}
177177
else if (type == LocalDateTime.class) {

0 commit comments

Comments
 (0)