From 940928b5a1da0f70ced7fa6c10cbed6bd8e03b6a Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sun, 3 Jan 2021 17:12:41 +0100 Subject: [PATCH] Cleanup boolean literals A number of places are using explicit literals where not needed, as pointed out by sonar. Fix these sites. Change-Id: I0ad6a690c66fb3b53bb4cd5c549399b665e7bc98 Signed-off-by: Robert Varga --- .../bgp/parser/spi/AttributeUtil.java | 6 ++-- .../community/ExtendedCommunityUtil.java | 9 ++--- .../bgp/rib/impl/EffectiveRibInWriter.java | 2 +- .../AbstractEROWithSubobjectsParser.java | 8 ++--- .../AbstractXROWithSubobjectsParser.java | 4 +-- ...ExplicitExclusionRouteSubobjectParser.java | 2 +- .../pcep/server/provider/MessagesUtil.java | 35 +++++++++---------- 7 files changed, 28 insertions(+), 38 deletions(-) diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AttributeUtil.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AttributeUtil.java index f919a0d17b..ae7907937d 100644 --- a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AttributeUtil.java +++ b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/AttributeUtil.java @@ -10,15 +10,13 @@ package org.opendaylight.protocol.bgp.parser.spi; import io.netty.buffer.ByteBuf; public final class AttributeUtil { - - private static final int MAX_ATTR_LENGTH_FOR_SINGLE_BYTE = 255; - public static final int OPTIONAL = 128; public static final int TRANSITIVE = 64; public static final int PARTIAL = 32; private static final int EXTENDED = 16; private AttributeUtil() { + // Hidden on purpose } /** @@ -32,7 +30,7 @@ public final class AttributeUtil { */ public static void formatAttribute(final int flags, final int type, final ByteBuf value, final ByteBuf buffer) { final int length = value.writerIndex(); - final boolean extended = length > MAX_ATTR_LENGTH_FOR_SINGLE_BYTE ? true : false; + final boolean extended = length > 255; buffer.writeByte(extended ? flags | EXTENDED : flags); buffer.writeByte(type); if (extended) { diff --git a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityUtil.java b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityUtil.java index 3386cb365f..d0299eb247 100644 --- a/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityUtil.java +++ b/bgp/parser-spi/src/main/java/org/opendaylight/protocol/bgp/parser/spi/extended/community/ExtendedCommunityUtil.java @@ -14,6 +14,7 @@ public final class ExtendedCommunityUtil { private static final byte NON_TRANS = 0x40; private ExtendedCommunityUtil() { + // Hidden on pupose } /** @@ -25,7 +26,7 @@ public final class ExtendedCommunityUtil { * type. */ public static int setTransitivity(final int type, final boolean isTransitive) { - return isTransitive ? type : ExtendedCommunityUtil.toNonTransitiveType(type); + return isTransitive ? type : type | NON_TRANS; } /** @@ -35,10 +36,6 @@ public final class ExtendedCommunityUtil { * @return True if input type is transitive, false if the type is non-transitive */ public static boolean isTransitive(final int type) { - return (type & NON_TRANS) == 0 ? true : false; - } - - private static int toNonTransitiveType(final int type) { - return type | NON_TRANS; + return (type & NON_TRANS) == 0; } } diff --git a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/EffectiveRibInWriter.java b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/EffectiveRibInWriter.java index a5f48fa1f9..19676c1da6 100644 --- a/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/EffectiveRibInWriter.java +++ b/bgp/rib-impl/src/main/java/org/opendaylight/protocol/bgp/rib/impl/EffectiveRibInWriter.java @@ -617,7 +617,7 @@ final class EffectiveRibInWriter implements PrefixesReceivedCounters, PrefixesIn private static boolean isLongLivedStaleTable(final Optional> optTable) { final Optional> optAttributes = NormalizedNodes.findNode(optTable, ATTRIBUTES_NID); - return optAttributes.isPresent() ? isLongLivedStale(extractContainer(optAttributes)) : false; + return optAttributes.isPresent() && isLongLivedStale(extractContainer(optAttributes)); } private static ContainerNode effectiveAttributes(final Optional> optUptodate) { diff --git a/pcep/base-parser/src/main/java/org/opendaylight/protocol/pcep/parser/object/AbstractEROWithSubobjectsParser.java b/pcep/base-parser/src/main/java/org/opendaylight/protocol/pcep/parser/object/AbstractEROWithSubobjectsParser.java index e3fdcdae8c..93067c9beb 100644 --- a/pcep/base-parser/src/main/java/org/opendaylight/protocol/pcep/parser/object/AbstractEROWithSubobjectsParser.java +++ b/pcep/base-parser/src/main/java/org/opendaylight/protocol/pcep/parser/object/AbstractEROWithSubobjectsParser.java @@ -24,7 +24,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; public abstract class AbstractEROWithSubobjectsParser extends CommonObjectParser implements ObjectSerializer { - private static final Logger LOG = LoggerFactory.getLogger(AbstractEROWithSubobjectsParser.class); private static final int HEADER_LENGTH = 2; @@ -42,11 +41,8 @@ public abstract class AbstractEROWithSubobjectsParser extends CommonObjectParser Preconditions.checkArgument(buffer != null, "Array of bytes is mandatory. Can't be null."); final List subs = new ArrayList<>(); while (buffer.isReadable()) { - final boolean loose = ((buffer.getUnsignedByte(buffer.readerIndex()) & (1 << Values.FIRST_BIT_OFFSET)) != 0) - ? true - : false; - final int type = (buffer.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES) - & ~(1 << Values.FIRST_BIT_OFFSET); + final boolean loose = (buffer.getUnsignedByte(buffer.readerIndex()) & 1 << Values.FIRST_BIT_OFFSET) != 0; + final int type = buffer.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES & ~(1 << Values.FIRST_BIT_OFFSET); final int length = buffer.readUnsignedByte() - HEADER_LENGTH; if (length > buffer.readableBytes()) { throw new PCEPDeserializerException( diff --git a/pcep/base-parser/src/main/java/org/opendaylight/protocol/pcep/parser/object/AbstractXROWithSubobjectsParser.java b/pcep/base-parser/src/main/java/org/opendaylight/protocol/pcep/parser/object/AbstractXROWithSubobjectsParser.java index bd23431dde..6294070cfd 100644 --- a/pcep/base-parser/src/main/java/org/opendaylight/protocol/pcep/parser/object/AbstractXROWithSubobjectsParser.java +++ b/pcep/base-parser/src/main/java/org/opendaylight/protocol/pcep/parser/object/AbstractXROWithSubobjectsParser.java @@ -44,8 +44,8 @@ public abstract class AbstractXROWithSubobjectsParser extends CommonObjectParser final List subs = new ArrayList<>(); while (buffer.isReadable()) { final boolean mandatory = - ((buffer.getUnsignedByte(buffer.readerIndex()) & (1 << Values.FIRST_BIT_OFFSET)) != 0) ? true : false; - final int type = UnsignedBytes.checkedCast((buffer.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES) + (buffer.getUnsignedByte(buffer.readerIndex()) & 1 << Values.FIRST_BIT_OFFSET) != 0; + final int type = UnsignedBytes.checkedCast(buffer.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES & ~(1 << Values.FIRST_BIT_OFFSET)); final int length = buffer.readUnsignedByte() - HEADER_LENGTH; if (length > buffer.readableBytes()) { diff --git a/pcep/base-parser/src/main/java/org/opendaylight/protocol/pcep/parser/subobject/EROExplicitExclusionRouteSubobjectParser.java b/pcep/base-parser/src/main/java/org/opendaylight/protocol/pcep/parser/subobject/EROExplicitExclusionRouteSubobjectParser.java index 039f12d9d0..87b9fc26dd 100644 --- a/pcep/base-parser/src/main/java/org/opendaylight/protocol/pcep/parser/subobject/EROExplicitExclusionRouteSubobjectParser.java +++ b/pcep/base-parser/src/main/java/org/opendaylight/protocol/pcep/parser/subobject/EROExplicitExclusionRouteSubobjectParser.java @@ -70,7 +70,7 @@ public class EROExplicitExclusionRouteSubobjectParser implements EROSubobjectPar .route.object.xro.Subobject> xros = new ArrayList<>(); while (buffer.isReadable()) { final boolean mandatory = - (buffer.getByte(buffer.readerIndex()) & 1 << Values.FIRST_BIT_OFFSET) != 0 ? true : false; + (buffer.getByte(buffer.readerIndex()) & 1 << Values.FIRST_BIT_OFFSET) != 0; final int type = buffer.readUnsignedByte() & Values.BYTE_MAX_VALUE_BYTES & ~(1 << Values.FIRST_BIT_OFFSET); final int length = buffer.readUnsignedByte() - HEADER_LENGTH; diff --git a/pcep/server/server-provider/src/main/java/org/opendaylight/bgpcep/pcep/server/provider/MessagesUtil.java b/pcep/server/server-provider/src/main/java/org/opendaylight/bgpcep/pcep/server/provider/MessagesUtil.java index 7ecfdf7c7f..66272b8ebd 100644 --- a/pcep/server/server-provider/src/main/java/org/opendaylight/bgpcep/pcep/server/provider/MessagesUtil.java +++ b/pcep/server/server-provider/src/main/java/org/opendaylight/bgpcep/pcep/server/provider/MessagesUtil.java @@ -5,7 +5,6 @@ * 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.bgpcep.pcep.server.provider; import com.google.common.collect.Lists; @@ -84,14 +83,14 @@ public final class MessagesUtil { public static final int PATH_DELAY = 12; private MessagesUtil() { - throw new UnsupportedOperationException(); + // Hidden on purpose } - public static Ero getEro(List pathDescriptions) { + public static Ero getEro(final List pathDescriptions) { /* Prepare ERO */ - final EroBuilder eroBuilder = new EroBuilder(); - eroBuilder.setIgnore(false); - eroBuilder.setProcessingRule(true); + final EroBuilder eroBuilder = new EroBuilder() + .setIgnore(false) + .setProcessingRule(true); final List eroSubs = new ArrayList<>(); /* Fulfill ERO sublist */ @@ -121,7 +120,7 @@ public final class MessagesUtil { } else { /* Prepare SubObject for Segment Routing */ SrEroType srEro = null; - if ((path.getLocalIpv4() != null) && (path.getRemoteIpv4() != null)) { + if (path.getLocalIpv4() != null && path.getRemoteIpv4() != null) { srEro = new SrEroTypeBuilder() .setNaiType(NaiType.Ipv4Adjacency) .setSid(path.getSid()) @@ -135,7 +134,7 @@ public final class MessagesUtil { .build()) .build(); } - if ((path.getLocalIpv6() != null) && (path.getRemoteIpv6() != null)) { + if (path.getLocalIpv6() != null && path.getRemoteIpv6() != null) { srEro = new SrEroTypeBuilder() .setNaiType(NaiType.Ipv6Adjacency) .setSid(path.getSid()) @@ -165,30 +164,30 @@ public final class MessagesUtil { return eroBuilder.build(); } - private static PathsBuilder buildPath(ConstrainedPath cpath) { + private static PathsBuilder buildPath(final ConstrainedPath cpath) { final PathsBuilder pathBuilder = new PathsBuilder(); /* Get ERO from Path Description */ pathBuilder.setEro(getEro(cpath.getPathDescription())); /* Fulfill Computed Metrics if available */ - final ArrayList metrics = new ArrayList(); + final ArrayList metrics = new ArrayList<>(); if (cpath.getMetric() != null) { final MetricBuilder metricBuilder = new MetricBuilder().setComputed(true) .setMetricType(Uint8.valueOf(IGP_METRIC)).setValue(new Float32( - ByteBuffer.allocate(4).putFloat(Float.valueOf(cpath.getMetric().floatValue())).array())); + ByteBuffer.allocate(4).putFloat(cpath.getMetric().floatValue()).array())); metrics.add(new MetricsBuilder().setMetric(metricBuilder.build()).build()); } if (cpath.getTeMetric() != null) { final MetricBuilder metricBuilder = new MetricBuilder().setComputed(true) .setMetricType(Uint8.valueOf(TE_METRIC)).setValue(new Float32( - ByteBuffer.allocate(4).putFloat(Float.valueOf(cpath.getTeMetric().floatValue())).array())); + ByteBuffer.allocate(4).putFloat(cpath.getTeMetric().floatValue()).array())); metrics.add(new MetricsBuilder().setMetric(metricBuilder.build()).build()); } if (cpath.getDelay() != null) { final MetricBuilder metricBuilder = new MetricBuilder().setComputed(true) .setMetricType(Uint8.valueOf(PATH_DELAY)).setValue(new Float32(ByteBuffer.allocate(4) - .putFloat(Float.valueOf(cpath.getDelay().getValue().floatValue())).array())); + .putFloat(cpath.getDelay().getValue().floatValue()).array())); metrics.add(new MetricsBuilder().setMetric(metricBuilder.build()).build()); } if (!metrics.isEmpty()) { @@ -198,7 +197,7 @@ public final class MessagesUtil { if (cpath.getBandwidth() != null) { final BandwidthBuilder bwBuilder = new BandwidthBuilder(); bwBuilder.setBandwidth(new Bandwidth(new Float32(ByteBuffer.allocate(4) - .putFloat(Float.valueOf(cpath.getBandwidth().getValue().floatValue())).array()))); + .putFloat(cpath.getBandwidth().getValue().floatValue()).array()))); pathBuilder.setBandwidth(bwBuilder.build()); if (cpath.getClassType() != null) { pathBuilder.setClassType(new ClassTypeBuilder().setClassType( @@ -210,10 +209,10 @@ public final class MessagesUtil { return pathBuilder; } - public static Pcrep createPcRepMessage(Rp rp, P2p p2p, ConstrainedPath cpath) { + public static Pcrep createPcRepMessage(final Rp rp, final P2p p2p, final ConstrainedPath cpath) { /* Prepare Path Object with ERO and Object from the Request */ - final ArrayList paths = new ArrayList(); + final ArrayList paths = new ArrayList<>(); PathsBuilder pathBuilder = buildPath(cpath); if (p2p.getLspa() != null) { @@ -238,11 +237,11 @@ public final class MessagesUtil { return new PcrepBuilder().setPcrepMessage(msgBuilder.build()).build(); } - public static Pcrep createNoPathMessage(Rp rp, byte reason) { + public static Pcrep createNoPathMessage(final Rp rp, final byte reason) { /* Prepare NoPath Object */ final Flags flags = new Flags(false, false, false, false, false, false, - (reason == UNKNOWN_DESTINATION) ? true : false, (reason == UNKNOWN_SOURCE) ? true : false); + reason == UNKNOWN_DESTINATION, reason == UNKNOWN_SOURCE); final NoPathVectorBuilder npvBuilder = new NoPathVectorBuilder().setFlags(flags); final TlvsBuilder tlvsBuilder = new TlvsBuilder().setNoPathVector(npvBuilder.build()); final NoPathBuilder npBuilder = new NoPathBuilder() -- 2.36.6