/* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 4.0.2
 *
 * Do not make changes to this file unless you know what you are doing--modify
 * the SWIG interface file instead.
 * ----------------------------------------------------------------------------- */

package org.quantlib;

public class Leg extends java.util.AbstractList<CashFlow> implements java.util.RandomAccess {
  private transient long swigCPtr;
  protected transient boolean swigCMemOwn;

  protected Leg(long cPtr, boolean cMemoryOwn) {
    swigCMemOwn = cMemoryOwn;
    swigCPtr = cPtr;
  }

  protected static long getCPtr(Leg obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }

  @SuppressWarnings("deprecation")
  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
        QuantLibJNI.delete_Leg(swigCPtr);
      }
      swigCPtr = 0;
    }
  }

  public Leg(CashFlow[] initialElements) {
    this();
    reserve(initialElements.length);

    for (CashFlow element : initialElements) {
      add(element);
    }
  }

  public Leg(Iterable<CashFlow> initialElements) {
    this();
    for (CashFlow element : initialElements) {
      add(element);
    }
  }

  public CashFlow get(int index) {
    return doGet(index);
  }

  public CashFlow set(int index, CashFlow e) {
    return doSet(index, e);
  }

  public boolean add(CashFlow e) {
    modCount++;
    doAdd(e);
    return true;
  }

  public void add(int index, CashFlow e) {
    modCount++;
    doAdd(index, e);
  }

  public CashFlow remove(int index) {
    modCount++;
    return doRemove(index);
  }

  protected void removeRange(int fromIndex, int toIndex) {
    modCount++;
    doRemoveRange(fromIndex, toIndex);
  }

  public int size() {
    return doSize();
  }

  public Leg() {
    this(QuantLibJNI.new_Leg__SWIG_0(), true);
  }

  public Leg(Leg other) {
    this(QuantLibJNI.new_Leg__SWIG_1(Leg.getCPtr(other), other), true);
  }

  public long capacity() {
    return QuantLibJNI.Leg_capacity(swigCPtr, this);
  }

  public void reserve(long n) {
    QuantLibJNI.Leg_reserve(swigCPtr, this, n);
  }

  public boolean isEmpty() {
    return QuantLibJNI.Leg_isEmpty(swigCPtr, this);
  }

  public void clear() {
    QuantLibJNI.Leg_clear(swigCPtr, this);
  }

  public Leg(int count, CashFlow value) {
    this(QuantLibJNI.new_Leg__SWIG_2(count, CashFlow.getCPtr(value), value), true);
  }

  private int doSize() {
    return QuantLibJNI.Leg_doSize(swigCPtr, this);
  }

  private void doAdd(CashFlow x) {
    QuantLibJNI.Leg_doAdd__SWIG_0(swigCPtr, this, CashFlow.getCPtr(x), x);
  }

  private void doAdd(int index, CashFlow x) {
    QuantLibJNI.Leg_doAdd__SWIG_1(swigCPtr, this, index, CashFlow.getCPtr(x), x);
  }

  private CashFlow doRemove(int index) {
    long cPtr = QuantLibJNI.Leg_doRemove(swigCPtr, this, index);
    return (cPtr == 0) ? null : new CashFlow(cPtr, true);
  }

  private CashFlow doGet(int index) {
    long cPtr = QuantLibJNI.Leg_doGet(swigCPtr, this, index);
    return (cPtr == 0) ? null : new CashFlow(cPtr, true);
  }

  private CashFlow doSet(int index, CashFlow val) {
    long cPtr = QuantLibJNI.Leg_doSet(swigCPtr, this, index, CashFlow.getCPtr(val), val);
    return (cPtr == 0) ? null : new CashFlow(cPtr, true);
  }

  private void doRemoveRange(int fromIndex, int toIndex) {
    QuantLibJNI.Leg_doRemoveRange(swigCPtr, this, fromIndex, toIndex);
  }

}
