Skip to content

Commit

Permalink
Makes sure that inserted terms are TermImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
Tpt committed Nov 20, 2019
1 parent 7c761c8 commit 1fd25c8
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 33 deletions.
Expand Up @@ -122,11 +122,15 @@ private static Map<String, MonolingualTextValue> constructTermMap(List<Monolingu
}
// We need to make sure the terms are of the right type, otherwise they will not
// be serialized correctly.
map.put(language, (term instanceof TermImpl) ? term : new TermImpl(term.getLanguageCode(), term.getText()));
map.put(language, toTerm(term));
}
return map;
}

private static MonolingualTextValue toTerm(MonolingualTextValue term) {
return (term instanceof TermImpl) ? term : new TermImpl(term.getLanguageCode(), term.getText());
}

private List<ItemIdValue> constructGrammaticalFeatures(List<String> grammaticalFeatures, String siteIri) {
List<ItemIdValue> output = new ArrayList<>(grammaticalFeatures.size());
for(String grammaticalFeature : grammaticalFeatures) {
Expand Down Expand Up @@ -195,7 +199,7 @@ public FormDocument withRevisionId(long newRevisionId) {
@Override
public FormDocument withRepresentation(MonolingualTextValue representation) {
Map<String, MonolingualTextValue> newRepresentations = new HashMap<>(representations);
newRepresentations.put(representation.getLanguageCode(), representation);
newRepresentations.put(representation.getLanguageCode(), toTerm(representation));
return new FormDocumentImpl(getEntityId(), newRepresentations, grammaticalFeatures, claims, revisionId);
}

Expand Down
Expand Up @@ -177,34 +177,25 @@ public ItemDocument withRevisionId(long newRevisionId) {

@Override
public ItemDocument withLabel(MonolingualTextValue newLabel) {
Map<String, MonolingualTextValue> newLabels = new HashMap<>(labels);
newLabels.put(newLabel.getLanguageCode(), newLabel);
return new ItemDocumentImpl(getItemId(),
newLabels, descriptions,
withTerm(labels, newLabel), descriptions,
aliases, claims,
sitelinks, revisionId);
}

@Override
public ItemDocument withDescription(MonolingualTextValue newDescription) {
Map<String, MonolingualTextValue> newDescriptions = new HashMap<>(descriptions);
newDescriptions.put(newDescription.getLanguageCode(), newDescription);
return new ItemDocumentImpl(getItemId(),
labels, newDescriptions,
labels, withTerm(descriptions, newDescription),
aliases, claims,
sitelinks, revisionId);
}

@Override
public ItemDocument withAliases(String language, List<MonolingualTextValue> aliases) {
Map<String, List<MonolingualTextValue>> newAliases = new HashMap<>(this.aliases);
for(MonolingualTextValue alias : aliases) {
Validate.isTrue(alias.getLanguageCode().equals(language));
}
newAliases.put(language, aliases);
return new ItemDocumentImpl(getItemId(),
labels, descriptions,
newAliases, claims,
withAliases(this.aliases, language, aliases), claims,
sitelinks, revisionId);
}

Expand Down
Expand Up @@ -115,6 +115,13 @@ protected static Map<String, MonolingualTextValue> constructTermMap(List<Monolin
return map;
}

protected static Map<String, MonolingualTextValue> withTerm(
Map<String, MonolingualTextValue> values, MonolingualTextValue value) {
Map<String, MonolingualTextValue> newValues = new HashMap<>(values);
newValues.put(value.getLanguageCode(), toTerm(value));
return newValues;
}

/**
* We need to make sure the terms are of the right type, otherwise they will not be serialized correctly.
*/
Expand Down
Expand Up @@ -168,11 +168,15 @@ private static Map<String, MonolingualTextValue> constructTermMap(List<Monolingu
}
// We need to make sure the terms are of the right type, otherwise they will not
// be serialized correctly.
map.put(language, (term instanceof TermImpl) ? term : new TermImpl(term.getLanguageCode(), term.getText()));
map.put(language, toTerm(term));
}
return map;
}

private static MonolingualTextValue toTerm(MonolingualTextValue term) {
return (term instanceof TermImpl) ? term : new TermImpl(term.getLanguageCode(), term.getText());
}

private static final Pattern CHILD_ID_PATTERN = Pattern.compile("^L\\d+-[FS]([1-9]\\d*)$");

private static int nextChildEntityId(List<? extends EntityDocument> childrenDocuments) {
Expand Down Expand Up @@ -289,7 +293,7 @@ public LexemeDocument withLanguage(ItemIdValue newLanguage) {
@Override
public LexemeDocument withLemma(MonolingualTextValue lemma) {
Map<String, MonolingualTextValue> newLemmas = new HashMap<>(lemmas);
newLemmas.put(lemma.getLanguageCode(), lemma);
newLemmas.put(lemma.getLanguageCode(), toTerm(lemma));
return new LexemeDocumentImpl(getEntityId(), lexicalCategory,
language, newLemmas, claims, forms, senses,
revisionId, nextFormId, nextSenseId);
Expand Down
Expand Up @@ -130,9 +130,7 @@ public MediaInfoDocument withRevisionId(long newRevisionId) {

@Override
public MediaInfoDocument withLabel(MonolingualTextValue newLabel) {
Map<String, MonolingualTextValue> newLabels = new HashMap<>(labels);
newLabels.put(newLabel.getLanguageCode(), newLabel);
return new MediaInfoDocumentImpl(getEntityId(), newLabels, claims, revisionId);
return new MediaInfoDocumentImpl(getEntityId(), withTerm(labels, newLabel), claims, revisionId);
}

@Override
Expand Down
Expand Up @@ -179,34 +179,25 @@ public PropertyDocument withRevisionId(long newRevisionId) {

@Override
public PropertyDocument withLabel(MonolingualTextValue newLabel) {
Map<String, MonolingualTextValue> newLabels = new HashMap<>(labels);
newLabels.put(newLabel.getLanguageCode(), newLabel);
return new PropertyDocumentImpl(getEntityId(),
newLabels, descriptions,
withTerm(labels, newLabel), descriptions,
aliases, claims,
datatype, revisionId);
}

@Override
public PropertyDocument withDescription(MonolingualTextValue newDescription) {
Map<String, MonolingualTextValue> newDescriptions = new HashMap<>(descriptions);
newDescriptions.put(newDescription.getLanguageCode(), newDescription);
return new PropertyDocumentImpl(getEntityId(),
labels, newDescriptions,
labels, withTerm(descriptions, newDescription),
aliases, claims,
datatype, revisionId);
}

@Override
public PropertyDocument withAliases(String language, List<MonolingualTextValue> aliases) {
Map<String, List<MonolingualTextValue>> newAliases = new HashMap<>(this.aliases);
for(MonolingualTextValue alias : aliases) {
Validate.isTrue(alias.getLanguageCode().equals(language));
}
newAliases.put(language, aliases);
return new PropertyDocumentImpl(getEntityId(),
labels, descriptions,
newAliases, claims,
withAliases(this.aliases, language, aliases), claims,
datatype, revisionId);
}

Expand Down
Expand Up @@ -112,11 +112,15 @@ private static Map<String, MonolingualTextValue> constructTermMap(List<Monolingu
}
// We need to make sure the terms are of the right type, otherwise they will not
// be serialized correctly.
map.put(language, (term instanceof TermImpl) ? term : new TermImpl(term.getLanguageCode(), term.getText()));
map.put(language, toTerm(term));
}
return map;
}

private static MonolingualTextValue toTerm(MonolingualTextValue term) {
return (term instanceof TermImpl) ? term : new TermImpl(term.getLanguageCode(), term.getText());
}

@JsonIgnore
@Override
public SenseIdValue getEntityId() {
Expand Down Expand Up @@ -160,7 +164,7 @@ public SenseDocument withRevisionId(long newRevisionId) {
@Override
public SenseDocument withGloss(MonolingualTextValue gloss) {
Map<String, MonolingualTextValue> newGlosses = new HashMap<>(glosses);
newGlosses.put(gloss.getLanguageCode(), gloss);
newGlosses.put(gloss.getLanguageCode(), toTerm(gloss));
return new SenseDocumentImpl(getEntityId(), newGlosses, claims, revisionId);
}

Expand Down
Expand Up @@ -183,6 +183,20 @@ private static Map<String, List<MonolingualTextValue>> constructTermListMap(List
return map;
}

protected static Map<String, List<MonolingualTextValue>> withAliases(
Map<String, List<MonolingualTextValue>> values, String language, List<MonolingualTextValue> aliases) {
Map<String, List<MonolingualTextValue>> newValues = new HashMap<>(values);
List<MonolingualTextValue> l = newValues.computeIfAbsent(language, (s) -> new ArrayList<>());
l.clear();
for(MonolingualTextValue term : aliases) {
if(!term.getLanguageCode().equals(language)) {
throw new IllegalArgumentException("The alias " + term + " does not have the same language as its group " + language);
}
l.add(toTerm(term));
}
return newValues;
}

/**
* We need to make sure the terms are of the right type, otherwise they will not be serialized correctly.
*/
Expand Down

0 comments on commit 1fd25c8

Please sign in to comment.