how to add a method specific to builder of specific type only

12 views
Skip to first unread message

mike

unread,
Nov 26, 2021, 9:16:00 AM11/26/21
to
Hi,

I want to add a method that is specific to DeleteSubtreeBuilder .

SubtreeBuilders.createDeleteSubtree().appendOperation(OperType.DELETE)

I just cannot seem to be able to add:

appendOperation(OperType type) so it only appears in the DeteleSubtreeBuilder that SubtreeBuilders.createDeleteSubtree() returns.

All help appreciated.

br,

//mike

public final class SubtreeBuilders {

public static SubtreeBuilder<DeleteSubtree> createDeleteSubtree() {
return new DeleteSubtreeBuilder();
}


public static SubtreeBuilder<SetSubtree> createSetSubtree() {
return new SetSubtreeBuilder();
}

static class DeleteSubtreeBuilder extends AbstractSubtreeBuilder<DeleteSubtree>{

@Override
protected DeleteSubtree internalBuild() {
return new DeleteSubtreeImpl(this);
}

}

}

Eric Sosman

unread,
Nov 26, 2021, 1:09:37 PM11/26/21
to
On 11/26/2021 9:15 AM, mike wrote:
>
> I want to add a method that is specific to DeleteSubtreeBuilder .
>
> SubtreeBuilders.createDeleteSubtree().appendOperation(OperType.DELETE)
>
> I just cannot seem to be able to add:
>
> appendOperation(OperType type) so it only appears in the DeteleSubtreeBuilder that SubtreeBuilders.createDeleteSubtree() returns.

Since createDeleteSubtree() returns a SubtreeBuilder reference, the
only methods callable through that reference are those belonging to (or
inherited by) the SubtreeBuilder class. Even if the actual instance
returned happens to be a subclass with additional methods, you cannot
use a SubtreeBuilder reference to call them.

I'm not entirely sure what to suggest, since I'm not entirely sure
I understand what you're trying to do. One likely possibility, though,
is to change createDeleteSubtree() to return a DeleteSubtreeBuilder
instead of a "vanilla" SubtreeBuilder (assuming DeleteSubtreeBuilder
is a subclass of SubtreeBuilder, which isn't actually spelled out in
the code provided). If the class relationships aren't as I assume, I'm
afraid I don't know how to help.

> public final class SubtreeBuilders {
>
> public static SubtreeBuilder<DeleteSubtree> createDeleteSubtree() {
> return new DeleteSubtreeBuilder();
> }
>
>
> public static SubtreeBuilder<SetSubtree> createSetSubtree() {
> return new SetSubtreeBuilder();
> }
>
> static class DeleteSubtreeBuilder extends AbstractSubtreeBuilder<DeleteSubtree>{

(I'm guessing AbstractSubtreeBuilder is a subclass of SubtreeBuilder
or implements a SubtreeBuilder interface, but I can't be sure.)

>
> @Override
> protected DeleteSubtree internalBuild() {
> return new DeleteSubtreeImpl(this);
> }
>
> }
>
> }

--
eso...@comcast-dot-net.invalid
Look on my code, ye Hackers, and guffaw!

Arne Vajhøj

unread,
Nov 26, 2021, 7:13:35 PM11/26/21
to
On 11/26/2021 9:15 AM, mike wrote:
> I want to add a method that is specific to DeleteSubtreeBuilder .
>
> SubtreeBuilders.createDeleteSubtree().appendOperation(OperType.DELETE)
>
> I just cannot seem to be able to add:
>
> appendOperation(OperType type) so it only appears in the DeteleSubtreeBuilder that SubtreeBuilders.createDeleteSubtree() returns.

> public final class SubtreeBuilders {
>
> public static SubtreeBuilder<DeleteSubtree> createDeleteSubtree() {
> return new DeleteSubtreeBuilder();
> }
>
>
> public static SubtreeBuilder<SetSubtree> createSetSubtree() {
> return new SetSubtreeBuilder();
> }
>
> static class DeleteSubtreeBuilder extends AbstractSubtreeBuilder<DeleteSubtree>{
>
> @Override
> protected DeleteSubtree internalBuild() {
> return new DeleteSubtreeImpl(this);
> }
>
> }
>
> }

A method needs to be defined in the type returned.

So something like:

package november;

public class BuildFun {
static interface DeleteSubtree {
}
static class DeleteSubtreeImpl implements DeleteSubtree {
}
static interface SetSubtree {
}
static class SetSubtreeImpl implements SetSubtree {
}
static interface SubtreeBuilder<T> {
public T internalBuild();
}
static interface SubtreeBuilderWithAppend<T> extends SubtreeBuilder<T> {
public SubtreeBuilderWithAppend<T> appendOperation(int op);
}
static abstract class AbstractSubtreeBuilder<T> implements
SubtreeBuilder<T> {
}
static abstract class AbstractSubtreeBuilderWithAppend<T> implements
SubtreeBuilderWithAppend<T> {
}
static class DeleteSubtreeBuilder extends
AbstractSubtreeBuilderWithAppend<DeleteSubtree> {
@Override
public DeleteSubtree internalBuild() {
return new DeleteSubtreeImpl();
}
@Override
public SubtreeBuilderWithAppend<DeleteSubtree>
appendOperation(int op) {
return this;
}
}
static class SetSubtreeBuilder extends AbstractSubtreeBuilder<SetSubtree> {
@Override
public SetSubtree internalBuild() {
return new SetSubtreeImpl();
}
}
static class SubtreeBuilders {
public static SubtreeBuilderWithAppend<DeleteSubtree>
createDeleteSubtree() {
return new DeleteSubtreeBuilder();
}
public static SubtreeBuilder<SetSubtree> createSetSubtree() {
return new SetSubtreeBuilder();
}
}
public static void main(String[] args) {
SubtreeBuilders.createDeleteSubtree().appendOperation(0).internalBuild();
SubtreeBuilders.createSetSubtree().internalBuild();
}
}

Arne

PS: Do you really need such a complex type structure??


Reply all
Reply to author
Forward
0 new messages