Skip to content

Commit

Permalink
Fix lookup entry types in enums (#5209)
Browse files Browse the repository at this point in the history
* Fix lookup entry types in enums

Make master work again!!!

* fix checkstyle

* fix checkstyle and fix type comparison to lower case

* simplify remove generics
  • Loading branch information
Siedlerchr authored and stefan-kolb committed Aug 24, 2019
1 parent 4477a13 commit b00fc22
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 15 deletions.
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"));
}
}

0 comments on commit b00fc22

Please sign in to comment.