package xpress.storage;

import org.joda.time.Interval;
import xpress.TimeEnum;
import xpress.Vote;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sechelc
 */
public class InMemoryRepository implements Repository {

    private static InMemoryRepository inMemoryRepository;

    private InMemoryRepository(){

    }

    public static InMemoryRepository getInstance(){
          if(inMemoryRepository ==null){
              inMemoryRepository = new InMemoryRepository();
          }
        return inMemoryRepository;
    }

    private List<Vote> voteList = new ArrayList<>();
    private final static long ONE_DAY = 1000*60*60*24;
    private final static long ONE_MONTH = ONE_DAY * 30;
    private final static long ONE_YEAR = ONE_MONTH * 12;

    @Override
    public void saveVote(Vote vote) {
        voteList.add(vote);
    }

    @Override
    public List<Vote> getVotes(Filter queryVote) {
        List<Vote> result = new ArrayList<>();
        boolean applyMoodQuery = false;
        boolean applyTimeQuery = false;
        boolean applyTagQuery = false;
        for (Vote vote : voteList) {
            if (queryVote.getMood() != null) {
                applyMoodQuery = true;
            }

            if (queryVote.getTag() != null) {
                applyTagQuery = true;
            }

            if (queryVote.getTime()!=null){
                applyTimeQuery = true;
            }
            boolean shouldAdd = true;
            shouldAdd = applyMoodQuery ? vote.getMood().name().equals(queryVote.getMood().name()): shouldAdd;
            shouldAdd = applyTagQuery ? vote.getTag().trim().equalsIgnoreCase(queryVote.getTag()): shouldAdd;
            shouldAdd = applyTimeQuery ? isInTimeRange(vote.getTime(), queryVote.getTime()): shouldAdd;
            if(shouldAdd){
                result.add(vote);
            }
        }
        return result;
    }

    private boolean isInTimeRange(long time, TimeEnum timeRange) {
        long now = System.currentTimeMillis();
        Interval interval = getIntervalFormTimeEnum(timeRange, now);
        return interval.contains(time);
    }

    private Interval getIntervalFormTimeEnum(TimeEnum timeRange, long now) {
        switch (timeRange) {
            case LAST_DAY:
                return new Interval(now - ONE_DAY, now);
            case LAST_MONTH:
                return new Interval(now - ONE_MONTH, now);
            case LAST_YEAR:
                return new Interval(now - ONE_YEAR, now);
            default:
                return  new Interval(0 , now);

        }
    }
}

@@ -14,24 +14,76 @@
 s.storage;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 import org.j
@@ -101,16 +101,17 @@
 terval;%0A
+%0A
 import x
@@ -151,60 +151,8 @@
 e;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A
@@ -287,16 +287,43 @@
 pository
+ = new InMemoryRepository()
 ;%0A%0A    p
@@ -349,16 +349,17 @@
 sitory()
+ 
 %7B%0A%0A    %7D
@@ -414,123 +414,10 @@
 ce()
+ 
 %7B
-%0A          if(inMemoryRepository ==null)%7B%0A              inMemoryRepository = new InMemoryRepository();%0A          %7D
 %0A   
@@ -556,15 +556,21 @@
 1000
-*60*60*
+ * 60 * 60 * 
 24;%0A
@@ -1277,15 +1277,18 @@
 me()
+ 
 !=
+ 
 null)
+ 
 %7B%0A  
@@ -1469,24 +1469,25 @@
 od().name())
+ 
 : shouldAdd;
@@ -1584,16 +1584,17 @@
 etTag())
+ 
 : should
@@ -1689,16 +1689,17 @@
 tTime())
+ 
 : should
@@ -1717,16 +1717,17 @@
       if
+ 
 (shouldA
@@ -1729,16 +1729,17 @@
 ouldAdd)
+ 
 %7B%0A      
@@ -2156,36 +2156,32 @@
 ange) %7B%0A        
-    
 case LAST_DAY:%0A 
@@ -2179,20 +2179,16 @@
 ST_DAY:%0A
-    
         
@@ -2232,36 +2232,32 @@
 , now);%0A        
- 
-   
 case LAST_MONTH:
@@ -2257,20 +2257,16 @@
 _MONTH:%0A
-    
         
@@ -2316,28 +2316,24 @@
 w);%0A        
- 
-   
 case LAST_YE
@@ -2336,20 +2336,16 @@
 T_YEAR:%0A
-    
         
@@ -2398,20 +2398,16 @@
         
-  
-  
 default:
@@ -2423,20 +2423,15 @@
     
-  
-  
 return 
- 
 new 
@@ -2440,17 +2440,16 @@
 terval(0
- 
 , now);%0A

fix for singleton antipattern