<commit_before>/**
 * Kuebiko - NoteManager.java
 * Copyright 2011 <NAME> (daveh303 at yahoo dot com).
 * TODO license info.
 */

package dmh.kuebiko.controller;

import static org.apache.commons.lang.StringUtils.isBlank;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;

import dmh.kuebiko.model.Note;
import dmh.kuebiko.model.NoteDao;
import dmh.kuebiko.model.ValidationException;
import dmh.kuebiko.util.NoteTitleFunction;

/**
 * Management class for notes. Acts as the note controller.
 *
 * @author davehuffman
 */
public class NoteManager {
    private final NoteDao noteDao;
    
    private List<Note> notes = null;

    private final Collection<Note> deletedNotes;
    
    public NoteManager(NoteDao noteDao) {
        this.noteDao = noteDao;
        
        deletedNotes = Lists.newArrayList();
        loadAllNotes();
    }

    private void loadAllNotes() {
        try {
            notes = noteDao.readNotes();
        } catch (IOException e) {
            throw new DataStoreException("Could not read notes.", e);
        }
    }
    
    public List<Note> getNotes() {
        return Collections.unmodifiableList(notes);
    }
    
    public List<String> getNoteTitles() {
        return Collections.unmodifiableList(
                Lists.transform(notes, NoteTitleFunction.getInstance()));
    }
    
    /**
     * @return True if there are no notes.
     */
    public boolean isEmpty() {
        return notes.isEmpty();
    }
    
    public int getNoteCount() {
        return notes.size();
    }
    
    public Note getNoteAt(int index) {
        return notes.get(index);
    }
    
    public void addNote(Note newNote) {
        notes.add(newNote);
    }
    
    public void deleteNote(Note note) {
        if (!notes.remove(note)) {
            throw new IllegalArgumentException(String.format(
                    "Note [%s] does not exist.", note));<commit_msg>Removed deprecated code (filtering logic). Added documentation. Added logic to generate unique default titles for new notes.<commit_after>/**
 * Kuebiko - NoteManager.java
 * Copyright 2011 <NAME> (daveh303 at yahoo dot com).
 * TODO license info.
 */

package dmh.kuebiko.controller;

import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;

import dmh.kuebiko.Main;
import dmh.kuebiko.model.Note;
import dmh.kuebiko.model.NoteDao;
import dmh.kuebiko.model.ValidationException;
import dmh.kuebiko.util.NoteTitleFunction;

/**
 * Management class for notes. Acts as the note controller.
 *
 * @author davehuffman
 */
public class NoteManager {
    static final String DEFAULT_NOTE_TITLE = "Untitled Note"; // TODO i18n.
    
    private final NoteDao noteDao;
    
    private List<Note> notes = null;

    private final Collection<Note> deletedNotes;
    
    /**
     * Constructor.
     * @param noteDao The DAO to use for note persistence.
     */
    public NoteManager(NoteDao noteDao) {
        this.noteDao = noteDao;
        
        deletedNotes = Lists.newArrayList();
        loadAllNotes();
    }

    private void loadAllNotes() {
        try {
            notes = Lists.newArrayList(noteDao.readNotes());
        } catch (IOException e) {
            throw new DataStoreException("Could not read notes.", e);
        }
    }
    
    /**
     * @return An immutable view of all notes in the stack.
     */
    public List<Note> getNotes() {
        return Collections.unmodifiableList(notes);
    }
    
    /**
     * @return A view containing the titles for all notes in the stack.
     */
    public List<String> getNoteTitles() {
        return Lists.transform(notes, NoteTitleFunction.getInstance());
    }
    
    /**
     * @return True if there are no notes.
     */
    public boolean isEmpty() {
        return notes.isEmpty();
    }
    
    public int getNoteCount() {
        return notes.size();
    }
    
    public Note getNoteAt(int index) {
        return notes.get(index);
    }
    
    /**
     * Add a new note with a default title.
     * @return The title of the new note.
     */
    public String addNewNote() {
        final String title = genUniqueNoteTitle();
        addNewNote(title);
        return title;
    }
    
    /**
     * Generate a unique note title for a new note.
     * @return A note title that is guaranteed to be unique among all notes
     *         in the stack.
     */
    String genUniqueNoteTitle() {
        // Get a list of default note titles.
        final Collection<String> defaultTitles = 
                Collections2.filter(getNoteTitles(), 
                        new Predicate<String>() {
                            @Override
                            public boolean apply(String input) {
                                return input.startsWith(DEFAULT_NOTE_TITLE);
                            }
                        });
        
        if (defaultTitles.size() == 0) {
            return DEFAULT_NOTE_TITLE;
        }
        
        // Default note titles exist; work thought the list and create a new, 
        // unique default note title.
        Pattern titleRegex = Pattern.compile(DEFAULT_NOTE_TITLE + " (\\d+)");
        int maxSuffix = 1;
        for (String title: defaultTitles) {
            Matcher matcher = titleRegex.matcher(title);
            if (matcher.matches()) {
                int suffix = Integer.parseInt(matcher.group(1));
                if (suffix > maxSuffix) {
                    maxSuffix = suffix;
                }
            }
        }
        String title = String.format("%s %d", DEFAULT_NOTE_TITLE, maxSuffix + 1);
        Main.log("genUniqueNoteTitle(); #=> [%s].", title);
        return title;
    }
    
    public void addNewNote(String title) {
        Note note = new Note();
        note.setTitle(title);
        addNote(note);
    }
    
    void addNote(Note newNote) {
        notes.add(newNote);
    }
    
    public void deleteNote(Note note) {
        if (!notes.remove(note)) {
            throw new IllegalArgumentException(String.format(
                    "Note [%s] does not exist.", note));
{'fraction_non_alphanumeric': 0.10079656862745098, 'fraction_numerical': 0.0035232843137254903, 'mean_word_length': 2.1224294595887137, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '5553275', 'n_tokens_mistral': 1913, 'n_tokens_neox': 1855, 'n_words': 489}