/** * return any possible matches * * @param match * @return */ private Map<String, Match<T>> getPossibleMatches(final String match) { HashMap<String, Match<T>> possibleMatches = new HashMap<>(); for (Entry<String, Match<T>> entry : matches.entrySet()) { Match<T> entryMatch = entry.getValue(); if (entryMatch.getPattern().matcher(match).matches()) { possibleMatches.put(entry.getKey(), entryMatch); } } return possibleMatches; }
@Override public List<T> values() { lock.readLock().lock(); try { ArrayList<T> values = new ArrayList<>(matches.size()); for (Match<T> matchValue : matches.values()) { values.add(matchValue.getValue()); } return values; } 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(); } }
/** * merge all the possible matches, if the values implement Mergeable then a full merge is done * * @param orderedMatches * @return */ private T merge(final Collection<Match<T>> orderedMatches) { T actualMatch = null; for (Match<T> match : orderedMatches) { if (actualMatch == null || !Mergeable.class.isAssignableFrom(actualMatch.getClass())) { actualMatch = match.getValue(); if (!Mergeable.class.isAssignableFrom(actualMatch.getClass())) { break; } } else { ((Mergeable) actualMatch).merge(match.getValue()); } } return actualMatch; }