-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a468951
commit 8b73005
Showing
6 changed files
with
243 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...el/src/main/java/org/wikidata/wdtk/datamodel/implementation/MonolingualTextValueImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...-datamodel/src/main/java/org/wikidata/wdtk/datamodel/interfaces/MonolingualTextValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...c/test/java/org/wikidata/wdtk/datamodel/implementation/MonoloingualTextValueImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |