Search in sources :

Example 1 with Prefix

use of org.checkerframework.checker.units.qual.Prefix in project checker-framework by typetools.

the class UnitsRelationsTools method getPrefix.

/**
 * Retrieves the SI Prefix of an Annotation
 *
 * @param unitsAnnotation an AnnotationMirror representing a Units Annotation
 * @return a Prefix value (including Prefix.one), or null if it has none
 */
@Nullable
public static Prefix getPrefix(@Nullable final AnnotationMirror unitsAnnotation) {
    AnnotationValue annotationValue = getAnnotationMirrorPrefix(unitsAnnotation);
    // if this Annotation has no prefix, return null
    if (hasNoPrefix(annotationValue)) {
        return null;
    }
    // if the Annotation has a value, then detect and match the string name of the prefix, and
    // return the matching Prefix
    String prefixString = annotationValue.getValue().toString();
    for (Prefix prefix : Prefix.values()) {
        if (prefixString.equals(prefix.toString())) {
            return prefix;
        }
    }
    // if none of the strings match, then return null
    return null;
}
Also used : AnnotationValue(javax.lang.model.element.AnnotationValue) Prefix(org.checkerframework.checker.units.qual.Prefix) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 2 with Prefix

use of org.checkerframework.checker.units.qual.Prefix in project checker-framework by typetools.

the class UnitsAnnotatedTypeFactory method aliasedAnnotation.

// Converts all metric-prefixed units' alias annotations (eg @kg) into base unit annotations
// with prefix values (eg @g(Prefix.kilo))
@Override
public AnnotationMirror aliasedAnnotation(AnnotationMirror anno) {
    // Get the name of the aliased annotation
    String aname = anno.getAnnotationType().toString();
    // annotation
    if (aliasMap.containsKey(aname)) {
        // if so return it
        return aliasMap.get(aname);
    }
    boolean built = false;
    AnnotationMirror result = null;
    // if not, look for the UnitsMultiple meta annotations of this aliased annotation
    for (AnnotationMirror metaAnno : anno.getAnnotationType().asElement().getAnnotationMirrors()) {
        // see if the meta annotation is UnitsMultiple
        if (isUnitsMultiple(metaAnno)) {
            // retrieve the Class of the base unit annotation
            Class<? extends Annotation> baseUnitAnnoClass = AnnotationUtils.getElementValueClass(metaAnno, "quantity", true).asSubclass(Annotation.class);
            // retrieve the SI Prefix of the aliased annotation
            Prefix prefix = AnnotationUtils.getElementValueEnum(metaAnno, "prefix", Prefix.class, true);
            // Build a base unit annotation with the prefix applied
            result = UnitsRelationsTools.buildAnnoMirrorWithSpecificPrefix(processingEnv, baseUnitAnnoClass, prefix);
            // TODO: assert that this annotation is a prefix multiple of a Unit that's in the
            // supported type qualifiers list currently this breaks for externally loaded
            // annotations if the order was an alias before a base annotation.
            // assert isSupportedQualifier(result);
            built = true;
            break;
        }
    }
    if (built) {
        // aliases shouldn't have Prefix.one, but if it does then clean it up here
        if (UnitsRelationsTools.getPrefix(result) == Prefix.one) {
            result = removePrefix(result);
        }
        // add this to the alias map
        aliasMap.put(aname, result);
        return result;
    }
    return super.aliasedAnnotation(anno);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) Prefix(org.checkerframework.checker.units.qual.Prefix)

Example 3 with Prefix

use of org.checkerframework.checker.units.qual.Prefix in project checker-framework by typetools.

the class UnitsRelationsDefault method multiplication.

/**
 * Provides rules for resolving the result Unit of the multiplication of checker-framework
 * provided Units
 */
@Override
@Nullable
public AnnotationMirror multiplication(AnnotatedTypeMirror lht, AnnotatedTypeMirror rht) {
    // checking SI units only
    if (UnitsRelationsTools.hasSpecificUnitIgnoringPrefix(lht, m) && UnitsRelationsTools.hasSpecificUnitIgnoringPrefix(rht, m)) {
        if (UnitsRelationsTools.hasNoPrefix(lht) && UnitsRelationsTools.hasNoPrefix(rht)) {
            // m * m
            return m2;
        }
        Prefix lhtPrefix = UnitsRelationsTools.getPrefix(lht);
        Prefix rhtPrefix = UnitsRelationsTools.getPrefix(rht);
        if (bothHaveSpecificPrefix(lhtPrefix, rhtPrefix, Prefix.kilo)) {
            // km * km
            return km2;
        } else if (bothHaveSpecificPrefix(lhtPrefix, rhtPrefix, Prefix.one)) {
            // m(Prefix.one) * m(Prefix.one)
            return m2;
        } else if (bothHaveSpecificPrefix(lhtPrefix, rhtPrefix, Prefix.milli)) {
            // mm * mm
            return mm2;
        } else {
            return null;
        }
    } else if (havePairOfUnitsIgnoringOrder(lht, s, rht, mPERs)) {
        // s * mPERs or mPERs * s => m
        return m;
    } else if (havePairOfUnitsIgnoringOrder(lht, s, rht, mPERs2)) {
        // s * mPERs2 or mPERs2 * s => mPERs
        return mPERs;
    } else if (havePairOfUnitsIgnoringOrder(lht, h, rht, kmPERh)) {
        // h * kmPERh or kmPERh * h => km
        return km;
    } else {
        return null;
    }
}
Also used : Prefix(org.checkerframework.checker.units.qual.Prefix) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

Prefix (org.checkerframework.checker.units.qual.Prefix)3 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 AnnotationMirror (javax.lang.model.element.AnnotationMirror)1 AnnotationValue (javax.lang.model.element.AnnotationValue)1