Save This Page
Home » apache-ivyde-sources-2.0.0.final-200907011148-RELEASE » org.apache.ivyde.eclipse.workspaceresolver » [javadoc | source]
    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one or more
    3    *  contributor license agreements.  See the NOTICE file distributed with
    4    *  this work for additional information regarding copyright ownership.
    5    *  The ASF licenses this file to You under the Apache License, Version 2.0
    6    *  (the "License"); you may not use this file except in compliance with
    7    *  the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    *
   17    */
   18   package org.apache.ivyde.eclipse.workspaceresolver;
   19   
   20   import java.util.Comparator;
   21   
   22   import org.apache.ivy.core.IvyContext;
   23   import org.apache.ivy.core.module.id.ModuleRevisionId;
   24   import org.apache.ivy.plugins.latest.ArtifactInfo;
   25   import org.apache.ivy.plugins.latest.ComparatorLatestStrategy;
   26   import org.apache.ivy.plugins.latest.LatestRevisionStrategy;
   27   import org.apache.ivy.plugins.version.VersionMatcher;
   28   
   29   /**
   30    * A dumb copy of the {@link LatestRevisionStrategy} but that take into account the latest and
   31    * working revision as superior to any other revision.
   32    */
   33   public class IvyDEStrategy extends ComparatorLatestStrategy {
   34   
   35       final class MridComparator implements Comparator {
   36           public int compare(Object o1, Object o2) {
   37               String rev1 = ((ModuleRevisionId) o1).getRevision();
   38               String rev2 = ((ModuleRevisionId) o2).getRevision();
   39   
   40               boolean latestRev1 = rev1.startsWith("latest") || rev1.startsWith("working");
   41               boolean latestRev2 = rev2.startsWith("latest") || rev2.startsWith("working");
   42               if (latestRev1 && !latestRev2) {
   43                   return 1;
   44               } else if (latestRev2 && !latestRev1) {
   45                   return -1;
   46               }
   47   
   48               rev1 = rev1.replaceAll("([a-zA-Z])(\\d)", "$1.$2");
   49               rev1 = rev1.replaceAll("(\\d)([a-zA-Z])", "$1.$2");
   50               rev2 = rev2.replaceAll("([a-zA-Z])(\\d)", "$1.$2");
   51               rev2 = rev2.replaceAll("(\\d)([a-zA-Z])", "$1.$2");
   52   
   53               String[] parts1 = rev1.split("[\\._\\-\\+]");
   54               String[] parts2 = rev2.split("[\\._\\-\\+]");
   55   
   56               int i = 0;
   57               for (; i < parts1.length && i < parts2.length; i++) {
   58                   if (parts1[i].equals(parts2[i])) {
   59                       continue;
   60                   }
   61                   boolean is1Number = isNumber(parts1[i]);
   62                   boolean is2Number = isNumber(parts2[i]);
   63                   if (is1Number && !is2Number) {
   64                       return 1;
   65                   }
   66                   if (is2Number && !is1Number) {
   67                       return -1;
   68                   }
   69                   if (is1Number && is2Number) {
   70                       return Long.valueOf(parts1[i]).compareTo(Long.valueOf(parts2[i]));
   71                   }
   72                   return parts1[i].compareTo(parts2[i]);
   73               }
   74               if (i < parts1.length) {
   75                   return isNumber(parts1[i]) ? 1 : -1;
   76               }
   77               if (i < parts2.length) {
   78                   return isNumber(parts2[i]) ? -1 : 1;
   79               }
   80               return 0;
   81           }
   82   
   83           private boolean isNumber(String str) {
   84               return str.matches("\\d+");
   85           }
   86       }
   87   
   88       /**
   89        * Compares two ArtifactInfo by their revision. Revisions are compared using an algorithm
   90        * inspired by PHP version_compare one, unless a dynamic revision is given, in which case the
   91        * version matcher is used to perform the comparison.
   92        */
   93       final class ArtifactInfoComparator implements Comparator {
   94           public int compare(Object o1, Object o2) {
   95               String rev1 = ((ArtifactInfo) o1).getRevision();
   96               String rev2 = ((ArtifactInfo) o2).getRevision();
   97   
   98               /*
   99                * The revisions can still be not resolved, so we use the current version matcher to
  100                * know if one revision is dynamic, and in this case if it should be considered greater
  101                * or lower than the other one. Note that if the version matcher compare method returns
  102                * 0, it's because it's not possible to know which revision is greater. In this case we
  103                * consider the dynamic one to be greater, because most of the time it will then be
  104                * actually resolved and a real comparison will occur.
  105                */
  106               VersionMatcher vmatcher = IvyContext.getContext().getSettings().getVersionMatcher();
  107               ModuleRevisionId mrid1 = ModuleRevisionId.newInstance("", "", rev1);
  108               ModuleRevisionId mrid2 = ModuleRevisionId.newInstance("", "", rev2);
  109               if (vmatcher.isDynamic(mrid1)) {
  110                   int c = vmatcher.compare(mrid1, mrid2, mridComparator);
  111                   return c >= 0 ? 1 : -1;
  112               } else if (vmatcher.isDynamic(mrid2)) {
  113                   int c = vmatcher.compare(mrid2, mrid1, mridComparator);
  114                   return c >= 0 ? -1 : 1;
  115               }
  116   
  117               return mridComparator.compare(mrid1, mrid2);
  118           }
  119       }
  120   
  121       private final Comparator mridComparator = new MridComparator();
  122   
  123       private final Comparator artifactInfoComparator = new ArtifactInfoComparator();
  124   
  125       public IvyDEStrategy() {
  126           setComparator(artifactInfoComparator);
  127           setName("ivyde-latest-revision");
  128       }
  129   
  130   }

Save This Page
Home » apache-ivyde-sources-2.0.0.final-200907011148-RELEASE » org.apache.ivyde.eclipse.workspaceresolver » [javadoc | source]