Skip to content

Commit

Permalink
Merge pull request #451 from Wikidata/small-cleanups
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
wetneb committed Oct 21, 2019
2 parents eb50180 + 74ff61e commit e05fc92
Show file tree
Hide file tree
Showing 105 changed files with 482 additions and 721 deletions.
Expand Up @@ -198,14 +198,8 @@ public T withAlias(String text, String languageCode) {
* @return builder object to continue construction
*/
public T withStatement(Statement statement) {
PropertyIdValue pid = statement.getMainSnak()
.getPropertyId();
ArrayList<Statement> pidStatements = this.statements.get(pid);
if (pidStatements == null) {
pidStatements = new ArrayList<Statement>();
this.statements.put(pid, pidStatements);
}

PropertyIdValue pid = statement.getMainSnak().getPropertyId();
ArrayList<Statement> pidStatements = this.statements.computeIfAbsent(pid, k -> new ArrayList<>());
pidStatements.add(statement);
return getThis();
}
Expand Down
Expand Up @@ -22,6 +22,8 @@

import org.wikidata.wdtk.datamodel.interfaces.*;

import java.util.Objects;

/**
* Static class for checking the equality of arbitrary data objects using only
* their interfaces. This can be used to implement the equals() method of
Expand Down Expand Up @@ -226,15 +228,11 @@ public static boolean equalsQuantityValue(QuantityValue o1, Object o2) {
}
QuantityValue other = (QuantityValue) o2;
return o1.getNumericValue().equals(other.getNumericValue())
&& equalsNullable(o1.getLowerBound(), other.getLowerBound())
&& equalsNullable(o1.getUpperBound(), other.getUpperBound())
&& Objects.equals(o1.getLowerBound(), other.getLowerBound())
&& Objects.equals(o1.getUpperBound(), other.getUpperBound())
&& o1.getUnit().equals(other.getUnit());
}

private static boolean equalsNullable(Object o1, Object o2) {
return o1 == null ? o2 == null : o1.equals(o2);
}

/**
* Returns true if the parameters are two {@link ValueSnak} objects with
* exactly the same data. It does not matter if they are different
Expand Down
Expand Up @@ -136,12 +136,7 @@ protected List<SnakGroup> getSnakGroups() {
* @return
*/
protected ArrayList<Snak> getSnakList(PropertyIdValue propertyIdValue) {
ArrayList<Snak> result = this.snaks.get(propertyIdValue);
if (result == null) {
result = new ArrayList<Snak>();
this.snaks.put(propertyIdValue, result);
}
return result;
return this.snaks.computeIfAbsent(propertyIdValue, k -> new ArrayList<>());
}

}
Expand Up @@ -279,12 +279,7 @@ protected List<SnakGroup> getQualifierGroups() {
* @return
*/
protected ArrayList<Snak> getQualifierList(PropertyIdValue propertyIdValue) {
ArrayList<Snak> result = this.qualifiers.get(propertyIdValue);
if (result == null) {
result = new ArrayList<Snak>();
this.qualifiers.put(propertyIdValue, result);
}
return result;
return this.qualifiers.computeIfAbsent(propertyIdValue, k -> new ArrayList<>());
}

/**
Expand Down
@@ -1,7 +1,5 @@
package org.wikidata.wdtk.datamodel.helpers;

import org.wikidata.wdtk.datamodel.implementation.UnsupportedEntityIdValueImpl;

/*
* #%L
* Wikidata Toolkit Data Model
Expand Down Expand Up @@ -558,7 +556,6 @@ public static String toString(SenseDocument o) {
*/
public static String toString(MediaInfoDocument o) {
StringBuilder sb = new StringBuilder();
boolean first;
sb.append("==MediaInfoDocument ").append(o.getEntityId().getIri());
sb.append(" (r").append(o.getRevisionId()).append(") ");
sb.append("==");
Expand Down
Expand Up @@ -26,7 +26,6 @@
import org.wikidata.wdtk.datamodel.helpers.Hash;
import org.wikidata.wdtk.datamodel.helpers.ToString;
import org.wikidata.wdtk.datamodel.interfaces.DatatypeIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ValueVisitor;


import java.util.regex.Matcher;
Expand Down
Expand Up @@ -26,7 +26,6 @@
import org.apache.commons.lang3.Validate;
import org.wikidata.wdtk.datamodel.interfaces.EntityDocument;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.MediaInfoDocument;

/**
* Abstract Jackson implementation of {@link EntityDocument}. Like all Jackson
Expand Down
@@ -1,8 +1,5 @@
package org.wikidata.wdtk.datamodel.implementation;

import java.util.List;
import java.util.Map;

/*
* #%L
* Wikidata Toolkit Data Model
Expand All @@ -28,17 +25,12 @@
import org.wikidata.wdtk.datamodel.helpers.Hash;
import org.wikidata.wdtk.datamodel.helpers.ToString;
import org.wikidata.wdtk.datamodel.interfaces.*;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.Validate;
import org.wikidata.wdtk.datamodel.helpers.Equality;
import org.wikidata.wdtk.datamodel.helpers.Hash;
import org.wikidata.wdtk.datamodel.helpers.ToString;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
Expand Down
Expand Up @@ -57,7 +57,7 @@ public class SitesImpl implements Sites {
* @author Markus Kroetzsch
*
*/
class SiteInformation {
static class SiteInformation {
final String siteKey;
final String group;
final String languageCode;
Expand Down Expand Up @@ -88,13 +88,11 @@ class SiteInformation {

int iFileName = filePath.indexOf("$1");
this.filePathPre = filePath.substring(0, iFileName);
this.filePathPost = filePath.substring(iFileName + 2,
filePath.length());
this.filePathPost = filePath.substring(iFileName + 2);

int iPageName = pagePath.indexOf("$1");
this.pagePathPre = pagePath.substring(0, iPageName);
this.pagePathPost = pagePath.substring(iPageName + 2,
pagePath.length());
this.pagePathPost = pagePath.substring(iPageName + 2);
}

/**
Expand Down Expand Up @@ -157,7 +155,7 @@ String addProtocolPrefix(String urlPrefix) {
}
}

final HashMap<String, SiteInformation> sites = new HashMap<String, SiteInformation>();
final HashMap<String, SiteInformation> sites = new HashMap<>();

@Override
public void setSiteInformation(String siteKey, String group,
Expand Down
Expand Up @@ -22,13 +22,10 @@
* #L%
*/

import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.Value;

import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -43,7 +40,7 @@
* @author Antonin Delpeuch
*
*/
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "snaktype")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "snaktype")
@JsonSubTypes({
@Type(value = NoValueSnakImpl.class, name = SnakImpl.JSON_SNAK_TYPE_NOVALUE),
@Type(value = SomeValueSnakImpl.class, name = SnakImpl.JSON_SNAK_TYPE_SOMEVALUE),
Expand Down
Expand Up @@ -11,7 +11,6 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonDeserializer.None;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

/*
Expand Down Expand Up @@ -42,7 +41,7 @@
*
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonDeserialize(using = None.class)
@JsonDeserialize()
public class StringValueImpl extends ValueImpl implements StringValue {

private final String value;
Expand Down
Expand Up @@ -43,7 +43,7 @@
*
*/
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({ //TODO: drop in future release
@Type(value = ItemDocumentImpl.class, name = EntityDocumentImpl.JSON_TYPE_ITEM),
@Type(value = PropertyDocumentImpl.class, name = EntityDocumentImpl.JSON_TYPE_PROPERTY),
Expand Down
Expand Up @@ -87,7 +87,6 @@ public class TimeValueImpl extends ValueImpl implements TimeValue {
* @param timezoneOffset
* offset in minutes that should be applied when displaying this
* time
* @return a {@link TimeValue} corresponding to the input
*/
public TimeValueImpl(long year, byte month, byte day, byte hour, byte minute,
byte second, byte precision, int beforeTolerance,
Expand Down
Expand Up @@ -40,7 +40,6 @@
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.JsonDeserializer.None;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

/**
Expand All @@ -53,7 +52,7 @@
*
* @author Antonin Delpeuch
*/
@JsonDeserialize(using = None.class)
@JsonDeserialize()
public class UnsupportedEntityIdValueImpl extends ValueImpl implements UnsupportedEntityIdValue {

private final JacksonIdValue value;
Expand Down
Expand Up @@ -31,7 +31,6 @@
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonDeserializer.None;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

Expand All @@ -46,7 +45,7 @@
* @author Antonin Delpeuch
*
*/
@JsonDeserialize(using = None.class)
@JsonDeserialize()
public class UnsupportedValueImpl extends ValueImpl implements UnsupportedValue {

private final String typeString;
Expand Down Expand Up @@ -106,7 +105,7 @@ public int hashCode() {
*/
@Override
public boolean equals(Object other) {
if (other == null || !(other instanceof UnsupportedValueImpl)) {
if (!(other instanceof UnsupportedValueImpl)) {
return false;
}
UnsupportedValueImpl otherValue = (UnsupportedValueImpl) other;
Expand Down
Expand Up @@ -27,7 +27,6 @@
* #L%
*/

import org.wikidata.wdtk.datamodel.interfaces.MediaInfoIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Value;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand Down
@@ -1,8 +1,5 @@
package org.wikidata.wdtk.datamodel.interfaces;

import org.wikidata.wdtk.datamodel.helpers.Equality;
import org.wikidata.wdtk.datamodel.helpers.Hash;

/*
* #%L
* Wikidata Toolkit Data Model
Expand Down
Expand Up @@ -20,7 +20,6 @@
* #L%
*/

import java.util.List;
import java.util.Set;

/**
Expand Down
Expand Up @@ -286,7 +286,7 @@ public interface TimeValue extends Value {
* This conversion can fail if not enough information is available
* (for example, we need at least day precision to convert from Julian to Gregorian).
*
* @return a TimeValue that uses the Gregorian calendar, or {@ref Optional::empty} if the conversion failed.
* @return a TimeValue that uses the Gregorian calendar, or {@link Optional#empty} if the conversion failed.
*/
Optional<TimeValue> toGregorian();

Expand Down
Expand Up @@ -34,5 +34,5 @@ public interface UnsupportedEntityIdValue extends EntityIdValue {
/**
* The type of entity as represented in the JSON serialization.
*/
public String getEntityTypeJsonString();
String getEntityTypeJsonString();
}
Expand Up @@ -39,5 +39,5 @@ public interface UnsupportedValue extends Value {
* @return
* the value of "type" in the JSON representation of this value.
*/
public String getTypeJsonString();
String getTypeJsonString();
}
Expand Up @@ -616,10 +616,6 @@ public static String getLanguageCode(String wikimediaLanguageCode) {
* the preferred language code corresponding to the original language code
*/
public static String fixLanguageCodeIfDeprecated(String wikimediaLanguageCode) {
if (DEPRECATED_LANGUAGE_CODES.containsKey(wikimediaLanguageCode)) {
return DEPRECATED_LANGUAGE_CODES.get(wikimediaLanguageCode);
} else {
return wikimediaLanguageCode;
}
return DEPRECATED_LANGUAGE_CODES.getOrDefault(wikimediaLanguageCode, wikimediaLanguageCode);
}
}
Expand Up @@ -40,7 +40,7 @@
*/
public class DatamodelConverterTest {

class BrokenItemIdValue implements ItemIdValue {
static class BrokenItemIdValue implements ItemIdValue {

@Override
public String getEntityType() {
Expand Down
Expand Up @@ -33,7 +33,6 @@

import org.junit.Test;
import org.wikidata.wdtk.datamodel.implementation.DataObjectFactoryImpl;
import org.wikidata.wdtk.datamodel.interfaces.Claim;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
Expand Down
Expand Up @@ -22,7 +22,6 @@

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collections;

import org.junit.Test;
Expand Down
Expand Up @@ -20,16 +20,14 @@
* #L%
*/

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement;

import static org.junit.Assert.*;

public class StatementDocumentTest {
private static ItemIdValue Q1 = Datamodel.makeWikidataItemIdValue("Q1");
private static ItemIdValue Q2 = Datamodel.makeWikidataItemIdValue("Q2");
Expand All @@ -50,15 +48,15 @@ public void testFindStatement() {

assertTrue(id.hasStatement(P1));
assertTrue(id.hasStatement("P1"));
assertEquals(null, id.findStatement(P1));
assertEquals(null, id.findStatement("P1"));
assertNull(id.findStatement(P1));
assertNull(id.findStatement("P1"));
assertTrue(id.hasStatement(P2));
assertTrue(id.hasStatement("P2"));
assertEquals(s3, id.findStatement(P2));
assertEquals(s3, id.findStatement("P2"));
assertFalse(id.hasStatement(P3));
assertFalse(id.hasStatement("P3"));
assertEquals(null, id.findStatement(P3));
assertEquals(null, id.findStatement("P3"));
assertNull(id.findStatement(P3));
assertNull(id.findStatement("P3"));
}
}
Expand Up @@ -24,10 +24,8 @@

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import org.junit.Test;
import org.wikidata.wdtk.datamodel.helpers.DatamodelConverterTest.ValueType;
import org.wikidata.wdtk.datamodel.interfaces.*;

public class DataObjectFactoryImplTest {
Expand Down

0 comments on commit e05fc92

Please sign in to comment.