/** * remove a match from the repository * * @param match the match to remove */ @Override public void removeMatch(final String match) { lock.writeLock().lock(); try { String modMatch = matchModifier.modify(match); boolean isImmutable = immutables.contains(modMatch); if (isImmutable) { logger.debug("Cannot remove match " + modMatch + " since it came from a main config"); } else { /** * clear the cache before removing the match. This will force any thread at * {@link #getMatch(String)} to get the lock to recompute. */ clearCache(); matches.remove(modMatch); onChange(); } } finally { lock.writeLock().unlock(); } }
/** * return the value held against the nearest match * * @param match the match to look for * @return the value */ @Override public T getMatch(final String match) { String modifiedMatch = matchModifier.modify(match); T cacheResult = cache.get(modifiedMatch); if (cacheResult != null) { return cacheResult; } lock.readLock().lock(); try { T actualMatch; Map<String, Match<T>> possibleMatches = getPossibleMatches(modifiedMatch); Collection<Match<T>> orderedMatches = sort(possibleMatches); actualMatch = merge(orderedMatches); T value = actualMatch != null ? actualMatch : defaultmatch; if (value != null) { cache.put(modifiedMatch, value); } return value; } finally { lock.readLock().unlock(); } }
private void addMatch(final String match, final T value, final boolean immutableMatch, boolean notifyListeners) { lock.writeLock().lock(); try { String modifiedMatch = matchModifier.modify(match); clearCache(); if (immutableMatch) { immutables.add(modifiedMatch); } Match.verify(modifiedMatch, wildcardConfiguration); Match<T> match1 = new Match<>(modifiedMatch, value, wildcardConfiguration); matches.put(modifiedMatch, match1); } finally { lock.writeLock().unlock(); } // Calling the onChange outside of the wrieLock as some listeners may be doing reads on the matches if (notifyListeners) { onChange(); } }