From 14a21710359f2ae688288087a661bd9f27a16772 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Wed, 23 Dec 2020 07:50:43 +0530 Subject: [PATCH] Use Objects.requireNonNull(). --- .../org/schabi/newpipe/about/LicenseFragment.java | 8 +++----- .../newpipe/settings/DownloadSettingsFragment.java | 5 +---- .../java/us/shandian/giga/get/DownloadMission.java | 5 +++-- .../us/shandian/giga/get/DownloadRunnable.java | 4 ++-- .../giga/get/sqlite/FinishedMissionStore.java | 14 +++++--------- .../us/shandian/giga/io/CircularFileWriter.java | 5 ++--- 6 files changed, 16 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java b/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java index 8367a75dc..6e48a0e14 100644 --- a/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java +++ b/app/src/main/java/org/schabi/newpipe/about/LicenseFragment.java @@ -19,6 +19,7 @@ import org.schabi.newpipe.util.ShareUtils; import java.io.Serializable; import java.util.Arrays; import java.util.Comparator; +import java.util.Objects; import io.reactivex.rxjava3.disposables.CompositeDisposable; @@ -35,12 +36,9 @@ public class LicenseFragment extends Fragment { private final CompositeDisposable compositeDisposable = new CompositeDisposable(); public static LicenseFragment newInstance(final SoftwareComponent[] softwareComponents) { - if (softwareComponents == null) { - throw new NullPointerException("softwareComponents is null"); - } - final LicenseFragment fragment = new LicenseFragment(); final Bundle bundle = new Bundle(); - bundle.putParcelableArray(ARG_COMPONENTS, softwareComponents); + bundle.putParcelableArray(ARG_COMPONENTS, Objects.requireNonNull(softwareComponents)); + final LicenseFragment fragment = new LicenseFragment(); fragment.setArguments(bundle); return fragment; } diff --git a/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java index a4b29fc49..8742f0937 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/DownloadSettingsFragment.java @@ -246,10 +246,7 @@ public class DownloadSettingsFragment extends BasePreferenceFragment { // revoke permissions on the old save path (required for SAF only) - final Context context = getContext(); - if (context == null) { - throw new NullPointerException("getContext()"); - } + final Context context = requireContext(); forgetSAFTree(context, defaultPreferences.getString(key, "")); diff --git a/app/src/main/java/us/shandian/giga/get/DownloadMission.java b/app/src/main/java/us/shandian/giga/get/DownloadMission.java index d7c586083..2b3faa3e0 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadMission.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadMission.java @@ -22,6 +22,7 @@ import java.net.SocketTimeoutException; import java.net.URL; import java.net.UnknownHostException; import java.nio.channels.ClosedByInterruptException; +import java.util.Objects; import javax.net.ssl.SSLException; @@ -154,8 +155,8 @@ public class DownloadMission extends Mission { public transient Thread init = null; public DownloadMission(String[] urls, StoredFileHelper storage, char kind, Postprocessing psInstance) { - if (urls == null) throw new NullPointerException("urls is null"); - if (urls.length < 1) throw new IllegalArgumentException("urls is empty"); + if (Objects.requireNonNull(urls).length < 1) + throw new IllegalArgumentException("urls array is empty"); this.urls = urls; this.kind = kind; this.offsets = new long[urls.length]; diff --git a/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java b/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java index 7fb12d088..6f504cea3 100644 --- a/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java +++ b/app/src/main/java/us/shandian/giga/get/DownloadRunnable.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.nio.channels.ClosedByInterruptException; +import java.util.Objects; import us.shandian.giga.get.DownloadMission.Block; import us.shandian.giga.get.DownloadMission.HttpError; @@ -29,8 +30,7 @@ public class DownloadRunnable extends Thread { private HttpURLConnection mConn; DownloadRunnable(DownloadMission mission, int id) { - if (mission == null) throw new NullPointerException("mission is null"); - mMission = mission; + mMission = Objects.requireNonNull(mission); mId = id; } diff --git a/app/src/main/java/us/shandian/giga/get/sqlite/FinishedMissionStore.java b/app/src/main/java/us/shandian/giga/get/sqlite/FinishedMissionStore.java index 1d1dca0df..15c45c6fd 100644 --- a/app/src/main/java/us/shandian/giga/get/sqlite/FinishedMissionStore.java +++ b/app/src/main/java/us/shandian/giga/get/sqlite/FinishedMissionStore.java @@ -12,6 +12,7 @@ import androidx.annotation.NonNull; import java.io.File; import java.util.ArrayList; +import java.util.Objects; import us.shandian.giga.get.DownloadMission; import us.shandian.giga.get.FinishedMission; @@ -140,9 +141,7 @@ public class FinishedMissionStore extends SQLiteOpenHelper { } private FinishedMission getMissionFromCursor(Cursor cursor) { - if (cursor == null) throw new NullPointerException("cursor is null"); - - String kind = cursor.getString(cursor.getColumnIndex(KEY_KIND)); + String kind = Objects.requireNonNull(cursor).getString(cursor.getColumnIndex(KEY_KIND)); if (kind == null || kind.isEmpty()) kind = "?"; String path = cursor.getString(cursor.getColumnIndexOrThrow(KEY_PATH)); @@ -186,15 +185,13 @@ public class FinishedMissionStore extends SQLiteOpenHelper { } public void addFinishedMission(DownloadMission downloadMission) { - if (downloadMission == null) throw new NullPointerException("downloadMission is null"); + ContentValues values = getValuesOfMission(Objects.requireNonNull(downloadMission)); SQLiteDatabase database = getWritableDatabase(); - ContentValues values = getValuesOfMission(downloadMission); database.insert(FINISHED_TABLE_NAME, null, values); } public void deleteMission(Mission mission) { - if (mission == null) throw new NullPointerException("mission is null"); - String ts = String.valueOf(mission.timestamp); + String ts = String.valueOf(Objects.requireNonNull(mission).timestamp); SQLiteDatabase database = getWritableDatabase(); @@ -212,9 +209,8 @@ public class FinishedMissionStore extends SQLiteOpenHelper { } public void updateMission(Mission mission) { - if (mission == null) throw new NullPointerException("mission is null"); + ContentValues values = getValuesOfMission(Objects.requireNonNull(mission)); SQLiteDatabase database = getWritableDatabase(); - ContentValues values = getValuesOfMission(mission); String ts = String.valueOf(mission.timestamp); int rowsAffected; diff --git a/app/src/main/java/us/shandian/giga/io/CircularFileWriter.java b/app/src/main/java/us/shandian/giga/io/CircularFileWriter.java index 4d62ab200..dbceeb091 100644 --- a/app/src/main/java/us/shandian/giga/io/CircularFileWriter.java +++ b/app/src/main/java/us/shandian/giga/io/CircularFileWriter.java @@ -7,6 +7,7 @@ import org.schabi.newpipe.streams.io.SharpStream; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; +import java.util.Objects; public class CircularFileWriter extends SharpStream { @@ -27,9 +28,7 @@ public class CircularFileWriter extends SharpStream { private BufferedFile aux; public CircularFileWriter(SharpStream target, File temp, OffsetChecker checker) throws IOException { - if (checker == null) { - throw new NullPointerException("checker is null"); - } + Objects.requireNonNull(checker); if (!temp.exists()) { if (!temp.createNewFile()) {