• 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

50.0
/api/src/main/java/net/automatalib/alphabet/Alphabet.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.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 net.automatalib.exception.GrowingAlphabetNotSupportedException;
27
import org.checkerframework.checker.nullness.qual.Nullable;
28

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

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

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

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

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

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

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

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

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

118
    /**
119
     * Casts {@code this} alphabet to a {@link GrowingAlphabet} if possible (i.e., if the alphabet actually is a
120
     * {@link GrowingAlphabet}). Otherwise, throws a {@link GrowingAlphabetNotSupportedException}.
121
     *
122
     * @return the same alphabet instance, casted to a {@link GrowingAlphabet}.
123
     *
124
     * @throws GrowingAlphabetNotSupportedException
125
     *         if {@code this} alphabet does not implement {@link GrowingAlphabet}
126
     */
127
    default GrowingAlphabet<I> asGrowingAlphabetOrThrowException() {
128
        if (this instanceof GrowingAlphabet) {
1✔
129
            return (GrowingAlphabet<I>) this;
1✔
130
        } else {
131
            throw new GrowingAlphabetNotSupportedException(this);
1✔
132
        }
133
    }
134
}
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