1 /*
2  * Copyright (C) 2022 The Android Open Source Project
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 
17 package android.speech;
18 
19 import android.annotation.NonNull;
20 import android.os.Parcelable;
21 
22 import com.android.internal.util.DataClass;
23 import com.android.internal.util.Preconditions;
24 
25 import java.util.ArrayList;
26 import java.util.List;
27 
28 /**
29  * List of alternative hypotheses for a specific span of a speech recognition result string.
30  *
31  * <p> A single {@link SpeechRecognizer} result is represented as a {@link String}. For a specific
32  * span of the originally recognized result string, the recognizer may provide alternative
33  * hypotheses of what it may have recognized. A span is specifically a substring and is thereby
34  * defined by its start and end positions in the originally recognized string. Alternative
35  * hypotheses are represented as strings which may replace that substring.
36  *
37  * <p> These alternatives can be used to enhance recognition by adding/re-ranking/applying or in
38  * other ways manipulating the SpeechRecognizer results before powering dictation features.
39  */
40 @DataClass(
41         genEqualsHashCode = true,
42         genParcelable = true,
43         genToString = true
44 )
45 public final class AlternativeSpan implements Parcelable {
46     /**
47      * The start position of the span of the originally recognized string.
48      *
49      * <p> Must be set to a non-negative value before building.
50      */
51     private final int mStartPosition;
52 
53     /**
54      * The exclusive end position of the span of the originally recognized string.
55      *
56      * <p> Must be set to a value greater than the start of the span before building.
57      */
58     private final int mEndPosition;
59 
60     /**
61      * All the alternatives for the [mStart, mEnd) span.
62      *
63      * <p> Must not be empty. The object will only be created
64      * if there are some alternatives for the given span.
65      *
66      * <p> The alternatives may be strings of different lengths than the span they can replace.
67      */
68     @NonNull
69     @DataClass.PluralOf("alternative")
70     private final List<String> mAlternatives;
71 
onConstructed()72     private void onConstructed() {
73         Preconditions.checkArgumentNonnegative(mStartPosition,
74                 "The range start must be non-negative.");
75         Preconditions.checkArgument(mStartPosition < mEndPosition,
76                 "Illegal range [%d, %d), must be start < end.", mStartPosition, mEndPosition);
77         Preconditions.checkCollectionNotEmpty(mAlternatives,
78                 "List of alternative strings must not be empty.");
79     }
80 
81 
82 
83     // Code below generated by codegen v1.0.23.
84     //
85     // DO NOT MODIFY!
86     // CHECKSTYLE:OFF Generated code
87     //
88     // To regenerate run:
89     // $ codegen $ANDROID_BUILD_TOP/frameworks/base/core/java/android/speech/AlternativeSpan.java
90     //
91     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
92     //   Settings > Editor > Code Style > Formatter Control
93     //@formatter:off
94 
95 
96     /**
97      * Creates a new AlternativeSpan.
98      *
99      * @param startPosition
100      *   The start position of the span of the originally recognized string.
101      *
102      *   <p> Must be set to a non-negative value before building.
103      * @param endPosition
104      *   The exclusive end position of the span of the originally recognized string.
105      *
106      *   <p> Must be set to a value greater than the start of the span before building.
107      * @param alternatives
108      *   All the alternatives for the [mStart, mEnd) span.
109      *
110      *   <p> Must not be empty. The object will only be created
111      *   if there are some alternatives for the given span.
112      *
113      *   <p> The alternatives may be strings of different lengths than the span they can replace.
114      */
115     @DataClass.Generated.Member
116     public AlternativeSpan(
117             int startPosition,
118             int endPosition,
119             @NonNull List<String> alternatives) {
120         this.mStartPosition = startPosition;
121         this.mEndPosition = endPosition;
122         this.mAlternatives = alternatives;
123         com.android.internal.util.AnnotationValidations.validate(
124                 NonNull.class, null, mAlternatives);
125 
126         onConstructed();
127     }
128 
129     /**
130      * The start position of the span of the originally recognized string.
131      *
132      * <p> Must be set to a non-negative value before building.
133      */
134     @DataClass.Generated.Member
135     public int getStartPosition() {
136         return mStartPosition;
137     }
138 
139     /**
140      * The exclusive end position of the span of the originally recognized string.
141      *
142      * <p> Must be set to a value greater than the start of the span before building.
143      */
144     @DataClass.Generated.Member
145     public int getEndPosition() {
146         return mEndPosition;
147     }
148 
149     /**
150      * All the alternatives for the [mStart, mEnd) span.
151      *
152      * <p> Must not be empty. The object will only be created
153      * if there are some alternatives for the given span.
154      *
155      * <p> The alternatives may be strings of different lengths than the span they can replace.
156      */
157     @DataClass.Generated.Member
158     public @NonNull List<String> getAlternatives() {
159         return mAlternatives;
160     }
161 
162     @Override
163     @DataClass.Generated.Member
164     public String toString() {
165         // You can override field toString logic by defining methods like:
166         // String fieldNameToString() { ... }
167 
168         return "AlternativeSpan { " +
169                 "startPosition = " + mStartPosition + ", " +
170                 "endPosition = " + mEndPosition + ", " +
171                 "alternatives = " + mAlternatives +
172         " }";
173     }
174 
175     @Override
176     @DataClass.Generated.Member
177     public boolean equals(@android.annotation.Nullable Object o) {
178         // You can override field equality logic by defining either of the methods like:
179         // boolean fieldNameEquals(AlternativeSpan other) { ... }
180         // boolean fieldNameEquals(FieldType otherValue) { ... }
181 
182         if (this == o) return true;
183         if (o == null || getClass() != o.getClass()) return false;
184         @SuppressWarnings("unchecked")
185         AlternativeSpan that = (AlternativeSpan) o;
186         //noinspection PointlessBooleanExpression
187         return true
188                 && mStartPosition == that.mStartPosition
189                 && mEndPosition == that.mEndPosition
190                 && java.util.Objects.equals(mAlternatives, that.mAlternatives);
191     }
192 
193     @Override
194     @DataClass.Generated.Member
195     public int hashCode() {
196         // You can override field hashCode logic by defining methods like:
197         // int fieldNameHashCode() { ... }
198 
199         int _hash = 1;
200         _hash = 31 * _hash + mStartPosition;
201         _hash = 31 * _hash + mEndPosition;
202         _hash = 31 * _hash + java.util.Objects.hashCode(mAlternatives);
203         return _hash;
204     }
205 
206     @Override
207     @DataClass.Generated.Member
208     public void writeToParcel(@NonNull android.os.Parcel dest, int flags) {
209         // You can override field parcelling by defining methods like:
210         // void parcelFieldName(Parcel dest, int flags) { ... }
211 
212         dest.writeInt(mStartPosition);
213         dest.writeInt(mEndPosition);
214         dest.writeStringList(mAlternatives);
215     }
216 
217     @Override
218     @DataClass.Generated.Member
219     public int describeContents() { return 0; }
220 
221     /** @hide */
222     @SuppressWarnings({"unchecked", "RedundantCast"})
223     @DataClass.Generated.Member
224     /* package-private */ AlternativeSpan(@NonNull android.os.Parcel in) {
225         // You can override field unparcelling by defining methods like:
226         // static FieldType unparcelFieldName(Parcel in) { ... }
227 
228         int startPosition = in.readInt();
229         int endPosition = in.readInt();
230         List<String> alternatives = new ArrayList<>();
231         in.readStringList(alternatives);
232 
233         this.mStartPosition = startPosition;
234         this.mEndPosition = endPosition;
235         this.mAlternatives = alternatives;
236         com.android.internal.util.AnnotationValidations.validate(
237                 NonNull.class, null, mAlternatives);
238 
239         onConstructed();
240     }
241 
242     @DataClass.Generated.Member
243     public static final @NonNull Parcelable.Creator<AlternativeSpan> CREATOR
244             = new Parcelable.Creator<AlternativeSpan>() {
245         @Override
246         public AlternativeSpan[] newArray(int size) {
247             return new AlternativeSpan[size];
248         }
249 
250         @Override
251         public AlternativeSpan createFromParcel(@NonNull android.os.Parcel in) {
252             return new AlternativeSpan(in);
253         }
254     };
255 
256     @DataClass.Generated(
257             time = 1656603431902L,
258             codegenVersion = "1.0.23",
259             sourceFile = "frameworks/base/core/java/android/speech/AlternativeSpan.java",
260             inputSignatures = "private final  int mStartPosition\nprivate final  int mEndPosition\nprivate final @android.annotation.NonNull @com.android.internal.util.DataClass.PluralOf(\"alternative\") java.util.List<java.lang.String> mAlternatives\nprivate  void onConstructed()\nclass AlternativeSpan extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genEqualsHashCode=true, genParcelable=true, genToString=true)")
261     @Deprecated
262     private void __metadata() {}
263 
264 
265     //@formatter:on
266     // End of generated code
267 
268 }
269