001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.version;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.apache.ivy.core.module.id.ModuleRevisionId;
027: import org.apache.ivy.plugins.matcher.Matcher;
028:
029: /**
030: *
031: */
032: public class PatternVersionMatcher extends AbstractVersionMatcher {
033:
034: private List matches = new ArrayList();
035:
036: private Map revisionMatches = new HashMap(); // revision -> list of Match instances
037:
038: private boolean init = false;
039:
040: public void addMatch(Match match) {
041: matches.add(match);
042: }
043:
044: private void init() {
045: if (!init) {
046: for (Iterator it = matches.iterator(); it.hasNext();) {
047: Match match = (Match) it.next();
048: List revMatches = (List) revisionMatches.get(match
049: .getRevision());
050: if (revMatches == null) {
051: revMatches = new ArrayList();
052: revisionMatches
053: .put(match.getRevision(), revMatches);
054: }
055: revMatches.add(match);
056: }
057: init = true;
058: }
059: }
060:
061: /**
062: * {@inheritDoc}
063: */
064: public boolean accept(ModuleRevisionId askedMrid,
065: ModuleRevisionId foundMrid) {
066: init();
067: boolean accept = false;
068:
069: String revision = askedMrid.getRevision();
070: int bracketIndex = revision.indexOf('(');
071: if (bracketIndex > 0) {
072: revision = revision.substring(0, bracketIndex);
073: }
074:
075: List revMatches = (List) revisionMatches.get(revision);
076:
077: if (revMatches != null) {
078: Iterator it = revMatches.iterator();
079: while (!accept && it.hasNext()) {
080: Match match = (Match) it.next();
081: Matcher matcher = match.getPatternMatcher(askedMrid);
082: accept = matcher.matches(foundMrid.getRevision());
083: }
084: }
085:
086: return accept;
087: }
088:
089: /**
090: * {@inheritDoc}
091: */
092: public boolean isDynamic(ModuleRevisionId askedMrid) {
093: init();
094: String revision = askedMrid.getRevision();
095: int bracketIndex = revision.indexOf('(');
096: if (bracketIndex > 0) {
097: revision = revision.substring(0, bracketIndex);
098: }
099: return revisionMatches.containsKey(revision);
100: }
101:
102: }
|