0

I have a class called - SparqlResource.java and in the class I am instantiating four objects like this-

public static final SparqlResource MARK_SIMPLE_TYPE = new SparqlResource("ldmext/MarkSimpleType.rq");
public static final SparqlResource FORTRESS_HAS_ENVOY = new SparqlResource("ldmext/FortressHasEnvoy.rq");
public static final SparqlResource FORTRESS_HAS_GUARD = new SparqlResource("ldmext/FortressHasGuard.rq");
public static final SparqlResource FORTRESS_HAS_PORT = new SparqlResource("ldmext/FortressHasPort.rq");

Now from another class - JenaLanguageConstructor.java, I am referencing these objects like this-

runOneQuery(SparqlResource.MARK_SIMPLE_TYPE, true);
runOneQuery(SparqlResource.FORTRESS_HAS_ENVOY, true);
runOneQuery(SparqlResource.FORTRESS_HAS_GUARD, true);
runOneQuery(SparqlResource.FORTRESS_HAS_PORT, true);

Now my question is is there any way I can use enums to achieve this, if so then can any one please give me a sample code which I can use to create the enum?

1

2 Answers 2

1
public enum SPARQLENUM {
    MARK_SIMPLE_TYPE("ldmext/MarkSimpleType.rq") ,
    FORTRESS_HAS_ENVOY("ldmext/FortressHasEnvoy.rq") ,
    FORTRESS_HAS_GUARD("ldmext/FortressHasGuard.rq") ,
    FORTRESS_HAS_PORT("ldmext/FortressHasPort.rq");

    private String value;

    private SPARQLENUM(String value) {
        this.value = value;
    }


    public String getValue(){
        return value;
    }


}

And you can call it this way:

SPARQLENUM.FORTRESS_HAS_ENVOY.getValue()

EDITED

If you need the SparqlResource object, you can create the enum this way:

public enum SPARQLENUM {
    MARK_SIMPLE_TYPE(new SparqlResource("ldmext/MarkSimpleType.rq")) ,
    FORTRESS_HAS_ENVOY(new SparqlResource("ldmext/FortressHasEnvoy.rq")) ,
    FORTRESS_HAS_GUARD(new SparqlResource("ldmext/FortressHasGuard.rq")) ,
    FORTRESS_HAS_PORT(new SparqlResource("ldmext/FortressHasPort.rq"));

    private SparqlResource value;

    private SPARQLENUM(SparqlResource value) {
        this.value = value;
    }


    public SparqlResource getValue(){
        return value;
    }


}
Sign up to request clarification or add additional context in comments.

Comments

1

Well, creating an enum wouldn't be that hard:

enum MyEnum {
  VALUE1("name 1"),
  VALUE2("name 2");

  private String name;

  private MyEnum(String n) {
    name = n;
  }

  //whatever else you need
}

2 Comments

hi, can you please let me how can I call one of the enums from the other class JenaLanguageConstructor.java.
@SomSarkar you use it just as any other class, e.g. runOneQuery( MyEnum e, boolean flag) and then runOneQuery(VALUE1, true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.