+abstract class KeyedListNodeCodecContext<D extends DataObject & Identifiable<?>> extends ListNodeCodecContext<D> {
+ private static final class Ordered<D extends DataObject & Identifiable<?>> extends KeyedListNodeCodecContext<D> {
+ Ordered(final DataContainerCodecPrototype<ListSchemaNode> prototype, final Method keyMethod,
+ final IdentifiableItemCodec codec) {
+ super(prototype, keyMethod, codec);
+ }
+ }
+
+ private static final class Unordered<D extends DataObject & Identifiable<?>> extends KeyedListNodeCodecContext<D> {
+ private static final MethodType KEY_TYPE = MethodType.methodType(Object.class, DataObject.class);
+
+ private final MethodHandle keyHandle;
+
+ Unordered(final DataContainerCodecPrototype<ListSchemaNode> prototype, final Method keyMethod,
+ final IdentifiableItemCodec codec) {
+ super(prototype, keyMethod, codec);
+
+ try {
+ this.keyHandle = MethodHandles.publicLookup().unreflect(keyMethod).asType(KEY_TYPE);
+ } catch (IllegalAccessException | WrongMethodTypeException e) {
+ throw new LinkageError("Failed to acquire method " + keyMethod, e);
+ }
+ }
+
+ @Override
+ Map<?, D> fromMap(final MapNode nodes) {
+ final Collection<MapEntryNode> value = nodes.getValue();
+ final Builder<Object, D> builder = ImmutableMap.builderWithExpectedSize(value.size());
+ // FIXME: Could be this lazy transformed map?
+ for (MapEntryNode node : value) {
+ final D entry = fromMapEntry(node);
+ builder.put(getKey(entry), entry);
+ }
+ return builder.build();
+ }
+
+ @SuppressWarnings("checkstyle:illegalCatch")
+ private Object getKey(final D entry) {
+ try {
+ return keyHandle.invokeExact(entry);
+ } catch (Throwable e) {
+ throw new LinkageError("Failed to extract key from " + entry, e);
+ }
+ }
+ }
+