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 package of PreviewLayout #5702

Merged
merged 21 commits into from
Mar 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions docs/adr/0008-use-public-final-instead-of-getters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Use `public final` instead of getters to offer access to immutable variables

## Context and Problem Statement

When making immutable data accessible in a java class, should it be using getters or by non-modifiable fields?

## Considered Options

* Offer public static field
* Offer getters

## Decision Outcome

Chosen option: "Offer public static field", because getters used to be a convention which was even more manifested due to libraries depending on the existence on getters/setters. In the case of immutable variables, adding public getters is just useless since one is not hiding anything.

### Positive Consequences

* Shorter code

### Negative Consequences

* new comers could get confused, because getters/setters are still teached
36 changes: 14 additions & 22 deletions src/main/java/org/jabref/logic/bst/VM.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ private VM(CommonTree tree) {
if (context == null) {
throw new VMException("Call.type$ can only be called from within a context (ITERATE or REVERSE).");
}
VM.this.execute(context.getBibtexEntry().getType().getName(), context);
VM.this.execute(context.entry.getType().getName(), context);
});

buildInFunctions.put("change.case$", new ChangeCaseFunction(this));
Expand Down Expand Up @@ -303,7 +303,7 @@ private VM(CommonTree tree) {
if (context == null) {
throw new VMException("Must have an entry to cite$");
}
stack.push(context.getBibtexEntry().getCiteKeyOptional().orElse(null));
stack.push(context.entry.getCiteKeyOptional().orElse(null));
});

/*
Expand Down Expand Up @@ -581,7 +581,7 @@ private VM(CommonTree tree) {
throw new VMException("type$ need a context.");
}

stack.push(context.getBibtexEntry().getType().getName());
stack.push(context.entry.getType().getName());
});

/*
Expand Down Expand Up @@ -925,16 +925,16 @@ public String run(Collection<BibEntry> bibEntries, BibDatabase bibDatabase) {
*/
private void read(BibDatabase bibDatabase) {
for (BstEntry e : entries) {
for (Map.Entry<String, String> mEntry : e.getFields().entrySet()) {
for (Map.Entry<String, String> mEntry : e.fields.entrySet()) {
Field field = FieldFactory.parseField(mEntry.getKey());
String fieldValue = e.getBibtexEntry().getResolvedFieldOrAlias(field, bibDatabase).orElse(null);
String fieldValue = e.entry.getResolvedFieldOrAlias(field, bibDatabase).orElse(null);
mEntry.setValue(fieldValue);
}
}

for (BstEntry e : entries) {
if (!e.getFields().containsKey(StandardField.CROSSREF.getName())) {
e.getFields().put(StandardField.CROSSREF.getName(), null);
if (!e.fields.containsKey(StandardField.CROSSREF.getName())) {
e.fields.put(StandardField.CROSSREF.getName(), null);
}
}
}
Expand Down Expand Up @@ -981,7 +981,7 @@ private void entry(Tree child) {
String name = t.getChild(i).getText();

for (BstEntry entry : entries) {
entry.getFields().put(name, null);
entry.fields.put(name, null);
}
}

Expand Down Expand Up @@ -1106,8 +1106,8 @@ private void execute(String name, BstEntry context) {

if (context != null) {

if (context.getFields().containsKey(name)) {
stack.push(context.getFields().get(name));
if (context.fields.containsKey(name)) {
stack.push(context.fields.get(name));
return;
}
if (context.localStrings.containsKey(name)) {
Expand Down Expand Up @@ -1174,25 +1174,17 @@ private void strings(Tree child) {

public static class BstEntry {

private final BibEntry entry;
public final BibEntry entry;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then this triggers a follow-up question (@LinusDietz): is it ok to make variables with mutable types (like Map<>) public via public final ? Because then outside code can modify the content of a class to their liking (which I guess is against some of the principles of object-oriented programming).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is effective Java #39. "Make defensive copies when needed".

You must program defensively, with the assumption that clients of your class will do their best to destroy its invariants.

However, did not see that being done much, so I am curios to the opinion of @LinusDietz


private final Map<String, String> localStrings = new HashMap<>();
public final Map<String, String> localStrings = new HashMap<>();

private final Map<String, String> fields = new HashMap<>();
public final Map<String, String> fields = new HashMap<>();

private final Map<String, Integer> localIntegers = new HashMap<>();
public final Map<String, Integer> localIntegers = new HashMap<>();

public BstEntry(BibEntry e) {
this.entry = e;
}

public Map<String, String> getFields() {
return fields;
}

public BibEntry getBibtexEntry() {
return entry;
}
}

private void push(Integer integer) {
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/jabref/logic/bst/TestVM.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void testVMSimple() throws RecognitionException, IOException {
assertEquals(2, vm.getStrings().size());
assertEquals(7, vm.getIntegers().size());
assertEquals(1, vm.getEntries().size());
assertEquals(5, vm.getEntries().get(0).getFields().size());
assertEquals(5, vm.getEntries().get(0).fields.size());
assertEquals(38, vm.getFunctions().size());
}

Expand Down Expand Up @@ -366,10 +366,10 @@ public void testSort() throws RecognitionException, IOException {
vm.run(v);

List<BstEntry> v2 = vm.getEntries();
assertEquals(Optional.of("a"), v2.get(0).getBibtexEntry().getCiteKeyOptional());
assertEquals(Optional.of("b"), v2.get(1).getBibtexEntry().getCiteKeyOptional());
assertEquals(Optional.of("c"), v2.get(2).getBibtexEntry().getCiteKeyOptional());
assertEquals(Optional.of("d"), v2.get(3).getBibtexEntry().getCiteKeyOptional());
assertEquals(Optional.of("a"), v2.get(0).entry.getCiteKeyOptional());
assertEquals(Optional.of("b"), v2.get(1).entry.getCiteKeyOptional());
assertEquals(Optional.of("c"), v2.get(2).entry.getCiteKeyOptional());
assertEquals(Optional.of("d"), v2.get(3).entry.getCiteKeyOptional());
}

@Test
Expand Down