Skip to content

Commit

Permalink
Change interfaces to make methods return an Optional instead of null
Browse files Browse the repository at this point in the history
  • Loading branch information
Julian Mendez committed Dec 25, 2015
1 parent 85ab3d6 commit e680d0b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private Map<Integer, Set<Integer>> computeDirectTypes(IntegerHierarchicalGraph h
Map<Integer, Set<Integer>> ret = new HashMap<>();
Set<Integer> individuals = getEntityManager().getEntities(IntegerEntityType.INDIVIDUAL, false);
individuals.forEach(indiv -> {
Set<Integer> subsumers = hierarchicalGraph.getParents(getEntityManager().getAuxiliaryNominal(indiv));
Set<Integer> subsumers = hierarchicalGraph.getParents(getEntityManager().getAuxiliaryNominal(indiv).get());
subsumers.forEach(elem -> {
if (getEntityManager().getAuxiliaryNominals().contains(elem)) {
throw new IllegalStateException("An individual has another individual as direct subsumer.");
Expand All @@ -179,11 +179,11 @@ private Map<Integer, Set<Integer>> computeSameIndividualMap(IntegerHierarchicalG
Set<Integer> individuals = getEntityManager().getEntities(IntegerEntityType.INDIVIDUAL, false);
individuals.forEach(indiv -> {
Set<Integer> equivalentClasses = hierarchicalGraph
.getEquivalents(getEntityManager().getAuxiliaryNominal(indiv));
.getEquivalents(getEntityManager().getAuxiliaryNominal(indiv).get());
Set<Integer> equivalents = new HashSet<>();
equivalentClasses.forEach(elem -> {
if (getEntityManager().getAuxiliaryNominals().contains(elem)) {
equivalents.add(getEntityManager().getIndividual(elem));
equivalents.add(getEntityManager().getIndividual(elem).get());
}
});
ret.put(indiv, Collections.unmodifiableSet(equivalents));
Expand Down Expand Up @@ -560,9 +560,9 @@ public boolean process() {

private void process(Integer cA, ExtensionEntry eX) {
if (eX.isImplication()) {
processImplication(cA, eX.asImplication());
processImplication(cA, eX.asImplication().get());
} else if (eX.isExistential()) {
processExistential(cA, eX.asExistential());
processExistential(cA, eX.asExistential().get());
} else {
throw new RuntimeException("Internal error: entry was not recognized " + eX);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
package de.tudresden.inf.lat.jcel.core.algorithm.cel;

import java.util.Objects;
import java.util.Optional;

/**
* This is an auxiliary class which corresponds to an existential entry in the
Expand Down Expand Up @@ -77,13 +78,13 @@ public ExistentialEntry(Integer property, Integer cls) {
}

@Override
public ExistentialEntry asExistential() {
return this;
public Optional<ExistentialEntry> asExistential() {
return Optional.of(this);
}

@Override
public ImplicationEntry asImplication() {
return null;
public Optional<ImplicationEntry> asImplication() {
return Optional.empty();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@

package de.tudresden.inf.lat.jcel.core.algorithm.cel;

import java.util.Optional;

/**
* This class models an entry for the queues. There are only two kinds of
* entries : the existential and the implication.
Expand All @@ -55,20 +57,22 @@
public interface ExtensionEntry {

/**
* Returns this entry as an existential entry.
* Returns an optional containing this entry if this is as an existential
* entry, or an empty optional otherwise.
*
* @return <code>this</code> if this is an existential entry, or
* <code>null</code> if not
* @return an optional containing this entry if this is as an existential
* entry, or an empty optional otherwise
*/
ExistentialEntry asExistential();
Optional<ExistentialEntry> asExistential();

/**
* Returns this entry as an implication entry.
* Returns an optional containing this entry if this is as an implication
* entry, or an empty optional otherwise.
*
* @return <code>this</code> if this is an implication entry, or
* <code>null</code> if not
* @return an optional containing this entry if this is as an implication
* entry, or an empty optional otherwise
*/
ImplicationEntry asImplication();
Optional<ImplicationEntry> asImplication();

/**
* Tell whether or not this is an existential entry.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

/**
Expand Down Expand Up @@ -79,13 +80,13 @@ public ImplicationEntry(Set<Integer> left, Integer right) {
}

@Override
public ExistentialEntry asExistential() {
return null;
public Optional<ExistentialEntry> asExistential() {
return Optional.empty();
}

@Override
public ImplicationEntry asImplication() {
return this;
public Optional<ImplicationEntry> asImplication() {
return Optional.of(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ private Map<Integer, Set<Integer>> computeDirectTypes(IntegerHierarchicalGraph h
Map<Integer, Set<Integer>> ret = new HashMap<>();
Set<Integer> individuals = getEntityManager().getIndividuals();
individuals.forEach(indiv -> {
Set<Integer> subsumers = hierarchicalGraph.getParents(getEntityManager().getAuxiliaryNominal(indiv));
Set<Integer> subsumers = hierarchicalGraph.getParents(getEntityManager().getAuxiliaryNominal(indiv).get());
subsumers.forEach(elem -> {
if (getEntityManager().getAuxiliaryNominals().contains(elem)) {
throw new IllegalStateException("An individual has another individual as direct subsumer.");
Expand Down Expand Up @@ -249,11 +249,11 @@ private Map<Integer, Set<Integer>> computeSameIndividualMap(IntegerHierarchicalG
Set<Integer> individuals = getEntityManager().getIndividuals();
individuals.forEach(indiv -> {
Set<Integer> equivalentClasses = hierarchicalGraph
.getEquivalents(getEntityManager().getAuxiliaryNominal(indiv));
.getEquivalents(getEntityManager().getAuxiliaryNominal(indiv).get());
Set<Integer> equivalents = new HashSet<>();
equivalentClasses.forEach(elem -> {
if (getEntityManager().getAuxiliaryNominals().contains(elem)) {
equivalents.add(getEntityManager().getIndividual(elem));
equivalents.add(getEntityManager().getIndividual(elem).get());
}
});
ret.put(indiv, Collections.unmodifiableSet(equivalents));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@

package de.tudresden.inf.lat.jcel.coreontology.datatype;

import java.util.Optional;
import java.util.Set;

/**
Expand Down Expand Up @@ -121,14 +122,17 @@ public interface IntegerEntityManager {
Set<Integer> getAuxiliaryInverseObjectProperties();

/**
* This method gives the auxiliary nominal related to a specific individual.
* Returns an optional containing the auxiliary nominal related to a
* specific individual, or an empty optional if the individual does not have
* any related auxiliary nominal.
*
* @param individual
* the individual
* @return the requested class or <code>null</code> if the individual does
* not have any related auxiliary nominal.
* @return an optional containing the auxiliary nominal related to a
* specific individual, or an empty optional if the individual does
* not have any related auxiliary nominal
*/
Integer getAuxiliaryNominal(Integer individual);
Optional<Integer> getAuxiliaryNominal(Integer individual);

/**
* Returns the set of auxiliary nominals.
Expand Down Expand Up @@ -162,14 +166,17 @@ public interface IntegerEntityManager {
Set<Integer> getEntities(IntegerEntityType type, boolean auxiliary);

/**
* This method gives the individual related to a specific auxiliary nominal.
* Returns an optional containing the individual related to a specific
* auxiliary nominal, or an empty optional if the auxiliary nominal does not
* have any related individual.
*
* @param auxNominal
* the auxiliary nominal
* @return the requested individual or <code>null</code> if the auxiliary
* nominal does not have any related individual.
* @return an optional containing the individual related to a specific
* auxiliary nominal, or an empty optional if the auxiliary nominal
* does not have any related individual.
*/
Integer getIndividual(Integer auxNominal);
Optional<Integer> getIndividual(Integer auxNominal);

/**
* Returns the set of individuals.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;

Expand Down Expand Up @@ -155,9 +156,9 @@ public Set<Integer> getAuxiliaryInverseObjectProperties() {
}

@Override
public Integer getAuxiliaryNominal(Integer individual) {
public Optional<Integer> getAuxiliaryNominal(Integer individual) {
Objects.requireNonNull(individual);
return this.auxNominalMap.get(individual);
return Optional.ofNullable(this.auxNominalMap.get(individual));
}

@Override
Expand Down Expand Up @@ -197,9 +198,9 @@ public Set<Integer> getEntities(IntegerEntityType type, boolean auxiliary) {
}

@Override
public Integer getIndividual(Integer auxNominal) {
public Optional<Integer> getIndividual(Integer auxNominal) {
Objects.requireNonNull(auxNominal);
return this.auxNominalInvMap.get(auxNominal);
return Optional.ofNullable(this.auxNominalInvMap.get(auxNominal));
}

@Override
Expand Down

0 comments on commit e680d0b

Please sign in to comment.