Skip to content

Commit

Permalink
Expose equate() on filtered types so...
Browse files Browse the repository at this point in the history
... Their rules can be reused.
  • Loading branch information
tommyettinger committed Dec 6, 2023
1 parent 72b083d commit cb99cdf
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,18 @@ protected int place (Object item) {
return super.place(item);
}

/**
* Compares two objects for equality by the rules this filtered data structure uses for keys.
* This will return true if the arguments are reference-equivalent or both null. Otherwise, it
* requires that both are {@link Iterable}s and compares them using the {@link #getFilter() filter}
* and {@link #getEditor() editor} of this object.
*
* @param left must be non-null; typically a key being compared, but not necessarily
* @param right may be null; typically a key being compared, but can often be null for an empty key slot, or some other type
* @return true if left and right are equivalent according to the rules this filtered type uses
*/
@Override
protected boolean equate (Object left, @Nullable Object right) {
public boolean equate (Object left, @Nullable Object right) {
if (left == right)
return true;
if(right == null) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,18 @@ protected int place (Object item) {
return super.place(item);
}

/**
* Compares two objects for equality by the rules this filtered data structure uses for keys.
* This will return true if the arguments are reference-equivalent or both null. Otherwise, it
* requires that both are {@link Iterable}s and compares them using the {@link #getFilter() filter}
* and {@link #getEditor() editor} of this object.
*
* @param left must be non-null; typically a key being compared, but not necessarily
* @param right may be null; typically a key being compared, but can often be null for an empty key slot, or some other type
* @return true if left and right are equivalent according to the rules this filtered type uses
*/
@Override
protected boolean equate (Object left, @Nullable Object right) {
public boolean equate (Object left, @Nullable Object right) {
if (left == right)
return true;
if(right == null) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,18 @@ protected int place (Object item) {
return super.place(item);
}

/**
* Compares two objects for equality by the rules this filtered data structure uses for keys.
* This will return true if the arguments are reference-equivalent or both null. Otherwise, it
* requires that both are {@link Iterable}s and compares them using the {@link #getFilter() filter}
* and {@link #getEditor() editor} of this object.
*
* @param left must be non-null; typically a key being compared, but not necessarily
* @param right may be null; typically a key being compared, but can often be null for an empty key slot, or some other type
* @return true if left and right are equivalent according to the rules this filtered type uses
*/
@Override
protected boolean equate (Object left, @Nullable Object right) {
public boolean equate (Object left, @Nullable Object right) {
if (left == right)
return true;
if(right == null) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,18 @@ protected int place (Object item) {
return super.place(item);
}

/**
* Compares two objects for equality by the rules this filtered data structure uses for keys.
* This will return true if the arguments are reference-equivalent or both null. Otherwise, it
* requires that both are {@link Iterable}s and compares them using the {@link #getFilter() filter}
* and {@link #getEditor() editor} of this object.
*
* @param left must be non-null; typically a key being compared, but not necessarily
* @param right may be null; typically a key being compared, but can often be null for an empty key slot, or some other type
* @return true if left and right are equivalent according to the rules this filtered type uses
*/
@Override
protected boolean equate (Object left, @Nullable Object right) {
public boolean equate (Object left, @Nullable Object right) {
if (left == right)
return true;
if(right == null) return false;
Expand Down
13 changes: 11 additions & 2 deletions src/main/java/com/github/tommyettinger/ds/FilteredStringMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,18 @@ protected int place (Object item) {
}
return super.place(item);
}

/**
* Compares two objects for equality by the rules this filtered data structure uses for keys.
* This will return true if the arguments are reference-equivalent or both null. Otherwise, it
* requires that both are {@link String}s and compares them using the {@link #getFilter() filter}
* and {@link #getEditor() editor} of this object.
*
* @param left must be non-null; typically a key being compared, but not necessarily
* @param right may be null; typically a key being compared, but can often be null for an empty key slot, or some other type
* @return true if left and right are equivalent according to the rules this filtered type uses
*/
@Override
protected boolean equate (Object left, @Nullable Object right) {
public boolean equate (Object left, @Nullable Object right) {
if (left == right)
return true;
if (right == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,18 @@ protected int place (Object item) {
return super.place(item);
}

/**
* Compares two objects for equality by the rules this filtered data structure uses for keys.
* This will return true if the arguments are reference-equivalent or both null. Otherwise, it
* requires that both are {@link String}s and compares them using the {@link #getFilter() filter}
* and {@link #getEditor() editor} of this object.
*
* @param left must be non-null; typically a key being compared, but not necessarily
* @param right may be null; typically a key being compared, but can often be null for an empty key slot, or some other type
* @return true if left and right are equivalent according to the rules this filtered type uses
*/
@Override
protected boolean equate (Object left, @Nullable Object right) {
public boolean equate (Object left, @Nullable Object right) {
if (left == right)
return true;
if (right == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,18 @@ protected int place (Object item) {
return super.place(item);
}

/**
* Compares two objects for equality by the rules this filtered data structure uses for keys.
* This will return true if the arguments are reference-equivalent or both null. Otherwise, it
* requires that both are {@link String}s and compares them using the {@link #getFilter() filter}
* and {@link #getEditor() editor} of this object.
*
* @param left must be non-null; typically a key being compared, but not necessarily
* @param right may be null; typically a key being compared, but can often be null for an empty key slot, or some other type
* @return true if left and right are equivalent according to the rules this filtered type uses
*/
@Override
protected boolean equate (Object left, @Nullable Object right) {
public boolean equate (Object left, @Nullable Object right) {
if (left == right)
return true;
if(right == null) return false;
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/github/tommyettinger/ds/FilteredStringSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,19 @@ protected int place (Object item) {
return super.place(item);
}

/**
* Compares two objects for equality by the rules this filtered data structure uses for keys.
* This will return true if the arguments are reference-equivalent or both null. Otherwise, it
* requires that both are {@link String}s and compares them using the {@link #getFilter() filter}
* and {@link #getEditor() editor} of this object.
*
* @param left must be non-null; typically a key being compared, but not necessarily
* @param right may be null; typically a key being compared, but can often be null for an empty key slot, or some other type
* @return true if left and right are equivalent according to the rules this filtered type uses
*/
@Override
protected boolean equate (Object left, @Nullable Object right) {
public boolean equate (Object left, @Nullable Object right) {

if (left == right)
return true;
if(right == null) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public void testOrderedMap () {
Assert.assertEquals(ent.getKey(), ent.getValue());
}
FilteredStringOrderedMap<String> fil = new FilteredStringOrderedMap<>(Character::isLetter, Character::toUpperCase,
new String[]{"foo", "bar", "baz"},
new String[]{"foo42", "bar666", "baz9001"},
new String[]{"foo", "bar", "baz"}
);
for(Map.Entry<String, String> ent : fil){
Assert.assertEquals(ent.getKey(), ent.getValue());
Assert.assertTrue(fil.equate(ent.getKey(), ent.getValue()));
}
CaseInsensitiveOrderedMap<ObjectList<String>> synonyms = CaseInsensitiveOrderedMap.with(
"intelligence", ObjectList.with("cunning", "acumen", "wits", "wisdom", "intellect"),
Expand Down
39 changes: 38 additions & 1 deletion src/test/java/com/github/tommyettinger/ds/test/FilteredTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

package com.github.tommyettinger.ds.test;

import com.github.tommyettinger.ds.FilteredIterableMap;
import com.github.tommyettinger.ds.FilteredIterableSet;
import com.github.tommyettinger.ds.ObjectDeque;
import com.github.tommyettinger.ds.ObjectList;
import org.junit.Assert;
import org.junit.Test;
Expand All @@ -26,12 +28,47 @@ public class FilteredTest {
@Test
public void testIterableSet() {
FilteredIterableSet<String, Iterable<String>> fil = FilteredIterableSet.with(
(String s) -> s.length() > 3, String::toUpperCase,
ObjectList.with("zzz", "bee", "binturong"),
ObjectDeque.with("hm?", "bee", "BINTURONG"),
ObjectList.with(":D", "bee", "Aardvark", "bandicoot")
);
Assert.assertEquals(2, fil.size());
}

/**
* This is a little odd... the key isn't updated but the value is... The keys are considered equivalent, though.
*/
@Test
public void testIterableMap() {
FilteredIterableMap<String, Iterable<String>, Integer> fil = FilteredIterableMap.with(
(String s) -> s.length() > 3, String::toUpperCase,
ObjectList.with("zzz", "bee", "binturong"), -1,
ObjectDeque.with("hm?", "bee", "BINTURONG"), 1,
ObjectList.with(":D", "bee", "Aardvark", "bandicoot"), 2
);
System.out.println(fil);
Assert.assertEquals(2, fil.size());
}
@Test
public void testIterableSetSubtype() {
FilteredIterableSet<String, ObjectList<String>> fil = FilteredIterableSet.with(
(String s) -> s.length() > 3, String::toUpperCase,
ObjectList.with("zzz", "bee", "binturong"),
ObjectList.with("hm?", "bee", "BINTURONG"),
ObjectList.with(":D", "bee", "Aardvark", "bandicoot")
);
// System.out.println(fil);
Assert.assertEquals(2, fil.size());
}
@Test
public void testIterableMapSubtype() {
FilteredIterableMap<String, ObjectList<String>, Integer> fil = FilteredIterableMap.with(
(String s) -> s.length() > 3, String::toUpperCase,
ObjectList.with("zzz", "bee", "binturong"), -1,
ObjectList.with("hm?", "bee", "BINTURONG"), 1,
ObjectList.with(":D", "bee", "Aardvark", "bandicoot"), 2
);
System.out.println(fil);
Assert.assertEquals(2, fil.size());
}
}

0 comments on commit cb99cdf

Please sign in to comment.