Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lookup entry types in enums #5209

Merged
merged 4 commits into from
Aug 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/org/jabref/model/entry/types/EntryType.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ public interface EntryType {
String getName();

String getDisplayName();

}
21 changes: 15 additions & 6 deletions src/main/java/org/jabref/model/entry/types/EntryTypeFactory.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.jabref.model.entry.types;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import org.jabref.model.entry.BibEntryType;
import org.jabref.model.util.OptionalUtil;

public class EntryTypeFactory {

Expand All @@ -22,10 +27,10 @@ public static boolean isEqualNameAndFieldBased(BibEntryType type1, BibEntryType
} else if ((type1 == null) || (type2 == null)) {
return false;
} else {
return type1.getType().equals(type2.getType())
&& type1.getRequiredFields().equals(type2.getRequiredFields())
&& type1.getOptionalFields().equals(type2.getOptionalFields())
&& type1.getSecondaryOptionalFields().equals(type2.getSecondaryOptionalFields());
return Objects.equals(type1.getType(), type2.getType())
&& Objects.equals(type1.getRequiredFields(), type2.getRequiredFields())
&& Objects.equals(type1.getOptionalFields(), type2.getOptionalFields())
&& Objects.equals(type1.getSecondaryOptionalFields(), type2.getSecondaryOptionalFields());
}
}

Expand All @@ -42,6 +47,10 @@ private static boolean isBiblatex(EntryType type) {
}

public static EntryType parse(String typeName) {
return OptionalUtil.orElse(StandardEntryType.fromName(typeName), new UnknownEntryType(typeName));

List<EntryType> types = new ArrayList<>(Arrays.<EntryType> asList(StandardEntryType.values()));
types.addAll(Arrays.<EntryType> asList(IEEETranEntryType.values()));

return types.stream().filter(type -> type.getName().equals(typeName.toLowerCase(Locale.ENGLISH))).findFirst().orElse(new UnknownEntryType(typeName));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.jabref.model.entry.types;

import java.util.Arrays;
import java.util.Locale;
import java.util.Optional;

public enum StandardEntryType implements EntryType {
// BibTeX
Expand Down Expand Up @@ -44,12 +42,6 @@ public enum StandardEntryType implements EntryType {
this.displayName = displayName;
}

public static Optional<StandardEntryType> fromName(String name) {
return Arrays.stream(StandardEntryType.values())
.filter(field -> field.getName().equalsIgnoreCase(name))
.findAny();
}

@Override
public String getName() {
return displayName.toLowerCase(Locale.ENGLISH);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
if ((o == null) || (getClass() != o.getClass())) {
return false;
}
UnknownEntryType that = (UnknownEntryType) o;
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/jabref/model/entry/EntryTypeFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jabref.model.entry;

import org.jabref.model.entry.types.EntryType;
import org.jabref.model.entry.types.EntryTypeFactory;
import org.jabref.model.entry.types.IEEETranEntryType;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class EntryTypeFactoryTest {

@Test
public void testParseEntryTypePatent() {
EntryType patent = IEEETranEntryType.Patent;
assertEquals(patent, EntryTypeFactory.parse("patent"));
}
}