package org.oskari.map.userlayer.input;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.geotools.data.DataStore;
import org.geotools.data.ogr.OGRDataStoreFactory;
import org.geotools.data.ogr.bridj.BridjOGRDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.referencing.CRS;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;
import fi.nls.oskari.service.ServiceException;

public class GPXParser implements FeatureCollectionParser {

    private static final Logger LOG = LogFactory.getLogger(KMLParser.class);

    public static final String SUFFIX = "GPX";

    private static final String[] TYPENAMES = {
            "tracks",
            "routes",
            "waypoints"
    };

    @Override
    public SimpleFeatureCollection parse(File file, CoordinateReferenceSystem sourceCRS,
            CoordinateReferenceSystem targetCRS) throws ServiceException {
        OGRDataStoreFactory factory = new BridjOGRDataStoreFactory();
        if (!factory.isAvailable()) {
            throw new ServiceException("GDAL library is not found for GPX import");
        }

        try {
            // GPX always lon,lat 4326
            sourceCRS = CRS.decode("EPSG:4326", true);
        } catch (FactoryException e) {
            throw new ServiceException("Failed to decode EPSG:4326");
        }

        Map<String, String> connectionParams = new HashMap<>();
        connectionParams.put("DriverName", "GPX");
        connectionParams.put("DatasourceName", file.getAbsolutePath());

        DataStore store = null;
        try {
            store = factory.createDataStore(connectionParams);
            String[] storeTypeNames = store.getTypeNames();
            LOG.debug("Found typeNames from GPX:", storeTypeNames);
            for (String typeName : TYPENAMES) {
                if (!contains(storeTypeNames, typeName)) {
                    LOG.debug("typeName not found from GPX:", typeName);
                    continue;
                }
                SimpleFeatureSource source = store.getFeatureSource(typeName);
                SimpleFeatureCollection collection = FeatureCollectionParsers.read(source, sourceCRS, targetCRS);
                if (!collection.isEmpty()) {
                    return collection;
                }
                LOG.info("FeatureCollection was empty, typeName:", typeName);
            }
            throw new ServiceException("Could not find any non-empty FeatureCollections from GPX file");
        } catch (Exception e) {
            throw new ServiceException("GPX parsing failed", e);
        } finally {
            if (store != null) {
                store.dispose();
            }
        }
    }

    private boolean contains(String[] a, String k) {
        for (String s : a) {
            if (s.equals(k)) {
                return true;
            }
        }
        return false;
    }

}

@@ -55,16 +55,41 @@
 o.File;%0A
+import java.util.Arrays;%0A
 import j
@@ -2070,17 +2070,21 @@
 if (
-!contains
+Arrays.stream
 (sto
@@ -2094,18 +2094,42 @@
 ypeNames
-, 
+).noneMatch(s -%3E s.equals(
 typeName
@@ -2130,16 +2130,17 @@
 peName))
+)
  %7B%0A     
@@ -2965,201 +2965,6 @@
  %7D%0A%0A
-    private boolean contains(String%5B%5D a, String k) %7B%0A        for (String s : a) %7B%0A            if (s.equals(k)) %7B%0A                return true;%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A
 %7D%0A

Use Stream.noneMatch() instead of writing a local helper function