From 1e8ef7c39cc40e801ef6fe8ec4a996cbd827c016 Mon Sep 17 00:00:00 2001 From: Stefan Kolb Date: Thu, 10 Dec 2015 17:53:16 +0100 Subject: [PATCH] Some refactoring --- .../sf/jabref/model/database/BibDatabase.java | 59 +++++++------------ 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/src/main/java/net/sf/jabref/model/database/BibDatabase.java b/src/main/java/net/sf/jabref/model/database/BibDatabase.java index 4cdfea919f7..cc3c5ae8a63 100644 --- a/src/main/java/net/sf/jabref/model/database/BibDatabase.java +++ b/src/main/java/net/sf/jabref/model/database/BibDatabase.java @@ -49,29 +49,32 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +/** + * A bibliograhpy database. + */ public class BibDatabase { private static final Log LOGGER = LogFactory.getLog(BibDatabase.class); + /** + * State attributes + */ private final Map entries = new ConcurrentHashMap<>(); - + // use a map instead of a set since i need to know how many of each key is in there + private final HashMap allKeys = new HashMap<>(); private String preamble; - + // All file contents below the last entry in the file + private String epilog = ""; private final Map bibtexStrings = new ConcurrentHashMap<>(); - private final Set changeListeners = new HashSet<>(); - - private boolean followCrossrefs = true; - /** - * All file contents below the last entry in the file + * Configuration */ - private String epilog = ""; + private boolean followCrossrefs = true; /** - * use a map instead of a set since i need to know how many of each key is - * inthere + * Behavior */ - private final HashMap allKeys = new HashMap<>(); + private final Set changeListeners = new HashSet<>(); public BibDatabaseType getBibType() { return BibDatabaseTypeDetection.inferType(entries.values()); @@ -102,11 +105,6 @@ public synchronized EntrySorter getSorter(Comparator comp) { return sorter; } - /** - * Just temporary, for testing purposes.... - * - * @return - */ public Map getEntryMap() { return entries; } @@ -164,7 +162,6 @@ public synchronized BibEntry getEntryByKey(String key) { } public synchronized BibEntry[] getEntriesByKey(String key) { - ArrayList result = new ArrayList<>(); for (BibEntry entry : entries.values()) { @@ -172,7 +169,6 @@ public synchronized BibEntry[] getEntriesByKey(String key) { result.add(entry); } } - return result.toArray(new BibEntry[result.size()]); } @@ -180,8 +176,7 @@ public synchronized BibEntry[] getEntriesByKey(String key) { * Inserts the entry, given that its ID is not already in use. * use Util.createId(...) to make up a unique ID for an entry. */ - public synchronized boolean insertEntry(BibEntry entry) - throws KeyCollisionException { + public synchronized boolean insertEntry(BibEntry entry) throws KeyCollisionException { String id = entry.getId(); if (getEntryById(id) != null) { throw new KeyCollisionException( @@ -324,21 +319,20 @@ public String resolveForStrings(String content) { * Take the given collection of BibtexEntry and resolve any string * references. * - * @param ent A collection of BibtexEntries in which all strings of the form + * @param entries A collection of BibtexEntries in which all strings of the form * #xxx# will be resolved against the hash map of string * references stored in the databasee. * @param inPlace If inPlace is true then the given BibtexEntries will be modified, if false then copies of the BibtexEntries are made before resolving the strings. * @return a list of bibtexentries, with all strings resolved. It is dependent on the value of inPlace whether copies are made or the given BibtexEntries are modified. */ - public List resolveForStrings(Collection ent, boolean inPlace) { - - if (ent == null) { + public List resolveForStrings(Collection entries, boolean inPlace) { + if (entries == null) { throw new IllegalArgumentException("entries must not be null"); } - List results = new ArrayList<>(ent.size()); + List results = new ArrayList<>(entries.size()); - for (BibEntry entry : ent) { + for (BibEntry entry : entries) { results.add(this.resolveForStrings(entry, inPlace)); } return results; @@ -358,7 +352,6 @@ public List resolveForStrings(Collection ent, boolean inPlac * given BibtexEntries is modified. */ public BibEntry resolveForStrings(BibEntry entry, boolean inPlace) { - if (!inPlace) { entry = (BibEntry) entry.clone(); } @@ -366,7 +359,6 @@ public BibEntry resolveForStrings(BibEntry entry, boolean inPlace) { for (Object field : entry.getFieldNames()) { entry.setField(field.toString(), this.resolveForStrings(entry.getField(field.toString()))); } - return entry; } @@ -378,10 +370,8 @@ public BibEntry resolveForStrings(BibEntry entry, boolean inPlace) { */ private String resolveString(String label, HashSet usedIds) { for (BibtexString string : bibtexStrings.values()) { - //Util.pr(label+" : "+string.getName()); if (string.getName().toLowerCase().equals(label.toLowerCase())) { - // First check if this string label has been resolved // earlier in this recursion. If so, we have a // circular reference, and have to stop to avoid @@ -417,7 +407,6 @@ private String resolveString(String label, HashSet usedIds) { } private String resolveContent(String res, HashSet usedIds) { - if (res.matches(".*#[^#]+#.*")) { StringBuilder newRes = new StringBuilder(); int piv = 0; @@ -579,7 +568,6 @@ public void removeDatabaseChangeListener(DatabaseChangeListener l) { * @return The resolved field value or null if not found. */ public static String getResolvedField(String field, BibEntry bibtex, BibDatabase database) { - if (field.equals("bibtextype")) { return bibtex.getType().getName(); } @@ -607,8 +595,7 @@ public static String getResolvedField(String field, BibEntry bibtex, BibDatabase } /** - * Returns a text with references resolved according to an optionally given - * database. + * Returns a text with references resolved according to an optionally given database. * * @param toResolve maybenull The text to resolve. * @param database maybenull The database to use for resolving the text. @@ -618,7 +605,6 @@ public static String getText(String toResolve, BibDatabase database) { if ((toResolve != null) && (database != null)) { return database.resolveForStrings(toResolve); } - return toResolve; } @@ -626,7 +612,6 @@ public void setFollowCrossrefs(boolean followCrossrefs) { this.followCrossrefs = followCrossrefs; } - /* * Entries are stored in a HashMap with the ID as key. What happens if * someone changes a BibtexEntry's ID after it has been added to this @@ -664,7 +649,6 @@ public void setFollowCrossrefs(boolean followCrossrefs) { } }; - public void setEpilog(String epilog) { this.epilog = epilog; } @@ -672,5 +656,4 @@ public void setEpilog(String epilog) { public String getEpilog() { return epilog; } - }