diff --git a/app/src/main/java/fr/free/nrw/commons/nearby/Label.java b/app/src/main/java/fr/free/nrw/commons/nearby/Label.java index dda7a87b4fd..c5d483a05de 100644 --- a/app/src/main/java/fr/free/nrw/commons/nearby/Label.java +++ b/app/src/main/java/fr/free/nrw/commons/nearby/Label.java @@ -4,12 +4,11 @@ import android.content.res.Resources; import android.os.Parcel; +import android.util.SparseArray; import androidx.annotation.DrawableRes; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import fr.free.nrw.commons.R; @@ -29,7 +28,6 @@ public enum Label { COTTAGE(R.array.cottage_QIDs, R.drawable.round_icon_house), FARMHOUSE(R.array.farmhouse_QIDs, R.drawable.round_icon_house), TEMPLE(R.array.temple_QIDs, R.drawable.round_icon_church), - CHURCH(R.array.church_QIDs, R.drawable.round_icon_church), GAS_STATION(R.array.gas_station_QIDs, R.drawable.round_icon_gas_station), RAILWAY_STATION(R.array.railway_station_QIDs, R.drawable.round_icon_railway_station), @@ -53,9 +51,14 @@ public enum Label { UNKNOWN(0, R.drawable.round_icon_unknown); /** - * Lookup map which maps Q-ID -> Label. + * Sorted array of Q-ID integers for binary-search lookup. + */ + private static volatile int[] QIDS = new int[0]; + + /** + * Labels parallel to {@link #QIDS}: QIDS[i] maps to QID_LABELS[i]. */ - public static final Map TEXT_TO_DESCRIPTION = new HashMap<>(); + private static volatile Label[] QID_LABELS = new Label[0]; private final int arrayResId; @DrawableRes @@ -73,21 +76,47 @@ public enum Label { } /** - * Loads Q-IDs from Android resources + * Loads Q-IDs from Android resources into sorted primitive arrays. + * This method is idempotent; subsequent calls after the first are no-ops. * - * @param context any Android context applicationContext - much safer + * @param context any Android context; applicationContext is safest */ - public static void init(final Context context) { + public static synchronized void init(final Context context) { + if (QIDS.length != 0) { + return; + } final Resources res = context.getResources(); + // load all values in temp map which will be sorted by default + final SparseArray