Line data Source code
1 : // Copyright (C) 2012 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.extensions.registration; 16 : 17 : import com.google.common.collect.ImmutableList; 18 : import com.google.inject.Binding; 19 : import com.google.inject.Inject; 20 : import com.google.inject.Injector; 21 : import com.google.inject.Provider; 22 : import com.google.inject.TypeLiteral; 23 : import java.util.ArrayList; 24 : import java.util.List; 25 : import java.util.concurrent.atomic.AtomicReference; 26 : 27 : class DynamicSetProvider<T> implements Provider<DynamicSet<T>> { 28 : private final TypeLiteral<T> type; 29 : 30 : @Inject private Injector injector; 31 : 32 153 : DynamicSetProvider(TypeLiteral<T> type) { 33 153 : this.type = type; 34 153 : } 35 : 36 : @Override 37 : public DynamicSet<T> get() { 38 152 : return new DynamicSet<>(find(injector, type)); 39 : } 40 : 41 : private static <T> ImmutableList<AtomicReference<Extension<T>>> find( 42 : Injector src, TypeLiteral<T> type) { 43 152 : List<Binding<T>> bindings = src.findBindingsByType(type); 44 152 : int cnt = bindings != null ? bindings.size() : 0; 45 152 : if (cnt == 0) { 46 152 : return ImmutableList.of(); 47 : } 48 152 : List<AtomicReference<Extension<T>>> r = new ArrayList<>(cnt); 49 152 : for (Binding<T> b : bindings) { 50 152 : if (b.getKey().getAnnotation() != null) { 51 152 : r.add(new AtomicReference<>(new Extension<>(PluginName.GERRIT, b.getProvider()))); 52 : } 53 152 : } 54 152 : return ImmutableList.copyOf(r); 55 : } 56 : }