package org.wikipedia.page.gallery;

import org.wikipedia.page.PageTitle;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class GalleryCollection {
    private static final int MIN_IMAGE_SIZE = 64;

    private List<GalleryItem> itemList;
    public List<GalleryItem> getItemList() {
        return itemList;
    }

    public JSONObject toJSON() {
        JSONObject json = new JSONObject();
        try {
            JSONArray itemsJSON = new JSONArray();
            for (GalleryItem item : itemList) {
                JSONObject itemJSON = item.toJSON();
                if (itemJSON != null) {
                    itemsJSON.put(itemJSON);
                }
            }
            json.put("items", itemsJSON);
            return json;
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    public GalleryCollection(JSONObject json) {
        itemList = new ArrayList<>();
        try {
            JSONArray itemsJSON = json.getJSONArray("items");
            for (int i = 0; i < itemsJSON.length(); i++) {
                JSONObject itemJSON = itemsJSON.getJSONObject(i);
                itemList.add(new GalleryItem(itemJSON));
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    public GalleryCollection(Map<PageTitle, GalleryItem> galleryMap) {
        itemList = new ArrayList<>();
        Iterator iterator = galleryMap.keySet().iterator();
        while (iterator.hasNext()) {
            GalleryItem item = galleryMap.get(iterator.next());
            if (item.getWidth() < MIN_IMAGE_SIZE || item.getHeight() < MIN_IMAGE_SIZE) {
                // reject gallery items if they're too small
                continue;
            } else if (item.getMimeType().contains("svg") || item.getMimeType().contains("png")) {
                // also reject SVG and PNG items by default, because they're likely to be
                // logos and/or presentational images
                continue;
            }
            itemList.add(item);
        }
    }
}

@@ -34,45 +34,8 @@
 y;%0A%0A
-import org.wikipedia.page.PageTitle;%0A
 impo
@@ -57,16 +57,16 @@
 NArray;%0A
+
 import o
@@ -127,28 +127,38 @@
 ort 
-java.util.ArrayList;
+org.wikipedia.page.PageTitle;%0A
 %0Aimp
@@ -171,24 +171,25 @@
 va.util.
-Iterator
+ArrayList
 ;%0Aimport
@@ -1527,90 +1527,47 @@
     
-Iterator iterator = galleryMap.keySet().iterator();%0A        while (iterator.hasNex
+for (PageTitle title : galleryMap.keySe
 t())
@@ -1619,23 +1619,13 @@
 get(
+t
 it
+l
 e
-rator.next()
 );%0A 

replace while loop with for-each