create AbstractNumericByteOperandParser 58/29758/4
authorClaudio D. Gasparini <cgaspari@cisco.com>
Mon, 16 Nov 2015 14:33:04 +0000 (15:33 +0100)
committerMilos Fabian <milfabia@cisco.com>
Sat, 21 Nov 2015 14:16:54 +0000 (14:16 +0000)
create AbstractNumericByteOperandParser and remove duplicate code.

Change-Id: Ibb58d848dd7f92780e824a867b680cb550b38107
Signed-off-by: Claudio D. Gasparini <cgaspari@cisco.com>
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/AbstractNumericByteOperandParser.java [new file with mode: 0644]
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/NumericOneByteOperandParser.java
bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/NumericTwoByteOperandParser.java

diff --git a/bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/AbstractNumericByteOperandParser.java b/bgp/flowspec/src/main/java/org/opendaylight/protocol/bgp/flowspec/AbstractNumericByteOperandParser.java
new file mode 100644 (file)
index 0000000..09d2e14
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.protocol.bgp.flowspec;
+
+import java.util.List;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
+
+/**
+ * Common parent class for numeric byte operands.
+ *
+ * @param <N> numeric operand type
+ */
+abstract class AbstractNumericByteOperandParser<N, V extends Number> extends AbstractNumericOperandParser<N> {
+
+    @Override
+    public final <T extends N> String toString(final List<T> list) {
+        final StringBuilder buffer = new StringBuilder();
+        boolean isFirst = true;
+        for (final T item : list) {
+            buffer.append(super.toString(getOp(item), isFirst));
+            buffer.append(getValue(item));
+            buffer.append(' ');
+            if (isFirst) {
+                isFirst = false;
+            }
+        }
+        return buffer.toString();
+    }
+
+    abstract <T extends N> V getValue(final T item);
+
+    abstract <T extends N> NumericOperand getOp(final T item);
+}
\ No newline at end of file
index ea05e9902ccbf368d1707fc86be365ea71223a75..a635d27f66138b55e974ab5597f0a54fe0a083b8 100644 (file)
@@ -10,11 +10,12 @@ package org.opendaylight.protocol.bgp.flowspec;
 import io.netty.buffer.ByteBuf;
 import java.util.List;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOneByteValue;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
 
 /**
  * Parser class for NumericOneByteValues.
  */
-public final class NumericOneByteOperandParser extends AbstractNumericOperandParser<NumericOneByteValue> {
+public final class NumericOneByteOperandParser extends AbstractNumericByteOperandParser<NumericOneByteValue, Short> {
 
     public static final NumericOneByteOperandParser INSTANCE;
 
@@ -29,7 +30,7 @@ public final class NumericOneByteOperandParser extends AbstractNumericOperandPar
     /**
      * Serializes Flowspec component type that has maximum of 1B sized value field and numeric operand.
      *
-     * @param list of operands to be serialized
+     * @param list        of operands to be serialized
      * @param nlriByteBuf where the operands will be serialized
      */
     @Override
@@ -40,19 +41,13 @@ public final class NumericOneByteOperandParser extends AbstractNumericOperandPar
         }
     }
 
-    // TODO: duplicate code with NumericTwoByteValue
     @Override
-    public <T extends NumericOneByteValue> String toString(final List<T> list) {
-        final StringBuilder buffer = new StringBuilder();
-        boolean isFirst = true;
-        for (final T item : list) {
-            buffer.append(super.toString(item.getOp(), isFirst));
-            buffer.append(item.getValue());
-            buffer.append(' ');
-            if (isFirst) {
-                isFirst = false;
-            }
-        }
-        return buffer.toString();
+    protected <T extends NumericOneByteValue> Short getValue(final T item) {
+        return item.getValue();
+    }
+
+    @Override
+    <T extends NumericOneByteValue> NumericOperand getOp(final T item) {
+        return item.getOp();
     }
 }
index 50d88feb79b50326f0459e23eeeb7423b1ee368b..dbce50e5dcfd159c1b4ac67cd13761914c9ce29b 100644 (file)
@@ -10,12 +10,13 @@ package org.opendaylight.protocol.bgp.flowspec;
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
 import java.util.List;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericTwoByteValue;
 
 /**
  * Parser class for NumericTwoByteValues.
  */
-public final class NumericTwoByteOperandParser extends AbstractNumericOperandParser<NumericTwoByteValue> {
+public final class NumericTwoByteOperandParser extends AbstractNumericByteOperandParser<NumericTwoByteValue, Integer> {
 
     public static final NumericTwoByteOperandParser INSTANCE;
 
@@ -43,19 +44,13 @@ public final class NumericTwoByteOperandParser extends AbstractNumericOperandPar
         }
     }
 
-    // TODO: duplicate code with NumericOneByteValue
     @Override
-    public <T extends NumericTwoByteValue> String toString(final List<T> list) {
-        final StringBuilder buffer = new StringBuilder();
-        boolean isFirst = true;
-        for (final T item : list) {
-            buffer.append(super.toString(item.getOp(), isFirst));
-            buffer.append(item.getValue());
-            buffer.append(' ');
-            if (isFirst) {
-                isFirst = false;
-            }
-        }
-        return buffer.toString();
+    protected <T extends NumericTwoByteValue> Integer getValue(final T item) {
+        return item.getValue();
+    }
+
+    @Override
+    <T extends NumericTwoByteValue> NumericOperand getOp(final T item) {
+        return item.getOp();
     }
 }