Refactor ListEntryNodeDataWithSchema
[yangtools.git] / yang / yang-data-codec-xml / src / main / java / org / opendaylight / yangtools / yang / data / codec / xml / UnionXmlCodec.java
index e244617faeffc687c400540716913e10dc25d6e7..0abe970cc3d3a033a6766510087aada1b1ab7263 100644 (file)
@@ -5,11 +5,11 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.yangtools.yang.data.codec.xml;
 
-import com.google.common.base.Preconditions;
-import com.google.common.base.Verify;
+import static com.google.common.base.Verify.verify;
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.collect.ImmutableList;
 import java.util.Iterator;
 import java.util.List;
@@ -27,7 +27,7 @@ abstract class UnionXmlCodec<T> implements XmlCodec<T> {
         }
 
         @Override
-        public Class<Object> getDataClass() {
+        public Class<Object> getDataType() {
             return Object.class;
         }
     }
@@ -37,18 +37,18 @@ abstract class UnionXmlCodec<T> implements XmlCodec<T> {
 
         SingleType(final Class<T> dataClass, final List<XmlCodec<?>> codecs) {
             super(codecs);
-            this.dataClass = Preconditions.checkNotNull(dataClass);
+            this.dataClass = requireNonNull(dataClass);
         }
 
         @Override
-        public Class<T> getDataClass() {
+        public Class<T> getDataType() {
             return dataClass;
         }
     }
 
     private static final Logger LOG = LoggerFactory.getLogger(UnionXmlCodec.class);
 
-    private final List<XmlCodec<?>> codecs;
+    private final ImmutableList<XmlCodec<?>> codecs;
 
     UnionXmlCodec(final List<XmlCodec<?>> codecs) {
         this.codecs = ImmutableList.copyOf(codecs);
@@ -56,11 +56,11 @@ abstract class UnionXmlCodec<T> implements XmlCodec<T> {
 
     static UnionXmlCodec<?> create(final UnionTypeDefinition type, final List<XmlCodec<?>> codecs) {
         final Iterator<XmlCodec<?>> it = codecs.iterator();
-        Verify.verify(it.hasNext(), "Union %s has no subtypes", type);
+        verify(it.hasNext(), "Union %s has no subtypes", type);
 
-        Class<?> dataClass = it.next().getDataClass();
+        Class<?> dataClass = it.next().getDataType();
         while (it.hasNext()) {
-            final Class<?> next = it.next().getDataClass();
+            final Class<?> next = it.next().getDataType();
             if (!dataClass.equals(next)) {
                 LOG.debug("Type {} has diverse data classes: {} and {}", type, dataClass, next);
                 return new Diverse(codecs);
@@ -72,26 +72,28 @@ abstract class UnionXmlCodec<T> implements XmlCodec<T> {
     }
 
     @Override
-    public final T deserializeFromString(final NamespaceContext namespaceContext, final String input) {
+    @SuppressWarnings("checkstyle:illegalCatch")
+    public final T parseValue(final NamespaceContext ctx, final String str) {
         for (XmlCodec<?> codec : codecs) {
             final Object ret;
             try {
-                ret = codec.deserializeFromString(namespaceContext, input);
+                ret = codec.parseValue(ctx, str);
             } catch (RuntimeException e) {
-                LOG.debug("Codec {} did not accept input '{}'", codec, input, e);
+                LOG.debug("Codec {} did not accept input '{}'", codec, str, e);
                 continue;
             }
 
-            return getDataClass().cast(ret);
+            return getDataType().cast(ret);
         }
 
-        throw new IllegalArgumentException("Invalid value \"" + input + "\" for union type.");
+        throw new IllegalArgumentException("Invalid value \"" + str + "\" for union type.");
     }
 
     @Override
-    public void serializeToWriter(final XMLStreamWriter writer, final Object value) throws XMLStreamException {
+    @SuppressWarnings("checkstyle:illegalCatch")
+    public void writeValue(final XMLStreamWriter ctx, final Object value) throws XMLStreamException {
         for (XmlCodec<?> codec : codecs) {
-            if (!codec.getDataClass().isInstance(value)) {
+            if (!codec.getDataType().isInstance(value)) {
                 LOG.debug("Codec {} cannot accept input {}, skipping it", codec, value);
                 continue;
             }
@@ -99,7 +101,7 @@ abstract class UnionXmlCodec<T> implements XmlCodec<T> {
             @SuppressWarnings("unchecked")
             final XmlCodec<Object> objCodec = (XmlCodec<Object>) codec;
             try {
-                objCodec.serializeToWriter(writer, value);
+                objCodec.writeValue(ctx, value);
                 return;
             } catch (RuntimeException e) {
                 LOG.debug("Codec {} failed to serialize {}", codec, value, e);