Re: [picocontainer-dev] PicoContainer API enhancement

"Nick Sieger" <[email protected]>
Newsgroups gmane.comp.java.picocontainer.devel
Message-ID <[email protected]>
On 3/28/06, Jörg Schaible <[email protected]> wrote:
>
> For me this sounds as an useful add-on to pico-gems. All you need is a
> helper function like Builder.register(MPC, CM[]) that iterates over the
> array and calls mpc.registerComponent().
>
> WDYT?


That's in fact exactly what I have today, see below.  I suppose this doesn't
have to be part of the core API -- I just think it is an improvement over
the more verbose registerComponent* methods.  I tend to agree with you that
the registerComponent(CA) method is probably the only one that needs to be
in the core API.  Except that to folks who aren't familiar with Pico, the
various component adapters are not easily discovered.  To me it feels like a
nice simplification of the API to hide the complexity of some of the CAs
behind the facade of the ComponentMetadata concept.

But that's just me, I'm picky about the way code looks and feels above and
beyond whether it performs the desired function.

/Nick

public interface RegisterCallback {
    void register(MutablePicoContainer pico);
}

public class ComponentMetadata implements RegisterCallback {

    private final RegisterCallback callback;
    private final Object componentKey;

    public ComponentMetadata(final Class componentImplementation) {
        this.callback = new RegisterCallback() {
            public void register(MutablePicoContainer pico) {
                pico.registerComponentImplementation
(componentImplementation);
            }
        };
        this.componentKey = componentImplementation;
    }

    public ComponentMetadata(final Object componentKey, final Class
componentImplementation) {
        this.callback = new RegisterCallback() {
            public void register(MutablePicoContainer pico) {
                pico.registerComponentImplementation(componentKey,
componentImplementation);
            }
        };
        this.componentKey = componentKey;
    }

    public ComponentMetadata(final Object componentKey, final Class
componentImplementation, final Parameter[] componentParameters) {
        this.callback = new RegisterCallback() {
            public void register(MutablePicoContainer pico) {
                pico.registerComponentImplementation(componentKey,
componentImplementation, componentParameters);
            }
        };
        this.componentKey = componentKey;
    }

    public ComponentMetadata(final Class componentImplementation, final
Parameter[] componentParameters) {
        this.callback = new RegisterCallback() {
            public void register(MutablePicoContainer pico) {
                pico.registerComponentImplementation(componentImplementation,
componentImplementation, componentParameters);
            }
        };
        this.componentKey = componentImplementation;
    }

    public ComponentMetadata(final ComponentAdapter adapter) {
        this.callback = new RegisterCallback() {
            public void register(MutablePicoContainer pico) {
                pico.registerComponent(adapter);
            }
        };
        this.componentKey = adapter.getComponentKey();
    }

    public void register(MutablePicoContainer pico) {
        if (pico.getComponentAdapter(getComponentKey()) == null) {
            callback.register(pico);
        }
    }

    public Object getComponentKey() {
        return componentKey;
    }

    public int hashCode() {
        return componentKey.hashCode();
    }

    public boolean equals(Object o) {
        return o instanceof ComponentMetadata && ((ComponentMetadata)
o).componentKey.equals(this.componentKey);
    }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.