commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ans...@apache.org
Subject [1/4] incubator-commonsrdf git commit: Add note about not storing language tag field as an Optional if an implementation requires Serialzable objects
Date Sat, 28 Mar 2015 05:02:24 GMT
Repository: incubator-commonsrdf
Updated Branches:
  refs/heads/master b773bbff6 -> be1420cfe


Add note about not storing language tag field as an Optional if an
implementation requires Serialzable objects

Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/98df1c01
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/98df1c01
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/98df1c01

Branch: refs/heads/master
Commit: 98df1c018aa306214a8a954a7aedd14fcab850ef
Parents: b773bbf
Author: Peter Ansell <p_ansell@yahoo.com>
Authored: Sat Mar 28 15:29:25 2015 +1100
Committer: Peter Ansell <p_ansell@yahoo.com>
Committed: Sat Mar 28 15:33:48 2015 +1100

----------------------------------------------------------------------
 .../java/org/apache/commons/rdf/api/Literal.java  |  8 +++++++-
 .../apache/commons/rdf/simple/LiteralImpl.java    | 18 +++++++++++-------
 2 files changed, 18 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/98df1c01/api/src/main/java/org/apache/commons/rdf/api/Literal.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/org/apache/commons/rdf/api/Literal.java b/api/src/main/java/org/apache/commons/rdf/api/Literal.java
index d4caa63..3890404 100644
--- a/api/src/main/java/org/apache/commons/rdf/api/Literal.java
+++ b/api/src/main/java/org/apache/commons/rdf/api/Literal.java
@@ -17,6 +17,7 @@
  */
 package org.apache.commons.rdf.api;
 
+import java.io.Serializable;
 import java.util.Optional;
 
 /**
@@ -59,7 +60,12 @@ public interface Literal extends RDFTerm {
 	 * href="http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"
 	 * >http://www.w3.org/1999/02/22-rdf-syntax-ns#langString</a>, this method
 	 * must return {@link Optional#empty()}.
-	 *
+	 * <p>
+	 * Implementor note: If your application requires {@link Serializable}
+	 * objects, it is best not to store an {@link Optional} in a field. It is
+	 * recommended to use {@link Optional#ofNullable(Object)} to create the
+	 * return value for this method.
+	 * 
 	 * @return The {@link Optional} language tag for this literal. If
 	 *         {@link Optional#isPresent()} returns true, the value returned by
 	 *         {@link Optional#get()} must be a non-empty string conforming to

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/98df1c01/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java
----------------------------------------------------------------------
diff --git a/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java b/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java
index 521353d..b26cf6b 100644
--- a/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java
+++ b/simple/src/main/java/org/apache/commons/rdf/simple/LiteralImpl.java
@@ -33,9 +33,9 @@ class LiteralImpl implements Literal {
 
 	private static final String QUOTE = "\"";
 
-	private IRI dataType;
-	private Optional<String> languageTag;
-	private String lexicalForm;
+	private final IRI dataType;
+	private final String languageTag;
+	private final String lexicalForm;
 
 	public LiteralImpl(String literal) {
 		this(literal, Types.XSD_STRING);
@@ -45,12 +45,13 @@ class LiteralImpl implements Literal {
 		this.lexicalForm = Objects.requireNonNull(lexicalForm);
 		this.dataType = Types.get(Objects.requireNonNull(dataType)).orElse(
 				dataType);
-		this.languageTag = Optional.empty();
+		this.languageTag = null;
 	}
 
 	public LiteralImpl(String literal, String languageTag) {
 		this.lexicalForm = Objects.requireNonNull(literal);
-		this.languageTag = Optional.of(languageTag.toLowerCase(Locale.ENGLISH));
+		this.languageTag = Objects.requireNonNull(languageTag).toLowerCase(
+				Locale.ENGLISH);
 		if (languageTag.isEmpty()) {
 			// TODO: Check against
 			// http://www.w3.org/TR/n-triples/#n-triples-grammar
@@ -74,7 +75,7 @@ class LiteralImpl implements Literal {
 
 	@Override
 	public Optional<String> getLanguageTag() {
-		return languageTag;
+		return Optional.ofNullable(languageTag);
 	}
 
 	@Override
@@ -117,7 +118,10 @@ class LiteralImpl implements Literal {
 
 	@Override
 	public boolean equals(Object obj) {
-		if (!(obj instanceof Literal)) {
+		if (this == obj) {
+			return true;
+		}
+		if (obj == null || !(obj instanceof Literal)) {
 			return false;
 		}
 		Literal literal = (Literal) obj;


Mime
View raw message