1 /*
2  * Copyright (C) 2023 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.federatedcompute.common;
18 
19 import android.annotation.IntDef;
20 import android.annotation.Nullable;
21 import android.os.Parcelable;
22 
23 import com.android.internal.util.Preconditions;
24 import com.android.ondevicepersonalization.internal.util.DataClass;
25 
26 import java.lang.annotation.Retention;
27 
28 /**
29  * Training interval settings.
30  *
31  * @hide
32  */
33 @DataClass(genBuilder = true, genEqualsHashCode = true)
34 public final class TrainingInterval implements Parcelable {
35     /**
36      * The scheduling modes for a task. Recurrent tasks will be rescheduled after each run. One-off
37      * task will not be rescheduled if the task succeeds.
38      */
39     public static final int SCHEDULING_MODE_ONE_TIME = 1;
40 
41     public static final int SCHEDULING_MODE_RECURRENT = 2;
42 
43     @SchedulingMode private int mSchedulingMode;
44 
45     private long mMinimumIntervalMillis = 0L;
46 
47     // Code below generated by codegen v1.0.23.
48     //
49     // DO NOT MODIFY!
50     // CHECKSTYLE:OFF Generated code
51     //
52     // To regenerate run:
53     // $ codegen
54     // $ANDROID_BUILD_TOP/packages/modules/OnDevicePersonalization/framework/java/android/federatedcompute/common/TrainingInterval.java
55     //
56     // To exclude the generated code from IntelliJ auto-formatting enable (one-time):
57     //   Settings > Editor > Code Style > Formatter Control
58     // @formatter:off
59 
60     @IntDef(
61             prefix = "SCHEDULING_MODE_",
62             value = {SCHEDULING_MODE_ONE_TIME, SCHEDULING_MODE_RECURRENT})
63     @Retention(java.lang.annotation.RetentionPolicy.SOURCE)
64     @DataClass.Generated.Member
65     public @interface SchedulingMode {}
66 
67     @DataClass.Generated.Member
schedulingModeToString(@chedulingMode int value)68     public static String schedulingModeToString(@SchedulingMode int value) {
69         switch (value) {
70             case SCHEDULING_MODE_ONE_TIME:
71                 return "SCHEDULING_MODE_ONE_TIME";
72             case SCHEDULING_MODE_RECURRENT:
73                 return "SCHEDULING_MODE_RECURRENT";
74             default:
75                 return Integer.toHexString(value);
76         }
77     }
78 
79     @DataClass.Generated.Member
TrainingInterval( @chedulingMode int schedulingMode, long minimumIntervalMillis)80     /* package-private */ TrainingInterval(
81             @SchedulingMode int schedulingMode, long minimumIntervalMillis) {
82         this.mSchedulingMode = schedulingMode;
83 
84         if (!(mSchedulingMode == SCHEDULING_MODE_ONE_TIME)
85                 && !(mSchedulingMode == SCHEDULING_MODE_RECURRENT)) {
86             throw new java.lang.IllegalArgumentException(
87                     "schedulingMode was "
88                             + mSchedulingMode
89                             + " but must be one of: "
90                             + "SCHEDULING_MODE_ONE_TIME("
91                             + SCHEDULING_MODE_ONE_TIME
92                             + "), "
93                             + "SCHEDULING_MODE_RECURRENT("
94                             + SCHEDULING_MODE_RECURRENT
95                             + ")");
96         }
97 
98         Preconditions.checkArgument(
99                 schedulingMode != SCHEDULING_MODE_RECURRENT || minimumIntervalMillis > 0,
100                 "Recurrent jobs cannot have non-positive minimal interval.");
101 
102         this.mMinimumIntervalMillis = minimumIntervalMillis;
103 
104         // onConstructed(); // You can define this method to get a callback
105     }
106 
107     @DataClass.Generated.Member
getSchedulingMode()108     public @SchedulingMode int getSchedulingMode() {
109         return mSchedulingMode;
110     }
111 
112     @DataClass.Generated.Member
getMinimumIntervalMillis()113     public long getMinimumIntervalMillis() {
114         return mMinimumIntervalMillis;
115     }
116 
117     @Override
118     @DataClass.Generated.Member
equals(@ullable Object o)119     public boolean equals(@Nullable Object o) {
120         // You can override field equality logic by defining either of the methods like:
121         // boolean fieldNameEquals(TrainingInterval other) { ... }
122         // boolean fieldNameEquals(FieldType otherValue) { ... }
123 
124         if (this == o) return true;
125         if (o == null || getClass() != o.getClass()) return false;
126         @SuppressWarnings("unchecked")
127         TrainingInterval that = (TrainingInterval) o;
128         //noinspection PointlessBooleanExpression
129         return true
130                 && mSchedulingMode == that.mSchedulingMode
131                 && mMinimumIntervalMillis == that.mMinimumIntervalMillis;
132     }
133 
134     @Override
135     @DataClass.Generated.Member
hashCode()136     public int hashCode() {
137         // You can override field hashCode logic by defining methods like:
138         // int fieldNameHashCode() { ... }
139 
140         int _hash = 1;
141         _hash = 31 * _hash + mSchedulingMode;
142         _hash = 31 * _hash + Long.hashCode(mMinimumIntervalMillis);
143         return _hash;
144     }
145 
146     @Override
147     @DataClass.Generated.Member
writeToParcel(@ndroid.annotation.NonNull android.os.Parcel dest, int flags)148     public void writeToParcel(@android.annotation.NonNull android.os.Parcel dest, int flags) {
149         // You can override field parcelling by defining methods like:
150         // void parcelFieldName(Parcel dest, int flags) { ... }
151 
152         dest.writeInt(mSchedulingMode);
153         dest.writeLong(mMinimumIntervalMillis);
154     }
155 
156     @Override
157     @DataClass.Generated.Member
describeContents()158     public int describeContents() {
159         return 0;
160     }
161 
162     /** @hide */
163     @SuppressWarnings({"unchecked", "RedundantCast"})
164     @DataClass.Generated.Member
TrainingInterval(@ndroid.annotation.NonNull android.os.Parcel in)165     /* package-private */ TrainingInterval(@android.annotation.NonNull android.os.Parcel in) {
166         // You can override field unparcelling by defining methods like:
167         // static FieldType unparcelFieldName(Parcel in) { ... }
168 
169         int schedulingMode = in.readInt();
170         long minimumIntervalMillis = in.readLong();
171 
172         this.mSchedulingMode = schedulingMode;
173 
174         if (!(mSchedulingMode == SCHEDULING_MODE_ONE_TIME)
175                 && !(mSchedulingMode == SCHEDULING_MODE_RECURRENT)) {
176             throw new java.lang.IllegalArgumentException(
177                     "schedulingMode was "
178                             + mSchedulingMode
179                             + " but must be one of: "
180                             + "SCHEDULING_MODE_ONE_TIME("
181                             + SCHEDULING_MODE_ONE_TIME
182                             + "), "
183                             + "SCHEDULING_MODE_RECURRENT("
184                             + SCHEDULING_MODE_RECURRENT
185                             + ")");
186         }
187 
188         this.mMinimumIntervalMillis = minimumIntervalMillis;
189 
190         // onConstructed(); // You can define this method to get a callback
191     }
192 
193     @DataClass.Generated.Member
194     public static final @android.annotation.NonNull Parcelable.Creator<TrainingInterval> CREATOR =
195             new Parcelable.Creator<TrainingInterval>() {
196                 @Override
197                 public TrainingInterval[] newArray(int size) {
198                     return new TrainingInterval[size];
199                 }
200 
201                 @Override
202                 public TrainingInterval createFromParcel(
203                         @android.annotation.NonNull android.os.Parcel in) {
204                     return new TrainingInterval(in);
205                 }
206             };
207 
208     /** A builder for {@link TrainingInterval} */
209     @SuppressWarnings("WeakerAccess")
210     @DataClass.Generated.Member
211     public static final class Builder {
212 
213         private @SchedulingMode int mSchedulingMode;
214         private long mMinimumIntervalMillis;
215 
216         private long mBuilderFieldsSet = 0L;
217 
Builder()218         public Builder() {}
219 
220         @DataClass.Generated.Member
setSchedulingMode(@chedulingMode int value)221         public @android.annotation.NonNull Builder setSchedulingMode(@SchedulingMode int value) {
222             checkNotUsed();
223             mBuilderFieldsSet |= 0x1;
224             mSchedulingMode = value;
225             return this;
226         }
227 
228         @DataClass.Generated.Member
setMinimumIntervalMillis(long value)229         public @android.annotation.NonNull Builder setMinimumIntervalMillis(long value) {
230             checkNotUsed();
231             mBuilderFieldsSet |= 0x2;
232             mMinimumIntervalMillis = value;
233             return this;
234         }
235 
236         /** Builds the instance. This builder should not be touched after calling this! */
build()237         public @android.annotation.NonNull TrainingInterval build() {
238             checkNotUsed();
239             mBuilderFieldsSet |= 0x4; // Mark builder used
240 
241             if ((mBuilderFieldsSet & 0x2) == 0) {
242                 mMinimumIntervalMillis = 0L;
243             }
244             TrainingInterval o = new TrainingInterval(mSchedulingMode, mMinimumIntervalMillis);
245             return o;
246         }
247 
checkNotUsed()248         private void checkNotUsed() {
249             if ((mBuilderFieldsSet & 0x4) != 0) {
250                 throw new IllegalStateException(
251                         "This Builder should not be reused. Use a new Builder instance instead");
252             }
253         }
254     }
255 
256     // @formatter:on
257     // End of generated code
258 
259 }
260