package ru.job4j.map;

import javafx.beans.binding.ObjectExpression;

public class Map<K, V> {

    public Object[] container = new Object[10];
    private int capacity = 10;
    private int index = 0;

    public Object[] ensureCapacity(int length) {
        capacity = capacity + length;
        Object[] newArray = new Object[capacity];
        for (int i = 0; i < container.length; i++) {
            if (container[i] != null) {
                int cell = Math.abs(i % newArray.length);
                if (newArray[cell] == null) {
                    newArray[cell] = container[i];
                }
            }
        }
        return newArray;
    }

    public int getCell(K key) {
        return Math.abs(key.hashCode() % container.length);
    }

    public boolean addEntry(Object[] array, K key, V value) {
        int cell = getCell(key);
        if (array[cell] == null) {
            array[cell] = value;
            index++;
            return true;
        } else {
            return false;
        }
    }

    public boolean insert(K key, V value) {
        if (index > container.length - 1) {
            container = ensureCapacity(index + 10);
        }

        return addEntry(container, key, value);
    }

    public V get(K key) {
        int cell = getCell(key);
        return (V) container[cell];
    }

    public boolean delete(K key) {
        int cell = getCell(key);
        if (container[cell] != null) {
            container[cell] = null;
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        Entry<Integer, String> entry1 = new Entry<>(1, "first");
        Entry<Integer, String> entry2 = new Entry<>(2, "second");
        Entry<Integer, String> entry3 = new Entry<>(3, "third");
        Map<Integer, String> map = new Map<>();
        map.insert(entry1.key, entry1.value);
        map.insert(entry2.key, entry2.value);
        map.insert(entry3.key, entry3.value);

        for (int i = 0; i < map.container.length; i++) {
            System.out.println(i+ ") " + map.container[i]);
        }
    }
}

@@ -19,56 +19,8 @@
 ap;%0A
-%0Aimport javafx.beans.binding.ObjectExpression;%0A%0A
 publ
@@ -52,22 +52,21 @@
  public 
-Object
+Entry
 %5B%5D conta
@@ -80,17 +80,15 @@
 new 
-Object%5B10
+Entry%5B2
 %5D;%0A 
@@ -156,22 +156,21 @@
  public 
-Object
+Entry
 %5B%5D ensur
@@ -243,21 +243,17 @@
     
-Object%5B%5D newA
+Entry%5B%5D a
 rray
@@ -259,22 +259,21 @@
 y = new 
-Object
+Entry
 %5Bcapacit
@@ -357,20 +357,18 @@
 ainer%5Bi%5D
- 
 !=
- 
 null) %7B%0A
@@ -387,164 +387,70 @@
     
-int cell = Math.abs(i %25 newArray.length);%0A                if (newArray%5Bcell%5D == null) %7B%0A                    newArray%5Bcell%5D = container%5Bi%5D;%0A                %7D
+addEntry(array, (K) container%5Bi%5D.key, (V) container%5Bi%5D.value);
 %0A   
@@ -489,12 +489,9 @@
 urn 
-newA
+a
 rray
@@ -630,14 +630,13 @@
 try(
-Object
+Entry
 %5B%5D a
@@ -752,22 +752,38 @@
 %5Bcell%5D =
+ new Entry(key,
  value
+)
 ;%0A      
@@ -1028,25 +1028,24 @@
 ;%0A        %7D%0A
-%0A
         retu
@@ -1421,567 +1421,5 @@
  %7D%0A%0A
-    public static void main(String%5B%5D args) %7B%0A        Entry%3CInteger, String%3E entry1 = new Entry%3C%3E(1, %22first%22);%0A        Entry%3CInteger, String%3E entry2 = new Entry%3C%3E(2, %22second%22);%0A        Entry%3CInteger, String%3E entry3 = new Entry%3C%3E(3, %22third%22);%0A        Map%3CInteger, String%3E map = new Map%3C%3E();%0A        map.insert(entry1.key, entry1.value);%0A        map.insert(entry2.key, entry2.value);%0A        map.insert(entry3.key, entry3.value);%0A%0A        for (int i = 0; i %3C map.container.length; i++) %7B%0A            System.out.println(i+ %22) %22 + map.container%5Bi%5D);%0A        %7D%0A    %7D%0A%7D%0A
+%7D

task #1008