All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glassfish.soteria.cdi.CdiProducer Maven / Gradle / Ivy

There is a newer version: 3.0.3
Show newest version
/*
 * Copyright (c) 2015, 2020 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0, which is available at
 * http://www.eclipse.org/legal/epl-2.0.
 *
 * This Source Code may also be made available under the following Secondary
 * Licenses when the conditions for such availability set forth in the
 * Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
 * version 2 with the GNU Classpath Exception, which is available at
 * https://www.gnu.org/software/classpath/license.html.
 *
 * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
 */

package org.glassfish.soteria.cdi;

import static java.util.Arrays.asList;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static java.util.Collections.unmodifiableSet;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;

import jakarta.enterprise.context.Dependent;
import jakarta.enterprise.context.spi.CreationalContext;
import jakarta.enterprise.inject.spi.Bean;
import jakarta.enterprise.inject.spi.InjectionPoint;
import jakarta.enterprise.inject.spi.PassivationCapable;

// May be replaced by CDI 2.0 bean builder API when ready.
// See http://weld.cdi-spec.org/news/2015/02/25/weld-300Alpha5/#_bean_builder_api
public class CdiProducer implements Bean, PassivationCapable {
    
    private String id = this.getClass().getName();
    private String name;
    private Class beanClass = Object.class;
    private Set types = singleton(Object.class);
    private Set qualifiers = unmodifiableSet(asSet(new DefaultAnnotationLiteral(), new AnyAnnotationLiteral()));
    private Class scope = Dependent.class;
    private Function, T> create;
    
    @Override
    public String getId() {
        return id;
    }
    
    @Override
    public String getName() {
        return name;
    }
    
    @Override
    public Class getBeanClass() {
       return beanClass;
    }
    
    @Override
    public Set getTypes() {
        return types;
    }
    
    @Override
    public Set getQualifiers() {
        return qualifiers;
    }
    
    @Override
    public Class getScope() {
        return scope;
    }
    
    @Override
    public T create(CreationalContext creationalContext) {
        return create.apply(creationalContext);
    }
    
    @Override
    public void destroy(T instance, CreationalContext creationalContext) {
    }
    
    @Override
    public Set getInjectionPoints() {
        return emptySet();
    }
    
    @Override
    public Set> getStereotypes() {
        return emptySet();
    }
    
    @Override
    public boolean isAlternative() {
        return false;
    }

    @Override
    public boolean isNullable() {
        return false;
    }
    
    protected CdiProducer active(boolean active) {
        return this;
    }
    
    protected CdiProducer name(String name) {
        this.name = name;
        return this;
    }
    
    protected CdiProducer create(Function, T> create) {
        this.create = create;
        return this;
    }
    
    protected CdiProducer beanClass(Class beanClass) {
        this.beanClass = beanClass;
        return this;
    }
    
    protected CdiProducer types(Type... types) {
        this.types = asSet(types);
        return this;
    }
    
    protected CdiProducer beanClassAndType(Class beanClass) {
        beanClass(beanClass);
        types(beanClass);
        return this;
    }
    
    protected CdiProducer qualifiers(Annotation... qualifiers) {
        this.qualifiers = asSet(qualifiers);
        return this;
    }
    
    
    protected CdiProducer scope(Class scope) {
        this.scope = scope;
        return this;
    }
    
    protected CdiProducer addToId(Object object) {
        id = id + " " + object.toString();
        return this;
    }
    
    @SafeVarargs
    protected static  Set asSet(T... a) {
        return new HashSet<>(asList(a));
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy