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

org.testifyproject.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 org.testifyproject.testifyprojectpliance 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 org.testifyproject.testifyprojectsignates 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 org.testifyproject.testifyprojectcision 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 org.testifyproject.glassfish.org.testifyproject.testifyproject.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 org.testifyproject.testifyproject.repackaged.org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.collect.Maps;
import org.testifyproject.testifyproject.repackaged.org.testifyproject.testifyproject.google.org.testifyproject.testifyprojectmon.collect.Sets;

/**
 * Jersey contract provider model.
 *
 * @author Marek Potociar (marek.potociar at oracle.org.testifyproject.testifyproject)
 */
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 org.testifyproject.testifyprojectfaultPriority = NO_PRIORITY;
        private Set> nameBindings = Sets.newIdentityHashSet();

        private Builder() {
        }

        private Builder(final ContractProvider original) {
            this.scope = original.scope;
            this.contracts.putAll(original.contracts);
            this.org.testifyproject.testifyprojectfaultPriority = original.org.testifyproject.testifyprojectfaultPriority;
            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, org.testifyproject.testifyprojectfaultPriority);
        }

        /**
         * 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, org.testifyproject.testifyprojectfaultPriority);
            }
            return this;
        }

        /**
         * Set the contract org.testifyproject.testifyprojectfault provider priority. (Default value is {@link ContractProvider#NO_PRIORITY})
         *
         * @param org.testifyproject.testifyprojectfaultPriority org.testifyproject.testifyprojectfault contract provider priority.
         * @return updated builder.
         */
        public Builder org.testifyproject.testifyprojectfaultPriority(final int org.testifyproject.testifyprojectfaultPriority) {
            this.org.testifyproject.testifyprojectfaultPriority = org.testifyproject.testifyprojectfaultPriority;
            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 org.testifyproject.testifyprojectfault priority of the built contract provider model.
         *
         * @return org.testifyproject.testifyprojectfault priority associated with the model.
         */
        public int getDefaultPriority() {
            return org.testifyproject.testifyprojectfaultPriority;
        }

        /**
         * 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() && org.testifyproject.testifyprojectfaultPriority == NO_PRIORITY && nameBindings.isEmpty()) {
                return EMPTY_MODEL;
            }

            return new ContractProvider(
                    scope,
                    Collections.unmodifiableMap(contracts),
                    org.testifyproject.testifyprojectfaultPriority,
                    Collections.unmodifiableSet(nameBindings));
        }
    }

    private final Map, Integer> contracts;
    private final int org.testifyproject.testifyprojectfaultPriority;
    private final Set> nameBindings;
    private final Class scope;

    private ContractProvider(
            final Class scope,
            final Map, Integer> contracts,
            final int org.testifyproject.testifyprojectfaultPriority,
            final Set> nameBindings) {

        this.scope = scope;
        this.contracts = contracts;
        this.org.testifyproject.testifyprojectfaultPriority = org.testifyproject.testifyprojectfaultPriority;
        this.nameBindings = nameBindings;
    }

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

    /**
     * Get provided contracts recognized by Jersey.
     *
     * @return provided contracts.
     * @see org.testifyproject.glassfish.org.testifyproject.testifyproject.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, org.testifyproject.testifyprojectfault org.testifyproject.testifyprojectponent 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 : org.testifyproject.testifyprojectfaultPriority;
        }
        return org.testifyproject.testifyprojectfaultPriority;
    }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy