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

LearnLib / automatalib / 6763327895

05 Nov 2023 07:29PM UTC coverage: 89.726% (-0.1%) from 89.868%
6763327895

push

github

mtf90
fix typo

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

99 existing lines in 18 files now uncovered.

15677 of 17472 relevant lines covered (89.73%)

1.66 hits per line

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

41.18
/api/src/main/java/net/automatalib/alphabet/Alphabet.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.alphabet;
17

18
import java.util.Collection;
19
import java.util.Comparator;
20
import java.util.Objects;
21
import java.util.function.IntFunction;
22
import java.util.function.ToIntFunction;
23

24
import net.automatalib.common.smartcollection.ArrayWritable;
25
import net.automatalib.common.util.mapping.Mapping;
26
import org.checkerframework.checker.nullness.qual.Nullable;
27

28
/**
29
 * Class implementing an (indexed) alphabet. An alphabet is a collection of symbols, where each symbol has a (unique)
30
 * index. Apart from serving as a collection, this class also provides a one-to-one mapping between symbols and
31
 * indices.
32
 *
33
 * @param <I>
34
 *         symbol type
35
 */
36
public interface Alphabet<I> extends ArrayWritable<I>, Collection<I>, Comparator<I>, IntFunction<I>, ToIntFunction<I> {
37

38
    @Override
39
    default I apply(int index) {
40
        return getSymbol(index);
1✔
41
    }
42

43
    /**
44
     * Returns the symbol with the given index in this alphabet.
45
     *
46
     * @param index
47
     *         the index of the requested symbol.
48
     *
49
     * @return symbol with the given index.
50
     *
51
     * @throws IllegalArgumentException
52
     *         if there is no symbol with this index.
53
     */
54
    I getSymbol(int index);
55

56
    @Override
57
    default int applyAsInt(I symbol) {
58
        return getSymbolIndex(symbol);
×
59
    }
60

61
    /**
62
     * Returns the index of the given symbol in the alphabet.
63
     *
64
     * @param symbol
65
     *         the symbol whose index should be determined
66
     *
67
     * @return the index of the given symbol
68
     *
69
     * @throws IllegalArgumentException
70
     *         if the provided symbol does not belong to the alphabet.
71
     */
72
    int getSymbolIndex(I symbol);
73

74
    @Override
75
    default int compare(I o1, I o2) {
76
        return getSymbolIndex(o1) - getSymbolIndex(o2);
1✔
77
    }
78

79
    @Override
80
    default void writeToArray(int offset, @Nullable Object[] array, int tgtOfs, int num) {
81
        for (int i = 0; i < num; i++) {
1✔
82
            array[tgtOfs + i] = getSymbol(offset + i);
1✔
83
        }
84
    }
1✔
85

86
    default <I2> Mapping<I2, I> translateFrom(Alphabet<I2> other) {
87
        if (other.size() > size()) {
1✔
88
            throw new IllegalArgumentException(
×
UNCOV
89
                    "Cannot translate from an alphabet with " + other.size() + " elements into an alphabet with only " +
×
UNCOV
90
                    size() + " elements");
×
91
        }
92
        return i -> getSymbol(other.getSymbolIndex(i));
1✔
93
    }
94

95
    /**
96
     * Checks whether the given symbol is part of the alphabet.
97
     * <p>
98
     * <b>Caution:</b> the default implementation is rather inefficient and should be overridden, if possible.
99
     *
100
     * @param symbol
101
     *         the symbol to check
102
     *
103
     * @return {@code true} iff the symbol is part of the alphabet
104
     */
105
    default boolean containsSymbol(I symbol) {
106
        try {
107
            int index = getSymbolIndex(symbol);
×
UNCOV
108
            if (index < 0 || index >= size()) {
×
109
                return false;
×
110
            }
111
            return Objects.equals(symbol, getSymbol(index));
×
UNCOV
112
        } catch (IllegalArgumentException ex) {
×
UNCOV
113
            return false;
×
114
        }
115
    }
116
}
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