Lines 44-51
Link Here
|
44 |
|
44 |
|
45 |
import java.util.ArrayList; |
45 |
import java.util.ArrayList; |
46 |
import java.util.Collections; |
46 |
import java.util.Collections; |
|
|
47 |
import java.util.Comparator; |
47 |
import java.util.List; |
48 |
import java.util.List; |
48 |
import net.java.html.BrwsrCtx; |
49 |
import net.java.html.BrwsrCtx; |
|
|
50 |
import org.netbeans.html.context.spi.Contexts; |
49 |
|
51 |
|
50 |
/** Implementation detail. Holds list of technologies for particular |
52 |
/** Implementation detail. Holds list of technologies for particular |
51 |
* {@link BrwsrCtx}. |
53 |
* {@link BrwsrCtx}. |
Lines 54-66
Link Here
|
54 |
*/ |
56 |
*/ |
55 |
public final class CtxImpl { |
57 |
public final class CtxImpl { |
56 |
private final List<Bind<?>> techs; |
58 |
private final List<Bind<?>> techs; |
|
|
59 |
private final Object[] context; |
57 |
|
60 |
|
58 |
public CtxImpl() { |
61 |
public CtxImpl(Object[] context) { |
59 |
techs = new ArrayList<Bind<?>>(); |
62 |
this(context, new ArrayList<Bind<?>>()); |
60 |
} |
63 |
} |
61 |
|
64 |
|
62 |
private CtxImpl(List<Bind<?>> techs) { |
65 |
private CtxImpl(Object[] context, List<Bind<?>> techs) { |
63 |
this.techs = techs; |
66 |
this.techs = techs; |
|
|
67 |
this.context = context; |
64 |
} |
68 |
} |
65 |
|
69 |
|
66 |
public static <Tech> Tech find(BrwsrCtx context, Class<Tech> technology) { |
70 |
public static <Tech> Tech find(BrwsrCtx context, Class<Tech> technology) { |
Lines 74-82
Link Here
|
74 |
} |
78 |
} |
75 |
|
79 |
|
76 |
public BrwsrCtx build() { |
80 |
public BrwsrCtx build() { |
77 |
Collections.sort(techs); |
81 |
Collections.sort(techs, new BindCompare()); |
78 |
final List<Bind<?>> arr = Collections.unmodifiableList(techs); |
82 |
final List<Bind<?>> arr = Collections.unmodifiableList(techs); |
79 |
CtxImpl impl = new CtxImpl(arr); |
83 |
CtxImpl impl = new CtxImpl(context, arr); |
80 |
BrwsrCtx ctx = CtxAccssr.getDefault().newContext(impl); |
84 |
BrwsrCtx ctx = CtxAccssr.getDefault().newContext(impl); |
81 |
return ctx; |
85 |
return ctx; |
82 |
} |
86 |
} |
Lines 85-91
Link Here
|
85 |
techs.add(new Bind<Tech>(type, impl, priority)); |
89 |
techs.add(new Bind<Tech>(type, impl, priority)); |
86 |
} |
90 |
} |
87 |
|
91 |
|
88 |
private static final class Bind<Tech> implements Comparable<Bind<?>> { |
92 |
private static final class Bind<Tech> { |
89 |
private final Class<Tech> clazz; |
93 |
private final Class<Tech> clazz; |
90 |
private final Tech impl; |
94 |
private final Tech impl; |
91 |
private final int priority; |
95 |
private final int priority; |
Lines 97-112
Link Here
|
97 |
} |
101 |
} |
98 |
|
102 |
|
99 |
@Override |
103 |
@Override |
100 |
public int compareTo(Bind<?> o) { |
|
|
101 |
if (priority != o.priority) { |
102 |
return priority - o.priority; |
103 |
} |
104 |
return clazz.getName().compareTo(o.clazz.getName()); |
105 |
} |
106 |
|
107 |
@Override |
108 |
public String toString() { |
104 |
public String toString() { |
109 |
return "Bind{" + "clazz=" + clazz + "@" + clazz.getClassLoader() + ", impl=" + impl + ", priority=" + priority + '}'; |
105 |
return "Bind{" + "clazz=" + clazz + "@" + clazz.getClassLoader() + ", impl=" + impl + ", priority=" + priority + '}'; |
110 |
} |
106 |
} |
111 |
} |
107 |
} |
|
|
108 |
|
109 |
private final class BindCompare implements Comparator<Bind<?>> { |
110 |
boolean isPrefered(Bind<?> b) { |
111 |
final Class<?> implClazz = b.impl.getClass(); |
112 |
Contexts.TechnologyId id = implClazz.getAnnotation(Contexts.TechnologyId.class); |
113 |
if (id == null) { |
114 |
return false; |
115 |
} |
116 |
for (String v : id.value()) { |
117 |
for (Object c : context) { |
118 |
if (v.equals(c)) { |
119 |
return true; |
120 |
} |
121 |
} |
122 |
} |
123 |
return false; |
124 |
} |
125 |
|
126 |
@Override |
127 |
public int compare(Bind<?> o1, Bind<?> o2) { |
128 |
boolean p1 = isPrefered(o1); |
129 |
boolean p2 = isPrefered(o2); |
130 |
if (p1 != p2) { |
131 |
return p1 ? -1 : 1; |
132 |
} |
133 |
if (o1.priority != o2.priority) { |
134 |
return o1.priority - o2.priority; |
135 |
} |
136 |
return o1.clazz.getName().compareTo(o2.clazz.getName()); |
137 |
} |
138 |
} // end of BindCompare |
112 |
} |
139 |
} |