/
ItemCategoryConfigurator.java
64 lines (55 loc) · 2.02 KB
/
ItemCategoryConfigurator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package jenkins.model.ItemCategory;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.ExtensionPoint;
import hudson.model.TopLevelItemDescriptor;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.logging.Logger;
/**
* A mapper of {@link ItemCategory}s to {@link hudson.model.Item}s.
*/
public abstract class ItemCategoryConfigurator implements ExtensionPoint {
public static final Logger LOGGER = Logger.getLogger(ItemCategoryConfigurator.class.getName());
/**
* Provides the category for the requested item or null if this mapper doesn't have one.
*
* @param descriptor the item to categorise
*
* @return the category or null
*/
@CheckForNull
protected abstract ItemCategory getCategoryFor(@Nonnull TopLevelItemDescriptor descriptor);
/**
* Finds the category specified by the first mapper.
* If none can be found {@link ItemCategory.Default} is returned.
*
* @param descriptor the item to categorise.
*
* @return the category
*/
@Nonnull
public static ItemCategory getCategory(@Nonnull TopLevelItemDescriptor descriptor) {
for (ItemCategoryConfigurator m : all()) {
ItemCategory category = m.getCategoryFor(descriptor);
if (category != null) {
return category;
}
}
throw new IllegalStateException();
}
public static Collection<ItemCategoryConfigurator> all() {
return ExtensionList.lookup(ItemCategoryConfigurator.class);
}
/**
* Mapper implementation with the lowest ordinal that simply returns {@link ItemCategory.Default}.
*/
@Extension(ordinal = Integer.MIN_VALUE)
public static final class DefaultConfigurator extends ItemCategoryConfigurator {
@Override
public ItemCategory getCategoryFor(@Nonnull TopLevelItemDescriptor descriptor) {
return ExtensionList.lookup(ItemCategory.Default.class).iterator().next();
}
}
}