Jason Qlueses
Jason Qlueses

Reputation: 43

Check if contains in sub-classes

What I'm trying to accomplish is set the #example field to the current example sub-class instance wrapped in an optional, if the contains returns false on all sub-classes i'd like to return an Optional#empty

I have an abstract class.

public abstract class Example {

public abstract boolean contains();

}

I then have some sub-classes.

public final class Example1 extends Example {

@Override
public boolean contains() {
return true;
}

}

public final class Example2 extends Example {

@Override
public boolean contains() {
return true;
}

}

and I have a field to determine what example currently is present.

private final Optional<Example> example;

is there a way to loop through the sub-classes and return the #contains condition?

something like

Optional<Class> clazz = Some sort of loop to check if the #contains condition returns true;

then set my example field

if(clazz.isPresent()) {
setExample((Example) clazz.get());
}

this is all pseudo-code, this will not work. If you guys know how to do this and could explain me that'd mean a lot :)

Upvotes: 3

Views: 74

Answers (3)

Greg Hilston
Greg Hilston

Reputation: 2424

Jason and I had a discussion in the comments section of his question.

Jason, I believe you are describing the functionality that is solved the use of the Decorator pattern.

Essentially, you'll have a required object of the class, which in your provided example is called "Example". The other classes that you called "Example1" and "Example2" become the decorators, or optional wrappers on the required object of the class.

I believe this answers your question and is what known as a design pattern.

A good tutorial is here: http://www.tutorialspoint.com/design_pattern/decorator_pattern.htm

Upvotes: 0

Joop Eggen
Joop Eggen

Reputation: 109613

It depends on the use-case. For singleton, enumeratable (listable) instances use an enum, where every enum constant can override.

public enum Example {
    EXAMPLE1() {

        @Override
        public abstract boolean contains() {
            return true;
        }
    },
    EXAMPLE2() {
        ...
    },
    EXAMPLE3() {
        ...
    };

    public abstract boolean contains();
}

Or in java 8 pass a function to the constructor:

public enum Example {
    EXAMPLE1(() -> true),
    EXAMPLE2(() -> false),
    EXAMPLE3(() -> System.currentMillis() % 2 == 0);

    private final Supplier<Boolean> cont;

    private Example(Supplier<Boolean> cont) {
        this.cont = cont;
    }   

    public boolean contains() {
        return cont.get();
    }
}

Upvotes: 0

Vale H
Vale H

Reputation: 491

I'm not sure I fully understand your question, but it seems like you are asking a standard inheritance question.

Say I have a list of examples. Set example to the first one for which contains() returns true:

List<Example> examples = Arrays.asList(new Example1(), new Example2(), ...);
for (Example ex: examples) {
    if (ex.contains()) {
        example = ex;
        break;
    }
}

If this is not what you were asking for, maybe try to clarify your question a bit more.

Upvotes: 2

Related Questions