// Path: shared/src/main/java/com/optimizely/ab/android/shared/Cache.java
// public class Cache {
// 
//     @NonNull private final Context context;
//     @NonNull private final Logger logger;
// 
//     /**
//      * Create new instance of {@link Cache}.
//      *
//      * @param context any {@link Context instance}
//      * @param logger  a {@link Logger} instances
//      */
//     public Cache(@NonNull Context context, @NonNull Logger logger) {
//         this.context = context;
//         this.logger = logger;
//     }
// 
//     /**
//      * Delete the cache file.
//      *
//      * @param filename the path to the file
//      * @return true if the file was deleted or false otherwise
//      */
//     public boolean delete(String filename) {
//         return context.deleteFile(filename);
//     }
// 
//     /**
//      * Check if the cache file exists.
//      *
//      * @param filename the path to the file
//      * @return true if the file exists or false otherwise
//      */
//     public boolean exists(String filename) {
//         String[] files = context.fileList();
//         return files != null && Arrays.asList(files).contains(filename);
//     }
// 
//     /**
//      * Load data from the cache file.
//      *
//      * @param filename the path to the file
//      * @return the loaded cache file as String or null if the file cannot be loaded
//      */
//     @Nullable
//     public String load(String filename) {
//         FileInputStream fileInputStream = null;
//         try {
//             fileInputStream = context.openFileInput(filename);
//             InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
//             BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//             StringBuilder sb = new StringBuilder();
//             String line;
//             while ((line = bufferedReader.readLine()) != null) {
//                 sb.append(line);
//             }
//             return sb.toString();
//         } catch (Exception e) {
//             logger.warn("Unable to load file {}.", filename);
//             return null;
//         } finally {
//             try {
//                 if (fileInputStream != null) {
//                     fileInputStream.close();
//                 }
//             } catch (Exception e) {
//                 logger.warn("Unable to close file {}.", filename, e);
//             }
//         }
//     }
// 
//     /**
//      * Save data to the cache file and overwrite any existing data.
//      *
//      * @param filename the path to the file
//      * @param data     the String data to write to the file
//      * @return true if the file was saved
//      */
//     public boolean save(String filename, String data) {
//         FileOutputStream fileOutputStream = null;
//         try {
//             fileOutputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
//             fileOutputStream.write(data.getBytes());
//             return true;
//         } catch (Exception e) {
//             logger.error("Error saving file {}.", filename);
//             return false;
//         } finally {
//             if (fileOutputStream != null) {
//                 try {
//                     fileOutputStream.close();
//                 } catch (Exception e) {
//                     logger.warn("Unable to close file {}.", filename, e);
// 
//                 }
//             }
//         }
//     }
// }


import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import static com.optimizely.ab.bucketing.UserProfileService.experimentBucketMapKey;
import static com.optimizely.ab.bucketing.UserProfileService.userIdKey;
import static com.optimizely.ab.bucketing.UserProfileService.variationIdKey;
import android.annotation.TargetApi;
import android.os.AsyncTask;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.optimizely.ab.android.shared.Cache;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import java.util.Iterator;
            logger.info("Saved user profile for {}.", userId);
        }
    }

    /**
     * Load the cache from disk to memory.
     */
    void start() {
        // Migrate legacy user profiles if found.
        migrateLegacyUserProfiles();

        try {
            JSONObject userProfilesJson = diskCache.load();
            Map<String, Map<String, Object>> userProfilesMap = UserProfileCacheUtils.convertJSONObjectToMap
                    (userProfilesJson);
            memoryCache.clear();
            memoryCache.putAll(userProfilesMap);
            logger.info("Loaded user profile cache from disk.");
        } catch (Exception e) {
            clear();
            logger.error("Unable to parse user profile cache from disk.", e);
        }
    }

    /**
     * Write-through cache persisted on disk.
     */
    static class DiskCache {

        private static final String FILE_NAME = "optly-user-profile-service-%s.json";
// Path: shared/src/main/java/com/optimizely/ab/android/shared/Cache.java
// public class Cache {
// 
//     @NonNull private final Context context;
//     @NonNull private final Logger logger;
// 
//     /**
//      * Create new instance of {@link Cache}.
//      *
//      * @param context any {@link Context instance}
//      * @param logger  a {@link Logger} instances
//      */
//     public Cache(@NonNull Context context, @NonNull Logger logger) {
//         this.context = context;
//         this.logger = logger;
//     }
// 
//     /**
//      * Delete the cache file.
//      *
//      * @param filename the path to the file
//      * @return true if the file was deleted or false otherwise
//      */
//     public boolean delete(String filename) {
//         return context.deleteFile(filename);
//     }
// 
//     /**
//      * Check if the cache file exists.
//      *
//      * @param filename the path to the file
//      * @return true if the file exists or false otherwise
//      */
//     public boolean exists(String filename) {
//         String[] files = context.fileList();
//         return files != null && Arrays.asList(files).contains(filename);
//     }
// 
//     /**
//      * Load data from the cache file.
//      *
//      * @param filename the path to the file
//      * @return the loaded cache file as String or null if the file cannot be loaded
//      */
//     @Nullable
//     public String load(String filename) {
//         FileInputStream fileInputStream = null;
//         try {
//             fileInputStream = context.openFileInput(filename);
//             InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
//             BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
//             StringBuilder sb = new StringBuilder();
//             String line;
//             while ((line = bufferedReader.readLine()) != null) {
//                 sb.append(line);
//             }
//             return sb.toString();
//         } catch (Exception e) {
//             logger.warn("Unable to load file {}.", filename);
//             return null;
//         } finally {
//             try {
//                 if (fileInputStream != null) {
//                     fileInputStream.close();
//                 }
//             } catch (Exception e) {
//                 logger.warn("Unable to close file {}.", filename, e);
//             }
//         }
//     }
// 
//     /**
//      * Save data to the cache file and overwrite any existing data.
//      *
//      * @param filename the path to the file
//      * @param data     the String data to write to the file
//      * @return true if the file was saved
//      */
//     public boolean save(String filename, String data) {
//         FileOutputStream fileOutputStream = null;
//         try {
//             fileOutputStream = context.openFileOutput(filename, Context.MODE_PRIVATE);
//             fileOutputStream.write(data.getBytes());
//             return true;
//         } catch (Exception e) {
//             logger.error("Error saving file {}.", filename);
//             return false;
//         } finally {
//             if (fileOutputStream != null) {
//                 try {
//                     fileOutputStream.close();
//                 } catch (Exception e) {
//                     logger.warn("Unable to close file {}.", filename, e);
// 
//                 }
//             }
//         }
//     }
// }


// Path: user-profile/src/main/java/com/optimizely/ab/android/user_profile/UserProfileCache.java
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import static com.optimizely.ab.bucketing.UserProfileService.experimentBucketMapKey;
import static com.optimizely.ab.bucketing.UserProfileService.userIdKey;
import static com.optimizely.ab.bucketing.UserProfileService.variationIdKey;
import android.annotation.TargetApi;
import android.os.AsyncTask;
import android.os.Build;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.optimizely.ab.android.shared.Cache;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import java.util.Iterator;

            logger.info("Saved user profile for {}.", userId);
        }
    }

    /**
     * Load the cache from disk to memory.
     */
    void start() {
        // Migrate legacy user profiles if found.
        migrateLegacyUserProfiles();

        try {
            JSONObject userProfilesJson = diskCache.load();
            Map<String, Map<String, Object>> userProfilesMap = UserProfileCacheUtils.convertJSONObjectToMap
                    (userProfilesJson);
            memoryCache.clear();
            memoryCache.putAll(userProfilesMap);
            logger.info("Loaded user profile cache from disk.");
        } catch (Exception e) {
            clear();
            logger.error("Unable to parse user profile cache from disk.", e);
        }
    }

    /**
     * Write-through cache persisted on disk.
     */
    static class DiskCache {

        private static final String FILE_NAME = "optly-user-profile-service-%s.json";
        @NonNull private final Cache cache;