package net.sf.jabref.logic;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import net.sf.jabref.model.EntryTypes;
import net.sf.jabref.model.database.BibDatabaseMode;
import net.sf.jabref.model.entry.CustomEntryType;
import net.sf.jabref.model.entry.EntryType;
import net.sf.jabref.preferences.JabRefPreferences;

public class CustomEntryTypesManager {

    public static final List<EntryType> ALL = new ArrayList<>();
    /**
     * Load all custom entry types from preferences. This method is
     * called from JabRef when the program starts.
     */
    public static void loadCustomEntryTypes(JabRefPreferences prefs) {
        int number = 0;
        Optional<CustomEntryType> type;
        while ((type = prefs.getCustomEntryType(number)).isPresent()) {
            EntryTypes.addOrModifyCustomEntryType(type.get(), prefs.getDefaultBibDatabaseMode());
            ALL.add(type.get());
            number++;
        }
    }

    /**
     * Iterate through all entry types, and store those that are
     * custom defined to preferences. This method is called from
     * JabRefFrame when the program closes.
     */
    public static void saveCustomEntryTypes(JabRefPreferences prefs) {
        Iterator<EntryType> iterator = EntryTypes.getAllValues(BibDatabaseMode.BIBTEX).iterator();
        int number = 0;

        while (iterator.hasNext()) {
            EntryType entryType = iterator.next();
            if (entryType instanceof CustomEntryType) {
                // Store this entry type.
                prefs.storeCustomEntryType((CustomEntryType) entryType, number);
                number++;
            }
        }
        // Then, if there are more 'old' custom types defined, remove these
        // from preferences. This is necessary if the number of custom types
        // has decreased.
        prefs.purgeCustomEntryTypes(number);
    }

}

@@ -40,25 +40,23 @@
 va.util.
-ArrayList
+HashMap
 ;%0Aimport
@@ -66,24 +66,20 @@
 va.util.
-Iterator
+List
 ;%0Aimport
@@ -81,36 +81,35 @@
 mport java.util.
-List
+Map
 ;%0Aimport java.ut
@@ -111,24 +111,33 @@
 va.util.
-Optional
+stream.Collectors
 ;%0A%0Aimpor
@@ -277,52 +277,8 @@
 pe;%0A
-import net.sf.jabref.model.entry.EntryType;%0A
 impo
@@ -394,43 +394,90 @@
 nal 
-List%3CEntryType%3E ALL = new ArrayList
+Map%3CBibDatabaseMode, List%3CCustomEntryType%3E%3E CUSTOM_TYPES_BY_MODE_MAP = new HashMap
 %3C%3E()
@@ -696,214 +696,410 @@
     
-int number = 0;%0A        Optional%3CCustomEntryType%3E type;%0A        while ((type = prefs.getCustomEntryType(number)).isPresent()) %7B%0A            EntryTypes.addOrModifyCustomEntryType(type.get(), prefs.getDefault
+List%3CCustomEntryType%3E customBibtexTypes = prefs.loadCustomEntryTypes(BibDatabaseMode.BIBTEX);%0A        for(CustomEntryType type : customBibtexTypes) %7B%0A            EntryTypes.addOrModifyCustomEntryType(type, BibDatabaseMode.BIBTEX);%0A        %7D%0A        CUSTOM_TYPES_BY_MODE_MAP.put(BibDatabaseMode.BIBTEX, customBibtexTypes);%0A%0A        List%3CCustomEntryType%3E customBiblatexTypes = prefs.loadCustomEntryTypes(
 BibD
@@ -1109,18 +1109,25 @@
 baseMode
-()
+.BIBLATEX
 );%0A     
@@ -1133,64 +1133,234 @@
     
-    ALL.add(type.get());%0A            number++;%0A        %7D
+for(CustomEntryType type :customBiblatexTypes) %7B%0A            EntryTypes.addOrModifyCustomEntryType(type, BibDatabaseMode.BIBLATEX);%0A        %7D%0A        CUSTOM_TYPES_BY_MODE_MAP.put(BibDatabaseMode.BIBLATEX, customBiblatexTypes);
 %0A   
@@ -1636,337 +1636,239 @@
     
-Iterator%3CEntryType%3E iterator = EntryTypes.getAllValues(BibDatabaseMode.BIBTEX).iterator();%0A        int number = 0;%0A%0A        while (iterator.hasNext()) %7B%0A            EntryType entryType = iterator.next(
+saveCustomEntryTypes(prefs, BibDatabaseMode.BIBTEX);%0A        saveCustomEntryTypes(prefs, BibDatabaseMode.BIBLATEX
 );%0A
+%0A
     
+%7D%0A%0A
     
-    if (entryType instanceof CustomEntryType) %7B%0A                // Store this entry type.%0A                prefs.store
+private static void saveCustomEntryTypes(JabRefPreferences prefs, BibDatabaseMode mode) %7B%0A        List%3C
 Cust
@@ -1882,275 +1882,229 @@
 Type
-((C
+%3E c
 ustom
-EntryType) entryType, number);%0A                number++;%0A            %7D%0A        %7D%0A        // Then, if there are more 'old' custom types defined, remove these%0A        // from preferences. This is necessary if the number of custom types%0A        // has decreased.
+BiblatexTypes = EntryTypes.getAllValues(mode).stream()%0A                .filter(type -%3E type instanceof CustomEntryType)%0A                .map(entryType -%3E (CustomEntryType) entryType).collect(Collectors.toList());%0A
 %0A   
@@ -2118,12 +2118,12 @@
 efs.
-purg
+stor
 eCus
@@ -2140,14 +2140,33 @@
 pes(
-number
+customBiblatexTypes, mode
 );%0A 

load and store databasemode specific lists of custom types