Skip to content
This repository has been archived by the owner on Jan 27, 2025. It is now read-only.

Commit

Permalink
Merge branch '95-bind' into oersi
Browse files Browse the repository at this point in the history
  • Loading branch information
fsteeg committed Dec 22, 2021
2 parents baa8cf2 + e8ac3b5 commit 13962dc
Show file tree
Hide file tree
Showing 3 changed files with 364 additions and 4 deletions.
9 changes: 5 additions & 4 deletions metafix/src/main/java/org/metafacture/metafix/Value.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
Expand Down Expand Up @@ -542,7 +543,7 @@ protected Hash() {
* @return true if this hash contains the metadata field, false otherwise
*/
public boolean containsField(final String field) {
return map.keySet().stream().anyMatch(fieldMatcher(field));
return matchFields(field, Stream::anyMatch);
}

/**
Expand Down Expand Up @@ -901,12 +902,12 @@ private void modifyFields(final String pattern, final Consumer<String> consumer)
}

private Stream<String> findFields(final String pattern) {
return map.keySet().stream().filter(fieldMatcher(pattern));
return matchFields(pattern, Stream::filter);
}

private Predicate<String> fieldMatcher(final String pattern) {
private <T> T matchFields(final String pattern, final BiFunction<Stream<String>, Predicate<String>, T> function) {
trie.put(pattern, pattern);
return field -> trie.get(field).contains(pattern);
return function.apply(map.keySet().stream(), f -> trie.get(f).contains(pattern));
}

}
Expand Down
167 changes: 167 additions & 0 deletions metafix/src/test/java/org/metafacture/metafix/HashValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public class HashValueTest {

private static final String FIELD = "field";
private static final String OTHER_FIELD = "other field";
private static final String ALTERNATE_FIELD = FIELD.replace("e", "i");

private static final String FIELD_CHARACTER_CLASS = FIELD.replace("e", "[aeiou]");
private static final String FIELD_ALTERNATION = FIELD + "|" + ALTERNATE_FIELD;
private static final String FIELD_WILDCARD = FIELD.replace("e", "?");

private static final Value VALUE = new Value("value");
private static final Value OTHER_VALUE = new Value("other value");
Expand Down Expand Up @@ -54,6 +59,30 @@ public void shouldNotContainNullValue() {
Assertions.assertFalse(hash.containsField(FIELD));
}

@Test
public void shouldContainCharacterClassField() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);

Assertions.assertTrue(hash.containsField(FIELD_CHARACTER_CLASS));
}

@Test
public void shouldContainAlternationField() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);

Assertions.assertTrue(hash.containsField(FIELD_ALTERNATION));
}

@Test
public void shouldContainWildcardField() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);

Assertions.assertTrue(hash.containsField(FIELD_WILDCARD));
}

@Test
public void shouldBeEmptyByDefault() {
final Value.Hash hash = newHash();
Expand Down Expand Up @@ -105,6 +134,57 @@ public void shouldGetExistingField() {
Assertions.assertEquals(VALUE, hash.get(FIELD));
}

@Test
public void shouldGetSingleCharacterClassField() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);

Assertions.assertEquals(VALUE, hash.get(FIELD_CHARACTER_CLASS));
}

@Test
public void shouldGetSingleAlternationField() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);

Assertions.assertEquals(VALUE, hash.get(FIELD_ALTERNATION));
}

@Test
public void shouldGetSingleWildcardField() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);

Assertions.assertEquals(VALUE, hash.get(FIELD_WILDCARD));
}

@Test
public void shouldGetMultipleCharacterClassFields() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);
hash.put(ALTERNATE_FIELD, OTHER_VALUE);

assertArray(hash.get(FIELD_CHARACTER_CLASS));
}

@Test
public void shouldGetMultipleAlternationFields() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);
hash.put(ALTERNATE_FIELD, OTHER_VALUE);

assertArray(hash.get(FIELD_ALTERNATION));
}

@Test
public void shouldGetMultipleWildcardFields() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);
hash.put(ALTERNATE_FIELD, OTHER_VALUE);

assertArray(hash.get(FIELD_WILDCARD));
}

@Test
public void shouldNotReplaceMissingField() {
final Value.Hash hash = newHash();
Expand Down Expand Up @@ -151,6 +231,48 @@ public void shouldRemoveExistingField() {
Assertions.assertFalse(hash.containsField(FIELD));
}

@Test
public void shouldRemoveCharacterClassFields() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);
hash.put(ALTERNATE_FIELD, VALUE);
hash.remove(FIELD_CHARACTER_CLASS);

Assertions.assertTrue(hash.isEmpty());
}

@Test
public void shouldRemoveAlternationFields() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);
hash.put(ALTERNATE_FIELD, VALUE);
hash.remove(FIELD_ALTERNATION);

Assertions.assertTrue(hash.isEmpty());
}

@Test
public void shouldRemoveWildcardFields() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);
hash.put(ALTERNATE_FIELD, VALUE);
hash.remove(FIELD_WILDCARD);

Assertions.assertTrue(hash.isEmpty());
}

@Test
public void shouldNotContainRemovedField() {
final Value.Hash hash = newHash();
Assertions.assertFalse(hash.containsField(FIELD));

hash.put(FIELD, VALUE);
Assertions.assertTrue(hash.containsField(FIELD));

hash.remove(FIELD);
Assertions.assertFalse(hash.containsField(FIELD));
}

@Test
public void shouldRetainFields() {
final Value.Hash hash = newHash();
Expand Down Expand Up @@ -184,6 +306,42 @@ public void shouldNotRetainMissingFields() {
Assertions.assertFalse(hash.containsField(OTHER_FIELD));
}

@Test
public void shouldRetainCharacterClassFields() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);
hash.put(OTHER_FIELD, OTHER_VALUE);

hash.retainFields(Arrays.asList(FIELD_CHARACTER_CLASS));

Assertions.assertTrue(hash.containsField(FIELD));
Assertions.assertFalse(hash.containsField(OTHER_FIELD));
}

@Test
public void shouldRetainAlternationFields() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);
hash.put(OTHER_FIELD, OTHER_VALUE);

hash.retainFields(Arrays.asList(FIELD_ALTERNATION));

Assertions.assertTrue(hash.containsField(FIELD));
Assertions.assertFalse(hash.containsField(OTHER_FIELD));
}

@Test
public void shouldRetainWildcardFields() {
final Value.Hash hash = newHash();
hash.put(FIELD, VALUE);
hash.put(OTHER_FIELD, OTHER_VALUE);

hash.retainFields(Arrays.asList(FIELD_WILDCARD));

Assertions.assertTrue(hash.containsField(FIELD));
Assertions.assertFalse(hash.containsField(OTHER_FIELD));
}

@Test
public void shouldRemoveEmptyValues() {
final Value.Hash hash = newHash();
Expand Down Expand Up @@ -217,4 +375,13 @@ private Value.Hash newHash() {
return Value.newHash().asHash();
}

private void assertArray(final Value result) {
Assertions.assertTrue(result.isArray());

final Value.Array array = result.asArray();
Assertions.assertEquals(2, array.size());
Assertions.assertEquals(VALUE, array.get(0));
Assertions.assertEquals(OTHER_VALUE, array.get(1));
}

}
Loading

0 comments on commit 13962dc

Please sign in to comment.