|
|
@@ -0,0 +1,194 @@
|
|
|
+/*
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ * @copyright Ranides Atterwim
|
|
|
+ * @license WTFPL
|
|
|
+ * @url http://ranides.net/projects/assira.drafts
|
|
|
+ */
|
|
|
+package net.ranides.assira.collections.map.impl;
|
|
|
+
|
|
|
+import net.ranides.assira.collections.map.IndexMap;
|
|
|
+import net.ranides.assira.generic.CompareUtils;
|
|
|
+import org.junit.Test;
|
|
|
+import static org.junit.Assert.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @author Ranides Atterwim <ranides@gmail.com>
|
|
|
+ */
|
|
|
+public class CIndexTreeMapTest {
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testBasic() {
|
|
|
+
|
|
|
+ IndexMap<Person> db = new CIndexTreeMap<>();
|
|
|
+ db.index("name", (a,b) -> CompareUtils.cmp(a.name, b.name));
|
|
|
+ db.index("surname", (a,b) -> CompareUtils.cmp(a.surname, b.surname));
|
|
|
+ db.index("age", (a,b) -> CompareUtils.cmp(a.age, b.age));
|
|
|
+
|
|
|
+ db.put(new Person("Tom", "A", 17));
|
|
|
+ db.put(new Person("Tom", "B", 23));
|
|
|
+ db.put(new Person("Joe", "A", 20));
|
|
|
+ db.put(new Person("Helene", "C", 19));
|
|
|
+ db.put(new Person("Andrew", "S", 23));
|
|
|
+ db.put(new Person("Helene", "C", 23));
|
|
|
+ db.put(new Person("George", "T", 17));
|
|
|
+ db.put(new Person("Peter", "A", 17));
|
|
|
+
|
|
|
+ System.out.printf("RESULT = %s%n", db.find()
|
|
|
+ .eq("name", new Person("Helene", "", 0))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ );
|
|
|
+ System.out.printf("RESULT = %s%n", db.find()
|
|
|
+ .eq("age", new Person("", "", 23))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ );
|
|
|
+ System.out.printf("RESULT = %s%n", db.find()
|
|
|
+ .eq("name", new Person("Helene", "", 0))
|
|
|
+ .eq("age", new Person("", "", 23))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ );
|
|
|
+ System.out.printf("RESULT = %s%n", db.find()
|
|
|
+ .eq("age", new Person("", "", 17))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ );
|
|
|
+ System.out.printf("RESULT = %s%n", db.find()
|
|
|
+ .eq("surname", new Person("", "A", 3))
|
|
|
+ .eq("age", new Person("", "", 17))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ );
|
|
|
+ System.out.printf("RESULT = %s%n", db.find()
|
|
|
+ .gt("age", new Person("", "", 17))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ );
|
|
|
+ System.out.printf("RESULT = %s%n", db.find()
|
|
|
+ .gt("age", new Person("", "", 40))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ );
|
|
|
+ System.out.printf("RESULT = %s%n", db.find()
|
|
|
+ .lt("age", new Person("", "", 18))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ );
|
|
|
+ System.out.printf("RESULT = %s%n", db.find()
|
|
|
+ .lt("age", new Person("", "", 17))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ );
|
|
|
+ System.out.printf("RESULT = %s%n", db.find()
|
|
|
+ .eq("name", new Person("Helene", "", 23))
|
|
|
+ .lt("age", new Person("Helene", "", 23))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ );
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ assertEquals("[{Helene C 19}, {Helene C 23}]", db.find()
|
|
|
+ .eq("name", new Person("Helene", "", 0))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ .toString()
|
|
|
+ );
|
|
|
+ assertEquals("[{Tom B 23}, {Helene C 23}, {Andrew S 23}]", db.find()
|
|
|
+ .eq("age", new Person("", "", 23))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ .toString()
|
|
|
+ );
|
|
|
+ assertEquals("[{Helene C 23}]", db.find()
|
|
|
+ .eq("name", new Person("Helene", "", 0))
|
|
|
+ .eq("age", new Person("", "", 23))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ .toString()
|
|
|
+ );
|
|
|
+ assertEquals("[{Peter A 17}, {Tom A 17}, {George T 17}]", db.find()
|
|
|
+ .eq("age", new Person("", "", 17))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ .toString()
|
|
|
+ );
|
|
|
+ assertEquals("[{Peter A 17}, {Tom A 17}]", db.find()
|
|
|
+ .eq("surname", new Person("", "A", 0))
|
|
|
+ .eq("age", new Person("", "", 17))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ .toString()
|
|
|
+ );
|
|
|
+ assertEquals("[{Joe A 20}, {Tom B 23}, {Helene C 19}, {Helene C 23}, {Andrew S 23}]", db.find()
|
|
|
+ .gt("age", new Person("", "", 17))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ .toString()
|
|
|
+ );
|
|
|
+ assertEquals("[]", db.find()
|
|
|
+ .gt("age", new Person("", "", 40))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ .toString()
|
|
|
+ );
|
|
|
+ assertEquals("[{Peter A 17}, {Tom A 17}, {George T 17}]", db.find()
|
|
|
+ .lt("age", new Person("", "", 18))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ .toString()
|
|
|
+ );
|
|
|
+ assertEquals("[]", db.find()
|
|
|
+ .lt("age", new Person("", "", 17))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ .toString()
|
|
|
+ );
|
|
|
+ assertEquals("[{Helene C 19}]", db.find()
|
|
|
+ .eq("name", new Person("Helene", "", 23))
|
|
|
+ .lt("age", new Person("Helene", "", 23))
|
|
|
+ .sort()
|
|
|
+ .list()
|
|
|
+ .toString()
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ private static final class Person implements Comparable<Person> {
|
|
|
+
|
|
|
+ public final String name;
|
|
|
+ public final String surname;
|
|
|
+ public final int age;
|
|
|
+
|
|
|
+ public Person(String name, String surname, int age) {
|
|
|
+ this.name = name;
|
|
|
+ this.surname = surname;
|
|
|
+ this.age = age;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return "{" + name + " " + surname + " " + age + "}";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int compareTo(Person that) {
|
|
|
+ int c;
|
|
|
+ c = CompareUtils.cmp(this.surname, that.surname);
|
|
|
+ if(c!=0) {
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+ c = CompareUtils.cmp(this.name, that.name);
|
|
|
+ if(c!=0) {
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+ c = CompareUtils.cmp(this.age, that.age);
|
|
|
+ return c;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|