Fix license header violations in sal-clustering-commons
[controller.git] / opendaylight / md-sal / sal-clustering-commons / src / main / java / org / opendaylight / controller / cluster / datastore / node / utils / PathUtils.java
index 6cdddfd2716c1cf0e008f47853877174ba61d4b3..2342bf88e9a1c81e78fd8915bce82878762c70e5 100644 (file)
@@ -1,24 +1,23 @@
 /*
+ * Copyright (c) 2014, 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
- *  Copyright (c) 2014 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
- *
+ * 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.controller.cluster.datastore.node.utils;
 
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-
+import com.google.common.base.Splitter;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 
 public class PathUtils {
+    private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings();
 
     /**
      * Given a YangInstanceIdentifier return a serialized version of the same
@@ -27,17 +26,22 @@ public class PathUtils {
      * @param path
      * @return
      */
-    public static String toString(YangInstanceIdentifier path){
-        StringBuilder sb = new StringBuilder();
-        Iterator<YangInstanceIdentifier.PathArgument> iterator =
+    public static String toString(YangInstanceIdentifier path) {
+        final Iterator<YangInstanceIdentifier.PathArgument> it =
             path.getPathArguments().iterator();
+        if (!it.hasNext()) {
+            return "";
+        }
 
-        while(iterator.hasNext()){
-            sb.append(toString(iterator.next()));
-            if(iterator.hasNext()){
-                sb.append("/");
+        final StringBuilder sb = new StringBuilder();
+        for (;;) {
+            sb.append(toString(it.next()));
+            if (!it.hasNext()) {
+                break;
             }
+            sb.append('/');
         }
+
         return sb.toString();
     }
 
@@ -70,13 +74,9 @@ public class PathUtils {
      * @return
      */
     public static YangInstanceIdentifier toYangInstanceIdentifier(String path){
-        String[] segments = path.split("/");
-
         List<YangInstanceIdentifier.PathArgument> pathArguments = new ArrayList<>();
-        for (String segment : segments) {
-            if (!"".equals(segment)) {
-                pathArguments.add(NodeIdentifierFactory.getArgument(segment));
-            }
+        for (String segment : SLASH_SPLITTER.split(path)) {
+            pathArguments.add(NodeIdentifierFactory.getArgument(segment));
         }
         return YangInstanceIdentifier.create(pathArguments);
     }