Ranides Atterwim 10 年 前
コミット
c2de81595a

+ 41 - 41
assira/src/main/java/net/ranides/assira/collection/HashComparator.java

@@ -1,41 +1,41 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.collection;
-
-import java.io.Serializable;
-import java.util.Comparator;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- * @param <Q>
- */
-public interface HashComparator<Q> extends HashFunction<Q>, Comparator<Q>, Serializable {
-
-    @Override
-    default boolean equals(Q value1, Q value2) {
-        return 0 == compare(value1, value2);
-    }
-    
-    @Override
-    default HashComparator<Q> reversed() {
-        HashComparator<Q> that = this;
-        return new HashComparator<Q>() {
-            @Override
-            public int compare(Q a, Q b) {
-                return that.compare(b, a);
-            }
-
-            @Override
-            public int hashCode(Q object) {
-                return that.hashCode(object);
-            }
-
-        };
-    }
-    
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.collection;
+
+import java.io.Serializable;
+import java.util.Comparator;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @param <Q>
+ */
+public interface HashComparator<Q> extends HashFunction<Q>, Comparator<Q>, Serializable {
+
+    @Override
+    default boolean equals(Q a, Q b) {
+        return 0 == compare(a, b);
+    }
+    
+    @Override
+    default HashComparator<Q> reversed() {
+        HashComparator<Q> that = this;
+        return new HashComparator<Q>() {
+            @Override
+            public int compare(Q a, Q b) {
+                return that.compare(b, a);
+            }
+
+            @Override
+            public int hashCode(Q object) {
+                return that.hashCode(object);
+            }
+
+        };
+    }
+    
+}

+ 1 - 1
assira/src/main/java/net/ranides/assira/collection/HashFunction.java

@@ -10,5 +10,5 @@ public interface HashFunction<K> {
 
     int hashCode(K object);
 
-    boolean equals(K value1, K value2);
+    boolean equals(K a, K b);
 }

+ 46 - 46
assira/src/main/java/net/ranides/assira/collection/HashIntComparator.java

@@ -1,46 +1,46 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.collection;
-
-import java.io.Serializable;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public interface HashIntComparator extends HashComparator<Integer>, IntComparator, Serializable {
-
-    int hashCode(int object);
-        
-    @Override
-    default int hashCode(Integer object) {
-        return hashCode(object.intValue());
-    }
-    
-    @Override
-    default boolean equals(Integer value1, Integer value2) {
-        return 0 == compare(value1, value2);
-    }
-    
-    @Override
-    default HashIntComparator reversed() {
-        HashIntComparator that = this;
-        return new HashIntComparator() {
-            @Override
-            public int compare(int a, int b) {
-                return that.compare(b, a);
-            }
-
-            @Override
-            public int hashCode(int object) {
-                return that.hashCode(object);
-            }
-
-        };
-    }
-    
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.collection;
+
+import java.io.Serializable;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public interface HashIntComparator extends HashComparator<Integer>, IntComparator, Serializable {
+
+    int hashCode(int object);
+        
+    @Override
+    default int hashCode(Integer object) {
+        return hashCode(object.intValue());
+    }
+    
+    @Override
+    default boolean equals(Integer a, Integer b) {
+        return 0 == compare(a, b);
+    }
+    
+    @Override
+    default HashIntComparator reversed() {
+        HashIntComparator that = this;
+        return new HashIntComparator() {
+            @Override
+            public int compare(int a, int b) {
+                return that.compare(b, a);
+            }
+
+            @Override
+            public int hashCode(int object) {
+                return that.hashCode(object);
+            }
+
+        };
+    }
+    
+}

+ 8 - 2
assira/src/test/java/net/ranides/assira/collection/ACollectionTest.java

@@ -34,8 +34,14 @@ public class ACollectionTest {
     
     @Test
     public void testToString() {
-        MCollection<TPoint> target = new MCollection<>($var.list(1,2,3,4,5));
-        assertEquals("[(1:1:10), (2:2:20), (3:3:30), (4:4:40), (5:5:50)]", target.toString());
+        MCollection<TPoint> target1 = new MCollection<>($var.list(1,2,3,4,5));
+        assertEquals("[(1:1:10), (2:2:20), (3:3:30), (4:4:40), (5:5:50)]", target1.toString());
+		
+		MCollection<TPoint> target2 = new MCollection<>($var.list(1));
+        assertEquals("[(1:1:10)]", target2.toString());
+		
+		MCollection<TPoint> target3 = new MCollection<>($var.list());
+        assertEquals("[]", target3.toString());
     }
     
     private static final class MCollection<T> extends ACollection<T> {

+ 71 - 65
assira/src/test/java/net/ranides/assira/collection/AIntCollectionTest.java

@@ -1,65 +1,71 @@
-/*
- * @author Ranides Atterwim <ranides@gmail.com>
- * @copyright Ranides Atterwim
- * @license WTFPL
- * @url http://ranides.net/projects/assira
- */
-package net.ranides.assira.collection;
-
-import java.util.ArrayList;
-import java.util.List;
-import net.ranides.assira.collection.iterators.IntIterator;
-import net.ranides.assira.collection.mockup.CollectionSuite;
-import net.ranides.assira.collection.mockup.TMaps;
-import net.ranides.assira.test.TCollection;
-import static org.junit.Assert.*;
-import org.junit.Test;
-
-/**
- *
- * @author Ranides Atterwim <ranides@gmail.com>
- */
-public class AIntCollectionTest {
-    
-    private final TCollection<Integer> $var = TMaps.MAP_IS.keys();
-
-    @Test
-    public void testSuite() {
-        CollectionSuite.SUITE
-			.param("collection!", $var)
-			.param("nest!", true)
-			.run((array) -> new TIntCollection($var.list(array)));
-    }
-    
-    @Test
-    public void testToString() {
-        TIntCollection target = new TIntCollection($var.list(1,2,3,4,5));
-        assertEquals("[1, 2, 3, 4, 5]", target.toString());
-    }
-    
-    private static final class TIntCollection extends AIntCollection {
-        
-        private final List<Integer> list;
-
-        public TIntCollection(TCollection.TItems<Integer> list) {
-            this.list = list.into(new ArrayList<>());
-        }
-
-        @Override
-        public boolean add(int k) {
-            return list.add(k);
-        }
-        
-        @Override
-        public int size() {
-            return list.size();
-        }
-
-        @Override
-        public IntIterator iterator() {
-            return IntIterator.wrap(list.iterator());
-        }
-        
-    }
-    
-}
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.collection;
+
+import java.util.ArrayList;
+import java.util.List;
+import net.ranides.assira.collection.iterators.IntIterator;
+import net.ranides.assira.collection.mockup.CollectionSuite;
+import net.ranides.assira.collection.mockup.TMaps;
+import net.ranides.assira.test.TCollection;
+import static org.junit.Assert.*;
+import org.junit.Test;
+
+/**
+ *
+ * @author Ranides Atterwim <ranides@gmail.com>
+ */
+public class AIntCollectionTest {
+    
+    private final TCollection<Integer> $var = TMaps.MAP_IS.keys();
+
+    @Test
+    public void testSuite() {
+        CollectionSuite.SUITE
+			.param("collection!", $var)
+			.param("nest!", true)
+			.run((array) -> new TIntCollection($var.list(array)));
+    }
+    
+    @Test
+    public void testToString() {
+        TIntCollection target1 = new TIntCollection($var.list(1,2,3,4,5));
+        assertEquals("[1, 2, 3, 4, 5]", target1.toString());
+		
+		TIntCollection target2 = new TIntCollection($var.list(1));
+        assertEquals("[1]", target2.toString());
+		
+		TIntCollection target3 = new TIntCollection($var.list());
+        assertEquals("[]", target3.toString());
+    }
+    
+    private static final class TIntCollection extends AIntCollection {
+        
+        private final List<Integer> list;
+
+        public TIntCollection(TCollection.TItems<Integer> list) {
+            this.list = list.into(new ArrayList<>());
+        }
+
+        @Override
+        public boolean add(int k) {
+            return list.add(k);
+        }
+        
+        @Override
+        public int size() {
+            return list.size();
+        }
+
+        @Override
+        public IntIterator iterator() {
+            return IntIterator.wrap(list.iterator());
+        }
+        
+    }
+    
+}

+ 69 - 0
assira/src/test/java/net/ranides/assira/collection/HashComparatorTest.java

@@ -0,0 +1,69 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.collection;
+
+import java.util.Arrays;
+import java.util.Locale;
+import java.util.Set;
+import java.util.TreeSet;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author msieron
+ */
+public class HashComparatorTest {
+	
+	private static final HashComparator<String> CMP = new HashComparator<String>() {
+
+		@Override
+		public int hashCode(String object) {
+			return object.toUpperCase(Locale.ROOT).hashCode();
+		}
+
+		@Override
+		public int compare(String o1, String o2) {
+			return o1.toUpperCase(Locale.ROOT).compareTo(o2.toUpperCase(Locale.ROOT));
+		}
+		
+	};
+	
+	private static final HashComparator<String> RCMP = CMP.reversed();
+
+	@Test
+	public void testCompare() {
+		Set<String> set1 = new TreeSet<>(CMP);
+		set1.addAll(Arrays.asList("a", "B", "c", "D"));
+		
+		Set<String> set2 = new TreeSet<>(RCMP);
+		set2.addAll(Arrays.asList("a", "B", "c", "D"));
+		
+		Set<String> set3 = new TreeSet<>();
+		set3.addAll(Arrays.asList("a", "B", "c", "D"));
+
+		assertEquals("[a, B, c, D]", set1.toString());
+		assertEquals("[D, c, B, a]", set2.toString());
+		assertEquals("[B, D, a, c]", set3.toString());
+	}
+	
+	@Test
+	public void testHash() {
+		assertTrue(CMP.hashCode("A") == CMP.hashCode("a"));
+		assertTrue(CMP.equals("A","a"));
+		
+		assertFalse(CMP.hashCode("B") == CMP.hashCode("a"));
+		assertFalse(CMP.equals("B","a"));
+		
+		assertTrue(RCMP.hashCode("A") == RCMP.hashCode("a"));
+		assertTrue(RCMP.equals("A","a"));
+		
+		assertFalse(RCMP.hashCode("B") == RCMP.hashCode("a"));
+		assertFalse(RCMP.equals("B","a"));
+	}
+	
+}

+ 70 - 0
assira/src/test/java/net/ranides/assira/collection/HashIntComparatorTest.java

@@ -0,0 +1,70 @@
+/*
+ * @author Ranides Atterwim <ranides@gmail.com>
+ * @copyright Ranides Atterwim
+ * @license WTFPL
+ * @url http://ranides.net/projects/assira
+ */
+package net.ranides.assira.collection;
+
+import java.util.Arrays;
+import java.util.Locale;
+import java.util.Set;
+import java.util.TreeSet;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author msieron
+ */
+public class HashIntComparatorTest {
+	
+	private static final HashIntComparator CMP = new HashIntComparator() {
+
+		@Override
+		public int hashCode(int object) {
+			return object % 10;
+		}
+
+		@Override
+		public int compare(int a, int b) {
+			return Integer.compare(a%10, b%10);
+		}
+		
+	};
+	
+	private static final HashIntComparator RCMP = CMP.reversed();
+
+	@Test
+	public void testCompare() {
+		Set<Integer> set1 = new TreeSet<>(CMP);
+		set1.addAll(Arrays.asList(21, 12, 23, 14));
+		
+		Set<Integer> set2 = new TreeSet<>(RCMP);
+		set2.addAll(Arrays.asList(21, 12, 23, 14));
+		
+		Set<Integer> set3 = new TreeSet<>();
+		set3.addAll(Arrays.asList(21, 12, 23, 14));
+
+		assertEquals("[21, 12, 23, 14]", set1.toString());
+		assertEquals("[14, 23, 12, 21]", set2.toString());
+		assertEquals("[12, 14, 21, 23]", set3.toString());
+	}
+	
+	@Test
+	public void testHash() {
+		assertTrue(CMP.hashCode((Integer)23) == CMP.hashCode((Integer)33));
+		assertTrue(CMP.equals(23,33));
+		
+		assertFalse(CMP.hashCode((Integer)23) == CMP.hashCode((Integer)24));
+		assertFalse(CMP.equals(23,24));
+		
+		assertTrue(RCMP.hashCode((Integer)23) == RCMP.hashCode((Integer)33));
+		assertTrue(RCMP.equals(23,33));
+		
+		assertFalse(RCMP.hashCode((Integer)23) == RCMP.hashCode((Integer)24));
+		assertFalse(RCMP.equals(23,24));
+		
+	}
+	
+}