/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 by Avro
 *
 * DO NOT EDIT DIRECTLY
 */
package org.apache.camel.dataformat.avro.example;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.nio.ByteBuffer;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Objects;

import org.apache.avro.AvroMissingFieldException;
import org.apache.avro.AvroRuntimeException;
import org.apache.avro.Schema;
import org.apache.avro.data.RecordBuilder;
import org.apache.avro.data.TimeConversions;
import org.apache.avro.io.DatumReader;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.message.BinaryMessageDecoder;
import org.apache.avro.message.BinaryMessageEncoder;
import org.apache.avro.message.SchemaStore;
import org.apache.avro.specific.AvroGenerated;
import org.apache.avro.specific.SpecificData;
import org.apache.avro.specific.SpecificRecord;
import org.apache.avro.specific.SpecificRecordBase;
import org.apache.avro.specific.SpecificRecordBuilderBase;

@AvroGenerated
public class DateRecord extends SpecificRecordBase implements SpecificRecord {
    private static final long serialVersionUID = 8653491882695437570L;

    public static final Schema SCHEMA$ = new Schema.Parser().parse(
            "{\"type\":\"record\",\"name\":\"DateRecord\",\"namespace\":\"org.apache.camel.dataformat.avro.example\",\"fields\":[{\"name\":\"date\",\"type\":[\"null\",{\"type\":\"int\",\"logicalType\":\"date\"}],\"default\":null},{\"name\":\"timestamp\",\"type\":[\"null\",{\"type\":\"long\",\"logicalType\":\"timestamp-millis\"}],\"default\":null}]}");

    public static Schema getClassSchema() {
        return SCHEMA$;
    }

    private static final SpecificData MODEL$ = new SpecificData();
    static {
        MODEL$.addLogicalTypeConversion(new TimeConversions.DateConversion());
        MODEL$.addLogicalTypeConversion(new TimeConversions.TimestampMillisConversion());
    }

    private static final BinaryMessageEncoder<DateRecord> ENCODER = new BinaryMessageEncoder<>(MODEL$, SCHEMA$);

    private static final BinaryMessageDecoder<DateRecord> DECODER = new BinaryMessageDecoder<>(MODEL$, SCHEMA$);

    /**
     * Return the BinaryMessageEncoder instance used by this class.
     *
     * @return the message encoder used by this class
     */
    public static BinaryMessageEncoder<DateRecord> getEncoder() {
        return ENCODER;
    }

    /**
     * Return the BinaryMessageDecoder instance used by this class.
     *
     * @return the message decoder used by this class
     */
    public static BinaryMessageDecoder<DateRecord> getDecoder() {
        return DECODER;
    }

    /**
     * Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
     *
     * @param  resolver a {@link SchemaStore} used to find schemas by fingerprint
     * @return          a BinaryMessageDecoder instance for this class backed by the given SchemaStore
     */
    public static BinaryMessageDecoder<DateRecord> createDecoder(SchemaStore resolver) {
        return new BinaryMessageDecoder<>(MODEL$, SCHEMA$, resolver);
    }

    /**
     * Serializes this DateRecord to a ByteBuffer.
     *
     * @return                     a buffer holding the serialized data for this instance
     * @throws java.io.IOException if this instance could not be serialized
     */
    public ByteBuffer toByteBuffer() throws IOException {
        return ENCODER.encode(this);
    }

    /**
     * Deserializes a DateRecord from a ByteBuffer.
     *
     * @param  b                   a byte buffer holding serialized data for an instance of this class
     * @return                     a DateRecord instance decoded from the given buffer
     * @throws java.io.IOException if the given bytes could not be deserialized into an instance of this class
     */
    public static DateRecord fromByteBuffer(
            ByteBuffer b)
            throws IOException {
        return DECODER.decode(b);
    }

    private LocalDate date;
    private Instant timestamp;

    /**
     * Default constructor. Note that this does not initialize fields to their default values from the schema. If that
     * is desired then one should use <code>newBuilder()</code>.
     */
    public DateRecord() {
    }

    /**
     * All-args constructor.
     *
     * @param date      The new value for date
     * @param timestamp The new value for timestamp
     */
    public DateRecord(LocalDate date, Instant timestamp) {
        this.date = date;
        this.timestamp = timestamp;
    }

    @Override
    public SpecificData getSpecificData() {
        return MODEL$;
    }

    @Override
    public Schema getSchema() {
        return SCHEMA$;
    }

    // Used by DatumWriter.  Applications should not call.
    @Override
    public Object get(int field$) {
        switch (field$) {
            case 0:
                return date;
            case 1:
                return timestamp;
            default:
                throw new IndexOutOfBoundsException("Invalid index: " + field$);
        }
    }

    // Used by DatumReader.  Applications should not call.
    @Override
    @SuppressWarnings(value = "unchecked")
    public void put(int field$, Object value$) {
        switch (field$) {
            case 0:
                date = (LocalDate) value$;
                break;
            case 1:
                timestamp = (Instant) value$;
                break;
            default:
                throw new IndexOutOfBoundsException("Invalid index: " + field$);
        }
    }

    /**
     * Gets the value of the 'date' field.
     *
     * @return The value of the 'date' field.
     */
    public LocalDate getDate() {
        return date;
    }

    /**
     * Sets the value of the 'date' field.
     *
     * @param value the value to set.
     */
    public void setDate(LocalDate value) {
        this.date = value;
    }

    /**
     * Gets the value of the 'timestamp' field.
     *
     * @return The value of the 'timestamp' field.
     */
    public Instant getTimestamp() {
        return timestamp;
    }

    /**
     * Sets the value of the 'timestamp' field.
     *
     * @param value the value to set.
     */
    public void setTimestamp(Instant value) {
        this.timestamp = value;
    }

    /**
     * Creates a new DateRecord RecordBuilder.
     *
     * @return A new DateRecord RecordBuilder
     */
    public static Builder newBuilder() {
        return new Builder();
    }

    /**
     * Creates a new DateRecord RecordBuilder by copying an existing Builder.
     *
     * @param  other The existing builder to copy.
     * @return       A new DateRecord RecordBuilder
     */
    public static Builder newBuilder(Builder other) {
        if (other == null) {
            return new Builder();
        } else {
            return new Builder(other);
        }
    }

    /**
     * Creates a new DateRecord RecordBuilder by copying an existing DateRecord instance.
     *
     * @param  other The existing instance to copy.
     * @return       A new DateRecord RecordBuilder
     */
    public static Builder newBuilder(DateRecord other) {
        if (other == null) {
            return new Builder();
        } else {
            return new DateRecord.Builder(other);
        }
    }

    /**
     * RecordBuilder for DateRecord instances.
     */
    @AvroGenerated
    public static class Builder extends SpecificRecordBuilderBase<DateRecord>
            implements RecordBuilder<DateRecord> {

        private LocalDate date;
        private Instant timestamp;

        /** Creates a new Builder */
        private Builder() {
            super(SCHEMA$, MODEL$);
        }

        /**
         * Creates a Builder by copying an existing Builder.
         *
         * @param other The existing Builder to copy.
         */
        private Builder(DateRecord.Builder other) {
            super(other);
            if (isValidValue(fields()[0], other.date)) {
                this.date = data().deepCopy(fields()[0].schema(), other.date);
                fieldSetFlags()[0] = other.fieldSetFlags()[0];
            }
            if (isValidValue(fields()[1], other.timestamp)) {
                this.timestamp = data().deepCopy(fields()[1].schema(), other.timestamp);
                fieldSetFlags()[1] = other.fieldSetFlags()[1];
            }
        }

        /**
         * Creates a Builder by copying an existing DateRecord instance
         *
         * @param other The existing instance to copy.
         */
        private Builder(DateRecord other) {
            super(SCHEMA$, MODEL$);
            if (isValidValue(fields()[0], other.date)) {
                this.date = data().deepCopy(fields()[0].schema(), other.date);
                fieldSetFlags()[0] = true;
            }
            if (isValidValue(fields()[1], other.timestamp)) {
                this.timestamp = data().deepCopy(fields()[1].schema(), other.timestamp);
                fieldSetFlags()[1] = true;
            }
        }

        /**
         * Gets the value of the 'date' field.
         *
         * @return The value.
         */
        public LocalDate getDate() {
            return date;
        }

        /**
         * Sets the value of the 'date' field.
         *
         * @param  value The value of 'date'.
         * @return       This builder.
         */
        public DateRecord.Builder setDate(LocalDate value) {
            validate(fields()[0], value);
            this.date = value;
            fieldSetFlags()[0] = true;
            return this;
        }

        /**
         * Checks whether the 'date' field has been set.
         *
         * @return True if the 'date' field has been set, false otherwise.
         */
        public boolean hasDate() {
            return fieldSetFlags()[0];
        }

        /**
         * Clears the value of the 'date' field.
         *
         * @return This builder.
         */
        public DateRecord.Builder clearDate() {
            date = null;
            fieldSetFlags()[0] = false;
            return this;
        }

        /**
         * Gets the value of the 'timestamp' field.
         *
         * @return The value.
         */
        public Instant getTimestamp() {
            return timestamp;
        }

        /**
         * Sets the value of the 'timestamp' field.
         *
         * @param  value The value of 'timestamp'.
         * @return       This builder.
         */
        public DateRecord.Builder setTimestamp(Instant value) {
            validate(fields()[1], value);
            this.timestamp = value;
            fieldSetFlags()[1] = true;
            return this;
        }

        /**
         * Checks whether the 'timestamp' field has been set.
         *
         * @return True if the 'timestamp' field has been set, false otherwise.
         */
        public boolean hasTimestamp() {
            return fieldSetFlags()[1];
        }

        /**
         * Clears the value of the 'timestamp' field.
         *
         * @return This builder.
         */
        public DateRecord.Builder clearTimestamp() {
            timestamp = null;
            fieldSetFlags()[1] = false;
            return this;
        }

        @Override
        @SuppressWarnings("unchecked")
        public DateRecord build() {
            try {
                DateRecord record = new DateRecord();
                record.date = fieldSetFlags()[0] ? this.date : (LocalDate) defaultValue(fields()[0]);
                record.timestamp = fieldSetFlags()[1] ? this.timestamp : (Instant) defaultValue(fields()[1]);
                return record;
            } catch (AvroMissingFieldException e) {
                throw e;
            } catch (java.lang.Exception e) {
                throw new AvroRuntimeException(e);
            }
        }
    }

    @SuppressWarnings("unchecked")
    private static final DatumWriter<DateRecord> WRITER$
            = (DatumWriter<DateRecord>) MODEL$.createDatumWriter(SCHEMA$);

    @Override
    public void writeExternal(ObjectOutput out)
            throws IOException {
        WRITER$.write(this, SpecificData.getEncoder(out));
    }

    @SuppressWarnings("unchecked")
    private static final DatumReader<DateRecord> READER$
            = (DatumReader<DateRecord>) MODEL$.createDatumReader(SCHEMA$);

    @Override
    public void readExternal(ObjectInput in)
            throws IOException {
        READER$.read(this, SpecificData.getDecoder(in));
    }

    @Override
    public int hashCode() {
        int result = 1;
        result = 31 * result + (date == null ? 0 : date.hashCode());
        result = 31 * result + (timestamp == null ? 0 : timestamp.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof DateRecord)) {
            return false;
        }
        DateRecord other = (DateRecord) o;
        if (!Objects.equals(this.date, other.date)) {
            return false;
        }
        if (!Objects.equals(this.timestamp, other.timestamp)) {
            return false;
        }
        return true;
    }
}
