package org.opendaylight.yangtools.yang.data.codec.gson;
import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import com.google.common.base.MoreObjects;
+import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.net.URI;
-import java.util.Iterator;
-import javax.annotation.Nonnull;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
private final JSONStreamWriterContext parent;
private final boolean mandatory;
private final int depth;
- private boolean emittedMyself = false;
+
+ private boolean emittedMyself;
+ private boolean inChild;
/**
* Construct a new context.
final StringBuilder sb = new StringBuilder();
// Prepend module name if namespaces do not match
- final URI ns = qname.getNamespace();
- if (!ns.equals(getNamespace())) {
- final Iterator<Module> modules = schema.findModules(ns).iterator();
- checkArgument(modules.hasNext(), "Could not find module for namespace {}", ns);
-
- sb.append(modules.next().getName());
- sb.append(':');
+ final QNameModule module = qname.getModule();
+ if (!module.getNamespace().equals(getNamespace())) {
+ final Optional<String> modules = schema.findModule(module).map(Module::getName);
+ checkArgument(modules.isPresent(), "Could not find module for namespace {}", module);
+ sb.append(modules.get()).append(':');
}
sb.append(qname.getLocalName());
*
* @return Namespace as URI
*/
- protected abstract @Nonnull URI getNamespace();
+ protected abstract @NonNull URI getNamespace();
/**
* Emit the start of an element.
private void emitMyself(final SchemaContext schema, final JsonWriter writer) throws IOException {
if (!emittedMyself) {
if (parent != null) {
- parent.emittingChild(schema, writer);
+ parent.emitMyself(schema, writer);
}
emitStart(schema, writer);
* @throws IOException when writer reports it
*/
final void emittingChild(final SchemaContext schema, final JsonWriter writer) throws IOException {
+ checkState(!inChild, "Duplicate child encountered");
emitMyself(schema, writer);
+ inChild = true;
}
/**
* @throws IllegalArgumentException if this node cannot be ended (e.g. root)
*/
final JSONStreamWriterContext endNode(final SchemaContext schema, final JsonWriter writer) throws IOException {
+ if (inChild) {
+ inChild = false;
+ return this;
+ }
if (!emittedMyself && mandatory) {
emitMyself(schema, writer);
}
-
if (emittedMyself) {
emitEnd(writer);
}
return parent;
}
+
+ @Override
+ public final String toString() {
+ return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
+ }
+
+ protected ToStringHelper addToStringAttributes(final ToStringHelper helper) {
+ return helper;
+ }
}