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

pl.chalapuk.muice.internal.Scopes Maven / Gradle / Ivy

/*
 * Copyright (C) 2013 Maciej Chałapuk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.chalapuk.muice.internal;

import javax.inject.Singleton;
import javax.inject.Provider;

import pl.chalapuk.muice.Key;
import pl.chalapuk.muice.Scope;

import com.google.common.base.Function;

/**
 * Contains scopes supported in Muice by default.
 * 
 * @author [email protected] (Maciej Chałapuk)
 */
public enum Scopes implements Scope {

    /**
     * Used when no scope is provided for a binding.
     */
    NONE {
        @Override
        public  Provider decorate(Key key, Provider unscoped) {
            return unscoped;
        }
    },

    /**
     * Bound by default to {@link Singleton} annotation.
     */
    SINGLETON {
        @Override
        public  Provider decorate(Key key, final Provider unscoped) {

            return new Provider() {
                private Function, T> mGetter = new Function, T>() {

                    @Override
                    public T apply(Provider p) {
                        final T val = p.get();
                        mGetter = new Function, T>() {

                            @Override
                            public T apply(Provider arg0) {
                                return val;
                            }
                        };
                        return val;
                    }
                };

                @Override
                public T get() {
                    return mGetter.apply(unscoped);
                }
            };
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy