package storage;

import com.google.inject.Singleton;
import model.Record;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;

import java.util.ArrayList;
import java.util.Collection;

/**
 * (c) Roman Gordeev
 * <p/>
 * 2014 июл 09
 */
@Singleton
public class InMemoryStorage implements StorageService
{
    public InMemoryStorage() {
        this.storage = new ArrayList<Record>();
    }

    @Override
    public Record get(final Long id) {

        Record result = CollectionUtils.find(storage, new Predicate<Record>() {
            @Override
            public boolean evaluate(Record object) {
                return id == object.getId();
            }
        });

        return result == null ? new Record() : result;
    }

    @Override
    public Collection<Record> list() {
        return storage;
    }

    @Override
    public Record add(Record record)
    {
        storage.add(record);
        return record;
    }

    @Override
    public Record update(final Record record)
    {
        Record byID = CollectionUtils.find(storage, new Predicate<Record>() {
            @Override
            public boolean evaluate(Record object) {
                return record.getId() == object.getId();
            }
        });
        byID.setNote(record.getNote());

        return byID;
    }

    @Override
    public void delete(final Record record)
    {
        Collection<Record> delete = CollectionUtils.select(storage, new Predicate<Record>() {
            @Override
            public boolean evaluate(Record object) {
                return record.getId() == object.getId();
            }
        });

        storage = CollectionUtils.removeAll(storage, delete);
    }

    private Collection<Record> storage;
}

@@ -673,20 +673,24 @@
 eturn id
- == 
+.equals(
 object.g
@@ -687,32 +687,33 @@
 s(object.getId()
+)
 ;%0A            %7D%0A
@@ -1235,36 +1235,40 @@
 n record.getId()
- == 
+.equals(
 object.getId();%0A
@@ -1257,32 +1257,33 @@
 s(object.getId()
+)
 ;%0A            %7D%0A
@@ -1633,20 +1633,24 @@
 .getId()
- == 
+.equals(
 object.g
@@ -1655,16 +1655,17 @@
 .getId()
+)
 ;%0A      

fix inmemorystorage problem