package ru.agolovin;

import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

/**
 * @author agolovin (agolovin@list.ru)
 * @version $Id$
 * @since 0.1
 */

public class MyMap<T, V> implements Iterable<V> {

    private Node<T, V>[] nodes;
    private int position = 0;

    public MyMap() {
        int capacity = 16;
        this.nodes = new Node[capacity];
    }

    boolean insert(T key, V value) {
        return false;
    }

    private boolean insertVal(int hash, T key, V value) {
        boolean result = false;
        int hashK = hash(key);
        int pos = getPosition(hash, nodes.length);



        if (this.nodes.length == 0 || this.nodes == null || position > (this.nodes.length - 1)) {
            this.nodes = resize();
        }
        return false;
    }

    final Node<T, V>[] resize() {
        int newCapacity = this.nodes.length * 2;
        Node<T, V>[] newTab = (Node<T, V>[]) new Node[newCapacity];
        for (int j = 0; j < this.nodes.length; ++j) {
            Node<T, V> e;
            if ((e = this.nodes[j]) != null) {
                newTab[indexOfTable(e.hash, newCapacity)] = e;
            }
        }
        return newTab;
    }

    V get(T key) {
        return null;
    }

    boolean delete(T key) {
        return false;
    }


    private int hash(Object key) {
        int h = key.hashCode();
        return h ^ (h >>> 16);
    }

    private int getPosition(int hashValue, int length) {
        return (length - 1) & hashValue;
    }


    @Override
    public Iterator<V> iterator() {
        return new Iterator<V>() {
            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public V next() {
                return null;
            }
        };
    }

    private class Node<T, V> implements Map.Entry<T, V> {

        Node<T, V> next;
        T key;
        V value;
        int hash;

        public Node(int hash, T key, V value, Node<T, V> next) {
            this.next = next;
            this.key = key;
            this.value = value;
            this.hash = hash;
        }

        public int hashCode() {
            return Objects.hashCode(key) ^ Objects.hashCode(value);
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Node<T, V> node = (Node<T, V>) o;
            return hash == node.hash &&
                    Objects.equals(key, node.key) &&
                    Objects.equals(value, node.value);
        }

        @Override
        public T getKey() {
            return this.key;
        }

        @Override
        public V setValue(V value) {
            this.value = value;
            return this.value;
        }

        @Override
        public V getValue() {
            return this.value;
        }
    }
}

@@ -620,17 +620,16 @@
 gth);%0A%0A%0A
-%0A
         
@@ -796,16 +796,24 @@
  %7D%0A%0A    
+private 
 final No
@@ -907,19 +907,19 @@
 V%3E%5B%5D new
-Tab
+Map
  = (Node
@@ -968,45 +968,36 @@
 or (
-int j = 0; j %3C this.nodes.length; ++j
+Node%3CT, V%3E node : this.nodes
 ) %7B%0A
@@ -1020,16 +1020,23 @@
 %3CT, V%3E e
+ = node
 ;%0A      
@@ -1048,28 +1048,10 @@
  if 
-(
 (e
- = this.nodes%5Bj%5D)
  != 
@@ -1081,24 +1081,23 @@
  new
-Tab%5BindexOfTable
+Map%5BgetPosition
 (e.h
@@ -1166,11 +1166,11 @@
  new
-Tab
+Map
 ;%0A  

add task8 in part5 chapter5 package2