Eliminate shift variable
[mdsal.git] / model / ietf / ietf-type-util / src / main / java / org / opendaylight / mdsal / model / ietf / util / Ipv4Utils.java
index f8797a65660b374262d0771a2ef13b42124f0244..847b1914be2d5cde08e9c87e7ed925ec6925a208 100644 (file)
@@ -7,6 +7,8 @@
  */
 package org.opendaylight.mdsal.model.ietf.util;
 
+import org.eclipse.jdt.annotation.NonNull;
+
 /**
  * IPv4 address parsing for ietf-inet-types ipv4-address. This is an internal implementation class, not meant to be
  * exposed in any shape or form to the outside world, as the code relies on the fact that the strings presented to it
@@ -14,10 +16,10 @@ package org.opendaylight.mdsal.model.ietf.util;
  */
 final class Ipv4Utils {
     private Ipv4Utils() {
-        throw new UnsupportedOperationException();
+
     }
 
-    static void fillIpv4Bytes(final byte[] bytes, final int byteStart, final String str, final int strStart,
+    static void fillIpv4Bytes(final byte @NonNull[] bytes, final int byteStart, final String str, final int strStart,
             final int strLimit) {
         int out = byteStart;
         int val = 0;
@@ -27,10 +29,40 @@ final class Ipv4Utils {
                 bytes[out++] = (byte) val;
                 val = 0;
             } else {
-                val = 10 * val + (c - '0');
+                val = 10 * val + c - '0';
             }
         }
 
         bytes[out] = (byte) val;
     }
+
+    static int addressBits(final String str, final int limit) {
+        int prev = 0;
+        int current = 0;
+        for (int i = 0; i < limit; ++i) {
+            final char c = str.charAt(i);
+            if (c == '.') {
+                prev = prev << 8 | current;
+                current = 0;
+            } else {
+                current = 10 * current + c - '0';
+            }
+        }
+        return prev << 8 | current;
+    }
+
+    static byte @NonNull[] addressBytes(final String str, final int limit) {
+        final byte[] bytes = new byte[4];
+        Ipv4Utils.fillIpv4Bytes(bytes, 0, str, 0, limit);
+        return bytes;
+    }
+
+    static String addressString(final int bits) {
+        return new StringBuilder(15)
+                .append(bits >>> 24).append('.')
+                .append(bits >>> 16 & 0xFF).append('.')
+                .append(bits >>> 8 & 0xFF).append('.')
+                .append(bits & 0xFF)
+                .toString();
+    }
 }