1111package org .eclipse .rdf4j .sail .lmdb .model ;
1212
1313import java .io .ObjectStreamException ;
14+ import java .util .Objects ;
1415
15- import org .eclipse .rdf4j .model .impl .SimpleIRI ;
16+ import org .eclipse .rdf4j .model .IRI ;
17+ import org .eclipse .rdf4j .model .util .URIUtil ;
1618import org .eclipse .rdf4j .sail .lmdb .ValueStoreRevision ;
1719import org .slf4j .Logger ;
1820import org .slf4j .LoggerFactory ;
1921
20- public class LmdbIRI extends SimpleIRI implements LmdbResource {
22+ public class LmdbIRI implements LmdbResource , IRI {
2123
2224 private static final long serialVersionUID = -5888138591826143179L ;
2325 private static final Logger log = LoggerFactory .getLogger (LmdbIRI .class );
@@ -31,13 +33,21 @@ public class LmdbIRI extends SimpleIRI implements LmdbResource {
3133 private volatile long internalID ;
3234
3335 private volatile boolean initialized = false ;
36+ /**
37+ * The IRI string.
38+ */
39+ private String iriString ;
40+
41+ /**
42+ * An index indicating the first character of the local name in the IRI string, -1 if not yet set.
43+ */
44+ private int localNameIdx ;
3445
3546 /*--------------*
3647 * Constructors *
3748 *--------------*/
3849
3950 public LmdbIRI (ValueStoreRevision revision , long internalID ) {
40- super ();
4151 setInternalID (internalID , revision );
4252 }
4353
@@ -46,17 +56,19 @@ public LmdbIRI(ValueStoreRevision revision, String uri) {
4656 }
4757
4858 public LmdbIRI (ValueStoreRevision revision , String uri , long internalID ) {
49- super (uri );
59+ setIRIString (uri );
5060 setInternalID (internalID , revision );
5161 this .initialized = true ;
5262 }
5363
54- public LmdbIRI (ValueStoreRevision revision , String namespace , String localname ) {
55- this (revision , namespace + localname );
64+ public LmdbIRI (ValueStoreRevision revision , String namespace , String localName ) {
65+ this (revision , namespace , localName , UNKNOWN_ID );
5666 }
5767
58- public LmdbIRI (ValueStoreRevision revision , String namespace , String localname , long internalID ) {
59- this (revision , namespace + localname , internalID );
68+ public LmdbIRI (ValueStoreRevision revision , String namespace , String localName , long internalID ) {
69+ this .revision = revision ;
70+ setNamespaceAndIri (namespace , localName );
71+ setInternalID (internalID , revision );
6072 }
6173
6274 /*---------*
@@ -79,31 +91,50 @@ public long getInternalID() {
7991 return internalID ;
8092 }
8193
82- @ Override
83- public void setIRIString (String iriString ) {
84- super .setIRIString (iriString );
94+ private void setIRIString (String iriString ) {
95+ Objects .requireNonNull (iriString , "iriString must not be null" );
96+
97+ if (iriString .indexOf (':' ) < 0 ) {
98+ throw new IllegalArgumentException ("Not a valid (absolute) IRI: " + iriString );
99+ }
100+
101+ this .iriString = iriString ;
102+ this .localNameIdx = -1 ;
85103 }
86104
87105 @ Override
88106 public String getNamespace () {
107+
89108 init ();
90- return super .getNamespace ();
109+ if (localNameIdx < 0 ) {
110+ localNameIdx = URIUtil .getLocalNameIndex (iriString );
111+ }
112+
113+ return iriString .substring (0 , localNameIdx );
91114 }
92115
93116 @ Override
94117 public String getLocalName () {
118+
95119 init ();
96- return super .getLocalName ();
120+ if (localNameIdx < 0 ) {
121+ localNameIdx = URIUtil .getLocalNameIndex (iriString );
122+ }
123+
124+ return iriString .substring (localNameIdx );
97125 }
98126
99127 @ Override
100128 public String stringValue () {
129+ if (iriString != null ) {
130+ return iriString ;
131+ }
101132 init ();
102- return super . stringValue () ;
133+ return iriString ;
103134 }
104135
105136 public void init () {
106- if (!initialized ) {
137+ if (iriString == null && !initialized ) {
107138 synchronized (this ) {
108139 if (!initialized ) {
109140 boolean resolved = revision .resolveValue (internalID , this );
@@ -122,20 +153,75 @@ public boolean equals(Object o) {
122153 return true ;
123154 }
124155
125- if (o instanceof LmdbIRI && internalID != UNKNOWN_ID ) {
156+ if (o == null ) {
157+ return false ;
158+ }
159+
160+ if (o .getClass () == LmdbIRI .class ) {
161+ if (internalID == UNKNOWN_ID ) {
162+ boolean equals = stringValue ().equals (((IRI ) o ).stringValue ());
163+ if (equals && revision .equals (((LmdbIRI ) o ).revision )) {
164+ internalID = ((LmdbIRI ) o ).internalID ;
165+ }
166+ return equals ;
167+ }
168+
126169 LmdbIRI otherLmdbURI = (LmdbIRI ) o ;
127170
128- if (otherLmdbURI .internalID != UNKNOWN_ID && revision .equals (otherLmdbURI .revision )) {
171+ if (revision .equals (otherLmdbURI .revision )) {
172+ if (otherLmdbURI .internalID == UNKNOWN_ID ) {
173+ boolean equals = stringValue ().equals (((IRI ) o ).stringValue ());
174+ if (equals ) {
175+ otherLmdbURI .internalID = this .internalID ;
176+ }
177+ return equals ;
178+ }
179+
129180 // LmdbURI's from the same revision of the same lmdb store, with
130181 // both ID's set
131- return internalID == otherLmdbURI .internalID ;
182+ boolean equal = internalID == otherLmdbURI .internalID ;
183+ if (equal ) {
184+ if (iriString == null ) {
185+ iriString = otherLmdbURI .iriString ;
186+ localNameIdx = otherLmdbURI .localNameIdx ;
187+ } else if (otherLmdbURI .iriString == null ) {
188+ otherLmdbURI .iriString = iriString ;
189+ otherLmdbURI .localNameIdx = localNameIdx ;
190+ }
191+ }
192+ return equal ;
132193 }
133194 }
134- return super .equals (o );
195+
196+ if (!(o instanceof IRI )) {
197+ return false ;
198+ }
199+
200+ return stringValue ().equals (((IRI ) o ).stringValue ());
201+ }
202+
203+ @ Override
204+ public int hashCode () {
205+ if (this .iriString != null ) {
206+ return this .iriString .hashCode ();
207+ }
208+
209+ init ();
210+ return iriString .hashCode ();
135211 }
136212
137213 protected Object writeReplace () throws ObjectStreamException {
138214 init ();
139215 return this ;
140216 }
217+
218+ @ Override
219+ public String toString () {
220+ return stringValue ();
221+ }
222+
223+ public void setNamespaceAndIri (String namespace , String localName ) {
224+ localNameIdx = namespace .length ();
225+ this .iriString = namespace + localName ;
226+ }
141227}
0 commit comments