Home > Net >  Java8 : Can't get value of an Optional.of(String) return type
Java8 : Can't get value of an Optional.of(String) return type

Time:12-17

In the context of using the OWLAPI 4.0 API, this following line of code: ontologyIRI = IRI.create(o.getOntologyID().getOntologyIRI().toString());

returns the following string : "Optional.of(http://www.indytion.com/music/composition)".

What I need is the sole string "http://www.indytion.com/music/composition".

I tried to declare ontologyIRI as Optional and use .get() method, .orElse(), etc. to no avail. I still have the returned string that includes the 'optional.of()' part.

My question is : How could I get the internal string?

Thank you very much for your help.

Edit : The full code the method

    private void LoadOntology(String ontologyPath)
    {
        OWLOntologyManager man = OWLManager.createOWLOntologyManager();
        OWLOntology o;

        File ontologyFile = new File(ontologyPath);
        Optional<IRI> ontologyIRI;
        try {

            o = man.loadOntologyFromOntologyDocument(ontologyFile);
            ontologyIRI = Optional.of(IRI.create(String.valueOf(o.getOntologyID().getOntologyIRI()).toString()));
            System.out.println("Ontology IRI is: "   ontologyIRI.get());
        } catch (OWLOntologyCreationException e) {
            e.printStackTrace();
        }
    }

The System.out.println() returns exactly this string:
"Ontology IRI = Optional.of(http://www.indytion.com/music/composition)"

CodePudding user response:

Use .get() instead of toString()

//Returns 'Optional[example]'
Optional.of("example").toString();
    
//Returns 'example'
Optional.of("example").get();

CodePudding user response:

Short answer: Replace

Optional.of(IRI.create(String.valueOf(o.getOntologyID().getOntologyIRI()).toString()));

with

o.getOntologyID().getOntologyIRI().get();

Longer answer: you're doing an awful lot of back-and forth that's pointless at best and actively harmful in some cases:

In no particular order:

  • others have already commented that IRI instances are immutable, so creating a new one from an existing one is kind of pointless (if harmless).
  • calling Optional.of() if you don't intend to actually return an Optional is almost always a bad idea.
  • String.valueOf() is used to get a string-representation of some value and is usually most useful for debugging, but should not be relied on to fully round-trip everything about an object (the same applies to toString().

So basically what you're left with is this:

  1. o.getOntologyID().getOntologyIRI() gives you an Optional<IRI>
  2. you want an IRI.
  3. Optional::get returns the value contained in the optional, if one exists, so you simply need to call get()

If, however the Optional is empty (i.e. there is no underlying value) then get() will throw a NoSuchElementException. This might or might not be what you want. To work around this either call isPresent() before calling get() to check if a value exists or use any of the multitude of other accessor methods, some of which have "built-in checks" in a way.

  • Related