1 package org.eclipse.aether.internal.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.eclipse.aether.ConfigurationProperties;
28 import org.eclipse.aether.RepositorySystemSession;
29 import org.eclipse.aether.util.ConfigUtils;
30
31
32
33
34 final class PrioritizedComponents<T>
35 {
36
37 private static final String FACTORY_SUFFIX = "Factory";
38
39 private final Map<?, ?> configProps;
40
41 private final boolean useInsertionOrder;
42
43 private final List<PrioritizedComponent<T>> components;
44
45 private int firstDisabled;
46
47 PrioritizedComponents( RepositorySystemSession session )
48 {
49 this( session.getConfigProperties() );
50 }
51
52 PrioritizedComponents( Map<?, ?> configurationProperties )
53 {
54 configProps = configurationProperties;
55 useInsertionOrder =
56 ConfigUtils.getBoolean( configProps, ConfigurationProperties.DEFAULT_IMPLICIT_PRIORITIES,
57 ConfigurationProperties.IMPLICIT_PRIORITIES );
58 components = new ArrayList<PrioritizedComponent<T>>();
59 firstDisabled = 0;
60 }
61
62 public void add( T component, float priority )
63 {
64 Class<?> type = getImplClass( component );
65 int index = components.size();
66 priority = useInsertionOrder ? -index : ConfigUtils.getFloat( configProps, priority, getConfigKeys( type ) );
67 PrioritizedComponent<T> pc = new PrioritizedComponent<T>( component, type, priority, index );
68
69 if ( !useInsertionOrder )
70 {
71 index = Collections.binarySearch( components, pc );
72 if ( index < 0 )
73 {
74 index = -index - 1;
75 }
76 else
77 {
78 index++;
79 }
80 }
81 components.add( index, pc );
82
83 if ( index <= firstDisabled && !pc.isDisabled() )
84 {
85 firstDisabled++;
86 }
87 }
88
89 private static Class<?> getImplClass( Object component )
90 {
91 Class<?> type = component.getClass();
92
93 int idx = type.getName().indexOf( "$$" );
94 if ( idx >= 0 )
95 {
96 Class<?> base = type.getSuperclass();
97 if ( base != null && idx == base.getName().length() && type.getName().startsWith( base.getName() ) )
98 {
99 type = base;
100 }
101 }
102 return type;
103 }
104
105 static String[] getConfigKeys( Class<?> type )
106 {
107 List<String> keys = new ArrayList<String>();
108 keys.add( ConfigurationProperties.PREFIX_PRIORITY + type.getName() );
109 String sn = type.getSimpleName();
110 keys.add( ConfigurationProperties.PREFIX_PRIORITY + sn );
111 if ( sn.endsWith( FACTORY_SUFFIX ) )
112 {
113 keys.add( ConfigurationProperties.PREFIX_PRIORITY + sn.substring( 0, sn.length() - FACTORY_SUFFIX.length() ) );
114 }
115 return keys.toArray( new String[keys.size()] );
116 }
117
118 public boolean isEmpty()
119 {
120 return components.isEmpty();
121 }
122
123 public List<PrioritizedComponent<T>> getAll()
124 {
125 return components;
126 }
127
128 public List<PrioritizedComponent<T>> getEnabled()
129 {
130 return components.subList( 0, firstDisabled );
131 }
132
133 public void list( StringBuilder buffer )
134 {
135 for ( int i = 0; i < components.size(); i++ )
136 {
137 if ( i > 0 )
138 {
139 buffer.append( ", " );
140 }
141 PrioritizedComponent<?> component = components.get( i );
142 buffer.append( component.getType().getSimpleName() );
143 if ( component.isDisabled() )
144 {
145 buffer.append( " (disabled)" );
146 }
147 }
148 }
149
150 @Override
151 public String toString()
152 {
153 return components.toString();
154 }
155
156 }