diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2022-02-13 17:01:56 -0800 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2022-02-13 17:01:56 -0800 |
commit | 79730c0bf5d5fbe83204e52e73b4bf47964c5a2d (patch) | |
tree | fd6dfb4ee6408d086e9bd60463a314c732766c4f /src/main/java/org/traccar/storage/query/Columns.java | |
parent | 6ac6520eb0c96dcd690a5bd777a275673c7cd6e3 (diff) | |
download | trackermap-server-79730c0bf5d5fbe83204e52e73b4bf47964c5a2d.tar.gz trackermap-server-79730c0bf5d5fbe83204e52e73b4bf47964c5a2d.tar.bz2 trackermap-server-79730c0bf5d5fbe83204e52e73b4bf47964c5a2d.zip |
Implement new storage framework
Diffstat (limited to 'src/main/java/org/traccar/storage/query/Columns.java')
-rw-r--r-- | src/main/java/org/traccar/storage/query/Columns.java | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/main/java/org/traccar/storage/query/Columns.java b/src/main/java/org/traccar/storage/query/Columns.java new file mode 100644 index 000000000..2053c556f --- /dev/null +++ b/src/main/java/org/traccar/storage/query/Columns.java @@ -0,0 +1,63 @@ +package org.traccar.storage.query; + +import org.traccar.storage.QueryIgnore; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +abstract public class Columns { + + abstract public List<String> getColumns(Class<?> clazz); + + protected List<String> getAllColumns(Class<?> clazz) { + List<String> columns = new LinkedList<>(); + Method[] methods = clazz.getMethods(); + for (Method method : methods) { + if (method.getName().startsWith("set") && method.getParameterTypes().length == 1 + && !method.isAnnotationPresent(QueryIgnore.class)) { + columns.add(method.getName().substring(3).toLowerCase()); + } + } + return columns; + } + + public static class All extends Columns { + @Override + public List<String> getColumns(Class<?> clazz) { + return getAllColumns(clazz); + } + } + + public static class Include extends Columns { + private final List<String> columns; + + public Include(String... columns) { + this.columns = Arrays.stream(columns).collect(Collectors.toList()); + } + + @Override + public List<String> getColumns(Class<?> clazz) { + return columns; + } + } + + public static class Exclude extends Columns { + private final Set<String> columns; + + public Exclude(String... columns) { + this.columns = Arrays.stream(columns).collect(Collectors.toSet()); + } + + @Override + public List<String> getColumns(Class<?> clazz) { + return getAllColumns(clazz).stream() + .filter(column -> !columns.contains(column)) + .collect(Collectors.toList()); + } + } + +} |