public static BibtexDatabase createDatabase(Collection<BibtexEntry> bibentries) { purgeEmptyEntries(bibentries); BibtexDatabase database = new BibtexDatabase(); for (Iterator<BibtexEntry> i = bibentries.iterator(); i.hasNext();) { BibtexEntry entry = i.next(); try { entry.setId(Util.createNeutralId()); database.insertEntry(entry); } catch (KeyCollisionException ex) { System.err.println("KeyCollisionException [ addBibEntries(...) ]"); } } return database; }
/** * Adds the entries to the database, possibly checking for duplicates first. * @param filename If non-null, a message is printed to the status line describing * how many entries were imported, and from which file. If null, the message will not * be printed. * @param intoNew Determines if the entries will be put in a new database or in the current * one. */ public int addBibEntries(List<BibtexEntry> bibentries, String filename, boolean intoNew) { if (bibentries == null || bibentries.size() == 0) { // No entries found. We need a message for this. JOptionPane.showMessageDialog(JabRefFrame.this, Globals.lang("No entries found. Please make sure you are " +"using the correct import filter."), Globals.lang("Import failed"), JOptionPane.ERROR_MESSAGE); return 0; } int addedEntries = 0; // Set owner and timestamp fields: Util.setAutomaticFields(bibentries, Globals.prefs.getBoolean("overwriteOwner"), Globals.prefs.getBoolean("overwriteTimeStamp"), Globals.prefs.getBoolean("markImportedEntries")); if (intoNew || (tabbedPane.getTabCount() == 0)) { // Import into new database. BibtexDatabase database = new BibtexDatabase(); for (BibtexEntry entry : bibentries){ try { entry.setId(Util.createNeutralId()); database.insertEntry(entry); } catch (KeyCollisionException ex) { //ignore System.err.println("KeyCollisionException [ addBibEntries(...) ]"); } } // Metadata are only put in bibtex files, so we will not find it // in imported files. We therefore pass in an empty MetaData: BasePanel bp = new BasePanel(JabRefFrame.this, database, null, new MetaData(), Globals.prefs.get("defaultEncoding")); /* if (prefs.getBoolean("autoComplete")) { db.setCompleters(autoCompleters); } */ addedEntries = database.getEntryCount(); tabbedPane.add(GUIGlobals.untitledTitle, bp); bp.markBaseChanged(); tabbedPane.setSelectedComponent(bp); if (filename != null) output(Globals.lang("Imported database") + " '" + filename + "' " + Globals.lang("with") + " " + database.getEntryCount() + " " + Globals.lang("entries into new database") + "."); } else { // Import into current database. boolean checkForDuplicates = true; BasePanel basePanel = basePanel(); BibtexDatabase database = basePanel.database; int oldCount = database.getEntryCount(); NamedCompound ce = new NamedCompound(Globals.lang("Import entries")); mainLoop: for (BibtexEntry entry : bibentries){ boolean dupli = false; // Check for duplicates among the current entries: if (checkForDuplicates) { loop: for (Iterator<String> i2=database.getKeySet().iterator(); i2.hasNext();) { BibtexEntry existingEntry = database.getEntryById(i2.next()); if (DuplicateCheck.isDuplicate(entry, existingEntry )) { DuplicateResolverDialog drd = new DuplicateResolverDialog (JabRefFrame.this, existingEntry, entry, DuplicateResolverDialog.IMPORT_CHECK); drd.setVisible(true); int res = drd.getSelected(); if (res == DuplicateResolverDialog.KEEP_LOWER) { dupli = true; } else if (res == DuplicateResolverDialog.KEEP_UPPER) { database.removeEntry(existingEntry.getId()); ce.addEdit(new UndoableRemoveEntry (database, existingEntry, basePanel)); } else if (res == DuplicateResolverDialog.BREAK) { break mainLoop; } break loop; } } } if (!dupli) { try { entry.setId(Util.createNeutralId()); database.insertEntry(entry); ce.addEdit(new UndoableInsertEntry (database, entry, basePanel)); addedEntries++; } catch (KeyCollisionException ex) { //ignore System.err.println("KeyCollisionException [ addBibEntries(...) ]"); } } } if (addedEntries > 0) { ce.end(); basePanel.undoManager.addEdit(ce); basePanel.markBaseChanged(); if (filename != null) output(Globals.lang("Imported database") + " '" + filename + "' " + Globals.lang("with") + " " + (database.getEntryCount() - oldCount) + " " + Globals.lang("entries into new database") + "."); } } return addedEntries; }
Clone fragments detected by clone detection tool
File path: /jabref-2.10/src/java/net/sf/jabref/imports/ImportFormatReader.java File path: /jabref-2.10/src/java/net/sf/jabref/JabRefFrame.java
Method name: BibtexDatabase createDatabase(Collection) Method name: int addBibEntries(List, String, boolean)
Number of AST nodes: 4 Number of AST nodes: 3
1
public static BibtexDatabase createDatabase(Collection<BibtexEntry> bibentries) {
1
/**
2
    purgeEmptyEntries(bibentries);
2
     * Adds the entries to the database, possibly checking for duplicates first.
3
3
     * @param filename If non-null, a message is printed to the status line describing
4
    BibtexDatabase database = new BibtexDatabase();
4
     * how many entries were imported, and from which file. If null, the message will not
5
5
     * be printed.
6
    for (Iterator<BibtexEntry> i = bibentries.iterator(); i.hasNext();) {
6
     * @param intoNew Determines if the entries will be put in a new database or in the current
7
      BibtexEntry entry = i.next();
7
     * one.
8
8
     */
9
      try {
9
  public int addBibEntries(List<BibtexEntry> bibentries, String filename,
10
        entry.setId(Util.createNeutralId());
10
                           boolean intoNew) {
11
        database.insertEntry(entry);
11
          if (bibentries == null || bibentries.size() == 0) {
12
      } catch (KeyCollisionException ex) {
12
13
        System.err.println("KeyCollisionException [ addBibEntries(...) ]");
13
      // No entries found. We need a message for this.
14
      }
14
      JOptionPane.showMessageDialog(JabRefFrame.this, Globals.lang("No entries found. Please make sure you are "
15
    }
15
                                                      +"using the correct import filter."), Globals.lang("Import failed"),
16
16
                                    JOptionPane.ERROR_MESSAGE);
17
    return database;
17
      return 0;
18
  }
18
    }
19
20
      int addedEntries = 0;
21
22
    // Set owner and timestamp fields:
23
    Util.setAutomaticFields(bibentries, Globals.prefs.getBoolean("overwriteOwner"),
24
            Globals.prefs.getBoolean("overwriteTimeStamp"), Globals.prefs.getBoolean("markImportedEntries"));
25
26
    if (intoNew || (tabbedPane.getTabCount() == 0)) {
27
      // Import into new database.
28
      BibtexDatabase database = new BibtexDatabase();
29
      for (BibtexEntry entry : bibentries){
30
        try {
31
          entry.setId(Util.createNeutralId());
32
          database.insertEntry(entry);
33
        }
34
        catch (KeyCollisionException ex) {
35
          //ignore
36
          System.err.println("KeyCollisionException [ addBibEntries(...) ]");
37
        }
38
      }
39
      // Metadata are only put in bibtex files, so we will not find it
40
      // in imported files. We therefore pass in an empty MetaData:
41
      BasePanel bp = new BasePanel(JabRefFrame.this, database, null, new MetaData(), Globals.prefs.get("defaultEncoding"));
42
      /*
43
            if (prefs.getBoolean("autoComplete")) {
44
            db.setCompleters(autoCompleters);
45
            }
46
       */
47
      addedEntries = database.getEntryCount();
48
      tabbedPane.add(GUIGlobals.untitledTitle, bp);
49
      bp.markBaseChanged();
50
      tabbedPane.setSelectedComponent(bp);
51
      if (filename != null)
52
          output(Globals.lang("Imported database") + " '" + filename + "' " +
53
                 Globals.lang("with") + " " +
54
                 database.getEntryCount() + " " +
55
                 Globals.lang("entries into new database") + ".");
56
    }
57
    else {
58
      // Import into current database.
59
      boolean checkForDuplicates = true;
60
      BasePanel basePanel = basePanel();
61
      BibtexDatabase database = basePanel.database;
62
      int oldCount = database.getEntryCount();
63
      NamedCompound ce = new NamedCompound(Globals.lang("Import entries"));
64
65
      mainLoop: 
66
      for (BibtexEntry entry : bibentries){
67
        boolean dupli = false;
68
        // Check for duplicates among the current entries:
69
        if (checkForDuplicates) {
70
            loop: for (Iterator<String> i2=database.getKeySet().iterator();
71
                       i2.hasNext();) {
72
                BibtexEntry existingEntry = database.getEntryById(i2.next());
73
                if (DuplicateCheck.isDuplicate(entry, existingEntry
74
                )) {
75
                    DuplicateResolverDialog drd = new DuplicateResolverDialog
76
                        (JabRefFrame.this, existingEntry, entry, DuplicateResolverDialog.IMPORT_CHECK);
77
                    drd.setVisible(true);
78
                    int res = drd.getSelected();
79
                    if (res == DuplicateResolverDialog.KEEP_LOWER)   {
80
                        dupli = true;
81
                    }
82
                    else if (res == DuplicateResolverDialog.KEEP_UPPER) {
83
                        database.removeEntry(existingEntry.getId());
84
                        ce.addEdit(new UndoableRemoveEntry
85
                                   (database, existingEntry, basePanel));
86
                    } else if (res == DuplicateResolverDialog.BREAK) {
87
                        break mainLoop;
88
                    }
89
                    break loop;
90
                }
91
            }
92
        }
93
94
        if (!dupli) {
95
            try {
96
                entry.setId(Util.createNeutralId());
97
                database.insertEntry(entry);
98
                ce.addEdit(new UndoableInsertEntry
99
                           (database, entry, basePanel));
100
                addedEntries++;
101
            }
102
            catch (KeyCollisionException ex) {
103
                //ignore
104
                System.err.println("KeyCollisionException [ addBibEntries(...) ]");
105
            }
106
        }
107
      }
108
        if (addedEntries > 0) {
109
            ce.end();
110
            basePanel.undoManager.addEdit(ce);
111
            basePanel.markBaseChanged();
112
            if (filename != null)
113
                output(Globals.lang("Imported database") + " '" + filename + "' " +
114
                     Globals.lang("with") + " " +
115
                     (database.getEntryCount() - oldCount) + " " +
116
                     Globals.lang("entries into new database") + ".");
117
        }
118
119
    }
120
121
    return addedEntries;
122
  }
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements5
    Number of unmapped statements in the first code fragment0
    Number of unmapped statements in the second code fragment0
    Time elapsed for statement mapping (ms)0.0
    Similarity Score0.857
    Clone typeType 2
    Mapped Statements
    ID Statement ID Statement
    3
    for (Iterator<BibtexEntry> i = bibentries.iterator(); i.hasNext(); )
    3
    for (Iterator<BibtexEntry> i = bibentries.iterator(); i.hasNext(); )
    8
    for (BibtexEntry entry : bibentries)
    Differences
    Expression1Expression2Difference
    java.util.Collectionjava.util.ListVARIABLE_TYPE_MISMATCH
    Preondition Violations
    Type java.util.Collection<net.sf.jabref.BibtexEntry> of variable bibentries does not match with type java.util.List<net.sf.jabref.BibtexEntry> of variable bibentries
    • Make classes java.util.Collection and java.util.List extend a common superclass
    8
    for (BibtexEntry entry : bibentries)
    4
    BibtexEntry entry = i.next();
                                                                
    5
    try
    9
    try
    6
    entry.setId(Util.createNeutralId());
    10
    entry.setId(Util.createNeutralId());
    7
    database.insertEntry(entry);
    11
    database.insertEntry(entry);
    Precondition Violations (1)
    Row Violation
    1Type java.util.Collection<net.sf.jabref.BibtexEntry> of variable bibentries does not match with type java.util.List<net.sf.jabref.BibtexEntry> of variable bibentries