package com.zenika.cudf.adapter.cache;

import com.zenika.cudf.model.Binaries;
import com.zenika.cudf.model.Binary;
import com.zenika.cudf.model.BinaryId;

import java.util.HashSet;
import java.util.Set;

/**
 * @author Antoine Rouaze <antoine.rouaze@zenika.com>
 */
public class CachedBinaries implements Binaries {

    private static final String BINARY_ID_KEY_LIST = "BINARY_KEYS";

    private final Cache cache;

    public CachedBinaries(Cache cache) {
        this.cache = cache;
        this.cache.put(BINARY_ID_KEY_LIST, new HashSet<BinaryId>());
    }

    @Override
    public Binary getBinaryById(BinaryId binaryId) {
        return (Binary) cache.get(binaryId);
    }

    //TODO: This method may thrown an OutOfMemory exception about heap space.
    @Override
    public Set<Binary> getAllBinaries() {
        Set<BinaryId> binaryIds = (Set<BinaryId>) cache.get(BINARY_ID_KEY_LIST);
        Set<Binary> binaries = new HashSet<Binary>();
        for (BinaryId binaryId : binaryIds) {
            binaries.add((Binary) cache.get(binaryId));
        }
        return binaries;
    }

    @Override
    public void addBinary(Binary binary) {
        cache.put(binary.getBinaryId(), binary);
        Set<BinaryId> binaryIds = (Set<BinaryId>) cache.get(BINARY_ID_KEY_LIST);
        binaryIds.add(binary.getBinaryId());
    }

    @Override
    public void setBinaries(Set<Binary> binaries) {
        for (Binary binary : binaries) {
            addBinary(binary);
        }
    }
}

@@ -287,16 +287,20 @@
 Binaries
+%0A   
  impleme
@@ -315,17 +315,17 @@
 ries
- 
+%0A
 %7B%0A%0A    p
 riva
@@ -320,22 +320,21 @@
 %7B%0A%0A    p
-rivate
+ublic
  static 
@@ -441,16 +441,17 @@
 inaries(
+ 
 Cache ca
@@ -453,17 +453,22 @@
 he cache
-)
+ )%0A   
  %7B%0A     
@@ -513,16 +513,17 @@
 che.put(
+ 
 BINARY_I
@@ -557,16 +557,17 @@
 aryId%3E()
+ 
 );%0A    %7D
@@ -614,16 +614,17 @@
 aryById(
+ 
 BinaryId
@@ -632,17 +632,22 @@
 binaryId
-)
+ )%0A   
  %7B%0A     
@@ -675,24 +675,25 @@
 che.get(
+ 
 binaryId
 );%0A    %7D
@@ -684,16 +684,17 @@
 binaryId
+ 
 );%0A    %7D
@@ -826,16 +826,20 @@
 naries()
+%0A   
  %7B%0A     
@@ -885,32 +885,33 @@
 yId%3E) cache.get(
+ 
 BINARY_ID_KEY_LI
@@ -904,32 +904,33 @@
 NARY_ID_KEY_LIST
+ 
 );%0A        Set%3CB
@@ -979,24 +979,25 @@
        for (
+ 
 BinaryId bin
@@ -1013,17 +1013,26 @@
 inaryIds
-)
+ )%0A       
  %7B%0A     
@@ -1051,16 +1051,17 @@
 ies.add(
+ 
 (Binary)
@@ -1071,24 +1071,25 @@
 che.get(
+ 
 binaryId
 ));%0A    
@@ -1080,17 +1080,19 @@
 binaryId
-)
+ ) 
 );%0A     
@@ -1168,16 +1168,17 @@
 dBinary(
+ 
 Binary b
@@ -1182,17 +1182,22 @@
 y binary
-)
+ )%0A   
  %7B%0A     
@@ -1209,16 +1209,17 @@
 che.put(
+ 
 binary.g
@@ -1234,24 +1234,25 @@
 Id(), binary
+ 
 );%0A        S
@@ -1302,16 +1302,17 @@
 che.get(
+ 
 BINARY_I
@@ -1321,16 +1321,17 @@
 KEY_LIST
+ 
 );%0A     
@@ -1347,16 +1347,17 @@
 Ids.add(
+ 
 binary.g
@@ -1368,16 +1368,17 @@
 naryId()
+ 
 );%0A    %7D
@@ -1421,16 +1421,17 @@
 inaries(
+ 
 Set%3CBina
@@ -1434,33 +1434,38 @@
 Binary%3E binaries
-)
+ )%0A   
  %7B%0A        for (
@@ -1464,16 +1464,17 @@
    for (
+ 
 Binary b
@@ -1489,17 +1489,26 @@
 binaries
-)
+ )%0A       
  %7B%0A     
@@ -1524,22 +1524,24 @@
 dBinary(
+ 
 binary
+ 
 );%0A     

Fix constant access in CachedBinaries.