Skip to content

Commit

Permalink
Some refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Dec 10, 2015
1 parent 3495a52 commit 1e8ef7c
Showing 1 changed file with 21 additions and 38 deletions.
59 changes: 21 additions & 38 deletions src/main/java/net/sf/jabref/model/database/BibDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, BibEntry> 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<String, Integer> allKeys = new HashMap<>();
private String preamble;

// All file contents below the last entry in the file
private String epilog = "";
private final Map<String, BibtexString> bibtexStrings = new ConcurrentHashMap<>();

private final Set<DatabaseChangeListener> 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<String, Integer> allKeys = new HashMap<>();
private final Set<DatabaseChangeListener> changeListeners = new HashSet<>();

public BibDatabaseType getBibType() {
return BibDatabaseTypeDetection.inferType(entries.values());
Expand Down Expand Up @@ -102,11 +105,6 @@ public synchronized EntrySorter getSorter(Comparator<BibEntry> comp) {
return sorter;
}

/**
* Just temporary, for testing purposes....
*
* @return
*/
public Map<String, BibEntry> getEntryMap() {
return entries;
}
Expand Down Expand Up @@ -164,24 +162,21 @@ public synchronized BibEntry getEntryByKey(String key) {
}

public synchronized BibEntry[] getEntriesByKey(String key) {

ArrayList<BibEntry> result = new ArrayList<>();

for (BibEntry entry : entries.values()) {
if (key.equals(entry.getCiteKey())) {
result.add(entry);
}
}

return result.toArray(new BibEntry[result.size()]);
}

/**
* 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(
Expand Down Expand Up @@ -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<BibEntry> resolveForStrings(Collection<BibEntry> ent, boolean inPlace) {

if (ent == null) {
public List<BibEntry> resolveForStrings(Collection<BibEntry> entries, boolean inPlace) {
if (entries == null) {
throw new IllegalArgumentException("entries must not be null");
}

List<BibEntry> results = new ArrayList<>(ent.size());
List<BibEntry> results = new ArrayList<>(entries.size());

for (BibEntry entry : ent) {
for (BibEntry entry : entries) {
results.add(this.resolveForStrings(entry, inPlace));
}
return results;
Expand All @@ -358,15 +352,13 @@ public List<BibEntry> resolveForStrings(Collection<BibEntry> ent, boolean inPlac
* given BibtexEntries is modified.
*/
public BibEntry resolveForStrings(BibEntry entry, boolean inPlace) {

if (!inPlace) {
entry = (BibEntry) entry.clone();
}

for (Object field : entry.getFieldNames()) {
entry.setField(field.toString(), this.resolveForStrings(entry.getField(field.toString())));
}

return entry;
}

Expand All @@ -378,10 +370,8 @@ public BibEntry resolveForStrings(BibEntry entry, boolean inPlace) {
*/
private String resolveString(String label, HashSet<String> 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
Expand Down Expand Up @@ -417,7 +407,6 @@ private String resolveString(String label, HashSet<String> usedIds) {
}

private String resolveContent(String res, HashSet<String> usedIds) {

if (res.matches(".*#[^#]+#.*")) {
StringBuilder newRes = new StringBuilder();
int piv = 0;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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.
Expand All @@ -618,15 +605,13 @@ public static String getText(String toResolve, BibDatabase database) {
if ((toResolve != null) && (database != null)) {
return database.resolveForStrings(toResolve);
}

return toResolve;
}

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
Expand Down Expand Up @@ -664,13 +649,11 @@ public void setFollowCrossrefs(boolean followCrossrefs) {
}
};


public void setEpilog(String epilog) {
this.epilog = epilog;
}

public String getEpilog() {
return epilog;
}

}

0 comments on commit 1e8ef7c

Please sign in to comment.