-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from ate47/dev_quadparser
Add simple parser extension to parse NQuad and QuadString
- Loading branch information
Showing
9 changed files
with
605 additions
and
216 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
119 changes: 119 additions & 0 deletions
119
hdt-api/src/main/java/org/rdfhdt/hdt/quad/QuadString.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,119 @@ | ||
package org.rdfhdt.hdt.quad; | ||
|
||
import org.rdfhdt.hdt.exceptions.ParserException; | ||
import org.rdfhdt.hdt.triples.TripleString; | ||
|
||
public class QuadString extends TripleString { | ||
protected CharSequence context; | ||
|
||
public QuadString() { | ||
super(); | ||
context = ""; | ||
} | ||
|
||
public QuadString(CharSequence subject, CharSequence predicate, CharSequence object, CharSequence context) { | ||
super(subject, predicate, object); | ||
this.context = context; | ||
} | ||
|
||
public QuadString(TripleString other) { | ||
super(other); | ||
this.context = other.getObject(); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
super.clear(); | ||
context = ""; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (context.length() == 0) { | ||
return super.equals(other); | ||
} | ||
if (!(other instanceof QuadString)) { | ||
return false; | ||
} | ||
QuadString qs = (QuadString) other; | ||
return equalsCharSequence(subject, qs.subject) | ||
&& equalsCharSequence(predicate, qs.predicate) | ||
&& equalsCharSequence(object, qs.object) | ||
&& equalsCharSequence(context, qs.context); | ||
} | ||
|
||
@Override | ||
public CharSequence getGraph() { | ||
return context; | ||
} | ||
|
||
@Override | ||
public void setGraph(CharSequence context) { | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public void setAll(CharSequence subject, CharSequence predicate, CharSequence object) { | ||
setAll(subject, predicate, object, ""); | ||
} | ||
|
||
/** | ||
* Sets all components at once. Useful to reuse existing object instead of | ||
* creating new ones for performance. | ||
* | ||
* @param subject subject | ||
* @param predicate predicate | ||
* @param object object | ||
* @param context context | ||
*/ | ||
public void setAll(CharSequence subject, CharSequence predicate, CharSequence object, CharSequence context) { | ||
super.setAll(subject, predicate, object); | ||
this.context = context; | ||
} | ||
|
||
@Override | ||
public boolean match(TripleString pattern) { | ||
if (context.length() != 0 | ||
&& !(pattern instanceof QuadString && equalsCharSequence(((QuadString) pattern).context, context))) { | ||
// if a context is defined, we don't match | ||
return false; | ||
} | ||
if (pattern.getSubject().length() == 0 || equalsCharSequence(pattern.getSubject(), this.subject)) { | ||
if (pattern.getPredicate().length() == 0 || equalsCharSequence(pattern.getPredicate(), this.predicate)) { | ||
return pattern.getObject().length() == 0 || equalsCharSequence(pattern.getObject(), this.object); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return super.isEmpty() && context.length() == 0; | ||
} | ||
|
||
@Override | ||
public void read(String line) throws ParserException { | ||
super.read(line, true); | ||
} | ||
|
||
@Override | ||
public void read(String line, int start, int end) throws ParserException { | ||
super.read(line, start, end, true); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
// Same as Objects.hashCode(subject, predicate, object), with fewer | ||
// calls | ||
int s = subject == null ? 0 : subject.hashCode(); | ||
int p = predicate == null ? 0 : predicate.hashCode(); | ||
int o = object == null ? 0 : object.hashCode(); | ||
int c = context == null ? 0 : context.hashCode(); | ||
return 31 * (31 * (31 * (31 * s) + p) + o) + c; | ||
} | ||
|
||
@Override | ||
public QuadString tripleToString() { | ||
return new QuadString(subject.toString(), predicate.toString(), object.toString(), context.toString()); | ||
} | ||
} |
Oops, something went wrong.