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

package com.nanotec.nanolib;

public class StringStringMap extends java.util.AbstractMap<String, String> {
  private transient long swigCPtr;
  protected transient boolean swigCMemOwn;

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

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

  protected static long swigRelease(StringStringMap obj) {
    long ptr = 0;
    if (obj != null) {
      if (!obj.swigCMemOwn)
        throw new RuntimeException("Cannot release ownership as memory is not owned");
      ptr = obj.swigCPtr;
      obj.swigCMemOwn = false;
      obj.delete();
    }
    return ptr;
  }

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

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


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

  public boolean containsKey(java.lang.Object key) {
    if (!(key instanceof String)) {
      return false;
    }

    return containsImpl((String)key);
  }

  public String get(java.lang.Object key) {
    if (!(key instanceof String)) {
      return null;
    }

    Iterator itr = find((String) key);
    if (itr.isNot(end())) {
      return itr.getValue();
    }

    return null;
  }

  public String put(String key, String value) {
    Iterator itr = find((String) key);
    if (itr.isNot(end())) {
      String oldValue = itr.getValue();
      itr.setValue(value);
      return oldValue;
    } else {
      putUnchecked(key, value);
      return null;
    }
  }

  public String remove(java.lang.Object key) {
    if (!(key instanceof String)) {
      return null;
    }

    Iterator itr = find((String) key);
    if (itr.isNot(end())) {
      String oldValue = itr.getValue();
      removeUnchecked(itr);
      return oldValue;
    } else {
      return null;
    }
  }

  public java.util.Set<Entry<String, String>> entrySet() {
    java.util.Set<Entry<String, String>> setToReturn =
        new java.util.HashSet<Entry<String, String>>();

    Iterator itr = begin();
    final Iterator end = end();
    while (itr.isNot(end)) {
      setToReturn.add(new Entry<String, String>() {
        private Iterator iterator;

        private Entry<String, String> init(Iterator iterator) {
          this.iterator = iterator;
          return this;
        }

        public String getKey() {
          return iterator.getKey();
        }

        public String getValue() {
          return iterator.getValue();
        }

        public String setValue(String newValue) {
          String oldValue = iterator.getValue();
          iterator.setValue(newValue);
          return oldValue;
        }
      }.init(itr));
      itr = itr.getNextUnchecked();
    }

    return setToReturn;
  }

  public StringStringMap() {
    this(NanolibJNI.new_StringStringMap__SWIG_0(), true);
  }

  public StringStringMap(StringStringMap other) {
    this(NanolibJNI.new_StringStringMap__SWIG_1(StringStringMap.getCPtr(other), other), true);
  }

  static protected class Iterator {
    private transient long swigCPtr;
    protected transient boolean swigCMemOwn;
  
    protected Iterator(long cPtr, boolean cMemoryOwn) {
      swigCMemOwn = cMemoryOwn;
      swigCPtr = cPtr;
    }
  
    protected static long getCPtr(Iterator obj) {
      return (obj == null) ? 0 : obj.swigCPtr;
    }
  
    protected static long swigRelease(Iterator obj) {
      long ptr = 0;
      if (obj != null) {
        if (!obj.swigCMemOwn)
          throw new RuntimeException("Cannot release ownership as memory is not owned");
        ptr = obj.swigCPtr;
        obj.swigCMemOwn = false;
        obj.delete();
      }
      return ptr;
    }
  
    @SuppressWarnings("deprecation")
    protected void finalize() {
      delete();
    }
  
    public synchronized void delete() {
      if (swigCPtr != 0) {
        if (swigCMemOwn) {
          swigCMemOwn = false;
          NanolibJNI.delete_StringStringMap_Iterator(swigCPtr);
        }
        swigCPtr = 0;
      }
    }
  
    private StringStringMap.Iterator getNextUnchecked() {
      return new StringStringMap.Iterator(NanolibJNI.StringStringMap_Iterator_getNextUnchecked(swigCPtr, this), true);
    }
  
    private boolean isNot(StringStringMap.Iterator other) {
      return NanolibJNI.StringStringMap_Iterator_isNot(swigCPtr, this, StringStringMap.Iterator.getCPtr(other), other);
    }
  
    private String getKey() {
      return NanolibJNI.StringStringMap_Iterator_getKey(swigCPtr, this);
    }
  
    private String getValue() {
      return NanolibJNI.StringStringMap_Iterator_getValue(swigCPtr, this);
    }
  
    private void setValue(String newValue) {
      NanolibJNI.StringStringMap_Iterator_setValue(swigCPtr, this, newValue);
    }
  
  }

  public boolean isEmpty() {
    return NanolibJNI.StringStringMap_isEmpty(swigCPtr, this);
  }

  public void clear() {
    NanolibJNI.StringStringMap_clear(swigCPtr, this);
  }

  private StringStringMap.Iterator find(String key) {
    return new StringStringMap.Iterator(NanolibJNI.StringStringMap_find(swigCPtr, this, key), true);
  }

  private StringStringMap.Iterator begin() {
    return new StringStringMap.Iterator(NanolibJNI.StringStringMap_begin(swigCPtr, this), true);
  }

  private StringStringMap.Iterator end() {
    return new StringStringMap.Iterator(NanolibJNI.StringStringMap_end(swigCPtr, this), true);
  }

  private int sizeImpl() {
    return NanolibJNI.StringStringMap_sizeImpl(swigCPtr, this);
  }

  private boolean containsImpl(String key) {
    return NanolibJNI.StringStringMap_containsImpl(swigCPtr, this, key);
  }

  private void putUnchecked(String key, String value) {
    NanolibJNI.StringStringMap_putUnchecked(swigCPtr, this, key, value);
  }

  private void removeUnchecked(StringStringMap.Iterator itr) {
    NanolibJNI.StringStringMap_removeUnchecked(swigCPtr, this, StringStringMap.Iterator.getCPtr(itr), itr);
  }

}
