com.softicar.platform.common.code.reference.point.SourceCodeReferencePoints Maven / Gradle / Ivy
Show all versions of platform-common Show documentation
package com.softicar.platform.common.code.reference.point;
import com.softicar.platform.common.core.exceptions.SofticarDeveloperException;
import com.softicar.platform.common.core.utils.CastUtils;
import java.util.Collection;
import java.util.Optional;
import java.util.UUID;
/**
* Utility methods for classes implementing {@link ISourceCodeReferencePoint}.
*
* @author Oliver Richers
*/
public class SourceCodeReferencePoints {
/**
* Returns the {@link UUID} of the given {@link ISourceCodeReferencePoint}
* class from the {@link SourceCodeReferencePointUuid} annotation.
*
* @param referencePointClass
* the class of the reference point (never null)
* @return the {@link UUID} (never null)
* @throws SourceCodeReferencePointMissingUuidAnnotationException
* if the given {@link Class} is missing an
* {@link SourceCodeReferencePointUuid} annotation
*/
public static UUID getUuidOrThrow(Class referencePointClass) {
SourceCodeReferencePointUuid uuidAnnotation = referencePointClass.getAnnotation(SourceCodeReferencePointUuid.class);
if (uuidAnnotation != null) {
return UUID.fromString(uuidAnnotation.value());
} else {
throw new SourceCodeReferencePointMissingUuidAnnotationException(referencePointClass);
}
}
/**
* Returns the {@link ISourceCodeReferencePoint} instance of the given
* {@link Class}.
*
* @param referencePointClass
* the {@link Class} of the {@link ISourceCodeReferencePoint}
* (never null)
* @return the {@link ISourceCodeReferencePoint} instance (never
* null)
* @throws SourceCodeReferencePointMissingUuidAnnotationException
* if the given {@link Class} is missing an
* {@link SourceCodeReferencePointUuid} annotation
* @throws SourceCodeReferencePointMissingException
* if no matching {@link ISourceCodeReferencePoint} was found
*/
public static T getReferencePoint(Class referencePointClass) {
return getReferencePointOrThrow(getUuidOrThrow(referencePointClass), referencePointClass);
}
/**
* Returns the {@link ISourceCodeReferencePoint} matching the given
* {@link UUID}.
*
* @param uuid
* the {@link UUID} (never null)
* @return the {@link ISourceCodeReferencePoint} as {@link Optional}
*/
public static Optional getReferencePoint(UUID uuid) {
return SourceCodeReferencePointRegistry//
.getInstance()
.getReferencePoint(uuid);
}
/**
* Returns the {@link ISourceCodeReferencePoint} matching the given
* {@link UUID}.
*
* @param uuid
* the {@link UUID} (never null)
* @return the {@link ISourceCodeReferencePoint} (never null)
* @throws SourceCodeReferencePointMissingException
* if no matching {@link ISourceCodeReferencePoint} was found
*/
public static ISourceCodeReferencePoint getReferencePointOrThrow(UUID uuid) {
return getReferencePoint(uuid)//
.orElseThrow(() -> new SourceCodeReferencePointMissingException(uuid));
}
/**
* Returns the {@link ISourceCodeReferencePoint} matching the given
* {@link UUID} and implementing the given class.
*
* If no matching {@link ISourceCodeReferencePoint} was found, an empty
* {@link Optional} is returned.
*
* @param uuid
* the {@link UUID} (never null)
* @param targetClass
* the class to implement (never null)
* @return the optional {@link ISourceCodeReferencePoint} (never
* null)
*/
public static Optional getReferencePoint(UUID uuid, Class targetClass) {
return getReferencePoint(uuid).flatMap(referencePoint -> CastUtils.tryCast(referencePoint, targetClass));
}
/**
* Returns the {@link ISourceCodeReferencePoint} matching the given
* {@link UUID} and implementing the given class.
*
* @param uuid
* the {@link UUID} (never null)
* @param targetClass
* the class to implement (never null)
* @return the {@link ISourceCodeReferencePoint} (never null)
* @throws SourceCodeReferencePointMissingException
* if no matching {@link ISourceCodeReferencePoint} was found
*/
public static T getReferencePointOrThrow(UUID uuid, Class targetClass) {
ISourceCodeReferencePoint referencePoint = getReferencePointOrThrow(uuid);
return CastUtils//
.tryCast(referencePoint, targetClass)
.orElseThrow(
() -> new SofticarDeveloperException(//
"%s does not implement %s",
ISourceCodeReferencePoint.class.getSimpleName(),
targetClass.getCanonicalName()));
}
/**
* Returns all {@link ISourceCodeReferencePoint}s implementing the given
* given class.
*
* @param targetClass
* the class to implement (never null)
* @return all matching {@link ISourceCodeReferencePoint}s (never
* null)
*/
public static Collection getReferencePoints(Class targetClass) {
return SourceCodeReferencePointRegistry//
.getInstance()
.getReferencePoints(targetClass);
}
}