From bbeb33f9fc5912ab2a0fc751d7944dae241349dd Mon Sep 17 00:00:00 2001 From: Thibaud Giovannetti Date: Mon, 9 Oct 2017 10:58:46 +0200 Subject: [PATCH] Add SortingMode as array --- .../store2store/utils/SortingMode.java | 54 +++++++++++++++++-- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/store2store/src/main/java/com/playmoweb/store2store/utils/SortingMode.java b/store2store/src/main/java/com/playmoweb/store2store/utils/SortingMode.java index 0078881..d04f60a 100644 --- a/store2store/src/main/java/com/playmoweb/store2store/utils/SortingMode.java +++ b/store2store/src/main/java/com/playmoweb/store2store/utils/SortingMode.java @@ -1,6 +1,9 @@ package com.playmoweb.store2store.utils; +import java.util.AbstractMap; +import java.util.LinkedList; + /** * Bind a key to a sorting mode * @author Thibaud Giovannetti @@ -9,11 +12,52 @@ */ public final class SortingMode { public static final SortingMode DEFAULT = new SortingMode("id", SortType.ASCENDING); - public final String key; - public final SortType sort; + public final LinkedList> entries = new LinkedList<>(); + + public SortingMode(String key, SortType sortType) { + and(key, sortType); + } + + /** + * Create a list of Sorting rules with the same SortType + */ + public SortingMode(String[] keys, SortType sortType) { + for(String k : keys){ + if(k != null){ + this.and(k, sortType); + } + } + } + + /** + * Create a list of Sorting rules mapped by position in array + * @warn If the arrays have a different size, an exception will be throw + */ + public SortingMode(String[] keys, SortType[] sortTypes) { + if(keys.length != sortTypes.length){ + throw new IllegalArgumentException("The String[] (keys) and the SortType[] arrays must have the same size"); + } + + for (int i = 0; i < keys.length; i++) { + if(keys[i] != null && sortTypes[i] != null){ + and(keys[i], sortTypes[i]); + } + } + } + + /** + * Add a sort rule + * @note This rule is always added at the end and will apply after previous ones. + */ + public SortingMode and(String key, SortType sortType){ + entries.add(new AbstractMap.SimpleEntry<>(key, sortType)); + return this; + } - public SortingMode(String k, SortType s) { - key = k; - sort = s; + /** + * Build a new SortingMode object to allow static chaining + */ + public static SortingMode with(String key, SortType sortType){ + return new SortingMode(key, sortType); } }