• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LearnLib / automatalib / 6668764019

27 Oct 2023 02:48PM UTC coverage: 89.69% (+0.007%) from 89.683%
6668764019

push

github

mtf90
cleanup (Idefinite)Graph interfaces

22 of 22 new or added lines in 10 files covered. (100.0%)

15832 of 17652 relevant lines covered (89.69%)

1.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

44.12
/core/src/main/java/net/automatalib/graph/map/SimpleMapGraph.java
1
/* Copyright (C) 2013-2023 TU Dortmund
2
 * This file is part of AutomataLib, http://www.automatalib.net/.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package net.automatalib.graph.map;
17

18
import java.util.Collection;
19
import java.util.Collections;
20
import java.util.HashMap;
21
import java.util.HashSet;
22
import java.util.Iterator;
23
import java.util.Map;
24
import java.util.Objects;
25
import java.util.Set;
26
import java.util.function.Supplier;
27

28
import com.google.common.collect.Iterators;
29
import net.automatalib.graph.MutableGraph;
30
import net.automatalib.graph.ShrinkableGraph;
31
import org.checkerframework.checker.nullness.qual.Nullable;
32

33
/**
34
 * A very simple graph realization, where nodes can be arbitrary Java objects. This graph does not support edge
35
 * properties.
36
 * <p>
37
 * This class provides maximum flexibility, but should only be used if performance is not a major concern.
38
 *
39
 * @param <N>
40
 *         node type
41
 */
42
public class SimpleMapGraph<@Nullable N> implements MutableGraph<N, N, N, Void>, ShrinkableGraph<N, N> {
43

44
    private final Map<N, Collection<N>> structureMap;
45
    private final Supplier<? extends Collection<N>> adjCollSupplier;
46

47
    /**
48
     * Initializes a graph where the adjacency structure is stored using a {@link HashMap}, and adjacency information
49
     * for a single node is stored using {@link HashSet}s.
50
     */
51
    public SimpleMapGraph() {
52
        this(HashSet::new);
1✔
53
    }
1✔
54

55
    /**
56
     * Initializes a graph where the adjacency structure is stored using a {@link HashMap}, and adjacency information
57
     * for a single node is stored in data structures created via the provided supplier.
58
     *
59
     * @param adjCollSupplier
60
     *         the supplier for per-node adjacency collections
61
     */
62
    public SimpleMapGraph(Supplier<? extends Collection<N>> adjCollSupplier) {
63
        this(new HashMap<>(), adjCollSupplier);
1✔
64
    }
1✔
65

66
    /**
67
     * Initializes a graph using the given adjacency structure, and adjacency information for a single node is stored
68
     * using {@link HashSet}s.
69
     *
70
     * @param structureMap
71
     *         the map for the overall graph structure
72
     */
73
    public SimpleMapGraph(Map<N, Collection<N>> structureMap) {
74
        this(structureMap, HashSet::new);
×
75
    }
×
76

77
    /**
78
     * Initializes a graph using the given adjacency structure, and adjacency information for a single node is stored in
79
     * data structures created via the provided supplier.
80
     *
81
     * @param structureMap
82
     *         the map for the overall graph structure
83
     * @param adjCollSupplier
84
     *         the supplier for per-node adjacency collections
85
     */
86
    public SimpleMapGraph(Map<N, Collection<N>> structureMap, Supplier<? extends Collection<N>> adjCollSupplier) {
1✔
87
        this.structureMap = structureMap;
1✔
88
        this.adjCollSupplier = adjCollSupplier;
1✔
89
    }
1✔
90

91
    @Override
92
    public Collection<N> getAdjacentTargets(N node) {
93
        return getOutgoingEdges(node);
×
94
    }
95

96
    @Override
97
    public Iterator<N> getAdjacentTargetsIterator(N node) {
98
        return getOutgoingEdgesIterator(node);
×
99
    }
100

101
    @SuppressWarnings("nullness") // the passed structureMap decides whether we support nulls
102
    @Override
103
    public Collection<N> getOutgoingEdges(N node) {
104
        return Collections.unmodifiableCollection(structureMap.getOrDefault(node, Collections.emptySet()));
1✔
105
    }
106

107
    @Override
108
    public N getTarget(N edge) {
109
        return edge;
1✔
110
    }
111

112
    @Override
113
    public Set<N> getNodes() {
114
        return Collections.unmodifiableSet(structureMap.keySet());
×
115
    }
116

117
    @Override
118
    public Iterator<N> iterator() {
119
        return Iterators.unmodifiableIterator(structureMap.keySet().iterator());
1✔
120
    }
121

122
    @Override
123
    public N getNodeProperty(N node) {
124
        return node;
×
125
    }
126

127
    @Override
128
    public Void getEdgeProperty(N edge) {
129
        return null;
×
130
    }
131

132
    @Override
133
    public N addNode(@Nullable N property) {
134
        structureMap.putIfAbsent(property, adjCollSupplier.get());
1✔
135
        return property;
1✔
136
    }
137

138
    @SuppressWarnings("nullness") // the passed structureMap decides whether we support nulls
139
    @Override
140
    public N connect(N source, N target, Void property) {
141
        structureMap.get(source).add(target);
1✔
142
        return target;
1✔
143
    }
144

145
    @Override
146
    @Deprecated
147
    public void setNodeProperty(N node, N property) {
148
        if (!Objects.equals(node, property)) {
×
149
            throw new IllegalArgumentException();
×
150
        }
151
    }
×
152

153
    @Override
154
    public void setEdgeProperty(N edge, Void property) {}
×
155

156
    @Override
157
    public void removeNode(N node, @Nullable N replacement) {
158
        structureMap.remove(node);
×
159
        structureMap.values().forEach(a -> {
×
160
            if (a.remove(node) && replacement != null) {
×
161
                a.add(replacement);
×
162
            }
163
        });
×
164
    }
×
165

166
    @SuppressWarnings("nullness") // the passed structureMap decides whether we support nulls
167
    @Override
168
    public void removeEdge(N node, N edge) {
169
        structureMap.get(node).remove(edge);
×
170
    }
×
171

172
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc