package cgeo.geocaching.maps.mapsforge.v6.caches;

import android.support.annotation.NonNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;

import org.mapsforge.map.layer.Layer;

public class GeoitemLayers implements Collection<GeoitemLayer> {

    /**
     * ordered set of items to be displayed
     */
    private final HashMap<String, GeoitemLayer> geoitems = new LinkedHashMap<>();

    public synchronized Collection<String> getGeocodes() {
        return new ArrayList<>(geoitems.keySet());
    }

    public synchronized Collection<Layer> getAsLayers() {
        return new ArrayList<Layer>(this.geoitems.values());
    }

    public synchronized GeoitemLayer getItem(final String itemCode) {
        return geoitems.get(itemCode);
    }

    public synchronized Collection<Layer> getMatchingLayers(final Collection<String> newCodes) {
        final ArrayList<Layer> result = new ArrayList<>();
        for (final String code : newCodes) {
            if (geoitems.containsKey(code)) {
                result.add(geoitems.get(code));
            }
        }
        return result;
    }

    private boolean addInternal(final GeoitemLayer geoitem) {
        return geoitems.put(geoitem.getItemCode(), geoitem) != null;
    }

    @Override
    public synchronized boolean add(final GeoitemLayer geoitem) {
        return addInternal(geoitem);
    }

    @Override
    public synchronized boolean addAll(@NonNull final Collection<? extends GeoitemLayer> geoitemsIn) {
        boolean result = true;
        for (final GeoitemLayer geoitem : geoitemsIn) {
            if (!this.addInternal(geoitem)) {
                result = false;
            }
        }
        return result;
    }

    @Override
    public synchronized void clear() {
        this.geoitems.clear();
    }

    @Override
    public synchronized boolean contains(final Object object) {
        return this.geoitems.containsValue(object);
    }

    @Override
    public synchronized boolean containsAll(@NonNull final Collection<?> items) {
        return this.geoitems.values().containsAll(items);
    }

    @Override
    public synchronized boolean isEmpty() {
        return this.geoitems.isEmpty();
    }

    @Override
    @NonNull
    public synchronized Iterator<GeoitemLayer> iterator() {
        return new ArrayList<>(this.geoitems.values()).iterator();
    }

    private boolean removeInternal(final Object object) {
        if (object instanceof GeoitemLayer) {
            final GeoitemLayer item = (GeoitemLayer) object;
            return this.geoitems.remove(item.getItem().getItemCode()) != null;

        }

        return false;
    }

    @Override
    public synchronized boolean remove(final Object object) {
        return removeInternal(object);
    }

    @Override
    public synchronized boolean removeAll(@NonNull final Collection<?> items) {
        boolean result = true;
        for (final Object item : items) {
            if (!this.removeInternal(item)) {
                result = false;
            }
        }
        return result;
    }

    @Override
    public synchronized boolean retainAll(@NonNull final Collection<?> items) {

        return this.geoitems.values().retainAll(items);
    }

    @Override
    public synchronized int size() {

        return this.geoitems.size();
    }

    @Override
    @NonNull
    public synchronized Object[] toArray() {

        return this.geoitems.values().toArray();
    }

    @SuppressWarnings("hiding")
    @Override
    @NonNull
    public synchronized <GeoitemLayer> GeoitemLayer[] toArray(final GeoitemLayer[] array) {

        return this.geoitems.values().toArray(array);
    }

}

@@ -308,26 +308,24 @@
 lements 
-Collection
+Iterable
 %3CGeoitem
@@ -1194,592 +1194,136 @@
    p
-rivate boolean addInternal(final GeoitemLayer geoitem) %7B%0A        return geoitems.put(geoitem.getItemCode(), geoitem) != null;%0A    %7D%0A%0A    @Override%0A    public synchronized boolean add(final GeoitemLayer geoitem) %7B%0A        return addInternal(geoitem);%0A    %7D%0A%0A    @Override%0A    public synchronized boolean addAll(@NonNull final Collection%3C? extends GeoitemLayer%3E geoitemsIn) %7B%0A        boolean result = true;%0A        for (final GeoitemLayer geoitem : geoitemsIn) %7B%0A            if (!this.addInternal(geoitem)) %7B%0A                result = false;%0A            %7D%0A        %7D%0A        return result
+ublic synchronized boolean add(final GeoitemLayer geoitem) %7B%0A        return geoitems.put(geoitem.getItemCode(), geoitem) != null
 ;%0A  
@@ -1323,38 +1323,24 @@
 ull;%0A    %7D%0A%0A
-    @Override%0A
     public s
@@ -1408,411 +1408,8 @@
  %7D%0A%0A
-    @Override%0A    public synchronized boolean contains(final Object object) %7B%0A        return this.geoitems.containsValue(object);%0A    %7D%0A%0A    @Override%0A    public synchronized boolean containsAll(@NonNull final Collection%3C?%3E items) %7B%0A        return this.geoitems.values().containsAll(items);%0A    %7D%0A%0A    @Override%0A    public synchronized boolean isEmpty() %7B%0A        return this.geoitems.isEmpty();%0A    %7D%0A%0A
     
@@ -1570,22 +1570,34 @@
 %7D%0A%0A    p
-rivate
+ublic synchronized
  boolean
@@ -1603,24 +1603,16 @@
 n remove
-Internal
 (final O
@@ -1862,1011 +1862,76 @@
     
-@Override%0A    public synchronized boolean remove(final Object object) %7B%0A        return removeInternal(object);%0A    %7D%0A%0A    @Override%0A    public synchronized boolean removeAll(@NonNull final Collection%3C?%3E items) %7B%0A        boolean result = true;%0A        for (final Object item : items) %7B%0A            if (!this.removeInternal(item)) %7B%0A                result = false;%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A%0A    @Override%0A    public synchronized boolean retainAll(@NonNull final Collection%3C?%3E items) %7B%0A%0A        return this.geoitems.values().retainAll(items);%0A    %7D%0A%0A    @Override%0A    public synchronized int size() %7B%0A%0A        return this.geoitems.size();%0A    %7D%0A%0A    @Override%0A    @NonNull%0A    public synchronized Object%5B%5D toArray() %7B%0A%0A        return this.geoitems.values().toArray();%0A    %7D%0A%0A    @SuppressWarnings(%22hiding%22)%0A    @Override%0A    @NonNull%0A    public synchronized %3CGeoitemLayer%3E GeoitemLayer%5B%5D toArray(final GeoitemLayer%5B%5D array) %7B%0A%0A        return this.geoitems.values().toArray(array
+public synchronized int size() %7B%0A%0A        return this.geoitems.size(
 );%0A 

Implement more appropriate interface and remove unused methods