Skip to content

Commit

Permalink
New MonolingualTextValue type
Browse files Browse the repository at this point in the history
  • Loading branch information
mkroetzsch committed Feb 18, 2014
1 parent a468951 commit 8b73005
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.wikidata.wdtk.datamodel.interfaces.GlobeCoordinatesValue;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.datamodel.interfaces.NoValueSnak;
import org.wikidata.wdtk.datamodel.interfaces.PropertyDocument;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
Expand Down Expand Up @@ -91,6 +92,12 @@ public StringValue getStringValue(String string) {
return new StringValueImpl(string);
}

@Override
public MonolingualTextValue getMonolingualTextValue(String text,
String languageCode) {
return new MonolingualTextValueImpl(text, languageCode);
}

@Override
public QuantityValue getQuantityValue(BigDecimal numericValue,
BigDecimal lowerBound, BigDecimal upperBound) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.wikidata.wdtk.datamodel.implementation;

/*
* #%L
* Wikidata Toolkit Data Model
* %%
* Copyright (C) 2014 Wikidata Toolkit Developers
* %%
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import org.apache.commons.lang3.Validate;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;

public class MonolingualTextValueImpl implements MonolingualTextValue {

final String text;
final String languageCode;

/**
* Constructor. The language code can be any string; the class does not make
* any assumptions on how language codes are defined.
*
* @param text
* the text of the value
* @param languageCode
* the language code of the value
*/
MonolingualTextValueImpl(String text, String languageCode) {
Validate.notNull(text, "Text cannot be null");
Validate.notNull(languageCode, "Language code cannot be null");
this.text = text;
this.languageCode = languageCode;
}

@Override
public String getText() {
return this.text;
}

@Override
public String getLanguageCode() {
return this.languageCode;
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + languageCode.hashCode();
result = prime * result + text.hashCode();
return result;
}

/*
* (non-Javadoc)
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof MonolingualTextValueImpl)) {
return false;
}
MonolingualTextValueImpl other = (MonolingualTextValueImpl) obj;
return this.text.equals(other.text)
&& this.languageCode.equals(other.languageCode);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,18 @@ GlobeCoordinatesValue getGlobeCoordinatesValue(long latitude,
*/
StringValue getStringValue(String string);

/**
* Create a {@link MonolingualTextValue}.
*
* @param text
* the text of the value
* @param languageCode
* the language code of the value
* @return a {@link MonolingualValue} corresponding to the input
*/
MonolingualTextValue getMonolingualTextValue(String text,
String languageCode);

/**
* Create a {@link QuantityValue}.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.wikidata.wdtk.datamodel.interfaces;

/*
* #%L
* Wikidata Toolkit Data Model
* %%
* Copyright (C) 2014 Wikidata Toolkit Developers
* %%
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

/**
* A monolingual text value represents a text (string) in a certain language.
*
* @author Markus Kroetzsch
*
*/
public interface MonolingualTextValue extends Value {

/**
* Get the text of this value.
*
* @return a string
*/
String getText();

/**
* Get the language code of this value. The codes are usually based on the
* codes used internally in Wikibase, which in turn are the codes used in
* the Universal Language Selector extension. However, the data model as
* such does not restrict the strings that might be used here.
*
* @return a string that represents language
*/
String getLanguageCode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.wikidata.wdtk.datamodel.interfaces.GlobeCoordinatesValue;
import org.wikidata.wdtk.datamodel.interfaces.ItemDocument;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;
import org.wikidata.wdtk.datamodel.interfaces.NoValueSnak;
import org.wikidata.wdtk.datamodel.interfaces.PropertyDocument;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
Expand Down Expand Up @@ -110,6 +111,13 @@ public final void testGetStringValue() {
assertEquals(o1, o2);
}

@Test
public final void testGetMonolingualTextValue() {
MonolingualTextValue o1 = new MonolingualTextValueImpl("foo", "en");
MonolingualTextValue o2 = factory.getMonolingualTextValue("foo", "en");
assertEquals(o1, o2);
}

@Test
public final void testGetQuantityValue() {
BigDecimal nv = new BigDecimal(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.wikidata.wdtk.datamodel.implementation;

/*
* #%L
* Wikidata Toolkit Data Model
* %%
* Copyright (C) 2014 Wikidata Toolkit Developers
* %%
* 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.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;

import org.junit.Before;
import org.junit.Test;
import org.wikidata.wdtk.datamodel.interfaces.MonolingualTextValue;

public class MonoloingualTextValueImplTest {

MonolingualTextValue s1;
MonolingualTextValue s2;

@Before
public void setUp() throws Exception {
s1 = new MonolingualTextValueImpl("some string", "en");
s2 = new MonolingualTextValueImpl("some string", "en");
}

@Test
public void dataIsCorrect() {
assertEquals(s1.getText(), "some string");
assertEquals(s1.getLanguageCode(), "en");
}

@Test
public void equalityBasedOnContent() {
MonolingualTextValue s3 = new MonolingualTextValueImpl(
"another string", "en");
MonolingualTextValue s4 = new MonolingualTextValueImpl("some string",
"en-GB");

assertEquals(s1, s1);
assertEquals(s1, s2);
assertThat(s1, not(equalTo(s3)));
assertThat(s1, not(equalTo(s4)));
assertThat(s1, not(equalTo(null)));
assertFalse(s1.equals(this));
}

@Test
public void hashBasedOnContent() {
assertEquals(s1.hashCode(), s2.hashCode());
}

@Test(expected = NullPointerException.class)
public void textNotNull() {
new MonolingualTextValueImpl(null, "en");
}

@Test(expected = NullPointerException.class)
public void languageCodeNotNull() {
new MonolingualTextValueImpl("some text", null);
}

}

0 comments on commit 8b73005

Please sign in to comment.