From dfda4ea9aa46f845e7349da29370e522f9dedb91 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Tue, 5 Jun 2018 12:44:36 -0400 Subject: [PATCH] Annotate Rect/RectF with nullability annotations And a missing override or eight. Bug: 78245676 Test: none Change-Id: I4476646f03f88753db358d01a6ad65a0982a160f --- graphics/java/android/graphics/Rect.java | 39 +++++++++++++++++----------- graphics/java/android/graphics/RectF.java | 42 +++++++++++++++++++------------ 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/graphics/java/android/graphics/Rect.java b/graphics/java/android/graphics/Rect.java index 3843cb91154c..56a6820f4749 100644 --- a/graphics/java/android/graphics/Rect.java +++ b/graphics/java/android/graphics/Rect.java @@ -17,12 +17,13 @@ package android.graphics; import android.annotation.CheckResult; +import android.annotation.NonNull; import android.annotation.Nullable; import android.os.Parcel; import android.os.Parcelable; - import android.text.TextUtils; import android.util.proto.ProtoOutputStream; + import java.io.PrintWriter; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -89,7 +90,7 @@ public final class Rect implements Parcelable { * @param r The rectangle whose coordinates are copied into the new * rectangle. */ - public Rect(Rect r) { + public Rect(@Nullable Rect r) { if (r == null) { left = top = right = bottom = 0; } else { @@ -140,6 +141,7 @@ public final class Rect implements Parcelable { /** * Return a string representation of the rectangle in a compact form. */ + @NonNull public String toShortString() { return toShortString(new StringBuilder(32)); } @@ -148,7 +150,8 @@ public final class Rect implements Parcelable { * Return a string representation of the rectangle in a compact form. * @hide */ - public String toShortString(StringBuilder sb) { + @NonNull + public String toShortString(@NonNull StringBuilder sb) { sb.setLength(0); sb.append('['); sb.append(left); sb.append(','); sb.append(top); sb.append("]["); sb.append(right); @@ -164,6 +167,7 @@ public final class Rect implements Parcelable { * * @return Returns a new String of the form "left top right bottom" */ + @NonNull public String flattenToString() { StringBuilder sb = new StringBuilder(32); // WARNING: Do not change the format of this string, it must be @@ -182,7 +186,8 @@ public final class Rect implements Parcelable { * Returns a Rect from a string of the form returned by {@link #flattenToString}, * or null if the string is not of that form. */ - public static Rect unflattenFromString(String str) { + @Nullable + public static Rect unflattenFromString(@Nullable String str) { if (TextUtils.isEmpty(str)) { return null; } @@ -201,7 +206,7 @@ public final class Rect implements Parcelable { * Print short representation to given writer. * @hide */ - public void printShortString(PrintWriter pw) { + public void printShortString(@NonNull PrintWriter pw) { pw.print('['); pw.print(left); pw.print(','); pw.print(top); pw.print("]["); pw.print(right); pw.print(','); pw.print(bottom); pw.print(']'); @@ -215,7 +220,7 @@ public final class Rect implements Parcelable { * @param fieldId Field Id of the Rect as defined in the parent message * @hide */ - public void writeToProto(ProtoOutputStream protoOutputStream, long fieldId) { + public void writeToProto(@NonNull ProtoOutputStream protoOutputStream, long fieldId) { final long token = protoOutputStream.start(fieldId); protoOutputStream.write(RectProto.LEFT, left); protoOutputStream.write(RectProto.TOP, top); @@ -309,7 +314,7 @@ public final class Rect implements Parcelable { * @param src The rectangle whose coordinates are copied into this * rectangle. */ - public void set(Rect src) { + public void set(@NonNull Rect src) { this.left = src.left; this.top = src.top; this.right = src.right; @@ -366,7 +371,7 @@ public final class Rect implements Parcelable { * @hide * @param insets The rectangle specifying the insets on all side. */ - public void inset(Rect insets) { + public void inset(@NonNull Rect insets) { left += insets.left; top += insets.top; right -= insets.right; @@ -432,7 +437,7 @@ public final class Rect implements Parcelable { * @return true iff the specified rectangle r is inside or equal to this * rectangle */ - public boolean contains(Rect r) { + public boolean contains(@NonNull Rect r) { // check for empty first return this.left < this.right && this.top < this.bottom // now check for containment @@ -481,7 +486,7 @@ public final class Rect implements Parcelable { * return false and do not change this rectangle. */ @CheckResult - public boolean intersect(Rect r) { + public boolean intersect(@NonNull Rect r) { return intersect(r.left, r.top, r.right, r.bottom); } @@ -491,7 +496,7 @@ public final class Rect implements Parcelable { * @see #inset(int, int, int, int) but without checking if the rects overlap. * @hide */ - public void intersectUnchecked(Rect other) { + public void intersectUnchecked(@NonNull Rect other) { left = Math.max(left, other.left); top = Math.max(top, other.top); right = Math.min(right, other.right); @@ -511,7 +516,7 @@ public final class Rect implements Parcelable { * false and do not change this rectangle. */ @CheckResult - public boolean setIntersect(Rect a, Rect b) { + public boolean setIntersect(@NonNull Rect a, @NonNull Rect b) { if (a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom) { left = Math.max(a.left, b.left); top = Math.max(a.top, b.top); @@ -550,7 +555,7 @@ public final class Rect implements Parcelable { * @return true iff the two specified rectangles intersect. In no event are * either of the rectangles modified. */ - public static boolean intersects(Rect a, Rect b) { + public static boolean intersects(@NonNull Rect a, @NonNull Rect b) { return a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom; } @@ -587,7 +592,7 @@ public final class Rect implements Parcelable { * * @param r The rectangle being unioned with this rectangle */ - public void union(Rect r) { + public void union(@NonNull Rect r) { union(r.left, r.top, r.right, r.bottom); } @@ -634,6 +639,7 @@ public final class Rect implements Parcelable { /** * Parcelable interface methods */ + @Override public int describeContents() { return 0; } @@ -643,6 +649,7 @@ public final class Rect implements Parcelable { * a parcel, use readFromParcel() * @param out The parcel to write the rectangle's coordinates into */ + @Override public void writeToParcel(Parcel out, int flags) { out.writeInt(left); out.writeInt(top); @@ -654,6 +661,7 @@ public final class Rect implements Parcelable { /** * Return a new rectangle from the data in the specified parcel. */ + @Override public Rect createFromParcel(Parcel in) { Rect r = new Rect(); r.readFromParcel(in); @@ -663,6 +671,7 @@ public final class Rect implements Parcelable { /** * Return an array of rectangles of the specified size. */ + @Override public Rect[] newArray(int size) { return new Rect[size]; } @@ -674,7 +683,7 @@ public final class Rect implements Parcelable { * * @param in The parcel to read the rectangle's coordinates from */ - public void readFromParcel(Parcel in) { + public void readFromParcel(@NonNull Parcel in) { left = in.readInt(); top = in.readInt(); right = in.readInt(); diff --git a/graphics/java/android/graphics/RectF.java b/graphics/java/android/graphics/RectF.java index b49054550956..d6447ac237ab 100644 --- a/graphics/java/android/graphics/RectF.java +++ b/graphics/java/android/graphics/RectF.java @@ -16,12 +16,15 @@ package android.graphics; -import java.io.PrintWriter; - +import android.annotation.NonNull; +import android.annotation.Nullable; import android.os.Parcel; import android.os.Parcelable; + import com.android.internal.util.FastMath; +import java.io.PrintWriter; + /** * RectF holds four float coordinates for a rectangle. The rectangle is * represented by the coordinates of its 4 edges (left, top, right bottom). @@ -64,7 +67,7 @@ public class RectF implements Parcelable { * @param r The rectangle whose coordinates are copied into the new * rectangle. */ - public RectF(RectF r) { + public RectF(@Nullable RectF r) { if (r == null) { left = top = right = bottom = 0.0f; } else { @@ -75,7 +78,7 @@ public class RectF implements Parcelable { } } - public RectF(Rect r) { + public RectF(@Nullable Rect r) { if (r == null) { left = top = right = bottom = 0.0f; } else { @@ -104,6 +107,7 @@ public class RectF implements Parcelable { return result; } + @Override public String toString() { return "RectF(" + left + ", " + top + ", " + right + ", " + bottom + ")"; @@ -112,6 +116,7 @@ public class RectF implements Parcelable { /** * Return a string representation of the rectangle in a compact form. */ + @NonNull public String toShortString() { return toShortString(new StringBuilder(32)); } @@ -120,7 +125,8 @@ public class RectF implements Parcelable { * Return a string representation of the rectangle in a compact form. * @hide */ - public String toShortString(StringBuilder sb) { + @NonNull + public String toShortString(@NonNull StringBuilder sb) { sb.setLength(0); sb.append('['); sb.append(left); sb.append(','); sb.append(top); sb.append("]["); sb.append(right); @@ -132,7 +138,7 @@ public class RectF implements Parcelable { * Print short representation to given writer. * @hide */ - public void printShortString(PrintWriter pw) { + public void printShortString(@NonNull PrintWriter pw) { pw.print('['); pw.print(left); pw.print(','); pw.print(top); pw.print("]["); pw.print(right); pw.print(','); pw.print(bottom); pw.print(']'); @@ -207,7 +213,7 @@ public class RectF implements Parcelable { * @param src The rectangle whose coordinates are copied into this * rectangle. */ - public void set(RectF src) { + public void set(@NonNull RectF src) { this.left = src.left; this.top = src.top; this.right = src.right; @@ -220,7 +226,7 @@ public class RectF implements Parcelable { * @param src The rectangle whose coordinates are copied into this * rectangle. */ - public void set(Rect src) { + public void set(@NonNull Rect src) { this.left = src.left; this.top = src.top; this.right = src.right; @@ -315,7 +321,7 @@ public class RectF implements Parcelable { * @return true iff the specified rectangle r is inside or equal to this * rectangle */ - public boolean contains(RectF r) { + public boolean contains(@NonNull RectF r) { // check for empty first return this.left < this.right && this.top < this.bottom // now check for containment @@ -372,7 +378,7 @@ public class RectF implements Parcelable { * (and this rectangle is then set to that intersection) else * return false and do not change this rectangle. */ - public boolean intersect(RectF r) { + public boolean intersect(@NonNull RectF r) { return intersect(r.left, r.top, r.right, r.bottom); } @@ -388,7 +394,7 @@ public class RectF implements Parcelable { * this rectangle to that intersection. If they do not, return * false and do not change this rectangle. */ - public boolean setIntersect(RectF a, RectF b) { + public boolean setIntersect(@NonNull RectF a, @NonNull RectF b) { if (a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom) { left = Math.max(a.left, b.left); @@ -430,7 +436,7 @@ public class RectF implements Parcelable { * @return true iff the two specified rectangles intersect. In no event are * either of the rectangles modified. */ - public static boolean intersects(RectF a, RectF b) { + public static boolean intersects(@NonNull RectF a, @NonNull RectF b) { return a.left < b.right && b.left < a.right && a.top < b.bottom && b.top < a.bottom; } @@ -439,7 +445,7 @@ public class RectF implements Parcelable { * Set the dst integer Rect by rounding this rectangle's coordinates * to their nearest integer values. */ - public void round(Rect dst) { + public void round(@NonNull Rect dst) { dst.set(FastMath.round(left), FastMath.round(top), FastMath.round(right), FastMath.round(bottom)); } @@ -448,7 +454,7 @@ public class RectF implements Parcelable { * Set the dst integer Rect by rounding "out" this rectangle, choosing the * floor of top and left, and the ceiling of right and bottom. */ - public void roundOut(Rect dst) { + public void roundOut(@NonNull Rect dst) { dst.set((int) Math.floor(left), (int) Math.floor(top), (int) Math.ceil(right), (int) Math.ceil(bottom)); } @@ -490,7 +496,7 @@ public class RectF implements Parcelable { * * @param r The rectangle being unioned with this rectangle */ - public void union(RectF r) { + public void union(@NonNull RectF r) { union(r.left, r.top, r.right, r.bottom); } @@ -537,6 +543,7 @@ public class RectF implements Parcelable { /** * Parcelable interface methods */ + @Override public int describeContents() { return 0; } @@ -546,6 +553,7 @@ public class RectF implements Parcelable { * a parcel, use readFromParcel() * @param out The parcel to write the rectangle's coordinates into */ + @Override public void writeToParcel(Parcel out, int flags) { out.writeFloat(left); out.writeFloat(top); @@ -557,6 +565,7 @@ public class RectF implements Parcelable { /** * Return a new rectangle from the data in the specified parcel. */ + @Override public RectF createFromParcel(Parcel in) { RectF r = new RectF(); r.readFromParcel(in); @@ -566,6 +575,7 @@ public class RectF implements Parcelable { /** * Return an array of rectangles of the specified size. */ + @Override public RectF[] newArray(int size) { return new RectF[size]; } @@ -577,7 +587,7 @@ public class RectF implements Parcelable { * * @param in The parcel to read the rectangle's coordinates from */ - public void readFromParcel(Parcel in) { + public void readFromParcel(@NonNull Parcel in) { left = in.readFloat(); top = in.readFloat(); right = in.readFloat(); -- 2.11.0