Skip to content

Commit 54b3bf5

Browse files
committed
Mark ontology APIs as non-null by default with JSR 305
Address various missing corner cases where null should be handled such as having a null search index.
1 parent aa6b1ad commit 54b3bf5

24 files changed

Lines changed: 131 additions & 53 deletions

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@
231231
<version>5.7.1</version>
232232
</dependency>
233233

234+
<!-- Development tools -->
235+
<dependency>
236+
<groupId>com.google.code.findbugs</groupId>
237+
<artifactId>jsr305</artifactId>
238+
<version>3.0.2</version>
239+
</dependency>
240+
234241
<!-- Testing utilities -->
235242
<dependency>
236243
<groupId>junit</groupId>

src/ubic/basecode/ontology/jena/AbstractOntologyMemoryBackedService.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616

1717
import com.hp.hpl.jena.ontology.OntModel;
1818

19+
import javax.annotation.Nullable;
20+
import java.io.InputStream;
21+
1922
/**
2023
* This class has some stuff that's specific to in-memory ontologies. Unlike database backed ontologies we don't use a
2124
* pool keeping only one instance of model in memory.
@@ -25,7 +28,12 @@
2528
public abstract class AbstractOntologyMemoryBackedService extends AbstractOntologyService {
2629

2730
@Override
28-
protected synchronized OntModel loadModel() {
31+
protected OntModel loadModel() {
2932
return OntologyLoader.loadMemoryModel( this.getOntologyUrl(), this.getOntologyName() );
3033
}
34+
35+
@Override
36+
protected OntModel loadModelFromStream( InputStream is ) {
37+
return OntologyLoader.loadMemoryModel( is, this.getOntologyUrl() );
38+
}
3139
}

src/ubic/basecode/ontology/jena/AbstractOntologyResource.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package ubic.basecode.ontology.jena;
2020

2121
import com.hp.hpl.jena.ontology.OntResource;
22-
import com.hp.hpl.jena.rdf.model.Resource;
2322
import com.hp.hpl.jena.vocabulary.OWL2;
2423
import org.slf4j.Logger;
2524
import org.slf4j.LoggerFactory;

src/ubic/basecode/ontology/jena/AbstractOntologyService.java

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import ubic.basecode.ontology.search.OntologySearchException;
4444
import ubic.basecode.util.Configuration;
4545

46+
import javax.annotation.Nullable;
4647
import java.io.InputStream;
4748
import java.util.*;
4849
import java.util.concurrent.locks.Lock;
@@ -82,6 +83,7 @@ public abstract class AbstractOntologyService implements OntologyService {
8283
private OntModel model;
8384
private Map<String, String> alternativeIDs;
8485

86+
@Nullable
8587
private SearchIndex index;
8688

8789
private Set<Restriction> additionalRestrictions;
@@ -96,7 +98,7 @@ public void initialize( InputStream stream, boolean forceIndexing ) {
9698
initialize( stream, true, forceIndexing );
9799
}
98100

99-
private void initialize( InputStream stream, boolean forceLoad, boolean forceIndexing ) {
101+
private void initialize( @Nullable InputStream stream, boolean forceLoad, boolean forceIndexing ) {
100102
if ( !forceLoad && isInitialized ) {
101103
log.warn( "{} is already loaded, and force=false, not restarting", this );
102104
return;
@@ -149,8 +151,8 @@ private void initialize( InputStream stream, boolean forceLoad, boolean forceInd
149151
.toSet();
150152

151153
//Checks if the current ontology has changed since it was last loaded.
152-
boolean changed = OntologyLoader.hasChanged( cacheName );
153-
boolean indexExists = OntologyIndexer.getSubjectIndex( cacheName ) != null;
154+
boolean changed = cacheName == null || OntologyLoader.hasChanged( cacheName );
155+
boolean indexExists = cacheName != null && OntologyIndexer.getSubjectIndex( cacheName ) != null;
154156
boolean forceReindexing = forceLoad && forceIndexing;
155157

156158
/*
@@ -162,7 +164,11 @@ private void initialize( InputStream stream, boolean forceLoad, boolean forceInd
162164
if ( checkIfInterrupted() )
163165
return;
164166

165-
index = OntologyIndexer.indexOntology( cacheName, model, force );
167+
if ( cacheName != null ) {
168+
index = OntologyIndexer.indexOntology( cacheName, model, force );
169+
} else {
170+
index = null;
171+
}
166172

167173
// if interrupted, we don't need to replace the model and clear the *old* cache
168174
if ( checkIfInterrupted() )
@@ -175,8 +181,10 @@ private void initialize( InputStream stream, boolean forceLoad, boolean forceInd
175181
this.additionalRestrictions = additionalRestrictions;
176182
this.index = index;
177183
this.isInitialized = true;
178-
// now that the terms have been replaced, we can clear old caches
179-
OntologyLoader.deleteOldCache( cacheName );
184+
if ( cacheName != null ) {
185+
// now that the terms have been replaced, we can clear old caches
186+
OntologyLoader.deleteOldCache( cacheName );
187+
}
180188
} finally {
181189
lock.unlock();
182190
}
@@ -318,7 +326,6 @@ public Set<String> getAllURIs() {
318326

319327
@Override
320328
public OntologyResource getResource( String uri ) {
321-
if ( uri == null ) return null;
322329
Lock lock = rwLock.readLock();
323330
try {
324331
lock.lock();
@@ -348,7 +355,6 @@ public OntologyResource getResource( String uri ) {
348355

349356
@Override
350357
public OntologyTerm getTerm( String uri ) {
351-
if ( uri == null ) throw new IllegalArgumentException( "URI cannot be null" );
352358
Lock lock = rwLock.readLock();
353359
try {
354360
lock.lock();
@@ -521,16 +527,25 @@ public void waitForInitializationThread() throws InterruptedException {
521527
/**
522528
* Load a model from a given input stream.
523529
*/
524-
protected OntModel loadModelFromStream( InputStream is ) {
525-
return OntologyLoader.loadMemoryModel( is, this.getOntologyUrl() );
526-
}
530+
protected abstract OntModel loadModelFromStream( InputStream stream );
527531

532+
/**
533+
* A name for caching this ontology, or null to disable caching.
534+
* <p>
535+
* Note that if null is returned, the ontology will not have full-text search capabilities.
536+
*/
537+
@Nullable
528538
protected String getCacheName() {
529539
return getOntologyName();
530540
}
531541

532542
@Override
533543
public void index( boolean force ) {
544+
String cacheName = getCacheName();
545+
if ( cacheName == null ) {
546+
log.warn( "This ontology does not support indexing; assign a cache name to be used." );
547+
return;
548+
}
534549
SearchIndex index;
535550
Lock lock = rwLock.readLock();
536551
try {

src/ubic/basecode/ontology/jena/AnnotationPropertyImpl.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
*/
1919
package ubic.basecode.ontology.jena;
2020

21-
import com.hp.hpl.jena.ontology.OntProperty;
2221
import com.hp.hpl.jena.rdf.model.RDFNode;
2322
import com.hp.hpl.jena.rdf.model.Resource;
2423
import com.hp.hpl.jena.rdf.model.Statement;
25-
import com.hp.hpl.jena.rdf.model.impl.PropertyImpl;
2624
import com.hp.hpl.jena.vocabulary.RDFS;
2725
import ubic.basecode.ontology.jena.vocabulary.OBO;
2826
import ubic.basecode.ontology.model.AnnotationProperty;
2927

28+
import javax.annotation.Nullable;
29+
3030
/**
3131
* Note that this is a concrete instance of the annotation.
3232
*
@@ -43,7 +43,7 @@ public class AnnotationPropertyImpl extends AbstractOntologyResource implements
4343
* @param source ontology this relates to.
4444
* @param object of the statement
4545
*/
46-
public AnnotationPropertyImpl( com.hp.hpl.jena.ontology.AnnotationProperty prop, RDFNode object ) {
46+
public AnnotationPropertyImpl( com.hp.hpl.jena.ontology.AnnotationProperty prop, @Nullable RDFNode object ) {
4747
super( prop );
4848
this.property = prop;
4949

@@ -65,7 +65,7 @@ public AnnotationPropertyImpl( com.hp.hpl.jena.ontology.AnnotationProperty prop,
6565
}
6666

6767
@Override
68-
public boolean equals( Object obj ) {
68+
public boolean equals( @Nullable Object obj ) {
6969
if ( this == obj ) return true;
7070
if ( obj == null ) return false;
7171
if ( getClass() != obj.getClass() ) return false;

src/ubic/basecode/ontology/jena/DatatypePropertyImpl.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ class DatatypePropertyImpl extends OntologyPropertyImpl implements DatatypePrope
3232

3333
private final Class<?> type;
3434

35-
private final com.hp.hpl.jena.ontology.DatatypeProperty resource;
36-
3735
public DatatypePropertyImpl( com.hp.hpl.jena.ontology.DatatypeProperty resource ) {
3836
super( resource );
39-
this.resource = resource;
4037
this.type = PropertyFactory.convertType( resource );
4138
}
4239

src/ubic/basecode/ontology/jena/JenaUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99
import com.hp.hpl.jena.util.iterator.UniqueExtendedIterator;
1010
import org.apache.commons.lang3.time.StopWatch;
1111

12+
import javax.annotation.Nullable;
1213
import java.util.*;
1314
import java.util.function.Predicate;
1415

1516
public class JenaUtils {
1617

17-
public static Collection<OntClass> getParents( OntModel model, Collection<OntClass> ontClasses, boolean direct, Set<Restriction> additionalRestrictions ) {
18+
public static Collection<OntClass> getParents( OntModel model, Collection<OntClass> ontClasses, boolean direct, @Nullable Set<Restriction> additionalRestrictions ) {
1819
if ( ontClasses.isEmpty() ) {
1920
return Collections.emptySet();
2021
}
@@ -58,7 +59,7 @@ public static Collection<OntClass> getParents( OntModel model, Collection<OntCla
5859
return result;
5960
}
6061

61-
public static Collection<OntClass> getChildren( OntModel model, Collection<OntClass> terms, boolean direct, Set<Restriction> additionalRestrictions ) {
62+
public static Collection<OntClass> getChildren( OntModel model, Collection<OntClass> terms, boolean direct, @Nullable Set<Restriction> additionalRestrictions ) {
6263
if ( terms.isEmpty() ) {
6364
return Collections.emptySet();
6465
}

src/ubic/basecode/ontology/jena/ObjectPropertyImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ObjectPropertyImpl( ObjectProperty resource, Set<Restriction> additionalR
5151
@Override
5252
public Collection<OntologyTerm> getRange() {
5353
ExtendedIterator<? extends OntResource> iterator = resource.listRange();
54-
Collection<OntologyTerm> result = new HashSet<OntologyTerm>();
54+
Collection<OntologyTerm> result = new HashSet<>();
5555
while ( iterator.hasNext() ) {
5656
OntResource r = iterator.next();
5757
if ( r.isClass() ) {

src/ubic/basecode/ontology/jena/OntologyCardinalityRestrictionImpl.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package ubic.basecode.ontology.jena;
2020

2121
import com.hp.hpl.jena.ontology.Restriction;
22-
import com.hp.hpl.jena.rdf.model.Property;
2322
import ubic.basecode.ontology.model.OntologyCardinalityRestriction;
2423

2524
import java.util.Set;

src/ubic/basecode/ontology/jena/OntologyLoader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.slf4j.LoggerFactory;
3131
import ubic.basecode.util.Configuration;
3232

33+
import javax.annotation.Nullable;
3334
import java.io.*;
3435
import java.net.HttpURLConnection;
3536
import java.net.URL;
@@ -77,7 +78,7 @@ public static OntModel loadMemoryModel( String url ) {
7778
* @param url a URL where the OWL file is stored
7879
* @param cacheName unique name of this ontology, will be used to load from disk in case of failed url connection
7980
*/
80-
public static OntModel loadMemoryModel( String url, String cacheName ) {
81+
public static OntModel loadMemoryModel( String url, @Nullable String cacheName ) {
8182
StopWatch timer = new StopWatch();
8283
timer.start();
8384
OntModel model = getMemoryModel( url );

0 commit comments

Comments
 (0)