7426b1cb7330750fe8e45786c7289de052eab943
[idea/community.git] / platform / analysis-api / src / com / intellij / psi / WeighingService.java
1 // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.psi;
3
4 import com.intellij.openapi.util.Computable;
5 import com.intellij.openapi.util.Key;
6 import com.intellij.openapi.util.KeyedExtensionCollector;
7 import org.jetbrains.annotations.NotNull;
8 import org.jetbrains.annotations.Nullable;
9
10 import java.util.List;
11
12 /**
13  * @author peter
14  */
15 public class WeighingService {
16   private static final KeyedExtensionCollector<Weigher, Key> COLLECTOR = new KeyedExtensionCollector<>("com.intellij.weigher");
17
18   private WeighingService() { }
19
20   @NotNull
21   public static <T, Loc> WeighingComparable<T, Loc> weigh(Key<? extends Weigher<T, Loc>> key, T element, @Nullable Loc location) {
22     return weigh(key, new Computable.PredefinedValueComputable<>(element), location);
23   }
24
25   @NotNull
26   public static <T, Loc> WeighingComparable<T, Loc> weigh(Key<? extends Weigher<T, Loc>> key,
27                                                           Computable<? extends T> element,
28                                                           @Nullable Loc location) {
29     @SuppressWarnings("unchecked") Weigher<T, Loc>[] array = getWeighers(key).toArray(new Weigher[0]);
30     return new WeighingComparable<>(element, location, array);
31   }
32
33   public static <T, Loc> List<Weigher> getWeighers(Key<? extends Weigher<T, Loc>> key) {
34     return COLLECTOR.forKey(key);
35   }
36 }