// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
// Code generated by protocol buffer compiler. Do not edit!
package org.wpilib.math.proto;

import java.io.IOException;
import us.hebi.quickbuf.Descriptors;
import us.hebi.quickbuf.FieldName;
import us.hebi.quickbuf.InvalidProtocolBufferException;
import us.hebi.quickbuf.JsonSink;
import us.hebi.quickbuf.JsonSource;
import us.hebi.quickbuf.MessageFactory;
import us.hebi.quickbuf.ProtoMessage;
import us.hebi.quickbuf.ProtoSink;
import us.hebi.quickbuf.ProtoSource;
import us.hebi.quickbuf.RepeatedDouble;

/**
 * Protobuf type {@code ProtobufVector}
 */
@SuppressWarnings("hiding")
public final class ProtobufVector extends ProtoMessage<ProtobufVector> implements Cloneable {
  private static final long serialVersionUID = 0L;

  /**
   * <code>repeated double rows = 1;</code>
   */
  private final RepeatedDouble rows = RepeatedDouble.newEmptyInstance();

  private ProtobufVector() {
  }

  /**
   * @return a new empty instance of {@code ProtobufVector}
   */
  public static ProtobufVector newInstance() {
    return new ProtobufVector();
  }

  /**
   * <code>repeated double rows = 1;</code>
   * @return whether the rows field is set
   */
  public boolean hasRows() {
    return (bitField0_ & 0x00000001) != 0;
  }

  /**
   * <code>repeated double rows = 1;</code>
   * @return this
   */
  public ProtobufVector clearRows() {
    bitField0_ &= ~0x00000001;
    rows.clear();
    return this;
  }

  /**
   * <code>repeated double rows = 1;</code>
   *
   * This method returns the internal storage object without modifying any has state.
   * The returned object should not be modified and be treated as read-only.
   *
   * Use {@link #getMutableRows()} if you want to modify it.
   *
   * @return internal storage object for reading
   */
  public RepeatedDouble getRows() {
    return rows;
  }

  /**
   * <code>repeated double rows = 1;</code>
   *
   * This method returns the internal storage object and sets the corresponding
   * has state. The returned object will become part of this message and its
   * contents may be modified as long as the has state is not cleared.
   *
   * @return internal storage object for modifications
   */
  public RepeatedDouble getMutableRows() {
    bitField0_ |= 0x00000001;
    return rows;
  }

  /**
   * <code>repeated double rows = 1;</code>
   * @param value the rows to add
   * @return this
   */
  public ProtobufVector addRows(final double value) {
    bitField0_ |= 0x00000001;
    rows.add(value);
    return this;
  }

  /**
   * <code>repeated double rows = 1;</code>
   * @param values the rows to add
   * @return this
   */
  public ProtobufVector addAllRows(final double... values) {
    bitField0_ |= 0x00000001;
    rows.addAll(values);
    return this;
  }

  @Override
  public ProtobufVector copyFrom(final ProtobufVector other) {
    cachedSize = other.cachedSize;
    if ((bitField0_ | other.bitField0_) != 0) {
      bitField0_ = other.bitField0_;
      rows.copyFrom(other.rows);
    }
    return this;
  }

  @Override
  public ProtobufVector mergeFrom(final ProtobufVector other) {
    if (other.isEmpty()) {
      return this;
    }
    cachedSize = -1;
    if (other.hasRows()) {
      getMutableRows().addAll(other.rows);
    }
    return this;
  }

  @Override
  public ProtobufVector clear() {
    if (isEmpty()) {
      return this;
    }
    cachedSize = -1;
    bitField0_ = 0;
    rows.clear();
    return this;
  }

  @Override
  public ProtobufVector clearQuick() {
    if (isEmpty()) {
      return this;
    }
    cachedSize = -1;
    bitField0_ = 0;
    rows.clear();
    return this;
  }

  @Override
  public boolean equals(Object o) {
    if (o == this) {
      return true;
    }
    if (!(o instanceof ProtobufVector)) {
      return false;
    }
    ProtobufVector other = (ProtobufVector) o;
    return bitField0_ == other.bitField0_
      && (!hasRows() || rows.equals(other.rows));
  }

  @Override
  public void writeTo(final ProtoSink output) throws IOException {
    if ((bitField0_ & 0x00000001) != 0) {
      for (int i = 0; i < rows.length(); i++) {
        output.writeRawByte((byte) 9);
        output.writeDoubleNoTag(rows.array()[i]);
      }
    }
  }

  @Override
  protected int computeSerializedSize() {
    int size = 0;
    if ((bitField0_ & 0x00000001) != 0) {
      size += (1 + 8) * rows.length();
    }
    return size;
  }

  @Override
  @SuppressWarnings("fallthrough")
  public ProtobufVector mergeFrom(final ProtoSource input) throws IOException {
    // Enabled Fall-Through Optimization (QuickBuffers)
    int tag = input.readTag();
    while (true) {
      switch (tag) {
        case 10: {
          // rows [packed=true]
          input.readPackedDouble(rows);
          bitField0_ |= 0x00000001;
          tag = input.readTag();
          if (tag != 0) {
            break;
          }
        }
        case 0: {
          return this;
        }
        default: {
          if (!input.skipField(tag)) {
            return this;
          }
          tag = input.readTag();
          break;
        }
        case 9: {
          // rows [packed=false]
          tag = input.readRepeatedDouble(rows, tag);
          bitField0_ |= 0x00000001;
          break;
        }
      }
    }
  }

  @Override
  public void writeTo(final JsonSink output) throws IOException {
    output.beginObject();
    if ((bitField0_ & 0x00000001) != 0) {
      output.writeRepeatedDouble(FieldNames.rows, rows);
    }
    output.endObject();
  }

  @Override
  public ProtobufVector mergeFrom(final JsonSource input) throws IOException {
    if (!input.beginObject()) {
      return this;
    }
    while (!input.isAtEnd()) {
      switch (input.readFieldHash()) {
        case 3506649: {
          if (input.isAtField(FieldNames.rows)) {
            if (!input.trySkipNullValue()) {
              input.readRepeatedDouble(rows);
              bitField0_ |= 0x00000001;
            }
          } else {
            input.skipUnknownField();
          }
          break;
        }
        default: {
          input.skipUnknownField();
          break;
        }
      }
    }
    input.endObject();
    return this;
  }

  @Override
  public ProtobufVector clone() {
    return new ProtobufVector().copyFrom(this);
  }

  @Override
  public boolean isEmpty() {
    return ((bitField0_) == 0);
  }

  public static ProtobufVector parseFrom(final byte[] data) throws InvalidProtocolBufferException {
    return ProtoMessage.mergeFrom(new ProtobufVector(), data).checkInitialized();
  }

  public static ProtobufVector parseFrom(final ProtoSource input) throws IOException {
    return ProtoMessage.mergeFrom(new ProtobufVector(), input).checkInitialized();
  }

  public static ProtobufVector parseFrom(final JsonSource input) throws IOException {
    return ProtoMessage.mergeFrom(new ProtobufVector(), input).checkInitialized();
  }

  /**
   * @return factory for creating ProtobufVector messages
   */
  public static MessageFactory<ProtobufVector> getFactory() {
    return ProtobufVectorFactory.INSTANCE;
  }

  /**
   * @return this type's descriptor.
   */
  public static Descriptors.Descriptor getDescriptor() {
    return ProtobufWPIMathDescriptor.wpi_proto_ProtobufVector_descriptor;
  }

  private enum ProtobufVectorFactory implements MessageFactory<ProtobufVector> {
    INSTANCE;

    @Override
    public ProtobufVector create() {
      return ProtobufVector.newInstance();
    }
  }

  /**
   * Contains name constants used for serializing JSON
   */
  static class FieldNames {
    static final FieldName rows = FieldName.forField("rows");
  }
}
