import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.MultimapBuilder.SetMultimapBuilder;
import com.google.common.collect.Multimaps;
-import com.google.common.collect.SetMultimap;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
-import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.opendaylight.yangtools.yang.binding.contract.Naming;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
implements BindingDataObjectCodecTreeNode<D> {
private static final Logger LOG = LoggerFactory.getLogger(ChoiceNodeCodecContext.class);
- private final ImmutableMap<YangInstanceIdentifier.PathArgument, DataContainerCodecPrototype<?>> byYangCaseChild;
+ private final ImmutableMap<NodeIdentifier, DataContainerCodecPrototype<?>> byYangCaseChild;
private final ImmutableListMultimap<Class<?>, DataContainerCodecPrototype<?>> ambiguousByCaseChildClass;
private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byCaseChildClass;
private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byClass;
ChoiceNodeCodecContext(final DataContainerCodecPrototype<ChoiceRuntimeType> prototype) {
super(prototype);
- final Map<YangInstanceIdentifier.PathArgument, DataContainerCodecPrototype<?>> byYangCaseChildBuilder =
- new HashMap<>();
- final Map<Class<?>, DataContainerCodecPrototype<?>> byClassBuilder = new HashMap<>();
- final SetMultimap<Class<?>, DataContainerCodecPrototype<?>> childToCase =
- SetMultimapBuilder.hashKeys().hashSetValues().build();
+ final var byYangCaseChildBuilder = new HashMap<NodeIdentifier, DataContainerCodecPrototype<?>>();
+ final var byClassBuilder = new HashMap<Class<?>, DataContainerCodecPrototype<?>>();
+ final var childToCase = SetMultimapBuilder.hashKeys().hashSetValues()
+ .<Class<?>, DataContainerCodecPrototype<?>>build();
// Load case statements valid in this choice and keep track of their names
final var choiceType = prototype.getType();
// Updates collection of case children
@SuppressWarnings("unchecked")
- final Class<? extends DataObject> cazeCls = (Class<? extends DataObject>) cazeDef.getBindingClass();
- for (final Class<? extends DataObject> cazeChild : getChildrenClasses(cazeCls)) {
+ final var cazeCls = (Class<? extends DataObject>) cazeDef.getBindingClass();
+ for (var cazeChild : getChildrenClasses(cazeCls)) {
childToCase.put(cazeChild, cazeDef);
}
// Updates collection of YANG instance identifier to case
byYangCaseChild = ImmutableMap.copyOf(byYangCaseChildBuilder);
// Move unambiguous child->case mappings to byCaseChildClass, removing them from childToCase
- final ImmutableListMultimap.Builder<Class<?>, DataContainerCodecPrototype<?>> ambiguousByCaseBuilder =
- ImmutableListMultimap.builder();
- final Builder<Class<?>, DataContainerCodecPrototype<?>> unambiguousByCaseBuilder = ImmutableMap.builder();
- for (Entry<Class<?>, Set<DataContainerCodecPrototype<?>>> e : Multimaps.asMap(childToCase).entrySet()) {
- final Set<DataContainerCodecPrototype<?>> cases = e.getValue();
+ final var ambiguousByCaseBuilder = ImmutableListMultimap.<Class<?>, DataContainerCodecPrototype<?>>builder();
+ final var unambiguousByCaseBuilder = ImmutableMap.<Class<?>, DataContainerCodecPrototype<?>>builder();
+ for (var entry : Multimaps.asMap(childToCase).entrySet()) {
+ final var cases = entry.getValue();
if (cases.size() != 1) {
// Sort all possibilities by their FQCN to retain semi-predictable results
- final List<DataContainerCodecPrototype<?>> list = new ArrayList<>(e.getValue());
+ final var list = new ArrayList<>(entry.getValue());
list.sort(Comparator.comparing(proto -> proto.getBindingClass().getCanonicalName()));
- ambiguousByCaseBuilder.putAll(e.getKey(), list);
+ ambiguousByCaseBuilder.putAll(entry.getKey(), list);
} else {
- unambiguousByCaseBuilder.put(e.getKey(), cases.iterator().next());
+ unambiguousByCaseBuilder.put(entry.getKey(), cases.iterator().next());
}
}
byCaseChildClass = unambiguousByCaseBuilder.build();
* This is required due property of binding specification, that if choice is in grouping schema path location is
* lost, and users may use incorrect case class using copy builders.
*/
- final Map<Class<?>, DataContainerCodecPrototype<?>> bySubstitutionBuilder = new HashMap<>();
+ final var bySubstitutionBuilder = new HashMap<Class<?>, DataContainerCodecPrototype<?>>();
final var context = factory.getRuntimeContext();
for (var caseType : context.getTypes().allCaseChildren(choiceType)) {
final var caseName = caseType.getIdentifier();
// FIXME: do not rely on class loading here, the check we are performing should be possible on
// GeneratedType only -- or it can be provided by BindingRuntimeTypes -- i.e. rather than
// 'allCaseChildren()' it would calculate additional mappings we can use off-the-bat.
- final Class<?> substitution = loadCase(context, caseType);
+ final var substitution = loadCase(context, caseType);
- search: for (final Entry<Class<?>, DataContainerCodecPrototype<?>> real : byClassBuilder.entrySet()) {
+ search: for (var real : byClassBuilder.entrySet()) {
if (isSubstitutionFor(substitution, real.getKey())) {
bySubstitutionBuilder.put(substitution, real.getValue());
break search;
@SuppressWarnings("unchecked")
@Override
public <C extends DataObject> DataContainerCodecContext<C, ?> streamChild(final Class<C> childClass) {
- final DataContainerCodecPrototype<?> child = byClass.get(childClass);
+ final var child = byClass.get(childClass);
return (DataContainerCodecContext<C, ?>) childNonNull(child, childClass,
"Supplied class %s is not valid case in %s", childClass, bindingArg()).get();
}
@Override
public <C extends DataObject> Optional<DataContainerCodecContext<C, ?>> possibleStreamChild(
final Class<C> childClass) {
- final DataContainerCodecPrototype<?> child = byClass.get(childClass);
+ final var child = byClass.get(childClass);
if (child != null) {
return Optional.of((DataContainerCodecContext<C, ?>) child.get());
}
@Override
public NodeCodecContext yangPathArgumentChild(final YangInstanceIdentifier.PathArgument arg) {
final DataContainerCodecPrototype<?> cazeProto;
- if (arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
+ if (arg instanceof NodeIdentifierWithPredicates) {
cazeProto = byYangCaseChild.get(new NodeIdentifier(arg.getNodeType()));
} else {
cazeProto = byYangCaseChild.get(arg);
@SuppressWarnings("unchecked")
@SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "See FIXME below")
public D deserialize(final NormalizedNode data) {
- final ChoiceNode casted = checkDataArgument(ChoiceNode.class, data);
- final NormalizedNode first = Iterables.getFirst(casted.body(), null);
+ final var casted = checkDataArgument(ChoiceNode.class, data);
+ final var first = Iterables.getFirst(casted.body(), null);
if (first == null) {
// FIXME: this needs to be sorted out
return null;
}
- final DataContainerCodecPrototype<?> caze = byYangCaseChild.get(first.name());
+ final var caze = byYangCaseChild.get(first.name());
return (D) caze.getDataObject().deserialize(data);
}
}
DataContainerCodecContext<?, ?> getCaseByChildClass(final @NonNull Class<? extends DataObject> type) {
- DataContainerCodecPrototype<?> result = byCaseChildClass.get(type);
+ var result = byCaseChildClass.get(type);
if (result == null) {
// We have not found an unambiguous result, try ambiguous ones
- final List<DataContainerCodecPrototype<?>> inexact = ambiguousByCaseChildClass.get(type);
+ final var inexact = ambiguousByCaseChildClass.get(type);
if (!inexact.isEmpty()) {
result = inexact.get(0);
// Issue a warning, but only once so as not to flood the logs
if (ambiguousByCaseChildWarnings.add(type)) {
- LOG.warn("Ambiguous reference {} to child of {} resolved to {}, the first case in {} This mapping "
- + "is not guaranteed to be stable and is subject to variations based on runtime "
- + "circumstances. Please see the stack trace for hints about the source of ambiguity.",
- type, bindingArg(), result.getBindingClass(),
- Lists.transform(inexact, DataContainerCodecPrototype::getBindingClass), new Throwable());
+ LOG.warn("""
+ Ambiguous reference {} to child of {} resolved to {}, the first case in {} This mapping is \
+ not guaranteed to be stable and is subject to variations based on runtime circumstances. \
+ Please see the stack trace for hints about the source of ambiguity.""",
+ type, bindingArg(), result.getBindingClass(),
+ Lists.transform(inexact, DataContainerCodecPrototype::getBindingClass), new Throwable());
}
}
}
private static Iterable<Class<? extends DataObject>> getChildrenClasses(final Class<? extends DataContainer> type) {
checkArgument(type != null, "Target type must not be null");
checkArgument(DataContainer.class.isAssignableFrom(type), "Supplied type must be derived from DataContainer");
- List<Class<? extends DataObject>> ret = new LinkedList<>();
- for (Method method : type.getMethods()) {
- Optional<Class<? extends DataContainer>> entity = getYangModeledReturnType(method, Naming.GETTER_PREFIX);
+ final var ret = new LinkedList<Class<? extends DataObject>>();
+ for (var method : type.getMethods()) {
+ final var entity = getYangModeledReturnType(method, Naming.GETTER_PREFIX);
if (entity.isPresent()) {
ret.add((Class<? extends DataObject>) entity.orElseThrow());
}