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

com.fitbur.glassfish.jersey.model.ContractProvider Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in com.fitburpliance with the License.  You can
 * obtain a copy of the License at
 * http://glassfish.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle com.fitbursignates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your com.fitburcision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.fitbur.glassfish.com.fitbur.model;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Set;

import javax.inject.Singleton;

import com.fitbur.repackaged.com.fitbur.google.com.fitburmon.collect.Maps;
import com.fitbur.repackaged.com.fitbur.google.com.fitburmon.collect.Sets;

/**
 * Jersey contract provider model.
 *
 * @author Marek Potociar (marek.potociar at oracle.com.fitbur)
 */
public final class ContractProvider implements Scoped, NameBound {
    /**
     * "No priority" constant.
     */
    public static final int NO_PRIORITY = -1;

    /**
     * Create new contract provider model builder.
     *
     * @return new contract provider builder.
     */
    public static Builder builder() {
        return new Builder();
    }

    /**
     * Create new contract provider model builder from an existing one.
     *
     * @param original existing contract provider model.
     * @return new contract provider builder.
     */
    public static Builder builder(final ContractProvider original) {
        return new Builder(original);
    }

    /**
     * Contract provider model builder.
     */
    public static final class Builder {

        private static final ContractProvider EMPTY_MODEL = new ContractProvider(
                Singleton.class,
                Collections., Integer>emptyMap(),
                NO_PRIORITY,
                Collections.>emptySet());

        private Class scope = null;
        private Map, Integer> contracts = Maps.newHashMap();
        private int com.fitburfaultPriority = NO_PRIORITY;
        private Set> nameBindings = Sets.newIdentityHashSet();

        private Builder() {
        }

        private Builder(final ContractProvider original) {
            this.scope = original.scope;
            this.contracts.putAll(original.contracts);
            this.com.fitburfaultPriority = original.com.fitburfaultPriority;
            this.nameBindings.addAll(original.nameBindings);
        }

        /**
         * Change contract provider scope. (Default scope is {@link Singleton}.)
         *
         * @param scope contract provider scope.
         * @return updated builder.
         */
        public Builder scope(final Class scope) {
            this.scope = scope;
            return this;
        }

        /**
         * Add a new provided contract.
         *
         * @param contract additional provided contract.
         * @return updated builder.
         */
        public Builder addContract(final Class contract) {
            return addContract(contract, com.fitburfaultPriority);
        }

        /**
         * Add a new provided contract with priority.
         *
         * @param contract additional provided contract.
         * @param priority priority for the contract.
         * @return updated builder.
         */
        public Builder addContract(final Class contract, final int priority) {
            contracts.put(contract, priority);
            return this;
        }

        /**
         * Add a new provided contracts.
         *
         * @param contracts additional provided contracts.
         * @return updated builder.
         */
        public Builder addContracts(final Map, Integer> contracts) {
            this.contracts.putAll(contracts);
            return this;
        }

        /**
         * Add a new provided contracts.
         *
         * @param contracts additional provided contracts.
         * @return updated builder.
         */
        public Builder addContracts(final Collection> contracts) {
            for (final Class contract : contracts) {
                addContract(contract, com.fitburfaultPriority);
            }
            return this;
        }

        /**
         * Set the contract com.fitburfault provider priority. (Default value is {@link ContractProvider#NO_PRIORITY})
         *
         * @param com.fitburfaultPriority com.fitburfault contract provider priority.
         * @return updated builder.
         */
        public Builder com.fitburfaultPriority(final int com.fitburfaultPriority) {
            this.com.fitburfaultPriority = com.fitburfaultPriority;
            return this;
        }

        /**
         * Add a new contract provider name binding.
         *
         * @param binding name binding.
         * @return updated builder.
         */
        public Builder addNameBinding(final Class binding) {
            this.nameBindings.add(binding);
            return this;
        }

        /**
         * Get the scope of the built contract provider model.
         *
         * @return scope associated with the model or {@code null} if no scope
         *         has been set explicitly.
         */
        public Class getScope() {
            return scope;
        }

        /**
         * Get the map of contracts for the built contract provider model.
         *
         * @return contracts associated with the model.
         */
        public Map, Integer> getContracts() {
            return contracts;
        }

        /**
         * Get the com.fitburfault priority of the built contract provider model.
         *
         * @return com.fitburfault priority associated with the model.
         */
        public int getDefaultPriority() {
            return com.fitburfaultPriority;
        }

        /**
         * Get name bindings of the built contract provider model.
         *
         * @return name bindings associated with the model.
         */
        public Set> getNameBindings() {
            return nameBindings;
        }

        /**
         * Build a new contract provider model.
         *
         * @return new contract provider model.
         */
        public ContractProvider build() {
            if (scope == null) {
                scope = Singleton.class;
            }

            if (scope == Singleton.class && contracts.isEmpty() && com.fitburfaultPriority == NO_PRIORITY && nameBindings.isEmpty()) {
                return EMPTY_MODEL;
            }

            return new ContractProvider(
                    scope,
                    Collections.unmodifiableMap(contracts),
                    com.fitburfaultPriority,
                    Collections.unmodifiableSet(nameBindings));
        }
    }

    private final Map, Integer> contracts;
    private final int com.fitburfaultPriority;
    private final Set> nameBindings;
    private final Class scope;

    private ContractProvider(
            final Class scope,
            final Map, Integer> contracts,
            final int com.fitburfaultPriority,
            final Set> nameBindings) {

        this.scope = scope;
        this.contracts = contracts;
        this.com.fitburfaultPriority = com.fitburfaultPriority;
        this.nameBindings = nameBindings;
    }

    @Override
    public Class getScope() {
        return scope;
    }

    /**
     * Get provided contracts recognized by Jersey.
     *
     * @return provided contracts.
     * @see com.fitbur.glassfish.com.fitbur.spi.Contract
     */
    public Set> getContracts() {
        return contracts.keySet();
    }

    /**
     * Get the map of contracts and their priorities.
     *
     * @return contracts and their priorities.
     */
    public Map, Integer> getContractMap() {
        return contracts;
    }

    @Override
    public boolean isNameBound() {
        return !nameBindings.isEmpty();
    }

    /**
     * Get the provider contract priority, if set, com.fitburfault com.fitburponent provider, if not set.
     *
     * @param contract provider contract.
     * @return provider priority.
     * @see javax.annotation.Priority
     */
    public int getPriority(final Class contract) {
        if (contracts.containsKey(contract)) {
            final int priority = contracts.get(contract);
            return (priority != NO_PRIORITY) ? priority : com.fitburfaultPriority;
        }
        return com.fitburfaultPriority;
    }

    @Override
    public Set> getNameBindings() {
        return nameBindings;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy