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

LearnLib / automatalib / 13138848026

04 Feb 2025 02:53PM UTC coverage: 92.108% (+2.2%) from 89.877%
13138848026

push

github

mtf90
[maven-release-plugin] prepare release automatalib-0.12.0

16609 of 18032 relevant lines covered (92.11%)

1.7 hits per line

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

27.27
/commons/util/src/main/java/net/automatalib/common/util/mapping/Mappings.java
1
/* Copyright (C) 2013-2025 TU Dortmund University
2
 * This file is part of AutomataLib <https://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.common.util.mapping;
17

18
import java.util.Collection;
19
import java.util.Iterator;
20
import java.util.Map;
21

22
import net.automatalib.common.util.collection.CollectionUtil;
23
import net.automatalib.common.util.collection.IterableUtil;
24
import net.automatalib.common.util.collection.IteratorUtil;
25
import org.checkerframework.checker.index.qual.NonNegative;
26
import org.checkerframework.checker.nullness.qual.Nullable;
27

28
/**
29
 * Collection of various methods dealing with {@link Mapping}s.
30
 */
31
public final class Mappings {
32

33
    private static final Mapping<?, ?> NULL_MAPPING = (Mapping<Object, @Nullable Object>) elem -> null;
1✔
34

35
    private static final Mapping<?, ?> IDENTITY_MAPPING = (Mapping<Object, Object>) elem -> elem;
1✔
36

37
    private static final Mapping<?, String> TOSTRING_MAPPING = (Mapping<Object, String>) String::valueOf;
1✔
38

39
    private Mappings() {
40
        // prevent instantiation
41
    }
42

43
    /**
44
     * Retrieves the {@code null} mapping, which maps each domain value to {@code null}.
45
     *
46
     * @param <D>
47
     *         domain type
48
     * @param <R>
49
     *         range type
50
     *
51
     * @return the {@code null} mapping.
52
     */
53
    @SuppressWarnings("unchecked")
54
    public static <D, R> Mapping<D, R> nullMapping() {
55
        return (Mapping<D, R>) NULL_MAPPING;
1✔
56
    }
57

58
    /**
59
     * Retrieves the identity mapping, which maps each domain value to itself.
60
     *
61
     * @param <T>
62
     *         domain/range type
63
     *
64
     * @return the identity mapping.
65
     */
66
    @SuppressWarnings("unchecked")
67
    public static <T> Mapping<T, T> identity() {
68
        return (Mapping<T, T>) IDENTITY_MAPPING;
×
69
    }
70

71
    /**
72
     * Returns a mapping that maps objects to their {@link String} representation, as obtained by
73
     * {@link String#valueOf(Object)}.
74
     *
75
     * @param <D>
76
     *         domain type
77
     *
78
     * @return the "{@code toString()}" mapping
79
     */
80
    @SuppressWarnings("unchecked")
81
    public static <D> Mapping<D, String> toStringMapping() {
82
        return (Mapping<D, String>) TOSTRING_MAPPING;
×
83
    }
84

85
    /**
86
     * Returns a mapping that maps objects to a supertype representation.
87
     *
88
     * @param <D>
89
     *         domain type
90
     * @param <R>
91
     *         range type
92
     *
93
     * @return the "upcast" mapping
94
     */
95
    @SuppressWarnings("unchecked")
96
    public static <R, D extends R> Mapping<D, R> upcast() {
97
        return (Mapping<D, R>) IDENTITY_MAPPING;
×
98
    }
99

100
    /**
101
     * Retrieves the composition of two mappings, i.e., that mapping that results from applying the
102
     * {@link Mapping#get(Object)} method consecutively.
103
     *
104
     * @param <D>
105
     *         domain type of the first (and resulting) mapping
106
     * @param <I>
107
     *         intermediate type, range type of the first and domain type of the second mapping
108
     * @param <R>
109
     *         range type of the second (and resulting) mapping
110
     * @param first
111
     *         first mapping
112
     * @param second
113
     *         second mapping
114
     *
115
     * @return the composed mapping
116
     */
117
    public static <D, I, R> Mapping<D, R> compose(Mapping<D, ? extends I> first, Mapping<? super I, R> second) {
118
        return new MappingComposition<>(first, second);
1✔
119
    }
120

121
    /**
122
     * Applies a mapping to a collection, resulting in a collection containing the result of applying the specified
123
     * mapping to each element in the collection.
124
     * <p>
125
     * Note that more specific properties of the specified collection won't be preserved: If the given collection is
126
     * e.g. a set, and the provided mapping is not bijective, then the resulting collections may contain some values
127
     * multiple times.
128
     *
129
     * @param mapping
130
     *         the mapping to apply
131
     * @param coll
132
     *         the collection
133
     * @param <D>
134
     *         domain type
135
     * @param <R>
136
     *         range type
137
     *
138
     * @return the mapped collection
139
     */
140
    public static <D, R> Collection<R> apply(Mapping<? super D, ? extends R> mapping, Collection<D> coll) {
141
        return CollectionUtil.map(coll, mapping::get);
×
142
    }
143

144
    /**
145
     * Applies a mapping to an iterator. For the behavior, see {@link #apply(Mapping, Iterable)}. The resulting iterator
146
     * supports each operation which the underlying supports.
147
     *
148
     * @param mapping
149
     *         the mapping to apply
150
     * @param baseIt
151
     *         the underlying iterator
152
     * @param <D>
153
     *         domain type
154
     * @param <R>
155
     *         range type
156
     *
157
     * @return the mapped iterator
158
     */
159
    public static <D, R> Iterator<R> apply(Mapping<? super D, R> mapping, Iterator<? extends D> baseIt) {
160
        return IteratorUtil.map(baseIt, mapping::get);
×
161
    }
162

163
    /**
164
     * Applies a mapping to an iterable. The result is an iterable whose iterator returns the results of applying the
165
     * specified mapping to each of the elements returned by the original iterable.
166
     *
167
     * @param mapping
168
     *         the mapping to apply
169
     * @param it
170
     *         the underlying iterable
171
     * @param <D>
172
     *         domain type
173
     * @param <R>
174
     *         range type
175
     *
176
     * @return the mapped iterable.
177
     */
178
    public static <D, R> Iterable<R> apply(Mapping<? super D, ? extends R> mapping, Iterable<D> it) {
179
        return IterableUtil.map(it, mapping::get);
×
180
    }
181

182
    public static <D> D idGet(Mapping<D, D> mapping, D key) {
183
        return safeGet(mapping, key, key);
×
184
    }
185

186
    /**
187
     * Safely retrieves a value from a mapping. If the mapping is {@code null} or returns a {@code null} value, the
188
     * given fallback value is returned.
189
     *
190
     * @param mapping
191
     *         the mapping
192
     * @param key
193
     *         the key
194
     * @param fallback
195
     *         the fallback value to return if either the mapping or the originally returned value are {@code null}.
196
     * @param <D>
197
     *         domain type
198
     * @param <R>
199
     *         range type
200
     *
201
     * @return the value returned by the specified mapping, or the fallback value.
202
     */
203
    public static <D, R> R safeGet(@Nullable Mapping<? super D, ? extends R> mapping, D key, R fallback) {
204
        if (mapping == null) {
×
205
            return fallback;
×
206
        }
207
        R val = mapping.get(key);
×
208
        if (val == null) {
×
209
            return fallback;
×
210
        }
211
        return val;
×
212
    }
213

214
    public static <D, @Nullable R> R nullGet(@Nullable Mapping<? super D, ? extends R> mapping, D key) {
215
        return safeGet(mapping, key, null);
×
216
    }
217

218
    public static <D, R> Mapping<D, R> fromMap(Map<D, R> map) {
219
        return new MapMapping<>(map);
1✔
220
    }
221

222
    public static Mapping<String, Long> stringToIndex() {
223
        return StringIndexMapping::stringToIndex;
×
224
    }
225

226
    public static Mapping<@NonNegative Long, String> indexToString() {
227
        return StringIndexMapping::indexToString;
×
228
    }
229
}
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