<commit_before>package com.blocklaunch.blwarps.manager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.blocklaunch.blwarps.BLWarps;
import com.blocklaunch.blwarps.Warp;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.common.base.Optional;

public class FlatFileManager extends StorageManager {

	private ObjectMapper mapper = new ObjectMapper();

	private static final String ERROR_FILE_WRITE = "There was an error writing to the file!";
	private static final String ERROR_FILE_READ = "There was an error reading the warps file!";

	/**
	 * Reads in warps file, and de-serializes it to a List<Warp>
	 */
	@Override
	public boolean loadWarps() {
		Optional<List<Warp>> optWarps = readInWarps();
		if (!optWarps.isPresent()) {
			return false;
		}

		WarpManager.warps = optWarps.get();
		return true;
	}

	/**
	 * Read in warp file and deserialize to List<Warp> Insert new warp to List
	 * Serialize to JSON and write to file
	 * 
	 * @param warp
	 *            the new warp to save
	 * @return the success of the saving operation
	 */
	@Override
	boolean saveNewWarp(Warp warp) {
		Optional<List<Warp>> warpsOpt = readInWarps();

		List<Warp> currentlySavedWarps = new ArrayList<Warp>();
		if (warpsOpt.isPresent()) {
			currentlySavedWarps = warpsOpt.get();
		}
		currentlySavedWarps.add(warp);

		return writeOutWarps(currentlySavedWarps);
	}

	/**
	 * Read in warp file and deserialize to List<Warp>. Remove the warp from the
	 * list Serialize to JSON and write to file
	 * 
	 * @param warp
	 *            the warp to remove
	 * @return the success of the deletion operation
	 */
	@Override
	boolean deleteWarp(Warp warp) {
		Optional<List<Warp>> warpsOpt = readInWarps();
		List<Warp> warps;
		if (warpsOpt.isPresent()) {
			warps = warpsOpt.get();
		} else {
			return false;
		}

		for (Warp w : warps) {
			if (w.getName().equalsIgnoreCase(warp.getName())) {
				warps.remove(w);
			}
		}

		return writeOutWarps(warps);
	}

	/**<commit_msg>Fixing ConcurrentModificationException when deleting warps<commit_after>package com.blocklaunch.blwarps.manager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.blocklaunch.blwarps.BLWarps;
import com.blocklaunch.blwarps.Warp;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.common.base.Optional;

public class FlatFileManager extends StorageManager {

	private ObjectMapper mapper = new ObjectMapper();

	private static final String ERROR_FILE_WRITE = "There was an error writing to the file!";
	private static final String ERROR_FILE_READ = "There was an error reading the warps file!";

	/**
	 * Reads in warps file, and de-serializes it to a List<Warp>
	 */
	@Override
	public boolean loadWarps() {
		Optional<List<Warp>> optWarps = readInWarps();
		if (!optWarps.isPresent()) {
			return false;
		}

		WarpManager.warps = optWarps.get();
		return true;
	}

	/**
	 * Read in warp file and deserialize to List<Warp> Insert new warp to List
	 * Serialize to JSON and write to file
	 * 
	 * @param warp
	 *            the new warp to save
	 * @return the success of the saving operation
	 */
	@Override
	boolean saveNewWarp(Warp warp) {
		Optional<List<Warp>> warpsOpt = readInWarps();

		List<Warp> currentlySavedWarps = new ArrayList<Warp>();
		if (warpsOpt.isPresent()) {
			currentlySavedWarps = warpsOpt.get();
		}
		currentlySavedWarps.add(warp);

		return writeOutWarps(currentlySavedWarps);
	}

	/**
	 * Read in warp file and deserialize to List<Warp>. Remove the warp from the
	 * list Serialize to JSON and write to file
	 * 
	 * @param warp
	 *            the warp to remove
	 * @return the success of the deletion operation
	 */
	@Override
	boolean deleteWarp(Warp warp) {
		Optional<List<Warp>> warpsOpt = readInWarps();
		List<Warp> warps;
		if (warpsOpt.isPresent()) {
			warps = warpsOpt.get();
		} else {
			return false;
		}

		// Temporary warp for avoiding ConcurrentModificationException
		Warp warpToRemove = null;		
		for (Warp w : warps) {
			if (w.getName().equalsIgnoreCase(warp.getName())) {
				warpToRemove = w;
			}
		}
		if (warpToRemove != null)
			warps.remove(warpToRemove);

		return writeOutWarps(warps);
	}

	/**
{'fraction_non_alphanumeric': 0.10493273542600896, 'fraction_numerical': 0.0, 'mean_word_length': 3.875409836065574, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2370180', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1407, 'n_words': 401}