package dev.blunch.blunch.utils;

import com.firebase.client.Firebase;
import java.util.LinkedList;
import java.util.List;

public abstract class Repository<T extends Entity> {

    private final String FIREBASE_URI = "https://blunch.firebaseio.com/";

    public abstract T convert(Object o);

    public abstract String getObjectReference();

    public void insert(T t) {
        new Firebase(FIREBASE_URI).child(getObjectReference()).push().setValue(t);
    }

    public List<T> list() {
        Firebase mRef = new Firebase(FIREBASE_URI).child(getObjectReference());
        List<T> list = new LinkedList<>();
        for (Object o : mRef.getRepo().generateServerValues().values()) list.add(convert(o));
        return list;
    }

    public T findById(String id) {
        for (T t : list()) {
            if (t.getId().equals(id)) return t;
        }
        return null;
    }

    public void delete(String id) {
        T elem = findById(id);
        Firebase mRef = new Firebase(FIREBASE_URI).child(getObjectReference());
        mRef.child(id).removeValue();
    }
}

@@ -54,24 +54,201 @@
 .client.
-Firebase
+ChildEventListener;%0Aimport com.firebase.client.DataSnapshot;%0Aimport com.firebase.client.Firebase;%0Aimport com.firebase.client.FirebaseError;%0Aimport com.firebase.client.ValueEventListener
 ;%0Aimport
@@ -264,19 +264,22 @@
 l.Linked
-Lis
+HashSe
 t;%0Aimpor
@@ -294,14 +294,98 @@
 til.
-List;%0A
+Set;%0A%0A/**%0A * Repository abstract class%0A * @param %3CT%3E repository type%0A * @author albert%0A */
 %0Apub
@@ -513,368 +513,1441 @@
 /%22;%0A
-%0A
     p
-ublic abstract T convert(Object o);%0A%0A    public abstract String getObjectReference();%0A%0A    public void insert(T t) %7B%0A        new Firebase(FIREBASE_URI).child(getObjectReference()).push().setValue(t);%0A    %7D%0A%0A    public List%3CT%3E list() %7B%0A        Firebase mRef = new Firebase(FIREBASE_URI).child(getObjectReference());%0A        List%3CT%3E list = new LinkedList%3C%3E
+rivate Set%3CT%3E list;%0A%0A    public Repository() %7B%0A        list = new LinkedHashSet%3C%3E();%0A        Firebase mRef = new Firebase(FIREBASE_URI).child(getObjectReference());%0A        mRef.addValueEventListener(new ValueEventListener() %7B%0A            @Override%0A            public void onDataChange(DataSnapshot dataSnapshot) %7B%0A                list.clear();%0A                for (DataSnapshot data : dataSnapshot.getChildren()) %7B%0A                    list.add(convert(data));%0A                %7D%0A            %7D%0A            @Override%0A            public void onCancelled(FirebaseError firebaseError) %7B%0A                System.err.println(%22The read failed: %22 + firebaseError.getMessage());%0A            %7D%0A        %7D);%0A        mRef.addChildEventListener(new ChildEventListener() %7B%0A            @Override%0A            public void onChildAdded(DataSnapshot dataSnapshot, String s) %7B%0A                list.clear();%0A                for (DataSnapshot data : dataSnapshot.getChildren()) %7B%0A                    list.add(convert(data));%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void onChildChanged(DataSnapshot dataSnapshot, String s) %7B%0A                list.clear();%0A                for (DataSnapshot data : dataSnapshot.getChildren()) %7B%0A                    list.add(convert(data));%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void onChildRemoved(DataSnapshot dataSnapshot) %7B%0A                list.clear
 ();%0A
@@ -1958,93 +1958,780 @@
     
+  
-for (Object o : mRef.getRepo().generateServerValues().values()) list.add(convert(o));
+      for (DataSnapshot data : dataSnapshot.getChildren()) %7B%0A                    list.add(convert(data));%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void onChildMoved(DataSnapshot dataSnapshot, String s) %7B%0A                list.clear();%0A                for (DataSnapshot data : dataSnapshot.getChildren()) %7B%0A                    list.add(convert(data));%0A                %7D%0A            %7D%0A%0A            @Override%0A            public void onCancelled(FirebaseError firebaseError) %7B%0A                System.err.println(%22The read failed: %22 + firebaseError.getMessage());%0A            %7D%0A        %7D);%0A    %7D%0A%0A    public void insert(T t) %7B%0A        new Firebase(FIREBASE_URI).child(getObjectReference()).push().setValue(t);%0A    %7D%0A%0A    public Set%3CT%3E list() %7B
 %0A   
@@ -2945,39 +2945,8 @@
 ) %7B%0A
-        T elem = findById(id);%0A
     
@@ -3065,10 +3065,111 @@
 ;%0A    %7D%0A
-%7D
 %0A
+    public abstract T convert(DataSnapshot data);%0A%0A    public abstract String getObjectReference();%0A%0A%7D

fix problems with repository class