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 17 package com.android.codegentest; 18 19 import android.annotation.NonNull; 20 import android.os.Parcel; 21 import android.os.Parcelable; 22 import android.os.SystemClock; 23 24 import com.android.internal.util.DataClass; 25 26 import java.util.concurrent.TimeUnit; 27 28 @DataClass(genBuilder = true, genAidl = false, genToString = true) 29 public class SampleWithCustomBuilder implements Parcelable { 30 31 long delayAmount = 0; 32 @NonNull 33 TimeUnit delayUnit = TimeUnit.MILLISECONDS; 34 35 long creationTimestamp = SystemClock.uptimeMillis(); 36 37 /** 38 * You can declare a class named {@code BaseBuilder} to have the generated builder extend from 39 * it instead. 40 * 41 * Same rules apply where defining a non-abstract method will suppress the generation of a 42 * method with the same signature. 43 * 44 * For abstract generatable methods, implementations are generated as normal, but original 45 * visibility is used, allowing you to hide methods. 46 * 47 * Here for example, we hide {@link #setDelayUnit} and {@link #setDelayAmount} from public API, 48 * replacing it with {@link #setDelay} instead. 49 */ 50 // Suppress setter generation for a field that is not supposed to come from user input. 51 @DataClass.Suppress("setCreationTimestamp") 52 static abstract class BaseBuilder { 53 54 /** 55 * Hide methods by declaring them with reduced (package-private) visibility. 56 */ setDelayAmount(long value)57 abstract Builder setDelayAmount(long value); 58 59 /** 60 * Alternatively, hide methods by using @hide, to hide them from public API only. 61 * 62 * @hide 63 */ setDelayUnit(TimeUnit value)64 public abstract Builder setDelayUnit(TimeUnit value); 65 66 /** 67 * Can provide additional method on the builder, e.g. as a replacement for the ones we've 68 * just hidden. 69 */ setDelay(long amount, TimeUnit unit)70 public Builder setDelay(long amount, TimeUnit unit) { 71 setDelayAmount(amount); 72 setDelayUnit(unit); 73 return (Builder) this; 74 } 75 } 76 77 unparcelDelayUnit(Parcel p)78 private static TimeUnit unparcelDelayUnit(Parcel p) { 79 return TimeUnit.values()[p.readInt()]; 80 } 81 parcelDelayUnit(Parcel p, int flags)82 private void parcelDelayUnit(Parcel p, int flags) { 83 p.writeInt(delayUnit.ordinal()); 84 } 85 86 87 88 // Code below generated by codegen v1.0.23. 89 // 90 // DO NOT MODIFY! 91 // CHECKSTYLE:OFF Generated code 92 // 93 // To regenerate run: 94 // $ codegen $ANDROID_BUILD_TOP/frameworks/base/tests/Codegen/src/com/android/codegentest/SampleWithCustomBuilder.java 95 // 96 // To exclude the generated code from IntelliJ auto-formatting enable (one-time): 97 // Settings > Editor > Code Style > Formatter Control 98 //@formatter:off 99 100 101 @DataClass.Generated.Member SampleWithCustomBuilder( long delayAmount, @NonNull TimeUnit delayUnit, long creationTimestamp)102 /* package-private */ SampleWithCustomBuilder( 103 long delayAmount, 104 @NonNull TimeUnit delayUnit, 105 long creationTimestamp) { 106 this.delayAmount = delayAmount; 107 this.delayUnit = delayUnit; 108 com.android.internal.util.AnnotationValidations.validate( 109 NonNull.class, null, delayUnit); 110 this.creationTimestamp = creationTimestamp; 111 112 // onConstructed(); // You can define this method to get a callback 113 } 114 115 @DataClass.Generated.Member getDelayAmount()116 public long getDelayAmount() { 117 return delayAmount; 118 } 119 120 @DataClass.Generated.Member getDelayUnit()121 public @NonNull TimeUnit getDelayUnit() { 122 return delayUnit; 123 } 124 125 @DataClass.Generated.Member getCreationTimestamp()126 public long getCreationTimestamp() { 127 return creationTimestamp; 128 } 129 130 @Override 131 @DataClass.Generated.Member toString()132 public String toString() { 133 // You can override field toString logic by defining methods like: 134 // String fieldNameToString() { ... } 135 136 return "SampleWithCustomBuilder { " + 137 "delayAmount = " + delayAmount + ", " + 138 "delayUnit = " + delayUnit + ", " + 139 "creationTimestamp = " + creationTimestamp + 140 " }"; 141 } 142 143 @Override 144 @DataClass.Generated.Member writeToParcel(@onNull Parcel dest, int flags)145 public void writeToParcel(@NonNull Parcel dest, int flags) { 146 // You can override field parcelling by defining methods like: 147 // void parcelFieldName(Parcel dest, int flags) { ... } 148 149 dest.writeLong(delayAmount); 150 parcelDelayUnit(dest, flags); 151 dest.writeLong(creationTimestamp); 152 } 153 154 @Override 155 @DataClass.Generated.Member describeContents()156 public int describeContents() { return 0; } 157 158 /** @hide */ 159 @SuppressWarnings({"unchecked", "RedundantCast"}) 160 @DataClass.Generated.Member SampleWithCustomBuilder(@onNull Parcel in)161 protected SampleWithCustomBuilder(@NonNull Parcel in) { 162 // You can override field unparcelling by defining methods like: 163 // static FieldType unparcelFieldName(Parcel in) { ... } 164 165 long _delayAmount = in.readLong(); 166 TimeUnit _delayUnit = unparcelDelayUnit(in); 167 long _creationTimestamp = in.readLong(); 168 169 this.delayAmount = _delayAmount; 170 this.delayUnit = _delayUnit; 171 com.android.internal.util.AnnotationValidations.validate( 172 NonNull.class, null, delayUnit); 173 this.creationTimestamp = _creationTimestamp; 174 175 // onConstructed(); // You can define this method to get a callback 176 } 177 178 @DataClass.Generated.Member 179 public static final @NonNull Parcelable.Creator<SampleWithCustomBuilder> CREATOR 180 = new Parcelable.Creator<SampleWithCustomBuilder>() { 181 @Override 182 public SampleWithCustomBuilder[] newArray(int size) { 183 return new SampleWithCustomBuilder[size]; 184 } 185 186 @Override 187 public SampleWithCustomBuilder createFromParcel(@NonNull Parcel in) { 188 return new SampleWithCustomBuilder(in); 189 } 190 }; 191 192 /** 193 * A builder for {@link SampleWithCustomBuilder} 194 */ 195 @SuppressWarnings("WeakerAccess") 196 @DataClass.Generated.Member 197 public static class Builder extends BaseBuilder { 198 199 private long delayAmount; 200 private @NonNull TimeUnit delayUnit; 201 private long creationTimestamp; 202 203 private long mBuilderFieldsSet = 0L; 204 Builder()205 public Builder() { 206 } 207 208 @DataClass.Generated.Member 209 @Override setDelayAmount(long value)210 @NonNull Builder setDelayAmount(long value) { 211 checkNotUsed(); 212 mBuilderFieldsSet |= 0x1; 213 delayAmount = value; 214 return this; 215 } 216 217 @DataClass.Generated.Member 218 @Override setDelayUnit(@onNull TimeUnit value)219 public @NonNull Builder setDelayUnit(@NonNull TimeUnit value) { 220 checkNotUsed(); 221 mBuilderFieldsSet |= 0x2; 222 delayUnit = value; 223 return this; 224 } 225 226 /** Builds the instance. This builder should not be touched after calling this! */ build()227 public @NonNull SampleWithCustomBuilder build() { 228 checkNotUsed(); 229 mBuilderFieldsSet |= 0x8; // Mark builder used 230 231 if ((mBuilderFieldsSet & 0x1) == 0) { 232 delayAmount = 0; 233 } 234 if ((mBuilderFieldsSet & 0x2) == 0) { 235 delayUnit = TimeUnit.MILLISECONDS; 236 } 237 if ((mBuilderFieldsSet & 0x4) == 0) { 238 creationTimestamp = SystemClock.uptimeMillis(); 239 } 240 SampleWithCustomBuilder o = new SampleWithCustomBuilder( 241 delayAmount, 242 delayUnit, 243 creationTimestamp); 244 return o; 245 } 246 checkNotUsed()247 private void checkNotUsed() { 248 if ((mBuilderFieldsSet & 0x8) != 0) { 249 throw new IllegalStateException( 250 "This Builder should not be reused. Use a new Builder instance instead"); 251 } 252 } 253 } 254 255 @DataClass.Generated( 256 time = 1616541540898L, 257 codegenVersion = "1.0.23", 258 sourceFile = "frameworks/base/tests/Codegen/src/com/android/codegentest/SampleWithCustomBuilder.java", 259 inputSignatures = " long delayAmount\n @android.annotation.NonNull java.util.concurrent.TimeUnit delayUnit\n long creationTimestamp\nprivate static java.util.concurrent.TimeUnit unparcelDelayUnit(android.os.Parcel)\nprivate void parcelDelayUnit(android.os.Parcel,int)\nclass SampleWithCustomBuilder extends java.lang.Object implements [android.os.Parcelable]\nabstract com.android.codegentest.SampleWithCustomBuilder.Builder setDelayAmount(long)\npublic abstract com.android.codegentest.SampleWithCustomBuilder.Builder setDelayUnit(java.util.concurrent.TimeUnit)\npublic com.android.codegentest.SampleWithCustomBuilder.Builder setDelay(long,java.util.concurrent.TimeUnit)\nclass BaseBuilder extends java.lang.Object implements []\n@com.android.internal.util.DataClass(genBuilder=true, genAidl=false, genToString=true)\nabstract com.android.codegentest.SampleWithCustomBuilder.Builder setDelayAmount(long)\npublic abstract com.android.codegentest.SampleWithCustomBuilder.Builder setDelayUnit(java.util.concurrent.TimeUnit)\npublic com.android.codegentest.SampleWithCustomBuilder.Builder setDelay(long,java.util.concurrent.TimeUnit)\nclass BaseBuilder extends java.lang.Object implements []") 260 @Deprecated __metadata()261 private void __metadata() {} 262 263 264 //@formatter:on 265 // End of generated code 266 267 } 268