{{! Copyright (c) Meta Platforms, Inc. and affiliates. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. }} {{> AutoGenerated}} package {{struct:javaPackage}}; import com.facebook.swift.codec.*; import com.facebook.swift.codec.ThriftField.Requiredness; import com.facebook.swift.codec.ThriftField.Recursiveness; import java.util.*; import org.apache.thrift.*; import org.apache.thrift.transport.*; import org.apache.thrift.protocol.*; import static com.google.common.base.MoreObjects.toStringHelper; @SwiftGenerated @ThriftUnion("{{struct:name}}"){{> common/ObjectCustomAnnotation}} public final class {{struct:javaCapitalName}} implements com.facebook.thrift.payload.ThriftSerializable { {{> struct/AdapterDefinition}} private static final boolean allowNullFieldValues = System.getProperty("thrift.union.allow-null-field-values", "false").equalsIgnoreCase("true"); private static final TStruct STRUCT_DESC = new TStruct("{{struct:name}}"); private static final Map NAMES_TO_IDS = new HashMap(); public static final Map THRIFT_NAMES_TO_IDS = new HashMap(); private static final Map FIELD_METADATA = new HashMap<>(); private static final {{struct:javaCapitalName}} _DEFAULT = new {{struct:javaCapitalName}}(); {{#struct:fields}} public static final int _{{field:javaAllCapsName}} = {{field:key}}; private static final TField {{field:javaTFieldName}} = new TField("{{#field:FieldNameUnmangled?}}{{field:name}}{{/field:FieldNameUnmangled?}}{{^field:FieldNameUnmangled?}}{{field:javaName}}{{/field:FieldNameUnmangled?}}", TType.{{#field:type}}{{> TType}}{{/field:type}}, (short){{field:key}}); {{/struct:fields}} static { {{#struct:fields}} NAMES_TO_IDS.put("{{field:javaName}}", {{field:key}}); THRIFT_NAMES_TO_IDS.put("{{field:name}}", {{field:key}}); FIELD_METADATA.put({{field:key}}, {{field:javaTFieldName}}); {{/struct:fields}} {{#struct:thrift_uri}} com.facebook.thrift.type.TypeRegistry.add(new com.facebook.thrift.type.Type( new com.facebook.thrift.type.UniversalName("{{struct:thrift_uri}}"), {{struct:javaCapitalName}}.class, {{struct:javaCapitalName}}::read0)); {{/struct:thrift_uri}} } private java.lang.Object value; private short id; public static {{struct:javaCapitalName}} from(int _id, java.lang.Object _field) { return from((short) _id, _field); } public static {{struct:javaCapitalName}} from(short _id, java.lang.Object _field) { java.util.Objects.requireNonNull(_field); if (!FIELD_METADATA.containsKey(Integer.valueOf(_id))) { throw new java.lang.IllegalArgumentException("unknown field " + _id); } {{struct:javaCapitalName}} _u = new {{struct:javaCapitalName}}(); try { switch(_id) { {{#struct:fields}} case {{field:key}}: _u.id = _id; _u.value = ({{> FieldType}}) _field; return _u; {{/struct:fields}} default: throw new IllegalArgumentException("invalid type " + _field.getClass().getName() + " for field " + _id); } } catch (java.lang.Exception t) { throw new IllegalArgumentException("invalid type " + _field.getClass().getName() + " for field " + _id); } } {{> union/UnionBuilder}} {{^struct:unionFieldTypeUnique?}}{{! TODO: remove unionFieldTypeUnique related check and use builder pattern If union fields are not unique, we cannot generate thrift constructors, therefore setters are needed. }}{{> union/UnionSetter}}{{! }}{{/struct:unionFieldTypeUnique?}} {{#struct:fields}} {{> ThriftFieldAnnotation}} public {{> FieldType}} {{> GetterName}}() { if (this.id != {{field:key}}) { throw new IllegalStateException("Not a {{field:name}} element!"); } return ({{> FieldType}}) value; } public boolean isSet{{field:javaCapitalName}}() { return this.id == {{field:key}}; } {{/struct:fields}} @ThriftUnionId public short getThriftId() { return this.id; } public String getThriftName() { TField tField = (TField) FIELD_METADATA.get((int) this.id); if (tField == null) { return "null"; } else { return tField.name; } } public void accept(Visitor visitor) { {{#struct:fields}} if (isSet{{field:javaCapitalName}}()) { visitor.visit{{field:javaCapitalName}}({{> GetterName}}()); return; } {{/struct:fields}} } @java.lang.Override public String toString() { return toStringHelper(this) .add("value", value) .add("id", id) .add("name", getThriftName()) .add("type", value == null ? "" : value.getClass().getSimpleName()) .toString(); } @java.lang.Override public boolean equals(java.lang.Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } {{struct:javaCapitalName}} other = ({{struct:javaCapitalName}})o; return Objects.equals(this.id, other.id) && Objects.deepEquals(this.value, other.value); } @java.lang.Override public int hashCode() { return Arrays.deepHashCode(new java.lang.Object[] { id, value, }); } public interface Visitor { {{#struct:fields}} void visit{{field:javaCapitalName}}({{> FieldType}} {{field:javaName}}); {{/struct:fields}} } {{> union/UnionWrite}} {{> union/UnionRead}} public static {{struct:javaCapitalName}} defaultInstance() { return _DEFAULT; } }