Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | package com.android.codegentest; |
| 17 | |
| 18 | import android.annotation.FloatRange; |
| 19 | import android.annotation.IntDef; |
| 20 | import android.annotation.NonNull; |
| 21 | import android.annotation.Nullable; |
| 22 | import android.annotation.Size; |
| 23 | import android.annotation.StringDef; |
| 24 | import android.annotation.StringRes; |
| 25 | import android.annotation.UserIdInt; |
| 26 | import android.net.LinkAddress; |
| 27 | import android.os.Parcel; |
| 28 | import android.os.Parcelable; |
| 29 | import android.view.accessibility.AccessibilityNodeInfo; |
| 30 | |
| 31 | import com.android.internal.util.AnnotationValidations; |
| 32 | import com.android.internal.util.DataClass; |
| 33 | import com.android.internal.util.DataClass.Each; |
| 34 | import com.android.internal.util.Parcelling; |
| 35 | import com.android.internal.util.Preconditions; |
| 36 | |
| 37 | import java.io.PrintWriter; |
| 38 | import java.util.ArrayList; |
| 39 | import java.util.Date; |
| 40 | import java.util.List; |
| 41 | import java.util.Objects; |
| 42 | import java.util.regex.Pattern; |
| 43 | |
| 44 | /** |
| 45 | * Sample data class, showing off various code generation features. |
| 46 | * |
| 47 | * See javadoc on non-generated code for the explanation of the various features. |
| 48 | * |
| 49 | * See {@link SampleDataClassTest} for various invariants the generated code is expected to hold. |
| 50 | */ |
| 51 | @DataClass( |
| 52 | // genParcelable = true, // implied by `implements Parcelable` |
| 53 | // genAidl = true, // implied by `implements Parcelable` |
| 54 | // genGetters = true, // on by default |
| 55 | // genConstDefs = true, // implied by presence of constants with common prefix |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 56 | genBuilder = true, // on by default if optional fields present, but suppressed by |
| 57 | // genConstructor |
| 58 | genConstructor = true, // on by default but normally suppressed by genBuilder |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 59 | genEqualsHashCode = true, |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 60 | genToString = true, |
| 61 | genForEachField = true, |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 62 | genSetters = true |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 63 | ) |
| 64 | public final class SampleDataClass implements Parcelable { |
| 65 | |
| 66 | /** |
| 67 | * For any group of {@link int} or {@link String} constants like these, a corresponding |
| 68 | * {@link IntDef}/{@link StringDef} will get generated, with name based on common prefix |
| 69 | * by default. |
| 70 | * |
| 71 | * When {@link #SampleDataClass constructing} an instance, fields annotated with these |
| 72 | * annotations get automatically validated, with only provided constants being a valid value. |
| 73 | * |
| 74 | * @see StateName, the generated {@link StringDef} |
| 75 | * @see #mStateName annotated with {@link StateName} |
| 76 | */ |
| 77 | public static final String STATE_NAME_UNDEFINED = "?"; |
| 78 | public static final String STATE_NAME_ON = "on"; |
| 79 | public static final String STATE_NAME_OFF = "off"; |
| 80 | |
| 81 | /** |
| 82 | * Additionally, for any generated {@link IntDef} a corresponding static |
| 83 | * *ToString method will be also generated, and used in {@link #toString()}. |
| 84 | * |
| 85 | * @see #stateToString(int) |
| 86 | * @see #toString() |
| 87 | * @see State |
| 88 | */ |
| 89 | public static final int STATE_UNDEFINED = -1; |
| 90 | public static final int STATE_ON = 1; |
| 91 | public static final int STATE_OFF = 0; |
| 92 | |
| 93 | /** |
| 94 | * {@link IntDef}s with values specified in hex("0x...") are considered to be |
| 95 | * {@link IntDef#flag flags}, while ones specified with regular int literals are considered |
| 96 | * not to be flags. |
| 97 | * |
| 98 | * This affects their string representation, e.g. see the difference in |
| 99 | * {@link #requestFlagsToString} vs {@link #stateToString}. |
| 100 | * |
| 101 | * This also affects the validation logic when {@link #SampleDataClass constructing} |
| 102 | * an instance, with any flag combination("|") being valid. |
| 103 | * |
| 104 | * You can customize the name of the generated {@link IntDef}/{@link StringDef} annotation |
| 105 | * by annotating each constant with the desired name before running the generation. |
| 106 | * |
| 107 | * Here the annotation is named {@link RequestFlags} instead of the default {@code Flags}. |
| 108 | */ |
| 109 | public static final @RequestFlags int FLAG_MANUAL_REQUEST = 0x1; |
| 110 | public static final @RequestFlags int FLAG_COMPATIBILITY_MODE_REQUEST = 0x2; |
| 111 | public static final @RequestFlags int FLAG_AUGMENTED_REQUEST = 0x80000000; |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Any property javadoc should go onto the field, and will be copied where appropriate, |
| 116 | * including getters, constructor parameters, builder setters, etc. |
| 117 | * |
| 118 | * <p> |
| 119 | * This allows to avoid the burden of maintaining copies of the same documentation |
| 120 | * pieces in multiple places for each field. |
| 121 | */ |
| 122 | private int mNum; |
| 123 | /** |
| 124 | * Various javadoc features should work as expected when copied, e.g {@code code}, |
| 125 | * {@link #mName links}, <a href="https://google.com">html links</a>, etc. |
| 126 | * |
| 127 | * @see #mNum2 ..and so should blocks at the bottom, e.g. {@code @see} blocks. |
| 128 | */ |
| 129 | private int mNum2; |
| 130 | /** |
| 131 | * {@code @hide} javadoc annotation is also propagated, which can be used to adjust the |
| 132 | * desired public API surface. |
| 133 | * |
| 134 | * @see #getNum4() is hidden |
| 135 | * @see Builder#setNum4(int) also hidden |
| 136 | * @hide |
| 137 | */ |
| 138 | private int mNum4; |
| 139 | |
| 140 | /** |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 141 | * {@link Nullable} or {@link NonNull} annotation is required on all non-primitive fields. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 142 | */ |
| 143 | private @Nullable String mName; |
| 144 | /** |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 145 | * Fields with default value expressions ("mFoo = ...") are optional, and are automatically |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 146 | * initialized to the provided default expression, unless explicitly set. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 147 | * |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 148 | * When using a {@link Builder} optional fields are passed via a {@link Builder#setName2 setter} |
| 149 | * while mandatory fields are passed via {@link Builder#Builder constructor}. |
| 150 | */ |
| 151 | private @NonNull String mName2 = "Bob"; |
| 152 | /** |
| 153 | * Alternatively, when default value computation is expensive, |
| 154 | * {@link #defaultName4 defaultFieldName()} can be defined to compute the default value. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 155 | */ |
| 156 | private @NonNull String mName4; |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 157 | private static String defaultName4() { |
| 158 | // Expensive computation |
| 159 | return "Bob4"; |
| 160 | } |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * For parcelling, any field type supported by {@link Parcel} is supported out of the box. |
| 164 | * E.g. {@link Parcelable} subclasses, {@link String}, {@link int}, {@link boolean}, etc. |
| 165 | */ |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 166 | private @Nullable AccessibilityNodeInfo mOtherParcelable = null; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 167 | /** |
| 168 | * Additionally, support for parcelling other types can be added by implementing a |
| 169 | * {@link Parcelling}, and referencing it in the {@link DataClass.ParcelWith} field annotation. |
| 170 | * |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 171 | * @see MyDateParcelling an example {@link Parcelling} implementation |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 172 | */ |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 173 | @DataClass.ParcelWith(MyDateParcelling.class) |
| 174 | private @NonNull Date mDate = new Date(42 * 42); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 175 | /** |
| 176 | * If a {@link Parcelling} is fairly common, consider putting it in {@link Parcelling.BuiltIn} |
| 177 | * to encourage its reuse. |
| 178 | */ |
| 179 | @DataClass.ParcelWith(Parcelling.BuiltIn.ForPattern.class) |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 180 | private @NonNull Pattern mPattern = Pattern.compile(""); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 181 | |
| 182 | /** |
| 183 | * For lists, when using a {@link Builder}, other than a regular |
| 184 | * {@link Builder#setLinkAddresses2(List) setter}, and additional |
| 185 | * {@link Builder#addLinkAddresses2(LinkAddress) add} method is generated for convenience. |
| 186 | */ |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 187 | private @NonNull List<LinkAddress> mLinkAddresses2 = new ArrayList<>(); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 188 | /** |
| 189 | * For aesthetics, you may want to consider providing a singular version of the plural field |
| 190 | * name, which would be used for the {@link #mLinkAddresses2 above mentioned} "add" method. |
| 191 | * |
| 192 | * @see Builder#addLinkAddress(LinkAddress) |
| 193 | */ |
| 194 | @DataClass.PluralOf("linkAddress") |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 195 | private @NonNull ArrayList<LinkAddress> mLinkAddresses = new ArrayList<>(); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 196 | /** |
| 197 | * For array fields, when using a {@link Builder}, vararg argument format is used for |
| 198 | * convenience. |
| 199 | * |
| 200 | * @see Builder#setLinkAddresses4(LinkAddress...) |
| 201 | */ |
| 202 | private @Nullable LinkAddress[] mLinkAddresses4 = null; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 203 | |
| 204 | /** |
| 205 | * {@link IntDef}/{@link StringDef}-annotated fields propagate the annotation to |
| 206 | * getter/constructor/setter/builder parameters, making for a nicer api. |
| 207 | * |
| 208 | * @see #getStateName |
| 209 | * @see Builder#setStateName |
| 210 | */ |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 211 | private @StateName @NonNull String mStateName = STATE_NAME_UNDEFINED; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Fields annotated with {@link IntDef} annotations also get a proper {@link #toString()} value. |
| 214 | */ |
| 215 | private @RequestFlags int mFlags; |
| 216 | /** |
| 217 | * Above is true for both {@link IntDef#flag flags} and enum-like {@link IntDef}s |
| 218 | */ |
| 219 | private @State int mState = STATE_UNDEFINED; |
| 220 | |
| 221 | |
| 222 | /** |
| 223 | * Making a field public will suppress getter generation in favor of accessing it directly. |
| 224 | */ |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 225 | public @NonNull CharSequence charSeq = ""; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 226 | /** |
| 227 | * Final fields suppress generating a setter (when setters are requested). |
| 228 | */ |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 229 | private final @Nullable LinkAddress[] mLinkAddresses5; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 230 | /** |
| 231 | * Transient fields are completely ignored and can be used for caching. |
| 232 | */ |
| 233 | private transient LinkAddress[] mLinkAddresses6; |
| 234 | /** |
| 235 | * When using transient fields for caching it's often also a good idea to initialize them |
| 236 | * lazily. |
| 237 | * |
| 238 | * You can declare a special method like {@link #lazyInitTmpStorage()}, to let the |
| 239 | * {@link #getTmpStorage getter} lazily-initialize the value on demand. |
| 240 | */ |
| 241 | transient int[] mTmpStorage; |
| 242 | private int[] lazyInitTmpStorage() { |
| 243 | return new int[100]; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Fields with certain annotations are automatically validated in constructor |
| 248 | * |
| 249 | * You can see overloads in {@link AnnotationValidations} for a list of currently |
| 250 | * supported ones. |
| 251 | * |
| 252 | * You can also extend support to your custom annotations by creating another corresponding |
| 253 | * overloads like |
| 254 | * {@link AnnotationValidations#validate(Class, UserIdInt, int)}. |
| 255 | * |
| 256 | * @see #SampleDataClass |
| 257 | */ |
| 258 | private @StringRes int mStringRes = 0; |
| 259 | /** |
| 260 | * Validation annotations may also have parameters. |
| 261 | * |
| 262 | * Parameter values will be supplied to validation method as name-value pairs. |
| 263 | * |
| 264 | * @see AnnotationValidations#validate(Class, Size, int, String, int, String, int) |
| 265 | */ |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 266 | private @android.annotation.IntRange(from = 0, to = 6) int mDayOfWeek = 3; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 267 | /** |
| 268 | * Unnamed validation annotation parameter gets supplied to the validating method named as |
| 269 | * "value". |
| 270 | * |
| 271 | * Validation annotations following {@link Each} annotation, will be applied for each |
| 272 | * array/collection element instead. |
| 273 | * |
| 274 | * @see AnnotationValidations#validate(Class, Size, int, String, int) |
| 275 | */ |
| 276 | @Size(2) |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 277 | @NonNull |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 278 | @Each @FloatRange(from = 0f) |
| 279 | private float[] mCoords = new float[] {0f, 0f}; |
| 280 | |
| 281 | |
| 282 | /** |
| 283 | * Manually declaring any method that would otherwise be generated suppresses its generation, |
| 284 | * allowing for fine-grained overrides of the generated behavior. |
| 285 | */ |
| 286 | public LinkAddress[] getLinkAddresses4() { |
| 287 | //Suppress autogen |
| 288 | return null; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Additionally, some methods like {@link #equals}, {@link #hashCode}, {@link #toString}, |
| 293 | * {@link #writeToParcel}, {@link Parcelable.Creator#createFromParcel} allow you to define |
| 294 | * special methods to override their behavior on a per-field basis. |
| 295 | * |
| 296 | * See the generateted methods' descriptions for the detailed instructions of what the method |
| 297 | * signatures for such methods are expected to be. |
| 298 | * |
| 299 | * Here we use this to "fix" {@link Pattern} not implementing equals/hashCode. |
| 300 | * |
| 301 | * @see #equals |
| 302 | * @see #hashCode |
| 303 | */ |
| 304 | private boolean patternEquals(Pattern other) { |
| 305 | return Objects.equals(mPattern.pattern(), other.pattern()); |
| 306 | } |
| 307 | private int patternHashCode() { |
| 308 | return Objects.hashCode(mPattern.pattern()); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Similarly, {@link #onConstructed()}, if defined, gets called at the end of constructing an |
| 313 | * instance. |
| 314 | * |
| 315 | * At this point all fields should be in place, so this is the right place to put any custom |
| 316 | * validation logic. |
| 317 | */ |
| 318 | private void onConstructed() { |
| 319 | Preconditions.checkState(mNum2 == mNum4); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * {@link DataClass#genForEachField} can be used to generate a generic {@link #forEachField} |
| 324 | * utility, which can be used for various use-cases not covered out of the box. |
| 325 | * Callback passed to {@link #forEachField} will be called once per each property with its name |
| 326 | * and value. |
| 327 | * |
| 328 | * Here for example it's used to implement a typical dump method. |
| 329 | * |
| 330 | * Note that there are 2 {@link #forEachField} versions provided, one that treats each field |
| 331 | * value as an {@link Object}, thus boxing primitives if any, and one that additionally takes |
| 332 | * specialized callbacks for particular primitive field types used in given class. |
| 333 | * |
| 334 | * Some primitives like {@link Boolean}s and {@link Integer}s within [-128, 127] don't allocate |
| 335 | * when boxed, so it's up to you to decide which one to use for a given use-case. |
| 336 | */ |
| 337 | public void dump(PrintWriter pw) { |
| 338 | forEachField((self, name, value) -> { |
| 339 | pw.append(" ").append(name).append(": ").append(String.valueOf(value)).append('\n'); |
| 340 | }); |
| 341 | } |
| 342 | |
| 343 | |
| 344 | |
Eugene Susla | c49e8ee | 2019-10-01 11:49:15 -0700 | [diff] [blame^] | 345 | // Code below generated by codegen v1.0.3. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 346 | // |
| 347 | // DO NOT MODIFY! |
Eugene Susla | c49e8ee | 2019-10-01 11:49:15 -0700 | [diff] [blame^] | 348 | // CHECKSTYLE:OFF Generated code |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 349 | // |
| 350 | // To regenerate run: |
| 351 | // $ codegen $ANDROID_BUILD_TOP/frameworks/base/tests/Codegen/src/com/android/codegentest/SampleDataClass.java |
Eugene Susla | c49e8ee | 2019-10-01 11:49:15 -0700 | [diff] [blame^] | 352 | |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 353 | |
| 354 | @IntDef(prefix = "STATE_", value = { |
| 355 | STATE_UNDEFINED, |
| 356 | STATE_ON, |
| 357 | STATE_OFF |
| 358 | }) |
| 359 | @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE) |
| 360 | @DataClass.Generated.Member |
| 361 | public @interface State {} |
| 362 | |
| 363 | @DataClass.Generated.Member |
| 364 | public static String stateToString(@State int value) { |
| 365 | switch (value) { |
| 366 | case STATE_UNDEFINED: |
| 367 | return "STATE_UNDEFINED"; |
| 368 | case STATE_ON: |
| 369 | return "STATE_ON"; |
| 370 | case STATE_OFF: |
| 371 | return "STATE_OFF"; |
| 372 | default: return Integer.toHexString(value); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | @IntDef(flag = true, prefix = "FLAG_", value = { |
| 377 | FLAG_MANUAL_REQUEST, |
| 378 | FLAG_COMPATIBILITY_MODE_REQUEST, |
| 379 | FLAG_AUGMENTED_REQUEST |
| 380 | }) |
| 381 | @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE) |
| 382 | @DataClass.Generated.Member |
| 383 | public @interface RequestFlags {} |
| 384 | |
| 385 | @DataClass.Generated.Member |
| 386 | public static String requestFlagsToString(@RequestFlags int value) { |
| 387 | return com.android.internal.util.BitUtils.flagsToString( |
| 388 | value, SampleDataClass::singleRequestFlagsToString); |
| 389 | } |
| 390 | |
| 391 | @DataClass.Generated.Member |
| 392 | static String singleRequestFlagsToString(@RequestFlags int value) { |
| 393 | switch (value) { |
| 394 | case FLAG_MANUAL_REQUEST: |
| 395 | return "FLAG_MANUAL_REQUEST"; |
| 396 | case FLAG_COMPATIBILITY_MODE_REQUEST: |
| 397 | return "FLAG_COMPATIBILITY_MODE_REQUEST"; |
| 398 | case FLAG_AUGMENTED_REQUEST: |
| 399 | return "FLAG_AUGMENTED_REQUEST"; |
| 400 | default: return Integer.toHexString(value); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | @StringDef(prefix = "STATE_NAME_", value = { |
| 405 | STATE_NAME_UNDEFINED, |
| 406 | STATE_NAME_ON, |
| 407 | STATE_NAME_OFF |
| 408 | }) |
| 409 | @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.SOURCE) |
| 410 | @DataClass.Generated.Member |
| 411 | public @interface StateName {} |
| 412 | |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 413 | /** |
| 414 | * Creates a new SampleDataClass. |
| 415 | * |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 416 | * @param num |
| 417 | * Any property javadoc should go onto the field, and will be copied where appropriate, |
| 418 | * including getters, constructor parameters, builder setters, etc. |
| 419 | * |
| 420 | * <p> |
| 421 | * This allows to avoid the burden of maintaining copies of the same documentation |
| 422 | * pieces in multiple places for each field. |
| 423 | * @param num2 |
| 424 | * Various javadoc features should work as expected when copied, e.g {@code code}, |
| 425 | * {@link #mName links}, <a href="https://google.com">html links</a>, etc. |
| 426 | * @param num4 |
| 427 | * {@code @hide} javadoc annotation is also propagated, which can be used to adjust the |
| 428 | * desired public API surface. |
| 429 | * @param name |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 430 | * {@link Nullable} or {@link NonNull} annotation is required on all non-primitive fields. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 431 | * @param name2 |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 432 | * Fields with default value expressions ("mFoo = ...") are optional, and are automatically |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 433 | * initialized to the provided default expression, unless explicitly set. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 434 | * |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 435 | * When using a {@link Builder} optional fields are passed via a {@link Builder#setName2 setter} |
| 436 | * while mandatory fields are passed via {@link Builder#Builder constructor}. |
| 437 | * @param name4 |
| 438 | * Alternatively, when default value computation is expensive, |
| 439 | * {@link #defaultName4 defaultFieldName()} can be defined to compute the default value. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 440 | * @param otherParcelable |
| 441 | * For parcelling, any field type supported by {@link Parcel} is supported out of the box. |
| 442 | * E.g. {@link Parcelable} subclasses, {@link String}, {@link int}, {@link boolean}, etc. |
| 443 | * @param date |
| 444 | * Additionally, support for parcelling other types can be added by implementing a |
| 445 | * {@link Parcelling}, and referencing it in the {@link DataClass.ParcelWith} field annotation. |
| 446 | * @param pattern |
| 447 | * If a {@link Parcelling} is fairly common, consider putting it in {@link Parcelling.BuiltIn} |
| 448 | * to encourage its reuse. |
| 449 | * @param linkAddresses2 |
| 450 | * For lists, when using a {@link Builder}, other than a regular |
| 451 | * {@link Builder#setLinkAddresses2(List) setter}, and additional |
| 452 | * {@link Builder#addLinkAddresses2(LinkAddress) add} method is generated for convenience. |
| 453 | * @param linkAddresses |
| 454 | * For aesthetics, you may want to consider providing a singular version of the plural field |
| 455 | * name, which would be used for the {@link #mLinkAddresses2 above mentioned} "add" method. |
| 456 | * @param linkAddresses4 |
| 457 | * For array fields, when using a {@link Builder}, vararg argument format is used for |
| 458 | * convenience. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 459 | * @param stateName |
| 460 | * {@link IntDef}/{@link StringDef}-annotated fields propagate the annotation to |
| 461 | * getter/constructor/setter/builder parameters, making for a nicer api. |
| 462 | * @param flags |
| 463 | * Fields annotated with {@link IntDef} annotations also get a proper {@link #toString()} value. |
| 464 | * @param state |
| 465 | * Above is true for both {@link IntDef#flag flags} and enum-like {@link IntDef}s |
| 466 | * @param charSeq |
| 467 | * Making a field public will suppress getter generation in favor of accessing it directly. |
| 468 | * @param linkAddresses5 |
| 469 | * Final fields suppress generating a setter (when setters are requested). |
| 470 | * @param stringRes |
| 471 | * Fields with certain annotations are automatically validated in constructor |
| 472 | * |
| 473 | * You can see overloads in {@link AnnotationValidations} for a list of currently |
| 474 | * supported ones. |
| 475 | * |
| 476 | * You can also extend support to your custom annotations by creating another corresponding |
| 477 | * overloads like |
| 478 | * {@link AnnotationValidations#validate(Class, UserIdInt, int)}. |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 479 | * @param dayOfWeek |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 480 | * Validation annotations may also have parameters. |
| 481 | * |
| 482 | * Parameter values will be supplied to validation method as name-value pairs. |
| 483 | * @param coords |
| 484 | * Unnamed validation annotation parameter gets supplied to the validating method named as |
| 485 | * "value". |
| 486 | * |
| 487 | * Validation annotations following {@link Each} annotation, will be applied for each |
| 488 | * array/collection element instead. |
| 489 | */ |
| 490 | @DataClass.Generated.Member |
| 491 | public SampleDataClass( |
| 492 | int num, |
| 493 | int num2, |
| 494 | int num4, |
| 495 | @Nullable String name, |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 496 | @NonNull String name2, |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 497 | @NonNull String name4, |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 498 | @Nullable AccessibilityNodeInfo otherParcelable, |
| 499 | @NonNull Date date, |
| 500 | @NonNull Pattern pattern, |
| 501 | @NonNull List<LinkAddress> linkAddresses2, |
| 502 | @NonNull ArrayList<LinkAddress> linkAddresses, |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 503 | @Nullable LinkAddress[] linkAddresses4, |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 504 | @StateName @NonNull String stateName, |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 505 | @RequestFlags int flags, |
| 506 | @State int state, |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 507 | @NonNull CharSequence charSeq, |
| 508 | @Nullable LinkAddress[] linkAddresses5, |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 509 | @StringRes int stringRes, |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 510 | @android.annotation.IntRange(from = 0, to = 6) int dayOfWeek, |
| 511 | @Size(2) @NonNull @Each @FloatRange(from = 0f) float[] coords) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 512 | this.mNum = num; |
| 513 | this.mNum2 = num2; |
| 514 | this.mNum4 = num4; |
| 515 | this.mName = name; |
| 516 | this.mName2 = name2; |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 517 | AnnotationValidations.validate( |
| 518 | NonNull.class, null, mName2); |
| 519 | this.mName4 = name4; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 520 | AnnotationValidations.validate( |
| 521 | NonNull.class, null, mName4); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 522 | this.mOtherParcelable = otherParcelable; |
| 523 | this.mDate = date; |
| 524 | AnnotationValidations.validate( |
| 525 | NonNull.class, null, mDate); |
| 526 | this.mPattern = pattern; |
| 527 | AnnotationValidations.validate( |
| 528 | NonNull.class, null, mPattern); |
| 529 | this.mLinkAddresses2 = linkAddresses2; |
| 530 | AnnotationValidations.validate( |
| 531 | NonNull.class, null, mLinkAddresses2); |
| 532 | this.mLinkAddresses = linkAddresses; |
| 533 | AnnotationValidations.validate( |
| 534 | NonNull.class, null, mLinkAddresses); |
| 535 | this.mLinkAddresses4 = linkAddresses4; |
| 536 | this.mStateName = stateName; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 537 | |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 538 | if (!(Objects.equals(mStateName, STATE_NAME_UNDEFINED)) |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 539 | && !(Objects.equals(mStateName, STATE_NAME_ON)) |
| 540 | && !(Objects.equals(mStateName, STATE_NAME_OFF))) { |
| 541 | throw new java.lang.IllegalArgumentException( |
| 542 | "stateName was " + mStateName + " but must be one of: " |
| 543 | + "STATE_NAME_UNDEFINED(" + STATE_NAME_UNDEFINED + "), " |
| 544 | + "STATE_NAME_ON(" + STATE_NAME_ON + "), " |
| 545 | + "STATE_NAME_OFF(" + STATE_NAME_OFF + ")"); |
| 546 | } |
| 547 | |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 548 | AnnotationValidations.validate( |
| 549 | NonNull.class, null, mStateName); |
| 550 | this.mFlags = flags; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 551 | |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 552 | Preconditions.checkFlagsArgument( |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 553 | mFlags, |
| 554 | FLAG_MANUAL_REQUEST |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 555 | | FLAG_COMPATIBILITY_MODE_REQUEST |
| 556 | | FLAG_AUGMENTED_REQUEST); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 557 | this.mState = state; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 558 | |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 559 | if (!(mState == STATE_UNDEFINED) |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 560 | && !(mState == STATE_ON) |
| 561 | && !(mState == STATE_OFF)) { |
| 562 | throw new java.lang.IllegalArgumentException( |
| 563 | "state was " + mState + " but must be one of: " |
| 564 | + "STATE_UNDEFINED(" + STATE_UNDEFINED + "), " |
| 565 | + "STATE_ON(" + STATE_ON + "), " |
| 566 | + "STATE_OFF(" + STATE_OFF + ")"); |
| 567 | } |
| 568 | |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 569 | this.charSeq = charSeq; |
| 570 | AnnotationValidations.validate( |
| 571 | NonNull.class, null, charSeq); |
| 572 | this.mLinkAddresses5 = linkAddresses5; |
| 573 | this.mStringRes = stringRes; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 574 | AnnotationValidations.validate( |
| 575 | StringRes.class, null, mStringRes); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 576 | this.mDayOfWeek = dayOfWeek; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 577 | AnnotationValidations.validate( |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 578 | android.annotation.IntRange.class, null, mDayOfWeek, |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 579 | "from", 0, |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 580 | "to", 6); |
| 581 | this.mCoords = coords; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 582 | AnnotationValidations.validate( |
| 583 | Size.class, null, mCoords.length, |
| 584 | "value", 2); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 585 | AnnotationValidations.validate( |
| 586 | NonNull.class, null, mCoords); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 587 | int coordsSize = mCoords.length; |
| 588 | for (int i = 0; i < coordsSize; i++) { |
| 589 | AnnotationValidations.validate( |
| 590 | FloatRange.class, null, mCoords[i], |
| 591 | "from", 0f); |
| 592 | } |
| 593 | |
| 594 | |
| 595 | onConstructed(); |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Any property javadoc should go onto the field, and will be copied where appropriate, |
| 600 | * including getters, constructor parameters, builder setters, etc. |
| 601 | * |
| 602 | * <p> |
| 603 | * This allows to avoid the burden of maintaining copies of the same documentation |
| 604 | * pieces in multiple places for each field. |
| 605 | */ |
| 606 | @DataClass.Generated.Member |
| 607 | public int getNum() { |
| 608 | return mNum; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Various javadoc features should work as expected when copied, e.g {@code code}, |
| 613 | * {@link #mName links}, <a href="https://google.com">html links</a>, etc. |
| 614 | * |
| 615 | * @see #mNum2 ..and so should blocks at the bottom, e.g. {@code @see} blocks. |
| 616 | */ |
| 617 | @DataClass.Generated.Member |
| 618 | public int getNum2() { |
| 619 | return mNum2; |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * {@code @hide} javadoc annotation is also propagated, which can be used to adjust the |
| 624 | * desired public API surface. |
| 625 | * |
| 626 | * @see #getNum4() is hidden |
| 627 | * @see Builder#setNum4(int) also hidden |
| 628 | * @hide |
| 629 | */ |
| 630 | @DataClass.Generated.Member |
| 631 | public int getNum4() { |
| 632 | return mNum4; |
| 633 | } |
| 634 | |
| 635 | /** |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 636 | * {@link Nullable} or {@link NonNull} annotation is required on all non-primitive fields. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 637 | */ |
| 638 | @DataClass.Generated.Member |
| 639 | public @Nullable String getName() { |
| 640 | return mName; |
| 641 | } |
| 642 | |
| 643 | /** |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 644 | * Fields with default value expressions ("mFoo = ...") are optional, and are automatically |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 645 | * initialized to the provided default expression, unless explicitly set. |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 646 | * |
| 647 | * When using a {@link Builder} optional fields are passed via a {@link Builder#setName2 setter} |
| 648 | * while mandatory fields are passed via {@link Builder#Builder constructor}. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 649 | */ |
| 650 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 651 | public @NonNull String getName2() { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 652 | return mName2; |
| 653 | } |
| 654 | |
| 655 | /** |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 656 | * Alternatively, when default value computation is expensive, |
| 657 | * {@link #defaultName4 defaultFieldName()} can be defined to compute the default value. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 658 | */ |
| 659 | @DataClass.Generated.Member |
| 660 | public @NonNull String getName4() { |
| 661 | return mName4; |
| 662 | } |
| 663 | |
| 664 | /** |
| 665 | * For parcelling, any field type supported by {@link Parcel} is supported out of the box. |
| 666 | * E.g. {@link Parcelable} subclasses, {@link String}, {@link int}, {@link boolean}, etc. |
| 667 | */ |
| 668 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 669 | public @Nullable AccessibilityNodeInfo getOtherParcelable() { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 670 | return mOtherParcelable; |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * Additionally, support for parcelling other types can be added by implementing a |
| 675 | * {@link Parcelling}, and referencing it in the {@link DataClass.ParcelWith} field annotation. |
| 676 | * |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 677 | * @see MyDateParcelling an example {@link Parcelling} implementation |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 678 | */ |
| 679 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 680 | public @NonNull Date getDate() { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 681 | return mDate; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * If a {@link Parcelling} is fairly common, consider putting it in {@link Parcelling.BuiltIn} |
| 686 | * to encourage its reuse. |
| 687 | */ |
| 688 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 689 | public @NonNull Pattern getPattern() { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 690 | return mPattern; |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * For lists, when using a {@link Builder}, other than a regular |
| 695 | * {@link Builder#setLinkAddresses2(List) setter}, and additional |
| 696 | * {@link Builder#addLinkAddresses2(LinkAddress) add} method is generated for convenience. |
| 697 | */ |
| 698 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 699 | public @NonNull List<LinkAddress> getLinkAddresses2() { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 700 | return mLinkAddresses2; |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * For aesthetics, you may want to consider providing a singular version of the plural field |
| 705 | * name, which would be used for the {@link #mLinkAddresses2 above mentioned} "add" method. |
| 706 | * |
| 707 | * @see Builder#addLinkAddress(LinkAddress) |
| 708 | */ |
| 709 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 710 | public @NonNull ArrayList<LinkAddress> getLinkAddresses() { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 711 | return mLinkAddresses; |
| 712 | } |
| 713 | |
| 714 | /** |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 715 | * {@link IntDef}/{@link StringDef}-annotated fields propagate the annotation to |
| 716 | * getter/constructor/setter/builder parameters, making for a nicer api. |
| 717 | * |
| 718 | * @see #getStateName |
| 719 | * @see Builder#setStateName |
| 720 | */ |
| 721 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 722 | public @StateName @NonNull String getStateName() { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 723 | return mStateName; |
| 724 | } |
| 725 | |
| 726 | /** |
| 727 | * Fields annotated with {@link IntDef} annotations also get a proper {@link #toString()} value. |
| 728 | */ |
| 729 | @DataClass.Generated.Member |
| 730 | public @RequestFlags int getFlags() { |
| 731 | return mFlags; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * Above is true for both {@link IntDef#flag flags} and enum-like {@link IntDef}s |
| 736 | */ |
| 737 | @DataClass.Generated.Member |
| 738 | public @State int getState() { |
| 739 | return mState; |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * Final fields suppress generating a setter (when setters are requested). |
| 744 | */ |
| 745 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 746 | public @Nullable LinkAddress[] getLinkAddresses5() { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 747 | return mLinkAddresses5; |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Fields with certain annotations are automatically validated in constructor |
| 752 | * |
| 753 | * You can see overloads in {@link AnnotationValidations} for a list of currently |
| 754 | * supported ones. |
| 755 | * |
| 756 | * You can also extend support to your custom annotations by creating another corresponding |
| 757 | * overloads like |
| 758 | * {@link AnnotationValidations#validate(Class, UserIdInt, int)}. |
| 759 | * |
| 760 | * @see #SampleDataClass |
| 761 | */ |
| 762 | @DataClass.Generated.Member |
| 763 | public @StringRes int getStringRes() { |
| 764 | return mStringRes; |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Validation annotations may also have parameters. |
| 769 | * |
| 770 | * Parameter values will be supplied to validation method as name-value pairs. |
| 771 | * |
| 772 | * @see AnnotationValidations#validate(Class, Size, int, String, int, String, int) |
| 773 | */ |
| 774 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 775 | public @android.annotation.IntRange(from = 0, to = 6) int getDayOfWeek() { |
| 776 | return mDayOfWeek; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /** |
| 780 | * Unnamed validation annotation parameter gets supplied to the validating method named as |
| 781 | * "value". |
| 782 | * |
| 783 | * Validation annotations following {@link Each} annotation, will be applied for each |
| 784 | * array/collection element instead. |
| 785 | * |
| 786 | * @see AnnotationValidations#validate(Class, Size, int, String, int) |
| 787 | */ |
| 788 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 789 | public @Size(2) @NonNull @Each @FloatRange(from = 0f) float[] getCoords() { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 790 | return mCoords; |
| 791 | } |
| 792 | |
| 793 | /** |
| 794 | * When using transient fields for caching it's often also a good idea to initialize them |
| 795 | * lazily. |
| 796 | * |
| 797 | * You can declare a special method like {@link #lazyInitTmpStorage()}, to let the |
| 798 | * {@link #getTmpStorage getter} lazily-initialize the value on demand. |
| 799 | */ |
| 800 | @DataClass.Generated.Member |
| 801 | public int[] getTmpStorage() { |
| 802 | int[] tmpStorage = mTmpStorage; |
| 803 | if (tmpStorage == null) { |
| 804 | // You can mark field as volatile for thread-safe double-check init |
| 805 | tmpStorage = mTmpStorage = lazyInitTmpStorage(); |
| 806 | } |
| 807 | return tmpStorage; |
| 808 | } |
| 809 | |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 810 | /** |
| 811 | * Any property javadoc should go onto the field, and will be copied where appropriate, |
| 812 | * including getters, constructor parameters, builder setters, etc. |
| 813 | * |
| 814 | * <p> |
| 815 | * This allows to avoid the burden of maintaining copies of the same documentation |
| 816 | * pieces in multiple places for each field. |
| 817 | */ |
| 818 | @DataClass.Generated.Member |
| 819 | public SampleDataClass setNum(int value) { |
| 820 | mNum = value; |
| 821 | return this; |
| 822 | } |
| 823 | |
| 824 | /** |
| 825 | * Various javadoc features should work as expected when copied, e.g {@code code}, |
| 826 | * {@link #mName links}, <a href="https://google.com">html links</a>, etc. |
| 827 | * |
| 828 | * @see #mNum2 ..and so should blocks at the bottom, e.g. {@code @see} blocks. |
| 829 | */ |
| 830 | @DataClass.Generated.Member |
| 831 | public SampleDataClass setNum2(int value) { |
| 832 | mNum2 = value; |
| 833 | return this; |
| 834 | } |
| 835 | |
| 836 | /** |
| 837 | * {@code @hide} javadoc annotation is also propagated, which can be used to adjust the |
| 838 | * desired public API surface. |
| 839 | * |
| 840 | * @see #getNum4() is hidden |
| 841 | * @see Builder#setNum4(int) also hidden |
| 842 | * @hide |
| 843 | */ |
| 844 | @DataClass.Generated.Member |
| 845 | public SampleDataClass setNum4(int value) { |
| 846 | mNum4 = value; |
| 847 | return this; |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * {@link Nullable} or {@link NonNull} annotation is required on all non-primitive fields. |
| 852 | */ |
| 853 | @DataClass.Generated.Member |
| 854 | public SampleDataClass setName(@Nullable String value) { |
| 855 | mName = value; |
| 856 | return this; |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Fields with default value expressions ("mFoo = ...") are optional, and are automatically |
| 861 | * initialized to the provided default expression, unless explicitly set. |
| 862 | * |
| 863 | * When using a {@link Builder} optional fields are passed via a {@link Builder#setName2 setter} |
| 864 | * while mandatory fields are passed via {@link Builder#Builder constructor}. |
| 865 | */ |
| 866 | @DataClass.Generated.Member |
| 867 | public SampleDataClass setName2(@NonNull String value) { |
| 868 | mName2 = value; |
| 869 | AnnotationValidations.validate( |
| 870 | NonNull.class, null, mName2); |
| 871 | return this; |
| 872 | } |
| 873 | |
| 874 | /** |
| 875 | * Alternatively, when default value computation is expensive, |
| 876 | * {@link #defaultName4 defaultFieldName()} can be defined to compute the default value. |
| 877 | */ |
| 878 | @DataClass.Generated.Member |
| 879 | public SampleDataClass setName4(@NonNull String value) { |
| 880 | mName4 = value; |
| 881 | AnnotationValidations.validate( |
| 882 | NonNull.class, null, mName4); |
| 883 | return this; |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * For parcelling, any field type supported by {@link Parcel} is supported out of the box. |
| 888 | * E.g. {@link Parcelable} subclasses, {@link String}, {@link int}, {@link boolean}, etc. |
| 889 | */ |
| 890 | @DataClass.Generated.Member |
| 891 | public SampleDataClass setOtherParcelable(@Nullable AccessibilityNodeInfo value) { |
| 892 | mOtherParcelable = value; |
| 893 | return this; |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * Additionally, support for parcelling other types can be added by implementing a |
| 898 | * {@link Parcelling}, and referencing it in the {@link DataClass.ParcelWith} field annotation. |
| 899 | * |
| 900 | * @see MyDateParcelling an example {@link Parcelling} implementation |
| 901 | */ |
| 902 | @DataClass.Generated.Member |
| 903 | public SampleDataClass setDate(@NonNull Date value) { |
| 904 | mDate = value; |
| 905 | AnnotationValidations.validate( |
| 906 | NonNull.class, null, mDate); |
| 907 | return this; |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * If a {@link Parcelling} is fairly common, consider putting it in {@link Parcelling.BuiltIn} |
| 912 | * to encourage its reuse. |
| 913 | */ |
| 914 | @DataClass.Generated.Member |
| 915 | public SampleDataClass setPattern(@NonNull Pattern value) { |
| 916 | mPattern = value; |
| 917 | AnnotationValidations.validate( |
| 918 | NonNull.class, null, mPattern); |
| 919 | return this; |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * For lists, when using a {@link Builder}, other than a regular |
| 924 | * {@link Builder#setLinkAddresses2(List) setter}, and additional |
| 925 | * {@link Builder#addLinkAddresses2(LinkAddress) add} method is generated for convenience. |
| 926 | */ |
| 927 | @DataClass.Generated.Member |
| 928 | public SampleDataClass setLinkAddresses2(@NonNull List<LinkAddress> value) { |
| 929 | mLinkAddresses2 = value; |
| 930 | AnnotationValidations.validate( |
| 931 | NonNull.class, null, mLinkAddresses2); |
| 932 | return this; |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * For aesthetics, you may want to consider providing a singular version of the plural field |
| 937 | * name, which would be used for the {@link #mLinkAddresses2 above mentioned} "add" method. |
| 938 | * |
| 939 | * @see Builder#addLinkAddress(LinkAddress) |
| 940 | */ |
| 941 | @DataClass.Generated.Member |
| 942 | public SampleDataClass setLinkAddresses(@NonNull ArrayList<LinkAddress> value) { |
| 943 | mLinkAddresses = value; |
| 944 | AnnotationValidations.validate( |
| 945 | NonNull.class, null, mLinkAddresses); |
| 946 | return this; |
| 947 | } |
| 948 | |
| 949 | /** |
| 950 | * For array fields, when using a {@link Builder}, vararg argument format is used for |
| 951 | * convenience. |
| 952 | * |
| 953 | * @see Builder#setLinkAddresses4(LinkAddress...) |
| 954 | */ |
| 955 | @DataClass.Generated.Member |
| 956 | public SampleDataClass setLinkAddresses4(@Nullable LinkAddress... value) { |
| 957 | mLinkAddresses4 = value; |
| 958 | return this; |
| 959 | } |
| 960 | |
| 961 | /** |
| 962 | * {@link IntDef}/{@link StringDef}-annotated fields propagate the annotation to |
| 963 | * getter/constructor/setter/builder parameters, making for a nicer api. |
| 964 | * |
| 965 | * @see #getStateName |
| 966 | * @see Builder#setStateName |
| 967 | */ |
| 968 | @DataClass.Generated.Member |
| 969 | public SampleDataClass setStateName(@StateName @NonNull String value) { |
| 970 | mStateName = value; |
| 971 | |
| 972 | if (!(Objects.equals(mStateName, STATE_NAME_UNDEFINED)) |
| 973 | && !(Objects.equals(mStateName, STATE_NAME_ON)) |
| 974 | && !(Objects.equals(mStateName, STATE_NAME_OFF))) { |
| 975 | throw new java.lang.IllegalArgumentException( |
| 976 | "stateName was " + mStateName + " but must be one of: " |
| 977 | + "STATE_NAME_UNDEFINED(" + STATE_NAME_UNDEFINED + "), " |
| 978 | + "STATE_NAME_ON(" + STATE_NAME_ON + "), " |
| 979 | + "STATE_NAME_OFF(" + STATE_NAME_OFF + ")"); |
| 980 | } |
| 981 | |
| 982 | AnnotationValidations.validate( |
| 983 | NonNull.class, null, mStateName); |
| 984 | return this; |
| 985 | } |
| 986 | |
| 987 | /** |
| 988 | * Fields annotated with {@link IntDef} annotations also get a proper {@link #toString()} value. |
| 989 | */ |
| 990 | @DataClass.Generated.Member |
| 991 | public SampleDataClass setFlags(@RequestFlags int value) { |
| 992 | mFlags = value; |
| 993 | |
| 994 | Preconditions.checkFlagsArgument( |
| 995 | mFlags, |
| 996 | FLAG_MANUAL_REQUEST |
| 997 | | FLAG_COMPATIBILITY_MODE_REQUEST |
| 998 | | FLAG_AUGMENTED_REQUEST); |
| 999 | return this; |
| 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * Above is true for both {@link IntDef#flag flags} and enum-like {@link IntDef}s |
| 1004 | */ |
| 1005 | @DataClass.Generated.Member |
| 1006 | public SampleDataClass setState(@State int value) { |
| 1007 | mState = value; |
| 1008 | |
| 1009 | if (!(mState == STATE_UNDEFINED) |
| 1010 | && !(mState == STATE_ON) |
| 1011 | && !(mState == STATE_OFF)) { |
| 1012 | throw new java.lang.IllegalArgumentException( |
| 1013 | "state was " + mState + " but must be one of: " |
| 1014 | + "STATE_UNDEFINED(" + STATE_UNDEFINED + "), " |
| 1015 | + "STATE_ON(" + STATE_ON + "), " |
| 1016 | + "STATE_OFF(" + STATE_OFF + ")"); |
| 1017 | } |
| 1018 | |
| 1019 | return this; |
| 1020 | } |
| 1021 | |
| 1022 | /** |
| 1023 | * Fields with certain annotations are automatically validated in constructor |
| 1024 | * |
| 1025 | * You can see overloads in {@link AnnotationValidations} for a list of currently |
| 1026 | * supported ones. |
| 1027 | * |
| 1028 | * You can also extend support to your custom annotations by creating another corresponding |
| 1029 | * overloads like |
| 1030 | * {@link AnnotationValidations#validate(Class, UserIdInt, int)}. |
| 1031 | * |
| 1032 | * @see #SampleDataClass |
| 1033 | */ |
| 1034 | @DataClass.Generated.Member |
| 1035 | public SampleDataClass setStringRes(@StringRes int value) { |
| 1036 | mStringRes = value; |
| 1037 | AnnotationValidations.validate( |
| 1038 | StringRes.class, null, mStringRes); |
| 1039 | return this; |
| 1040 | } |
| 1041 | |
| 1042 | /** |
| 1043 | * Validation annotations may also have parameters. |
| 1044 | * |
| 1045 | * Parameter values will be supplied to validation method as name-value pairs. |
| 1046 | * |
| 1047 | * @see AnnotationValidations#validate(Class, Size, int, String, int, String, int) |
| 1048 | */ |
| 1049 | @DataClass.Generated.Member |
| 1050 | public SampleDataClass setDayOfWeek(@android.annotation.IntRange(from = 0, to = 6) int value) { |
| 1051 | mDayOfWeek = value; |
| 1052 | AnnotationValidations.validate( |
| 1053 | android.annotation.IntRange.class, null, mDayOfWeek, |
| 1054 | "from", 0, |
| 1055 | "to", 6); |
| 1056 | return this; |
| 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * Unnamed validation annotation parameter gets supplied to the validating method named as |
| 1061 | * "value". |
| 1062 | * |
| 1063 | * Validation annotations following {@link Each} annotation, will be applied for each |
| 1064 | * array/collection element instead. |
| 1065 | * |
| 1066 | * @see AnnotationValidations#validate(Class, Size, int, String, int) |
| 1067 | */ |
| 1068 | @DataClass.Generated.Member |
| 1069 | public SampleDataClass setCoords(@Size(2) @NonNull @Each @FloatRange(from = 0f) float... value) { |
| 1070 | mCoords = value; |
| 1071 | AnnotationValidations.validate( |
| 1072 | Size.class, null, mCoords.length, |
| 1073 | "value", 2); |
| 1074 | AnnotationValidations.validate( |
| 1075 | NonNull.class, null, mCoords); |
| 1076 | int coordsSize = mCoords.length; |
| 1077 | for (int i = 0; i < coordsSize; i++) { |
| 1078 | AnnotationValidations.validate( |
| 1079 | FloatRange.class, null, mCoords[i], |
| 1080 | "from", 0f); |
| 1081 | } |
| 1082 | |
| 1083 | return this; |
| 1084 | } |
| 1085 | |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1086 | @Override |
| 1087 | @DataClass.Generated.Member |
| 1088 | public String toString() { |
| 1089 | // You can override field toString logic by defining methods like: |
| 1090 | // String fieldNameToString() { ... } |
| 1091 | |
| 1092 | return "SampleDataClass { " + |
| 1093 | "num = " + mNum + ", " + |
| 1094 | "num2 = " + mNum2 + ", " + |
| 1095 | "num4 = " + mNum4 + ", " + |
| 1096 | "name = " + mName + ", " + |
| 1097 | "name2 = " + mName2 + ", " + |
| 1098 | "name4 = " + mName4 + ", " + |
| 1099 | "otherParcelable = " + mOtherParcelable + ", " + |
| 1100 | "date = " + mDate + ", " + |
| 1101 | "pattern = " + mPattern + ", " + |
| 1102 | "linkAddresses2 = " + mLinkAddresses2 + ", " + |
| 1103 | "linkAddresses = " + mLinkAddresses + ", " + |
| 1104 | "linkAddresses4 = " + java.util.Arrays.toString(mLinkAddresses4) + ", " + |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1105 | "stateName = " + mStateName + ", " + |
| 1106 | "flags = " + requestFlagsToString(mFlags) + ", " + |
| 1107 | "state = " + stateToString(mState) + ", " + |
| 1108 | "charSeq = " + charSeq + ", " + |
| 1109 | "linkAddresses5 = " + java.util.Arrays.toString(mLinkAddresses5) + ", " + |
| 1110 | "stringRes = " + mStringRes + ", " + |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1111 | "dayOfWeek = " + mDayOfWeek + ", " + |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1112 | "coords = " + java.util.Arrays.toString(mCoords) + |
| 1113 | " }"; |
| 1114 | } |
| 1115 | |
| 1116 | @Override |
| 1117 | @DataClass.Generated.Member |
| 1118 | public boolean equals(Object o) { |
| 1119 | // You can override field equality logic by defining either of the methods like: |
| 1120 | // boolean fieldNameEquals(SampleDataClass other) { ... } |
| 1121 | // boolean fieldNameEquals(FieldType otherValue) { ... } |
| 1122 | |
| 1123 | if (this == o) return true; |
| 1124 | if (o == null || getClass() != o.getClass()) return false; |
| 1125 | @SuppressWarnings("unchecked") |
| 1126 | SampleDataClass that = (SampleDataClass) o; |
| 1127 | //noinspection PointlessBooleanExpression |
| 1128 | return true |
| 1129 | && mNum == that.mNum |
| 1130 | && mNum2 == that.mNum2 |
| 1131 | && mNum4 == that.mNum4 |
| 1132 | && Objects.equals(mName, that.mName) |
| 1133 | && Objects.equals(mName2, that.mName2) |
| 1134 | && Objects.equals(mName4, that.mName4) |
| 1135 | && Objects.equals(mOtherParcelable, that.mOtherParcelable) |
| 1136 | && Objects.equals(mDate, that.mDate) |
| 1137 | && patternEquals(that.mPattern) |
| 1138 | && Objects.equals(mLinkAddresses2, that.mLinkAddresses2) |
| 1139 | && Objects.equals(mLinkAddresses, that.mLinkAddresses) |
| 1140 | && java.util.Arrays.equals(mLinkAddresses4, that.mLinkAddresses4) |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1141 | && Objects.equals(mStateName, that.mStateName) |
| 1142 | && mFlags == that.mFlags |
| 1143 | && mState == that.mState |
| 1144 | && Objects.equals(charSeq, that.charSeq) |
| 1145 | && java.util.Arrays.equals(mLinkAddresses5, that.mLinkAddresses5) |
| 1146 | && mStringRes == that.mStringRes |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1147 | && mDayOfWeek == that.mDayOfWeek |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1148 | && java.util.Arrays.equals(mCoords, that.mCoords); |
| 1149 | } |
| 1150 | |
| 1151 | @Override |
| 1152 | @DataClass.Generated.Member |
| 1153 | public int hashCode() { |
| 1154 | // You can override field hashCode logic by defining methods like: |
| 1155 | // int fieldNameHashCode() { ... } |
| 1156 | |
| 1157 | int _hash = 1; |
| 1158 | _hash = 31 * _hash + mNum; |
| 1159 | _hash = 31 * _hash + mNum2; |
| 1160 | _hash = 31 * _hash + mNum4; |
| 1161 | _hash = 31 * _hash + Objects.hashCode(mName); |
| 1162 | _hash = 31 * _hash + Objects.hashCode(mName2); |
| 1163 | _hash = 31 * _hash + Objects.hashCode(mName4); |
| 1164 | _hash = 31 * _hash + Objects.hashCode(mOtherParcelable); |
| 1165 | _hash = 31 * _hash + Objects.hashCode(mDate); |
| 1166 | _hash = 31 * _hash + patternHashCode(); |
| 1167 | _hash = 31 * _hash + Objects.hashCode(mLinkAddresses2); |
| 1168 | _hash = 31 * _hash + Objects.hashCode(mLinkAddresses); |
| 1169 | _hash = 31 * _hash + java.util.Arrays.hashCode(mLinkAddresses4); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1170 | _hash = 31 * _hash + Objects.hashCode(mStateName); |
| 1171 | _hash = 31 * _hash + mFlags; |
| 1172 | _hash = 31 * _hash + mState; |
| 1173 | _hash = 31 * _hash + Objects.hashCode(charSeq); |
| 1174 | _hash = 31 * _hash + java.util.Arrays.hashCode(mLinkAddresses5); |
| 1175 | _hash = 31 * _hash + mStringRes; |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1176 | _hash = 31 * _hash + mDayOfWeek; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1177 | _hash = 31 * _hash + java.util.Arrays.hashCode(mCoords); |
| 1178 | return _hash; |
| 1179 | } |
| 1180 | |
| 1181 | @DataClass.Generated.Member |
| 1182 | void forEachField( |
| 1183 | DataClass.PerIntFieldAction<SampleDataClass> actionInt, |
| 1184 | DataClass.PerObjectFieldAction<SampleDataClass> actionObject) { |
| 1185 | actionInt.acceptInt(this, "num", mNum); |
| 1186 | actionInt.acceptInt(this, "num2", mNum2); |
| 1187 | actionInt.acceptInt(this, "num4", mNum4); |
| 1188 | actionObject.acceptObject(this, "name", mName); |
| 1189 | actionObject.acceptObject(this, "name2", mName2); |
| 1190 | actionObject.acceptObject(this, "name4", mName4); |
| 1191 | actionObject.acceptObject(this, "otherParcelable", mOtherParcelable); |
| 1192 | actionObject.acceptObject(this, "date", mDate); |
| 1193 | actionObject.acceptObject(this, "pattern", mPattern); |
| 1194 | actionObject.acceptObject(this, "linkAddresses2", mLinkAddresses2); |
| 1195 | actionObject.acceptObject(this, "linkAddresses", mLinkAddresses); |
| 1196 | actionObject.acceptObject(this, "linkAddresses4", mLinkAddresses4); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1197 | actionObject.acceptObject(this, "stateName", mStateName); |
| 1198 | actionInt.acceptInt(this, "flags", mFlags); |
| 1199 | actionInt.acceptInt(this, "state", mState); |
| 1200 | actionObject.acceptObject(this, "charSeq", charSeq); |
| 1201 | actionObject.acceptObject(this, "linkAddresses5", mLinkAddresses5); |
| 1202 | actionInt.acceptInt(this, "stringRes", mStringRes); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1203 | actionInt.acceptInt(this, "dayOfWeek", mDayOfWeek); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1204 | actionObject.acceptObject(this, "coords", mCoords); |
| 1205 | } |
| 1206 | |
| 1207 | /** @deprecated May cause boxing allocations - use with caution! */ |
| 1208 | @Deprecated |
| 1209 | @DataClass.Generated.Member |
| 1210 | void forEachField(DataClass.PerObjectFieldAction<SampleDataClass> action) { |
| 1211 | action.acceptObject(this, "num", mNum); |
| 1212 | action.acceptObject(this, "num2", mNum2); |
| 1213 | action.acceptObject(this, "num4", mNum4); |
| 1214 | action.acceptObject(this, "name", mName); |
| 1215 | action.acceptObject(this, "name2", mName2); |
| 1216 | action.acceptObject(this, "name4", mName4); |
| 1217 | action.acceptObject(this, "otherParcelable", mOtherParcelable); |
| 1218 | action.acceptObject(this, "date", mDate); |
| 1219 | action.acceptObject(this, "pattern", mPattern); |
| 1220 | action.acceptObject(this, "linkAddresses2", mLinkAddresses2); |
| 1221 | action.acceptObject(this, "linkAddresses", mLinkAddresses); |
| 1222 | action.acceptObject(this, "linkAddresses4", mLinkAddresses4); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1223 | action.acceptObject(this, "stateName", mStateName); |
| 1224 | action.acceptObject(this, "flags", mFlags); |
| 1225 | action.acceptObject(this, "state", mState); |
| 1226 | action.acceptObject(this, "charSeq", charSeq); |
| 1227 | action.acceptObject(this, "linkAddresses5", mLinkAddresses5); |
| 1228 | action.acceptObject(this, "stringRes", mStringRes); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1229 | action.acceptObject(this, "dayOfWeek", mDayOfWeek); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1230 | action.acceptObject(this, "coords", mCoords); |
| 1231 | } |
| 1232 | |
| 1233 | @DataClass.Generated.Member |
| 1234 | static Parcelling<Date> sParcellingForDate = |
| 1235 | Parcelling.Cache.get( |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1236 | MyDateParcelling.class); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1237 | static { |
| 1238 | if (sParcellingForDate == null) { |
| 1239 | sParcellingForDate = Parcelling.Cache.put( |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1240 | new MyDateParcelling()); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | @DataClass.Generated.Member |
| 1245 | static Parcelling<Pattern> sParcellingForPattern = |
| 1246 | Parcelling.Cache.get( |
| 1247 | Parcelling.BuiltIn.ForPattern.class); |
| 1248 | static { |
| 1249 | if (sParcellingForPattern == null) { |
| 1250 | sParcellingForPattern = Parcelling.Cache.put( |
| 1251 | new Parcelling.BuiltIn.ForPattern()); |
| 1252 | } |
| 1253 | } |
| 1254 | |
| 1255 | @Override |
| 1256 | @DataClass.Generated.Member |
| 1257 | public void writeToParcel(Parcel dest, int flags) { |
| 1258 | // You can override field parcelling by defining methods like: |
| 1259 | // void parcelFieldName(Parcel dest, int flags) { ... } |
| 1260 | |
| 1261 | long flg = 0; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1262 | if (mName != null) flg |= 0x8; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1263 | if (mOtherParcelable != null) flg |= 0x40; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1264 | if (mLinkAddresses4 != null) flg |= 0x800; |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1265 | if (mLinkAddresses5 != null) flg |= 0x10000; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1266 | dest.writeLong(flg); |
| 1267 | dest.writeInt(mNum); |
| 1268 | dest.writeInt(mNum2); |
| 1269 | dest.writeInt(mNum4); |
| 1270 | if (mName != null) dest.writeString(mName); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1271 | dest.writeString(mName2); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1272 | dest.writeString(mName4); |
| 1273 | if (mOtherParcelable != null) dest.writeTypedObject(mOtherParcelable, flags); |
| 1274 | sParcellingForDate.parcel(mDate, dest, flags); |
| 1275 | sParcellingForPattern.parcel(mPattern, dest, flags); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1276 | dest.writeParcelableList(mLinkAddresses2, flags); |
| 1277 | dest.writeParcelableList(mLinkAddresses, flags); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1278 | if (mLinkAddresses4 != null) dest.writeTypedArray(mLinkAddresses4, flags); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1279 | dest.writeString(mStateName); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1280 | dest.writeInt(mFlags); |
| 1281 | dest.writeInt(mState); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1282 | dest.writeCharSequence(charSeq); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1283 | if (mLinkAddresses5 != null) dest.writeTypedArray(mLinkAddresses5, flags); |
| 1284 | dest.writeInt(mStringRes); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1285 | dest.writeInt(mDayOfWeek); |
| 1286 | dest.writeFloatArray(mCoords); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | @Override |
| 1290 | @DataClass.Generated.Member |
| 1291 | public int describeContents() { return 0; } |
| 1292 | |
| 1293 | @DataClass.Generated.Member |
| 1294 | public static final @NonNull Parcelable.Creator<SampleDataClass> CREATOR |
| 1295 | = new Parcelable.Creator<SampleDataClass>() { |
| 1296 | @Override |
| 1297 | public SampleDataClass[] newArray(int size) { |
| 1298 | return new SampleDataClass[size]; |
| 1299 | } |
| 1300 | |
| 1301 | @Override |
| 1302 | @SuppressWarnings({"unchecked", "RedundantCast"}) |
| 1303 | public SampleDataClass createFromParcel(Parcel in) { |
| 1304 | // You can override field unparcelling by defining methods like: |
| 1305 | // static FieldType unparcelFieldName(Parcel in) { ... } |
| 1306 | |
| 1307 | long flg = in.readLong(); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1308 | int num = in.readInt(); |
| 1309 | int num2 = in.readInt(); |
| 1310 | int num4 = in.readInt(); |
| 1311 | String name = (flg & 0x8) == 0 ? null : in.readString(); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1312 | String name2 = in.readString(); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1313 | String name4 = in.readString(); |
| 1314 | AccessibilityNodeInfo otherParcelable = (flg & 0x40) == 0 ? null : (AccessibilityNodeInfo) in.readTypedObject(AccessibilityNodeInfo.CREATOR); |
| 1315 | Date date = sParcellingForDate.unparcel(in); |
| 1316 | Pattern pattern = sParcellingForPattern.unparcel(in); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1317 | List<LinkAddress> linkAddresses2 = new ArrayList<>(); |
| 1318 | in.readParcelableList(linkAddresses2, LinkAddress.class.getClassLoader()); |
| 1319 | ArrayList<LinkAddress> linkAddresses = new ArrayList<>(); |
| 1320 | in.readParcelableList(linkAddresses, LinkAddress.class.getClassLoader()); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1321 | LinkAddress[] linkAddresses4 = (flg & 0x800) == 0 ? null : (LinkAddress[]) in.createTypedArray(LinkAddress.CREATOR); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1322 | String stateName = in.readString(); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1323 | int flags = in.readInt(); |
| 1324 | int state = in.readInt(); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1325 | CharSequence _charSeq = (CharSequence) in.readCharSequence(); |
| 1326 | LinkAddress[] linkAddresses5 = (flg & 0x10000) == 0 ? null : (LinkAddress[]) in.createTypedArray(LinkAddress.CREATOR); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1327 | int stringRes = in.readInt(); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1328 | int dayOfWeek = in.readInt(); |
| 1329 | float[] coords = in.createFloatArray(); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1330 | return new SampleDataClass( |
| 1331 | num, |
| 1332 | num2, |
| 1333 | num4, |
| 1334 | name, |
| 1335 | name2, |
| 1336 | name4, |
| 1337 | otherParcelable, |
| 1338 | date, |
| 1339 | pattern, |
| 1340 | linkAddresses2, |
| 1341 | linkAddresses, |
| 1342 | linkAddresses4, |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1343 | stateName, |
| 1344 | flags, |
| 1345 | state, |
| 1346 | _charSeq, |
| 1347 | linkAddresses5, |
| 1348 | stringRes, |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1349 | dayOfWeek, |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1350 | coords); |
| 1351 | } |
| 1352 | }; |
| 1353 | |
| 1354 | /** |
| 1355 | * A builder for {@link SampleDataClass} |
| 1356 | */ |
| 1357 | @SuppressWarnings("WeakerAccess") |
| 1358 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1359 | public static class Builder { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1360 | |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1361 | private int mNum; |
| 1362 | private int mNum2; |
| 1363 | private int mNum4; |
| 1364 | private @Nullable String mName; |
| 1365 | private @NonNull String mName2; |
| 1366 | private @NonNull String mName4; |
| 1367 | private @Nullable AccessibilityNodeInfo mOtherParcelable; |
| 1368 | private @NonNull Date mDate; |
| 1369 | private @NonNull Pattern mPattern; |
| 1370 | private @NonNull List<LinkAddress> mLinkAddresses2; |
| 1371 | private @NonNull ArrayList<LinkAddress> mLinkAddresses; |
| 1372 | private @Nullable LinkAddress[] mLinkAddresses4; |
| 1373 | private @StateName @NonNull String mStateName; |
| 1374 | private @RequestFlags int mFlags; |
| 1375 | private @State int mState; |
| 1376 | private @NonNull CharSequence charSeq; |
| 1377 | private @Nullable LinkAddress[] mLinkAddresses5; |
| 1378 | private @StringRes int mStringRes; |
| 1379 | private @android.annotation.IntRange(from = 0, to = 6) int mDayOfWeek; |
| 1380 | private @Size(2) @NonNull @Each @FloatRange(from = 0f) float[] mCoords; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1381 | |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1382 | private long mBuilderFieldsSet = 0L; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1383 | |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1384 | /** |
| 1385 | * Creates a new Builder. |
| 1386 | * |
| 1387 | * @param num |
| 1388 | * Any property javadoc should go onto the field, and will be copied where appropriate, |
| 1389 | * including getters, constructor parameters, builder setters, etc. |
| 1390 | * |
| 1391 | * <p> |
| 1392 | * This allows to avoid the burden of maintaining copies of the same documentation |
| 1393 | * pieces in multiple places for each field. |
| 1394 | * @param num2 |
| 1395 | * Various javadoc features should work as expected when copied, e.g {@code code}, |
| 1396 | * {@link #mName links}, <a href="https://google.com">html links</a>, etc. |
| 1397 | * @param num4 |
| 1398 | * {@code @hide} javadoc annotation is also propagated, which can be used to adjust the |
| 1399 | * desired public API surface. |
| 1400 | * @param name |
| 1401 | * {@link Nullable} or {@link NonNull} annotation is required on all non-primitive fields. |
| 1402 | * @param flags |
| 1403 | * Fields annotated with {@link IntDef} annotations also get a proper {@link #toString()} value. |
| 1404 | * @param linkAddresses5 |
| 1405 | * Final fields suppress generating a setter (when setters are requested). |
| 1406 | */ |
| 1407 | public Builder( |
| 1408 | int num, |
| 1409 | int num2, |
| 1410 | int num4, |
| 1411 | @Nullable String name, |
| 1412 | @RequestFlags int flags, |
| 1413 | @Nullable LinkAddress[] linkAddresses5) { |
| 1414 | mNum = num; |
| 1415 | mNum2 = num2; |
| 1416 | mNum4 = num4; |
| 1417 | mName = name; |
| 1418 | mFlags = flags; |
| 1419 | |
| 1420 | Preconditions.checkFlagsArgument( |
| 1421 | mFlags, |
| 1422 | FLAG_MANUAL_REQUEST |
| 1423 | | FLAG_COMPATIBILITY_MODE_REQUEST |
| 1424 | | FLAG_AUGMENTED_REQUEST); |
| 1425 | mLinkAddresses5 = linkAddresses5; |
| 1426 | } |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1427 | |
| 1428 | /** |
| 1429 | * Any property javadoc should go onto the field, and will be copied where appropriate, |
| 1430 | * including getters, constructor parameters, builder setters, etc. |
| 1431 | * |
| 1432 | * <p> |
| 1433 | * This allows to avoid the burden of maintaining copies of the same documentation |
| 1434 | * pieces in multiple places for each field. |
| 1435 | */ |
| 1436 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1437 | public @NonNull Builder setNum(int value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1438 | checkNotUsed(); |
| 1439 | mBuilderFieldsSet |= 0x1; |
| 1440 | mNum = value; |
| 1441 | return this; |
| 1442 | } |
| 1443 | |
| 1444 | /** |
| 1445 | * Various javadoc features should work as expected when copied, e.g {@code code}, |
| 1446 | * {@link #mName links}, <a href="https://google.com">html links</a>, etc. |
| 1447 | * |
| 1448 | * @see #mNum2 ..and so should blocks at the bottom, e.g. {@code @see} blocks. |
| 1449 | */ |
| 1450 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1451 | public @NonNull Builder setNum2(int value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1452 | checkNotUsed(); |
| 1453 | mBuilderFieldsSet |= 0x2; |
| 1454 | mNum2 = value; |
| 1455 | return this; |
| 1456 | } |
| 1457 | |
| 1458 | /** |
| 1459 | * {@code @hide} javadoc annotation is also propagated, which can be used to adjust the |
| 1460 | * desired public API surface. |
| 1461 | * |
| 1462 | * @see #getNum4() is hidden |
| 1463 | * @see Builder#setNum4(int) also hidden |
| 1464 | * @hide |
| 1465 | */ |
| 1466 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1467 | public @NonNull Builder setNum4(int value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1468 | checkNotUsed(); |
| 1469 | mBuilderFieldsSet |= 0x4; |
| 1470 | mNum4 = value; |
| 1471 | return this; |
| 1472 | } |
| 1473 | |
| 1474 | /** |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1475 | * {@link Nullable} or {@link NonNull} annotation is required on all non-primitive fields. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1476 | */ |
| 1477 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1478 | public @NonNull Builder setName(@Nullable String value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1479 | checkNotUsed(); |
| 1480 | mBuilderFieldsSet |= 0x8; |
| 1481 | mName = value; |
| 1482 | return this; |
| 1483 | } |
| 1484 | |
| 1485 | /** |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1486 | * Fields with default value expressions ("mFoo = ...") are optional, and are automatically |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1487 | * initialized to the provided default expression, unless explicitly set. |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1488 | * |
| 1489 | * When using a {@link Builder} optional fields are passed via a {@link Builder#setName2 setter} |
| 1490 | * while mandatory fields are passed via {@link Builder#Builder constructor}. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1491 | */ |
| 1492 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1493 | public @NonNull Builder setName2(@NonNull String value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1494 | checkNotUsed(); |
| 1495 | mBuilderFieldsSet |= 0x10; |
| 1496 | mName2 = value; |
| 1497 | return this; |
| 1498 | } |
| 1499 | |
| 1500 | /** |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1501 | * Alternatively, when default value computation is expensive, |
| 1502 | * {@link #defaultName4 defaultFieldName()} can be defined to compute the default value. |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1503 | */ |
| 1504 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1505 | public @NonNull Builder setName4(@NonNull String value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1506 | checkNotUsed(); |
| 1507 | mBuilderFieldsSet |= 0x20; |
| 1508 | mName4 = value; |
| 1509 | return this; |
| 1510 | } |
| 1511 | |
| 1512 | /** |
| 1513 | * For parcelling, any field type supported by {@link Parcel} is supported out of the box. |
| 1514 | * E.g. {@link Parcelable} subclasses, {@link String}, {@link int}, {@link boolean}, etc. |
| 1515 | */ |
| 1516 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1517 | public @NonNull Builder setOtherParcelable(@Nullable AccessibilityNodeInfo value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1518 | checkNotUsed(); |
| 1519 | mBuilderFieldsSet |= 0x40; |
| 1520 | mOtherParcelable = value; |
| 1521 | return this; |
| 1522 | } |
| 1523 | |
| 1524 | /** |
| 1525 | * Additionally, support for parcelling other types can be added by implementing a |
| 1526 | * {@link Parcelling}, and referencing it in the {@link DataClass.ParcelWith} field annotation. |
| 1527 | * |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1528 | * @see MyDateParcelling an example {@link Parcelling} implementation |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1529 | */ |
| 1530 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1531 | public @NonNull Builder setDate(@NonNull Date value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1532 | checkNotUsed(); |
| 1533 | mBuilderFieldsSet |= 0x80; |
| 1534 | mDate = value; |
| 1535 | return this; |
| 1536 | } |
| 1537 | |
| 1538 | /** |
| 1539 | * If a {@link Parcelling} is fairly common, consider putting it in {@link Parcelling.BuiltIn} |
| 1540 | * to encourage its reuse. |
| 1541 | */ |
| 1542 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1543 | public @NonNull Builder setPattern(@NonNull Pattern value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1544 | checkNotUsed(); |
| 1545 | mBuilderFieldsSet |= 0x100; |
| 1546 | mPattern = value; |
| 1547 | return this; |
| 1548 | } |
| 1549 | |
| 1550 | /** |
| 1551 | * For lists, when using a {@link Builder}, other than a regular |
| 1552 | * {@link Builder#setLinkAddresses2(List) setter}, and additional |
| 1553 | * {@link Builder#addLinkAddresses2(LinkAddress) add} method is generated for convenience. |
| 1554 | */ |
| 1555 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1556 | public @NonNull Builder setLinkAddresses2(@NonNull List<LinkAddress> value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1557 | checkNotUsed(); |
| 1558 | mBuilderFieldsSet |= 0x200; |
| 1559 | mLinkAddresses2 = value; |
| 1560 | return this; |
| 1561 | } |
| 1562 | |
| 1563 | /** @see #setLinkAddresses2 */ |
| 1564 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1565 | public @NonNull Builder addLinkAddresses2(LinkAddress value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1566 | // You can refine this method's name by providing item's singular name, e.g.: |
| 1567 | // @DataClass.PluralOf("item")) mItems = ... |
| 1568 | |
| 1569 | if (mLinkAddresses2 == null) setLinkAddresses2(new ArrayList<>()); |
| 1570 | mLinkAddresses2.add(value); |
| 1571 | return this; |
| 1572 | } |
| 1573 | |
| 1574 | /** |
| 1575 | * For aesthetics, you may want to consider providing a singular version of the plural field |
| 1576 | * name, which would be used for the {@link #mLinkAddresses2 above mentioned} "add" method. |
| 1577 | * |
| 1578 | * @see Builder#addLinkAddress(LinkAddress) |
| 1579 | */ |
| 1580 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1581 | public @NonNull Builder setLinkAddresses(@NonNull ArrayList<LinkAddress> value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1582 | checkNotUsed(); |
| 1583 | mBuilderFieldsSet |= 0x400; |
| 1584 | mLinkAddresses = value; |
| 1585 | return this; |
| 1586 | } |
| 1587 | |
| 1588 | /** @see #setLinkAddresses */ |
| 1589 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1590 | public @NonNull Builder addLinkAddress(LinkAddress value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1591 | if (mLinkAddresses == null) setLinkAddresses(new ArrayList<>()); |
| 1592 | mLinkAddresses.add(value); |
| 1593 | return this; |
| 1594 | } |
| 1595 | |
| 1596 | /** |
| 1597 | * For array fields, when using a {@link Builder}, vararg argument format is used for |
| 1598 | * convenience. |
| 1599 | * |
| 1600 | * @see Builder#setLinkAddresses4(LinkAddress...) |
| 1601 | */ |
| 1602 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1603 | public @NonNull Builder setLinkAddresses4(@Nullable LinkAddress... value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1604 | checkNotUsed(); |
| 1605 | mBuilderFieldsSet |= 0x800; |
| 1606 | mLinkAddresses4 = value; |
| 1607 | return this; |
| 1608 | } |
| 1609 | |
| 1610 | /** |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1611 | * {@link IntDef}/{@link StringDef}-annotated fields propagate the annotation to |
| 1612 | * getter/constructor/setter/builder parameters, making for a nicer api. |
| 1613 | * |
| 1614 | * @see #getStateName |
| 1615 | * @see Builder#setStateName |
| 1616 | */ |
| 1617 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1618 | public @NonNull Builder setStateName(@StateName @NonNull String value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1619 | checkNotUsed(); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1620 | mBuilderFieldsSet |= 0x1000; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1621 | mStateName = value; |
| 1622 | return this; |
| 1623 | } |
| 1624 | |
| 1625 | /** |
| 1626 | * Fields annotated with {@link IntDef} annotations also get a proper {@link #toString()} value. |
| 1627 | */ |
| 1628 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1629 | public @NonNull Builder setFlags(@RequestFlags int value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1630 | checkNotUsed(); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1631 | mBuilderFieldsSet |= 0x2000; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1632 | mFlags = value; |
| 1633 | return this; |
| 1634 | } |
| 1635 | |
| 1636 | /** |
| 1637 | * Above is true for both {@link IntDef#flag flags} and enum-like {@link IntDef}s |
| 1638 | */ |
| 1639 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1640 | public @NonNull Builder setState(@State int value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1641 | checkNotUsed(); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1642 | mBuilderFieldsSet |= 0x4000; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1643 | mState = value; |
| 1644 | return this; |
| 1645 | } |
| 1646 | |
| 1647 | /** |
| 1648 | * Making a field public will suppress getter generation in favor of accessing it directly. |
| 1649 | */ |
| 1650 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1651 | public @NonNull Builder setCharSeq(@NonNull CharSequence value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1652 | checkNotUsed(); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1653 | mBuilderFieldsSet |= 0x8000; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1654 | charSeq = value; |
| 1655 | return this; |
| 1656 | } |
| 1657 | |
| 1658 | /** |
| 1659 | * Final fields suppress generating a setter (when setters are requested). |
| 1660 | */ |
| 1661 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1662 | public @NonNull Builder setLinkAddresses5(@Nullable LinkAddress... value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1663 | checkNotUsed(); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1664 | mBuilderFieldsSet |= 0x10000; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1665 | mLinkAddresses5 = value; |
| 1666 | return this; |
| 1667 | } |
| 1668 | |
| 1669 | /** |
| 1670 | * Fields with certain annotations are automatically validated in constructor |
| 1671 | * |
| 1672 | * You can see overloads in {@link AnnotationValidations} for a list of currently |
| 1673 | * supported ones. |
| 1674 | * |
| 1675 | * You can also extend support to your custom annotations by creating another corresponding |
| 1676 | * overloads like |
| 1677 | * {@link AnnotationValidations#validate(Class, UserIdInt, int)}. |
| 1678 | * |
| 1679 | * @see #SampleDataClass |
| 1680 | */ |
| 1681 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1682 | public @NonNull Builder setStringRes(@StringRes int value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1683 | checkNotUsed(); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1684 | mBuilderFieldsSet |= 0x20000; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1685 | mStringRes = value; |
| 1686 | return this; |
| 1687 | } |
| 1688 | |
| 1689 | /** |
| 1690 | * Validation annotations may also have parameters. |
| 1691 | * |
| 1692 | * Parameter values will be supplied to validation method as name-value pairs. |
| 1693 | * |
| 1694 | * @see AnnotationValidations#validate(Class, Size, int, String, int, String, int) |
| 1695 | */ |
| 1696 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1697 | public @NonNull Builder setDayOfWeek(@android.annotation.IntRange(from = 0, to = 6) int value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1698 | checkNotUsed(); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1699 | mBuilderFieldsSet |= 0x40000; |
| 1700 | mDayOfWeek = value; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1701 | return this; |
| 1702 | } |
| 1703 | |
| 1704 | /** |
| 1705 | * Unnamed validation annotation parameter gets supplied to the validating method named as |
| 1706 | * "value". |
| 1707 | * |
| 1708 | * Validation annotations following {@link Each} annotation, will be applied for each |
| 1709 | * array/collection element instead. |
| 1710 | * |
| 1711 | * @see AnnotationValidations#validate(Class, Size, int, String, int) |
| 1712 | */ |
| 1713 | @DataClass.Generated.Member |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1714 | public @NonNull Builder setCoords(@Size(2) @NonNull @Each @FloatRange(from = 0f) float... value) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1715 | checkNotUsed(); |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1716 | mBuilderFieldsSet |= 0x80000; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1717 | mCoords = value; |
| 1718 | return this; |
| 1719 | } |
| 1720 | |
| 1721 | /** Builds the instance. This builder should not be touched after calling this! */ |
| 1722 | public SampleDataClass build() { |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1723 | checkNotUsed(); |
| 1724 | mBuilderFieldsSet |= 0x100000; // Mark builder used |
| 1725 | |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1726 | if ((mBuilderFieldsSet & 0x10) == 0) { |
| 1727 | mName2 = "Bob"; |
| 1728 | } |
| 1729 | if ((mBuilderFieldsSet & 0x20) == 0) { |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1730 | mName4 = defaultName4(); |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1731 | } |
| 1732 | if ((mBuilderFieldsSet & 0x40) == 0) { |
| 1733 | mOtherParcelable = null; |
| 1734 | } |
| 1735 | if ((mBuilderFieldsSet & 0x80) == 0) { |
| 1736 | mDate = new Date(42 * 42); |
| 1737 | } |
| 1738 | if ((mBuilderFieldsSet & 0x100) == 0) { |
| 1739 | mPattern = Pattern.compile(""); |
| 1740 | } |
| 1741 | if ((mBuilderFieldsSet & 0x200) == 0) { |
| 1742 | mLinkAddresses2 = new ArrayList<>(); |
| 1743 | } |
| 1744 | if ((mBuilderFieldsSet & 0x400) == 0) { |
| 1745 | mLinkAddresses = new ArrayList<>(); |
| 1746 | } |
| 1747 | if ((mBuilderFieldsSet & 0x800) == 0) { |
| 1748 | mLinkAddresses4 = null; |
| 1749 | } |
| 1750 | if ((mBuilderFieldsSet & 0x1000) == 0) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1751 | mStateName = STATE_NAME_UNDEFINED; |
| 1752 | } |
| 1753 | if ((mBuilderFieldsSet & 0x4000) == 0) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1754 | mState = STATE_UNDEFINED; |
| 1755 | } |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1756 | if ((mBuilderFieldsSet & 0x8000) == 0) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1757 | charSeq = ""; |
| 1758 | } |
| 1759 | if ((mBuilderFieldsSet & 0x20000) == 0) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1760 | mStringRes = 0; |
| 1761 | } |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1762 | if ((mBuilderFieldsSet & 0x40000) == 0) { |
| 1763 | mDayOfWeek = 3; |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1764 | } |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1765 | if ((mBuilderFieldsSet & 0x80000) == 0) { |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1766 | mCoords = new float[] { 0f, 0f }; |
| 1767 | } |
| 1768 | SampleDataClass o = new SampleDataClass( |
| 1769 | mNum, |
| 1770 | mNum2, |
| 1771 | mNum4, |
| 1772 | mName, |
| 1773 | mName2, |
| 1774 | mName4, |
| 1775 | mOtherParcelable, |
| 1776 | mDate, |
| 1777 | mPattern, |
| 1778 | mLinkAddresses2, |
| 1779 | mLinkAddresses, |
| 1780 | mLinkAddresses4, |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1781 | mStateName, |
| 1782 | mFlags, |
| 1783 | mState, |
| 1784 | charSeq, |
| 1785 | mLinkAddresses5, |
| 1786 | mStringRes, |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1787 | mDayOfWeek, |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1788 | mCoords); |
| 1789 | return o; |
| 1790 | } |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1791 | |
| 1792 | private void checkNotUsed() { |
| 1793 | if ((mBuilderFieldsSet & 0x100000) != 0) { |
| 1794 | throw new IllegalStateException( |
| 1795 | "This Builder should not be reused. Use a new Builder instance instead"); |
| 1796 | } |
| 1797 | } |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1798 | } |
| 1799 | |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1800 | @DataClass.Generated( |
Eugene Susla | c49e8ee | 2019-10-01 11:49:15 -0700 | [diff] [blame^] | 1801 | time = 1569956013899L, |
| 1802 | codegenVersion = "1.0.3", |
Eugene Susla | 3156a4c | 2019-07-25 14:05:12 -0700 | [diff] [blame] | 1803 | sourceFile = "frameworks/base/tests/Codegen/src/com/android/codegentest/SampleDataClass.java", |
| 1804 | inputSignatures = "public static final java.lang.String STATE_NAME_UNDEFINED\npublic static final java.lang.String STATE_NAME_ON\npublic static final java.lang.String STATE_NAME_OFF\npublic static final int STATE_UNDEFINED\npublic static final int STATE_ON\npublic static final int STATE_OFF\npublic static final @com.android.codegentest.SampleDataClass.RequestFlags int FLAG_MANUAL_REQUEST\npublic static final @com.android.codegentest.SampleDataClass.RequestFlags int FLAG_COMPATIBILITY_MODE_REQUEST\npublic static final @com.android.codegentest.SampleDataClass.RequestFlags int FLAG_AUGMENTED_REQUEST\nprivate int mNum\nprivate int mNum2\nprivate int mNum4\nprivate @android.annotation.Nullable java.lang.String mName\nprivate @android.annotation.NonNull java.lang.String mName2\nprivate @android.annotation.NonNull java.lang.String mName4\nprivate @android.annotation.Nullable android.view.accessibility.AccessibilityNodeInfo mOtherParcelable\nprivate @com.android.internal.util.DataClass.ParcelWith(com.android.codegentest.MyDateParcelling.class) @android.annotation.NonNull java.util.Date mDate\nprivate @com.android.internal.util.DataClass.ParcelWith(com.android.internal.util.Parcelling.BuiltIn.ForPattern.class) @android.annotation.NonNull java.util.regex.Pattern mPattern\nprivate @android.annotation.NonNull java.util.List<android.net.LinkAddress> mLinkAddresses2\nprivate @com.android.internal.util.DataClass.PluralOf(\"linkAddress\") @android.annotation.NonNull java.util.ArrayList<android.net.LinkAddress> mLinkAddresses\nprivate @android.annotation.Nullable android.net.LinkAddress[] mLinkAddresses4\nprivate @com.android.codegentest.SampleDataClass.StateName @android.annotation.NonNull java.lang.String mStateName\nprivate @com.android.codegentest.SampleDataClass.RequestFlags int mFlags\nprivate @com.android.codegentest.SampleDataClass.State int mState\npublic @android.annotation.NonNull java.lang.CharSequence charSeq\nprivate final @android.annotation.Nullable android.net.LinkAddress[] mLinkAddresses5\nprivate transient android.net.LinkAddress[] mLinkAddresses6\ntransient int[] mTmpStorage\nprivate @android.annotation.StringRes int mStringRes\nprivate @android.annotation.IntRange(from=0L, to=6L) int mDayOfWeek\nprivate @android.annotation.Size(2L) @android.annotation.NonNull @com.android.internal.util.DataClass.Each @android.annotation.FloatRange(from=0.0) float[] mCoords\nprivate static java.lang.String defaultName4()\nprivate int[] lazyInitTmpStorage()\npublic android.net.LinkAddress[] getLinkAddresses4()\nprivate boolean patternEquals(java.util.regex.Pattern)\nprivate int patternHashCode()\nprivate void onConstructed()\npublic void dump(java.io.PrintWriter)\nclass SampleDataClass extends java.lang.Object implements [android.os.Parcelable]\n@com.android.internal.util.DataClass(genBuilder=true, genConstructor=true, genEqualsHashCode=true, genToString=true, genForEachField=true, genSetters=true)") |
| 1805 | @Deprecated |
| 1806 | private void __metadata() {} |
| 1807 | |
Eugene Susla | 574b7e1 | 2019-03-13 13:16:33 -0700 | [diff] [blame] | 1808 | } |