/** * Add a new match to the repository * * @param match The regex to use to match against * @param value the value to hold against the match */ @Override public void addMatch(final String match, final T value, final boolean immutableMatch) { addMatch(match, value, immutableMatch, true); }
@Override public void addMatch(final String match, final T value) { addMatch(match, value, false); }
@Override public void swap(Set<Map.Entry<String, T>> entries) { lock.writeLock().lock(); try { clearCache(); immutables.clear(); matches.clear(); for (Map.Entry<String, T> entry : entries) { addMatch(entry.getKey(), entry.getValue(), true, false); } } finally { lock.writeLock().unlock(); } onChange(); }
@Test public void testMatchingDocs() throws Throwable { HierarchicalObjectRepository<String> repo = new HierarchicalObjectRepository<>(); repo.addMatch("a.b.#", "ab#"); repo.addMatch("a.b.d.#", "abd#"); repo.addMatch("#", "root"); Assert.assertEquals("ab#", repo.getMatch("a.b")); Assert.assertEquals("ab#", repo.getMatch("a.b.c")); Assert.assertEquals("abd#", repo.getMatch("a.b.d.lll")); Assert.assertEquals("root", repo.getMatch("z.z.z.z.z")); Assert.assertEquals("root", repo.getMatch("a.babc")); Assert.assertEquals("ab#", repo.getMatch("a.b.dabc")); Assert.assertEquals("abd#", repo.getMatch("a.b.d")); }
@Test public void testMatchingDocsCustomUnderscorDelimiter() throws Throwable { WildcardConfiguration wildcardConfiguration = new WildcardConfiguration(); wildcardConfiguration.setDelimiter('_'); HierarchicalObjectRepository<String> repo = new HierarchicalObjectRepository<>(wildcardConfiguration); repo.addMatch("a_b_#", "ab#"); repo.addMatch("a_b_d_#", "abd#"); repo.addMatch("#", "root"); Assert.assertEquals("ab#", repo.getMatch("a_b")); Assert.assertEquals("ab#", repo.getMatch("a_b_c")); Assert.assertEquals("abd#", repo.getMatch("a_b_d_lll")); Assert.assertEquals("root", repo.getMatch("z_z_z_z_z")); Assert.assertEquals("root", repo.getMatch("a_babc")); Assert.assertEquals("ab#", repo.getMatch("a_b_dabc")); Assert.assertEquals("abd#", repo.getMatch("a_b_d")); }
@Test public void testMatchingDocsCustomForwardSlashDelimiter() throws Throwable { WildcardConfiguration wildcardConfiguration = new WildcardConfiguration(); wildcardConfiguration.setDelimiter('/'); HierarchicalObjectRepository<String> repo = new HierarchicalObjectRepository<>(wildcardConfiguration); repo.addMatch("a/b/#", "ab#"); repo.addMatch("a/b/d/#", "abd#"); repo.addMatch("#", "root"); Assert.assertEquals("ab#", repo.getMatch("a/b")); Assert.assertEquals("ab#", repo.getMatch("a/b/c")); Assert.assertEquals("abd#", repo.getMatch("a/b/d/lll")); Assert.assertEquals("root", repo.getMatch("z/z/z/z/z")); Assert.assertEquals("root", repo.getMatch("a/babc")); Assert.assertEquals("ab#", repo.getMatch("a/b/dabc")); Assert.assertEquals("abd#", repo.getMatch("a/b/d")); }