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

Codebase Refactoring for Improved Readability and Maintainability #861

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand Down Expand Up @@ -90,41 +90,42 @@ public static boolean equalsDatatypeIdValue(DatatypeIdValue o1, Object o2) {
&& o1.getJsonString().equals(((DatatypeIdValue) o2).getJsonString());
}

/**
* Returns {@code true} if the parameters are two {@link TimeValue} objects with
* exactly the same data. It does not matter if they are different
* implementations of the interface as long as their content is the same.
*
* @param o1
* the first object to compare
* @param o2
* the second object to compare
* @return {@code true} if both objects are equal
*/
public static boolean equalsTimeValue(TimeValue o1, Object o2) {
if (o2 == null) {
return false;
}
if (o2 == o1) {
return true;
}
if (!(o2 instanceof TimeValue)) {
return false;
}
TimeValue other = (TimeValue) o2;
return o1.getYear() == other.getYear()
&& o1.getMonth() == other.getMonth()
&& o1.getDay() == other.getDay()
&& o1.getHour() == other.getHour()
&& o1.getMinute() == other.getMinute()
&& o1.getSecond() == other.getSecond()
&& o1.getPrecision() == other.getPrecision()
&& o1.getBeforeTolerance() == other.getBeforeTolerance()
&& o1.getAfterTolerance() == other.getAfterTolerance()
&& o1.getTimezoneOffset() == other.getTimezoneOffset()
&& o1.getPreferredCalendarModel().equals(
other.getPreferredCalendarModel());
}
/**
* Returns {@code true} if the parameters are two {@link TimeValue} objects with
* exactly the same data. It does not matter if they are different
* implementations of the interface as long as their content is the same.
*
* @param o1
* the first object to compare
* @param o2
* the second object to compare
* @return {@code true} if both objects are equal
*/
public static boolean equalsTimeValue(TimeValue o1, Object o2) {
if (o2 == null) {
return false;
}
if (o2 == o1) {
return true;
}
if (!(o2 instanceof TimeValue)) {
return false;
}
TimeValue other = (TimeValue) o2;

boolean equalTimeStamp =
(o1.getYear() == other.getYear() && o1.getMonth() == other.getMonth() && o1.getDay() == other.getDay()
&& o1.getHour() == other.getHour() && o1.getMinute() == other.getMinute()
&& o1.getSecond() == other.getSecond() && o1.getTimezoneOffset() == other.getTimezoneOffset());

boolean equalTolerance =
(o1.getBeforeTolerance() == other.getBeforeTolerance() && o1.getAfterTolerance() == other.getAfterTolerance());

return equalTimeStamp && equalTolerance
&& o1.getPrecision() == other.getPrecision()
&& o1.getPreferredCalendarModel()
.equals(other.getPreferredCalendarModel());
}

/**
* Returns {@code true} if the parameters are two {@link GlobeCoordinatesValue}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,74 +41,9 @@
*/
public class DatatypeIdImpl implements DatatypeIdValue {

/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_ITEM} in JSON.
*/
public static final String JSON_DT_ITEM = "wikibase-item";
/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_PROPERTY} in JSON.
*/
public static final String JSON_DT_PROPERTY = "wikibase-property";
/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_GLOBE_COORDINATES} in JSON.
*/
public static final String JSON_DT_GLOBE_COORDINATES = "globe-coordinate";
/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_ITEM} in JSON.
*/
public static final String JSON_DT_URL = "url";
/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_COMMONS_MEDIA} in JSON.
*/
public static final String JSON_DT_COMMONS_MEDIA = "commonsMedia";
/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_TIME} in JSON.
*/
public static final String JSON_DT_TIME = "time";
/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_QUANTITY} in JSON.
*/
public static final String JSON_DT_QUANTITY = "quantity";
/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_STRING} in JSON.
*/
public static final String JSON_DT_STRING = "string";
/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_MONOLINGUAL_TEXT} in JSON.
*/
public static final String JSON_DT_MONOLINGUAL_TEXT = "monolingualtext";
/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_EXTERNAL_ID} in JSON.
*/
public static final String JSON_DT_EXTERNAL_ID = "external-id";
/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_MATH} in JSON.
*/
public static final String JSON_DT_MATH = "math";
/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_GEO_SHAPE} in JSON.
*/
public static final String JSON_DT_GEO_SHAPE = "geo-shape";
/**
* String used to refer to the property datatype
* {@link DatatypeIdValue#DT_EDTF} in JSON.
*/
public static final String JSON_DT_EDTF = "edtf";
private final DatatypeJsonUtils datatypeJsonUtils = new DatatypeJsonUtils();

private static final Pattern JSON_DATATYPE_PATTERN = Pattern.compile("^[a-zA-Z\\-]+$");
private static final Pattern DATATYPE_ID_PATTERN = Pattern.compile("^http://wikiba\\.se/ontology#([a-zA-Z]+)$");

/**
* Datatype IRI as used in Wikidata Toolkit.
Expand All @@ -123,99 +58,6 @@ public class DatatypeIdImpl implements DatatypeIdValue {
*/
private final String jsonString;

/**
* Returns the WDTK datatype IRI for the property datatype as represented by
* the given JSON datatype string.
*
* @param jsonDatatype
* the JSON datatype string; case-sensitive
* @throws IllegalArgumentException
* if the given datatype string is not known
*/
public static String getDatatypeIriFromJsonDatatype(String jsonDatatype) {
switch (jsonDatatype) {
case JSON_DT_ITEM:
return DT_ITEM;
case JSON_DT_PROPERTY:
return DT_PROPERTY;
case JSON_DT_GLOBE_COORDINATES:
return DT_GLOBE_COORDINATES;
case JSON_DT_URL:
return DT_URL;
case JSON_DT_COMMONS_MEDIA:
return DT_COMMONS_MEDIA;
case JSON_DT_TIME:
return DT_TIME;
case JSON_DT_QUANTITY:
return DT_QUANTITY;
case JSON_DT_STRING:
return DT_STRING;
case JSON_DT_MONOLINGUAL_TEXT:
return DT_MONOLINGUAL_TEXT;
case JSON_DT_EDTF:
return DT_EDTF;
default:

String[] parts = jsonDatatype.split("-");
for(int i = 0; i < parts.length; i++) {
parts[i] = StringUtils.capitalize(parts[i]);
}
return "http://wikiba.se/ontology#" + StringUtils.join(parts);
}
}

/**
* Returns the JSON datatype for the property datatype as represented by
* the given WDTK datatype IRI string.
*
* @param datatypeIri
* the WDTK datatype IRI string; case-sensitive
* @throws IllegalArgumentException
* if the given datatype string is not known
* @deprecated this method is unreliable and will be removed in a future release.
*/
public static String getJsonDatatypeFromDatatypeIri(String datatypeIri) {
switch (datatypeIri) {
case DatatypeIdValue.DT_ITEM:
return DatatypeIdImpl.JSON_DT_ITEM;
case DatatypeIdValue.DT_GLOBE_COORDINATES:
return DatatypeIdImpl.JSON_DT_GLOBE_COORDINATES;
case DatatypeIdValue.DT_URL:
return DatatypeIdImpl.JSON_DT_URL;
case DatatypeIdValue.DT_COMMONS_MEDIA:
return DatatypeIdImpl.JSON_DT_COMMONS_MEDIA;
case DatatypeIdValue.DT_TIME:
return DatatypeIdImpl.JSON_DT_TIME;
case DatatypeIdValue.DT_QUANTITY:
return DatatypeIdImpl.JSON_DT_QUANTITY;
case DatatypeIdValue.DT_STRING:
return DatatypeIdImpl.JSON_DT_STRING;
case DatatypeIdValue.DT_MONOLINGUAL_TEXT:
return DatatypeIdImpl.JSON_DT_MONOLINGUAL_TEXT;
case DatatypeIdValue.DT_PROPERTY:
return DatatypeIdImpl.JSON_DT_PROPERTY;
case DatatypeIdValue.DT_EDTF:
return DatatypeIdImpl.JSON_DT_EDTF;
default:
//We apply the reverse algorithm of JacksonDatatypeId::getDatatypeIriFromJsonDatatype
Matcher matcher = DATATYPE_ID_PATTERN.matcher(datatypeIri);
if(!matcher.matches()) {
throw new IllegalArgumentException("Unknown datatype: " + datatypeIri);
}

StringBuilder jsonDatatypeBuilder = new StringBuilder();
for(char ch : StringUtils.uncapitalize(matcher.group(1)).toCharArray()) {
if(Character.isUpperCase(ch)) {
jsonDatatypeBuilder
.append('-')
.append(Character.toLowerCase(ch));
} else {
jsonDatatypeBuilder.append(ch);
}
}
return jsonDatatypeBuilder.toString();
}
}

/**
* Copy constructor.
Expand Down Expand Up @@ -244,7 +86,7 @@ public DatatypeIdImpl(String iri)
this.iri = iri;
// the JSON datatype is not supplied, so we fall back on our buggy heuristic
// to guess how it should be represented in JSON.
this.jsonString = getJsonDatatypeFromDatatypeIri(this.iri);
this.jsonString = datatypeJsonUtils.getJsonDatatypeFromDatatypeIri(this.iri);
}

/**
Expand All @@ -267,7 +109,7 @@ public DatatypeIdImpl(String iri, String jsonString)
throw new IllegalArgumentException("Invalid JSON datatype \"" + jsonString + "\"");
}
this.jsonString = jsonString;
this.iri = iri != null ? iri : getDatatypeIriFromJsonDatatype(jsonString);
this.iri = iri != null ? iri : datatypeJsonUtils.getDatatypeIriFromJsonDatatype(jsonString);
}

/**
Expand Down