diff --git a/README.md b/README.md index 69c38434..5f3e4ac2 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,8 @@ Please ensure that your stack size is at least 1MB (for Saxon). Using the Oracle # News and noteworthy +* v10.1.1 - work in progress + * Added new classes `IGenericPseudoVersionResolver` and `DefaultGenericPseudoVersionResolver` * v10.1.0 - 2024-12-05 * Requires ph-commons 11.1.11 * Added new interface `IValidationExecutorSetMutable` diff --git a/phive-api/src/main/java/com/helger/phive/api/diver/DefaultGenericPseudoVersionResolver.java b/phive-api/src/main/java/com/helger/phive/api/diver/DefaultGenericPseudoVersionResolver.java new file mode 100644 index 00000000..bf74b437 --- /dev/null +++ b/phive-api/src/main/java/com/helger/phive/api/diver/DefaultGenericPseudoVersionResolver.java @@ -0,0 +1,134 @@ +/* + * Copyright (C) 2014-2024 Philip Helger (www.helger.com) + * philip[at]helger[dot]com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.helger.phive.api.diver; + +import java.time.OffsetDateTime; +import java.util.Set; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.concurrent.GuardedBy; +import javax.annotation.concurrent.ThreadSafe; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.helger.commons.ValueEnforcer; +import com.helger.commons.annotation.Nonempty; +import com.helger.commons.collection.impl.CommonsHashMap; +import com.helger.commons.collection.impl.ICommonsMap; +import com.helger.commons.concurrent.SimpleReadWriteLock; +import com.helger.commons.state.ESuccess; +import com.helger.commons.string.ToStringGenerator; +import com.helger.diver.api.version.DVRPseudoVersionRegistry; +import com.helger.diver.api.version.IDVRPseudoVersion; +import com.helger.phive.api.config.PhivePseudoVersionRegistrarSPIImpl; +import com.helger.phive.api.executorset.IValidationExecutorSet; +import com.helger.phive.api.executorset.ValidationExecutorSetRegistry; +import com.helger.phive.api.source.IValidationSource; + +/** + * Default {@link IGenericPseudoVersionResolver} implementation based on a + * {@link ValidationExecutorSetRegistry}. + * + * @author Philip Helger + * @param + * The validation source type to be used. + * @since 10.1.1 + */ +@ThreadSafe +public class DefaultGenericPseudoVersionResolver implements + IGenericPseudoVersionResolver > +{ + /** + * The internal resolver to use + * + * @author Philip Helger + * @param + * The validation source type to be used. + * @since 10.1.1 + */ + @FunctionalInterface + public interface INestedPseudoVersionResolver + { + @Nullable + IValidationExecutorSet resolve (@Nonnull @Nonempty String sGroupID, + @Nonnull @Nonempty String sArtifactID, + @Nullable Set aVersionsToIgnore, + @Nullable OffsetDateTime aCheckDateTime); + } + + private static final Logger LOGGER = LoggerFactory.getLogger (DefaultGenericPseudoVersionResolver.class); + + private final SimpleReadWriteLock m_aRWLock = new SimpleReadWriteLock (); + @GuardedBy ("m_aRWLock") + private final ICommonsMap > m_aMap = new CommonsHashMap <> (); + + public DefaultGenericPseudoVersionResolver (@Nonnull final ValidationExecutorSetRegistry aVESRegistry) + { + ValueEnforcer.notNull (aVESRegistry, "VESRegistry"); + registerResolver (DVRPseudoVersionRegistry.OLDEST, + (gid, aid, vti, cdt) -> aVESRegistry.getOldestVersion (gid, aid, vti)); + registerResolver (DVRPseudoVersionRegistry.LATEST, + (gid, aid, vti, cdt) -> aVESRegistry.getLatestVersion (gid, aid, vti)); + registerResolver (DVRPseudoVersionRegistry.LATEST_RELEASE, + (gid, aid, vti, cdt) -> aVESRegistry.getLatestReleaseVersion (gid, aid, vti)); + registerResolver (PhivePseudoVersionRegistrarSPIImpl.LATEST_ACTIVE, aVESRegistry::getLatestActiveVersion); + registerResolver (PhivePseudoVersionRegistrarSPIImpl.LATEST_RELEASE_ACTIVE, + aVESRegistry::getLatestReleaseActiveVersion); + } + + @Nonnull + public final ESuccess registerResolver (@Nonnull final IDVRPseudoVersion aPseudoVersion, + @Nonnull final INestedPseudoVersionResolver aNestedResolver) + { + ValueEnforcer.notNull (aPseudoVersion, "PseudoVersion"); + ValueEnforcer.notNull (aNestedResolver, "NestedResolver"); + + return m_aRWLock.writeLockedGet ( () -> { + final var aOldNestedResolver = m_aMap.get (aPseudoVersion); + if (aOldNestedResolver != null) + { + LOGGER.error ("Another nested resolver for pseudo version " + aPseudoVersion + " is already registered"); + return ESuccess.FAILURE; + } + m_aMap.put (aPseudoVersion, aNestedResolver); + return ESuccess.SUCCESS; + }); + } + + @Nullable + public IValidationExecutorSet resolvePseudoVersion (@Nonnull final IDVRPseudoVersion aPseudoVersion, + @Nonnull @Nonempty final String sGroupID, + @Nonnull @Nonempty final String sArtifactID, + @Nullable final Set aVersionsToIgnore, + @Nullable final OffsetDateTime aCheckDateTime) + { + ValueEnforcer.notNull (aPseudoVersion, "PseudoVersion"); + ValueEnforcer.notEmpty (sGroupID, "GroupID"); + ValueEnforcer.notEmpty (sArtifactID, "ArtifactID"); + + final var aResolver = m_aRWLock.readLockedGet ( () -> m_aMap.get (aPseudoVersion)); + return aResolver != null ? aResolver.resolve (sGroupID, sArtifactID, aVersionsToIgnore, aCheckDateTime) : null; + } + + @Override + public String toString () + { + return new ToStringGenerator (null).append ("Map", m_aMap).getToString (); + } +} diff --git a/phive-api/src/main/java/com/helger/phive/api/diver/IGenericPseudoVersionResolver.java b/phive-api/src/main/java/com/helger/phive/api/diver/IGenericPseudoVersionResolver.java new file mode 100644 index 00000000..e34a47f7 --- /dev/null +++ b/phive-api/src/main/java/com/helger/phive/api/diver/IGenericPseudoVersionResolver.java @@ -0,0 +1,63 @@ +/* + * Copyright (C) 2014-2024 Philip Helger (www.helger.com) + * philip[at]helger[dot]com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.helger.phive.api.diver; + +import java.time.OffsetDateTime; +import java.util.Set; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +import com.helger.commons.annotation.Nonempty; +import com.helger.diver.api.version.IDVRPseudoVersion; + +/** + * Generic pseudo version resolver interface + * + * @author Philip Helger + * @param + * The type to which is resolved + * @since 10.1.1 + */ +public interface IGenericPseudoVersionResolver +{ + /** + * Get the element that matches the provided group ID, artifact ID and pseudo + * version. + * + * @param aPseudoVersion + * The pseudo version to resolve. May not be null. + * @param sGroupID + * VES Group ID to use. May neither be null nor empty. + * @param sArtifactID + * VES Artefact ID to use. May neither be null nor empty. + * @param aVersionsToIgnore + * An optional set of Version numbers not to consider. This may be used + * to exclude certain versions from being returned. May be + * null. + * @param aCheckDateTim + * The effective date for which a check should be performed. May be + * null to indicate "current date time". + * @return null if resolution fails + */ + @Nullable + RESULTTYPE resolvePseudoVersion (@Nonnull IDVRPseudoVersion aPseudoVersion, + @Nonnull @Nonempty String sGroupID, + @Nonnull @Nonempty String sArtifactID, + @Nullable Set aVersionsToIgnore, + @Nullable OffsetDateTime aCheckDateTim); +}