I read this article on Android Studio Support Annotations today and started using these annotations in my code, here is an example:
public final static class GoogleMapsZoomLevel {
public static final int MIN_ZOOM_LEVEL = 0;
public static final int MAX_ZOOM_LEVEL = 21;
..
public GoogleMapsZoomLevel(@IntRange(from=MIN_ZOOM_LEVEL, to=MAX_ZOOM_LEVEL) int zoomLevel) {
if (zoomLevel < MIN_ZOOM_LEVEL || zoomLevel > MAX_ZOOM_LEVEL) {
throw new IllegalArgumentException(ERROR_ZOOM_LEVEL_OUT_OF_BOUNDS);
}
this.zoomLevel = zoomLevel;
}
..
}
Further down in my code I have a class that accepts double
values in it's constructor, but there is no @DoubleRange annotation. Do I use @FloatRange or nothing at all? Same question for long
values
To enable annotations in your project, add the support-annotations dependency to your library or app. Any annotations you add then get checked when you run a code inspection or lint task.
annotation:annotation. Official Description: The Support Library is a static library that you can add to your Android application in order to use APIs that are either not available for older platform versions or utility APIs that aren't a part of the framework APIs.
@Retention(value = AnnotationRetention.SOURCE) @Target(allowedTargets = [AnnotationTarget.ANNOTATION_CLASS]) public annotation IntDef. Denotes that the annotated element of integer type, represents a logical type and that its value should be one of the explicitly named constants.
Actually, @FloatRange 's documentation states:
Denotes that the annotated element should be a float or double in the given range
And similar situation for @IntRange
Denotes that the annotated element should be an int or long in the given range
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With