<commit_before>/*
 * Copyright (c) 2015-2017, <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package no.vegvesen.nvdbapi.client.gson;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import no.vegvesen.nvdbapi.client.model.*;
import no.vegvesen.nvdbapi.client.model.areas.ContractArea;
import no.vegvesen.nvdbapi.client.model.areas.Route;
import no.vegvesen.nvdbapi.client.model.roadnet.roadsysref.RoadSysRef;
import no.vegvesen.nvdbapi.client.model.roadobjects.*;
import no.vegvesen.nvdbapi.client.model.datakatalog.DataType;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static no.vegvesen.nvdbapi.client.gson.GsonUtil.*;

public final class RoadObjectParser {
    private RoadObjectParser() {}

    public static RoadObject parse(Map<String, DataType> dataTypes, JsonObject obj) {
        Integer id = parseIntMember(obj, "id");

        Integer typeId = parseIntMember(obj, "metadata.type.id");
        Integer version = parseIntMember(obj, "metadata.versjon");
        LocalDate startDate = parseDateMember(obj, "metadata.startdato"), endDate = parseDateMember(obj, "metadata.sluttdato");
        LocalDateTime lastModified = parseDateTimeMember(obj, "metadata.sist_modifisert");

        List<Attribute> attributes = parseAttributes(dataTypes, obj);

        List<Association> childrenList = parseChildren(dataTypes, obj);

        List<Association> parentList = parseParents(dataTypes, obj);

        Location location = Optional.ofNullable(obj.get("lokasjon"))
                .map(e -> parseLocation(e.getAsJsonObject())).orElse(null);

        Geometry geometry = null;
        if (obj.has("geometri")) {
            geometry = GeometryParser.parse(obj.getAsJsonObject("geometri"));
        }

        List<Segment> segments = parseSegments(obj);

        return new RoadObject(id, typeId, version, startDate, endDate, segments, location, geometry,
                lastModified, attributes, childrenList, parentList);
    }

    private static List<Segment> parseSegments(JsonObject obj) {
        List<Segment> segments = Collections.emptyList();
        JsonArray segmenter = obj.getAsJsonArray("vegsegmenter");
        if (segmenter != null) {
            return StreamSupport.stream(segmenter.spliterator(), false)
                    .map(e -> parseSegment(e.getAsJsonObject()))
                    .collect(Collectors.toList());
        }
        return segments;
    }

    private static List<Association> parseParents(Map<String, DataType> dataTypes, JsonObject obj) {
        List<Association> parentList = Collections.emptyList();
        JsonArray parents = getArray(obj, "relasjoner.foreldre").orElse(null);
        return getAssociations(dataTypes, parentList, parents);
    }

    private static List<Association> parseChildren(Map<String, DataType> dataTypes, JsonObject obj) {
        List<Association> childrenList = Collections.emptyList();
        JsonArray children = getArray(obj, "relasjoner.barn").orElse(null);
        return getAssociations(dataTypes, childrenList, children);
    }

    private static List<Association> getAssociations(Map<String, DataType> dataTypes, List<Association> parentList, JsonArray parents) {
        if (parents != null) {
            parentList = StreamSupport.stream(parents.spliterator(), false)
                    .map(e -> parseAssociation(dataTypes, e.getAsJsonObject()))
                    .collect(Collectors.toList());
        }
        return parentList;
    }

    private static List<Attribute> parseAttributes(Map<String, DataType> dataTypes, JsonObject obj) {
        List<Attribute> attributes = Collections.emptyList();
        JsonArray egenskaper = obj.getAsJsonArray("egenskaper");
        if (egenskaper != null) {
            return StreamSupport.stream(egenskaper.spliterator(), false)
                    .map(e -> parseAttribute(dataTypes, e.getAsJsonObject()))
                    .collect(Collectors.toList());
        }
        return attributes;
    }

    private static Location parseLocation(JsonObject obj) {
        List<Integer> municipalities = parseIntListMember(obj, "kommuner");
        List<Integer> counties = parseIntListMember(obj, "fylker");
        List<Integer> regions  = parseIntListMember(obj, "regioner");
        List<Integer> departments = parseIntListMember(obj, "vegavdelinger");
        List<ContractArea> contractAreas = Collections.emptyList();
        if (obj.has("kontraktsområder")) {
            contractAreas = StreamSupport.stream(obj.getAsJsonArray("kontraktsområder").spliterator(), false)
                    .map(JsonElement::getAsJsonObject)
                    .map(AreaParser::parseContractArea)
                    .collect(Collectors.toList());
        }
        List<Route> nationalRoutes = Collections.emptyList();
        if (obj.has("riksvegruter")) {
            nationalRoutes = StreamSupport.stream(obj.getAsJsonArray("riksvegruter").spliterator(), false)
                    .map(JsonElement::getAsJsonObject).map(AreaParser::parseRoute)
                    .collect(Collectors.toList());
        }

        Geometry geometry = null;
        if (obj.has("geometri")) {
            geometry = GeometryParser.parse(obj.get("geometri").getAsJsonObject());
        }

        JsonArray refs = obj.getAsJsonArray("vegreferanser");
        List<RoadSysRef> roadRefs = Collections.emptyList();
        if (refs != null) {
            roadRefs = StreamSupport.stream(refs.spliterator(), false)
                    .map(JsonElement::getAsJsonObject)
                    .map(RoadSysRefParser::parse)
                    .collect(Collectors.toList());
        }<commit_msg>NVDB-3794: roadobjectparser : vegreferanser -> vegsystemreferanser<commit_after>/*
 * Copyright (c) 2015-2017, <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package no.vegvesen.nvdbapi.client.gson;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import no.vegvesen.nvdbapi.client.model.*;
import no.vegvesen.nvdbapi.client.model.areas.ContractArea;
import no.vegvesen.nvdbapi.client.model.areas.Route;
import no.vegvesen.nvdbapi.client.model.roadnet.roadsysref.RoadSysRef;
import no.vegvesen.nvdbapi.client.model.roadobjects.*;
import no.vegvesen.nvdbapi.client.model.datakatalog.DataType;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static no.vegvesen.nvdbapi.client.gson.GsonUtil.*;

public final class RoadObjectParser {
    private RoadObjectParser() {}

    public static RoadObject parse(Map<String, DataType> dataTypes, JsonObject obj) {
        Integer id = parseIntMember(obj, "id");

        Integer typeId = parseIntMember(obj, "metadata.type.id");
        Integer version = parseIntMember(obj, "metadata.versjon");
        LocalDate startDate = parseDateMember(obj, "metadata.startdato"), endDate = parseDateMember(obj, "metadata.sluttdato");
        LocalDateTime lastModified = parseDateTimeMember(obj, "metadata.sist_modifisert");

        List<Attribute> attributes = parseAttributes(dataTypes, obj);

        List<Association> childrenList = parseChildren(dataTypes, obj);

        List<Association> parentList = parseParents(dataTypes, obj);

        Location location = Optional.ofNullable(obj.get("lokasjon"))
                .map(e -> parseLocation(e.getAsJsonObject())).orElse(null);

        Geometry geometry = null;
        if (obj.has("geometri")) {
            geometry = GeometryParser.parse(obj.getAsJsonObject("geometri"));
        }

        List<Segment> segments = parseSegments(obj);

        return new RoadObject(id, typeId, version, startDate, endDate, segments, location, geometry,
                lastModified, attributes, childrenList, parentList);
    }

    private static List<Segment> parseSegments(JsonObject obj) {
        List<Segment> segments = Collections.emptyList();
        JsonArray segmenter = obj.getAsJsonArray("vegsegmenter");
        if (segmenter != null) {
            return StreamSupport.stream(segmenter.spliterator(), false)
                    .map(e -> parseSegment(e.getAsJsonObject()))
                    .collect(Collectors.toList());
        }
        return segments;
    }

    private static List<Association> parseParents(Map<String, DataType> dataTypes, JsonObject obj) {
        List<Association> parentList = Collections.emptyList();
        JsonArray parents = getArray(obj, "relasjoner.foreldre").orElse(null);
        return getAssociations(dataTypes, parentList, parents);
    }

    private static List<Association> parseChildren(Map<String, DataType> dataTypes, JsonObject obj) {
        List<Association> childrenList = Collections.emptyList();
        JsonArray children = getArray(obj, "relasjoner.barn").orElse(null);
        return getAssociations(dataTypes, childrenList, children);
    }

    private static List<Association> getAssociations(Map<String, DataType> dataTypes, List<Association> parentList, JsonArray parents) {
        if (parents != null) {
            parentList = StreamSupport.stream(parents.spliterator(), false)
                    .map(e -> parseAssociation(dataTypes, e.getAsJsonObject()))
                    .collect(Collectors.toList());
        }
        return parentList;
    }

    private static List<Attribute> parseAttributes(Map<String, DataType> dataTypes, JsonObject obj) {
        List<Attribute> attributes = Collections.emptyList();
        JsonArray egenskaper = obj.getAsJsonArray("egenskaper");
        if (egenskaper != null) {
            return StreamSupport.stream(egenskaper.spliterator(), false)
                    .map(e -> parseAttribute(dataTypes, e.getAsJsonObject()))
                    .collect(Collectors.toList());
        }
        return attributes;
    }

    private static Location parseLocation(JsonObject obj) {
        List<Integer> municipalities = parseIntListMember(obj, "kommuner");
        List<Integer> counties = parseIntListMember(obj, "fylker");
        List<Integer> regions  = parseIntListMember(obj, "regioner");
        List<Integer> departments = parseIntListMember(obj, "vegavdelinger");
        List<ContractArea> contractAreas = Collections.emptyList();
        if (obj.has("kontraktsområder")) {
            contractAreas = StreamSupport.stream(obj.getAsJsonArray("kontraktsområder").spliterator(), false)
                    .map(JsonElement::getAsJsonObject)
                    .map(AreaParser::parseContractArea)
                    .collect(Collectors.toList());
        }
        List<Route> nationalRoutes = Collections.emptyList();
        if (obj.has("riksvegruter")) {
            nationalRoutes = StreamSupport.stream(obj.getAsJsonArray("riksvegruter").spliterator(), false)
                    .map(JsonElement::getAsJsonObject).map(AreaParser::parseRoute)
                    .collect(Collectors.toList());
        }

        Geometry geometry = null;
        if (obj.has("geometri")) {
            geometry = GeometryParser.parse(obj.get("geometri").getAsJsonObject());
        }

        JsonArray refs = obj.getAsJsonArray("vegsystemreferanser");
        List<RoadSysRef> roadRefs = Collections.emptyList();
        if (refs != null) {
            roadRefs = StreamSupport.stream(refs.spliterator(), false)
                    .map(JsonElement::getAsJsonObject)
                    .map(RoadSysRefParser::parse)
                    .collect(Collectors.toList());
        }
{'fraction_non_alphanumeric': 0.10227352713451116, 'fraction_numerical': 0.0016893080875624693, 'mean_word_length': 3.3080654942389325, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '938420', 'n_tokens_mistral': 3965, 'n_tokens_neox': 3768, 'n_words': 955}