<commit_before>package ru.job4j.nonblock;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * class NonBlockCash.
 */
public class NonBlockCash {
    /** data.*/
    private ConcurrentHashMap<Integer, Model> data = new ConcurrentHashMap<>();

    /**
     * method add.
     * @param model - model to add.
     */
    public void add(Model model) {
        data.putIfAbsent(model.getId(), model);
    }

    /**
     * method delete.
     * @param id - id.
     * @return boolean.
     */
    public boolean delete(Integer id) {
        return data.remove(id) != null;
    }

    /**
     * method getModel.
     * @param id - id.
     * @return model.
     */
    public Model getModel(Integer id) {
        return data.get(id);
    }

    /**
     * method update.
     * @param model - model.
     */
    public void update(Model model) {
        Integer id = model.getId();
        int version = data.get(id).getVersion();
        data.computeIfPresent(id, (key, oldModel) -> {
            if (version != model.getVersion()) {
                throw new OptimisticException("Exception");
            }
            model.upDateVersion();
            return model;
        });
    }

    /**
     * method toString.
     * @return string.
     */
    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();
        for (Map.Entry<Integer, Model> model : data.entrySet()) {
            str.append(model.getValue().toString()).append("\n");
        }
        return str.toString();
    }
}

/**
 * class OptimisticException.
 */
class OptimisticException extends RuntimeException {
    /**
     * constructor.
     * @param msg - message.
     */<commit_msg>task 4741 NonBlocking cash remake<commit_after>package ru.job4j.nonblock;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * class NonBlockCash.
 */
public class NonBlockCash {
    /** data.*/
    private ConcurrentHashMap<Integer, Model> data = new ConcurrentHashMap<>();

    /**
     * method add.
     * @param model - model to add.
     */
    public void add(Model model) {
        data.putIfAbsent(model.getId(), model);
    }

    /**
     * method delete.
     * @param id - id.
     * @return boolean.
     */
    public boolean delete(Integer id) {
        return data.remove(id) != null;
    }

    /**
     * method getModel.
     * @param id - id.
     * @return model.
     */
    public Model getModel(Integer id) {
        return data.get(id);
    }

    /**
     * method update.
     * @param model - model.
     */
    public void update(Model model) {
        Integer id = model.getId();
        data.computeIfPresent(id, (key, oldModel) -> {
            int version = data.get(id).getVersion();
            if (version != model.getVersion()) {
                throw new OptimisticException("Exception");
            }
            model.upDateVersion();
            return model;
        });
    }

    /**
     * method toString.
     * @return string.
     */
    @Override
    public String toString() {
        StringBuilder str = new StringBuilder();
        for (Map.Entry<Integer, Model> model : data.entrySet()) {
            str.append(model.getValue().toString()).append("\n");
        }
        return str.toString();
    }
}

/**
 * class OptimisticException.
 */
class OptimisticException extends RuntimeException {
    /**
     * constructor.
     * @param msg - message.
     */
{'fraction_non_alphanumeric': 0.1359167148640833, 'fraction_numerical': 0.001735106998264893, 'mean_word_length': 1.8753117206982544, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '7658629', 'n_tokens_mistral': 996, 'n_tokens_neox': 995, 'n_words': 234}