create AbstractNumericByteOperandParser and remove duplicate code.
Change-Id: Ibb58d848dd7f92780e824a867b680cb550b38107
Signed-off-by: Claudio D. Gasparini <cgaspari@cisco.com>
--- /dev/null
+/*
+ * 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
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;
/**
* 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
}
}
- // 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();
}
}
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;
}
}
- // 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();
}
}