< Summary

Class:Itinero.Network.Profiles.RoutingNetworkProfilesConfiguration
Assembly:Itinero
File(s):/home/runner/work/routing2/routing2/src/Itinero/Network/Profiles/RoutingNetworkProfilesConfiguration.cs
Covered lines:0
Uncovered lines:53
Coverable lines:53
Total lines:99
Line coverage:0% (0 of 53)
Covered branches:0
Total branches:10
Branch coverage:0% (0 of 10)
Tag:224_14471318300

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor()100%10%
.ctor(...)100%10%
Clone()100%10%
HasProfile(...)100%10%
AddProfile(...)0%20%
TryGetProfileHandlerEdgeTypesCache(...)0%20%
get_Profiles()100%10%
WriteTo(...)0%20%
ReadFrom(...)0%40%

File(s)

/home/runner/work/routing2/routing2/src/Itinero/Network/Profiles/RoutingNetworkProfilesConfiguration.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.IO;
 3using System.Linq;
 4using Itinero.IO;
 5using Itinero.Profiles;
 6using Itinero.Profiles.Serialization;
 7using Itinero.Routing.Costs.Caches;
 8
 9namespace Itinero.Network.Profiles;
 10
 11internal class RoutingNetworkProfilesConfiguration
 12{
 13    private readonly Dictionary<string, (Profile profile, EdgeFactorCache cache)> _profiles;
 14
 015    public RoutingNetworkProfilesConfiguration()
 016    {
 017        _profiles = new Dictionary<string, (Profile profile, EdgeFactorCache cache)>();
 018    }
 19
 020    private RoutingNetworkProfilesConfiguration(
 021        Dictionary<string, (Profile profile, EdgeFactorCache cache)> profiles)
 022    {
 023        _profiles = new Dictionary<string, (Profile profile, EdgeFactorCache cache)>(profiles);
 024    }
 25
 26    public RoutingNetworkProfilesConfiguration Clone()
 027    {
 028        return new(_profiles);
 029    }
 30
 31    public bool HasProfile(string name)
 032    {
 033        return _profiles.ContainsKey(name);
 034    }
 35
 36    public bool AddProfile(Profile profile)
 037    {
 038        if (_profiles.ContainsKey(profile.Name))
 039        {
 040            return false;
 41        }
 42
 043        _profiles[profile.Name] = (profile, new EdgeFactorCache());
 044        return true;
 045    }
 46
 47    internal bool TryGetProfileHandlerEdgeTypesCache(Profile profile, out EdgeFactorCache? cache)
 048    {
 049        cache = null;
 050        if (!_profiles.TryGetValue(profile.Name, out var profileValue))
 051        {
 052            return false;
 53        }
 54
 055        cache = profileValue.cache;
 056        return true;
 057    }
 58
 059    public IEnumerable<Profile> Profiles => _profiles.Values.Select(x => x.profile);
 60
 61    public void WriteTo(Stream stream, IProfileSerializer profileSerializer)
 062    {
 63        // write version #.
 064        stream.WriteVarInt32(1);
 65
 66        // write number of profiles.
 067        stream.WriteVarInt32(_profiles.Count);
 68
 69        // write profiles.
 070        foreach (var (profile, _) in _profiles.Values)
 071        {
 072            stream.WriteProfile(profile, profileSerializer);
 073        }
 074    }
 75
 76    public static RoutingNetworkProfilesConfiguration
 77        ReadFrom(Stream stream, IProfileSerializer profileSerializer)
 078    {
 79        // verify version #.
 080        var version = stream.ReadVarInt32();
 081        if (version != 1)
 082        {
 083            throw new InvalidDataException("Invalid version #.");
 84        }
 85
 86        // read number of profiles.
 087        var profileCount = stream.ReadVarInt32();
 88
 89        // write profiles.
 090        var profiles = new Dictionary<string, (Profile profile, EdgeFactorCache cache)>(profileCount);
 091        for (var p = 0; p < profileCount; p++)
 092        {
 093            var profile = profileSerializer.ReadFrom(stream);
 094            profiles[profile.Name] = (profile, new EdgeFactorCache());
 095        }
 96
 097        return new RoutingNetworkProfilesConfiguration(profiles);
 098    }
 99}