AprVersionStrategy.java

  1. /*
  2.  * Licensed under the Apache License, Version 2.0 (the "License");
  3.  * you may not use this file except in compliance with the License.
  4.  * You may obtain a copy of the License at
  5.  *
  6.  * http://www.apache.org/licenses/LICENSE-2.0
  7.  *
  8.  * Unless required by applicable law or agreed to in writing, software
  9.  * distributed under the License is distributed on an "AS IS" BASIS,
  10.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11.  * See the License for the specific language governing permissions and
  12.  * limitations under the License.
  13.  */

  14. package org.basepom.mojo.dvc.strategy;

  15. import static com.google.common.base.Preconditions.checkNotNull;

  16. import java.util.Comparator;
  17. import javax.inject.Named;
  18. import javax.inject.Singleton;

  19. import com.google.common.base.Strings;
  20. import org.apache.maven.artifact.versioning.ArtifactVersion;
  21. import org.apache.maven.artifact.versioning.ComparableVersion;
  22. import org.apache.maven.artifact.versioning.DefaultArtifactVersion;

  23. /**
  24.  * Implements Apache versioning strategy for two or three digits. It expects versions formatted as x.y, x.y.z. Versions can have an additional qualifier.
  25.  * <p>
  26.  * Version A (xa.ya.za) can replace Version B (xb.yb.zb) if xa == xb and xa &gt;= xb. component z is always compatible.
  27.  * <p>
  28.  * If an additional qualifier exists, the qualifiers must match.
  29.  */
  30. @Named("apr")
  31. @Singleton
  32. public class AprVersionStrategy
  33.         implements Strategy {

  34.     private final Comparator<ArtifactVersion> comparator = Comparator
  35.             .comparing(ArtifactVersion::getMajorVersion, this::checkMajorCompatible)
  36.             .thenComparing(ArtifactVersion::getMinorVersion, this::checkMinorCompatible)
  37.             .thenComparing(ArtifactVersion::getIncrementalVersion, this::checkPatchCompatible)
  38.             .thenComparing(ArtifactVersion::getQualifier, (a, e) -> checkQualifierCompatible(Strings.nullToEmpty(a), Strings.nullToEmpty(e)));

  39.     @Override
  40.     public String getName() {
  41.         return "apr";
  42.     }

  43.     @Override
  44.     public final boolean isCompatible(final ComparableVersion expectedVersion, final ComparableVersion resolvedVersion) {
  45.         final ArtifactVersion aprExpectedVersion = new DefaultArtifactVersion(checkNotNull(expectedVersion, "expectedVersion is null").getCanonical());
  46.         final ArtifactVersion aprResolvedVersion = new DefaultArtifactVersion(checkNotNull(resolvedVersion, "resolvedVersion is null").getCanonical());

  47.         // Expected version must be before or equal the resolved version.
  48.         //
  49.         // for each method:
  50.         //
  51.         // -1 means incompatible.
  52.         //  0 means more testing.
  53.         //  1 means compatible.
  54.         //
  55.         return comparator.compare(aprExpectedVersion, aprResolvedVersion) >= 0; // more testing or compatible wins here.
  56.     }

  57.     protected int checkMajorCompatible(int expectedMajor, int resolvedMajor) {
  58.         if (expectedMajor != resolvedMajor) {
  59.             return -1; // incompatible if majors differ.
  60.         }
  61.         return 0; // otherwise more testing.
  62.     }

  63.     protected int checkMinorCompatible(int expectedMinor, int resolvedMinor) {
  64.         if (resolvedMinor < expectedMinor) {
  65.             return -1; // smaller version is not forward compatible
  66.         }

  67.         return 0; // otherwise more testing.
  68.     }

  69.     protected int checkPatchCompatible(int expectedPatch, int resolvedPatch) {
  70.         return 0; // all patches are compatible, check the qualifiers
  71.     }

  72.     protected int checkQualifierCompatible(String expectedQualifier, String resolvedQualifier) {
  73.         if (!expectedQualifier.equals(resolvedQualifier)) {
  74.             return -1; // if qualifiers don't match, things are not compatible (this makes things like 1.2.3-android and 1.2.3-jre not compatible!
  75.         }
  76.         return 0;
  77.     }
  78. }