Code improvements in Common Utils 53/37053/1
authorajay priyadarshi <ajay.priyadarshi@ril.com>
Tue, 20 Mar 2018 10:19:12 +0000 (15:49 +0530)
committerajay priyadarshi <ajay.priyadarshi@ril.com>
Tue, 20 Mar 2018 10:19:12 +0000 (15:49 +0530)
commons/utils/VarExprParser.java
file name: StringUtil.java,DriverThread.java

Change-Id: I53dfffa4584b0c99705645bdb2ec9cc3cbc59ecc
Issue-ID: VFC-837
Signed-off-by: ajay priyadarshi <ajay.priyadarshi@ril.com>
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/commons/utils/DateUtil.java
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/commons/utils/DriverThread.java
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/commons/utils/Gunzip.java
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/commons/utils/StringUtil.java
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/commons/utils/UnZip.java
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/commons/utils/VarExprParser.java
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/commons/utils/XmlUtil.java
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/commons/utils/Zip.java

index 9e84374..d1e5898 100644 (file)
@@ -71,18 +71,13 @@ public class DateUtil {
     }
 
     public static String addTime(String srcTimeString, String period) throws ParseException {
-       try{
-               String finaldate = getTimeString(srcTimeString);
-               SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
-               Date date = sdf.parse(finaldate);
-               Calendar calendar = Calendar.getInstance();
-               calendar.setTime(date);
-               calendar.add(Calendar.MINUTE, Integer.valueOf(period));
-               return sdf.format(calendar.getTime());
-       }catch( ParseException e){
-               throw e;
-       }
-       
+           String finaldate = getTimeString(srcTimeString);
+           SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+           Date date = sdf.parse(finaldate);
+           Calendar calendar = Calendar.getInstance();
+           calendar.setTime(date);
+           calendar.add(Calendar.MINUTE, Integer.valueOf(period));
+           return sdf.format(calendar.getTime());
     }
 
 }
index 64b518b..8e57d5e 100644 (file)
@@ -44,7 +44,7 @@ public abstract class DriverThread implements Runnable {
 
     public abstract void dispose();
 
-    final public void run() {
+    public final void run() {
         t = Thread.currentThread();
         if (name != null)
             t.setName(name);
@@ -52,7 +52,6 @@ public abstract class DriverThread implements Runnable {
         try {
             dispose();
         } catch (Exception e) {
-            //e.printStackTrace();
            log.error(" printStackTrace :", e);
         }
         this.setEnd(true);
index e6ed9d5..edbeebc 100644 (file)
@@ -29,7 +29,7 @@ public class Gunzip {
                                moveBytes(gzIn, fileOutput, -1, -1, 1024);
                        } 
                }catch(IOException e){
-               throw e;
+                       throw new IOException ("Gunzip", e);
                }
 
        }
@@ -44,10 +44,10 @@ public class Gunzip {
        }
 
        public long moveBytes(InputStream input, OutputStream output, long off, long len, int bufsize) throws IOException {
-               long skipped=0;
-               if (off > 0)
+               /*      long skipped=0;
+                       if (off > 0)
                        skipped = input.skip(off); // check if skipped is same as off
-
+               */
                long totalNum = 0;
                byte[] buf = new byte[bufsize];
 
@@ -65,7 +65,6 @@ public class Gunzip {
                        output.write(buf, 0, readNum);
                        totalNum += readNum;
                }
-               buf = null;
                return totalNum;
        }
 
index e09ff8d..5fa0f9a 100644 (file)
@@ -46,10 +46,6 @@ public class StringUtil {
     }
 
     public static boolean isBank(String str) {
-        if (str == null || str.trim().length() == 0) {
-
-            return true;
-        }
-        return false;
+           return (str == null || str.trim().length() == 0); 
     }
 }
index 504a47b..6d49d10 100644 (file)
@@ -58,26 +58,22 @@ public class UnZip {
                 }
             }
         } catch (IOException e) {
-            throw e;
+            throw new IOException("deCompress: ",e);
         }  
    }
 
     protected void deCompressFile(InputStream input, String toPath)
             throws IOException {
-        byte byteBuf[] = new byte[2048];
+        byte[] byteBuf = new byte[2048];
         String path = new File(toPath).getParent();
         if (!new File(path).exists()) {
             new File(path).mkdirs();
         }
-        FileOutputStream output = new FileOutputStream(toPath, false);
-        try {
+        try(FileOutputStream output = new FileOutputStream(toPath, false)){
             for (int count = 0; (count = input.read(byteBuf, 0, byteBuf.length)) != -1; )
                 output.write(byteBuf, 0, count);
         } catch (IOException e) {
-            throw e;
-        } finally {
-            output.close();
-            input.close();
-        }
+            throw new IOException ("deCompressFile: ", e);
+        } 
     }
 }
index 0e3f27c..5ea9a90 100644 (file)
@@ -25,12 +25,12 @@ import java.util.Locale;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-final public class VarExprParser {
+    public  final class VarExprParser {
     private static Log log = LogFactory.getFactory().getInstance(VarExprParser.class);
     private static Pattern varPattern = Pattern.compile("(\\$\\{([^\\}]+)\\})",
             Pattern.CASE_INSENSITIVE);
 
-    final static public String replaceVar(String str, long scan_start_time, long scan_stop_time) {
+    public static final String replaceVar(String str, long scanStartTime, long scanStopTime) {
         if (str.indexOf("${") == -1)
             return str;
 
@@ -46,16 +46,16 @@ final public class VarExprParser {
         str = str.replace("${e_hour}", "${SCAN_STOP_TIME,HH}");
         str = str.replace("${e_min}", "${SCAN_STOP_TIME,mm}");
 
-        String expr = null, varName = null, value = null;
+        String expr, varName, value ;
         Matcher matcher = varPattern.matcher(str);
         while (matcher.find()) {
             value = null;
             expr = matcher.group(1);
             varName = matcher.group(2);
             if (expr.indexOf("${SCAN_START_TIME") != -1) {
-                value = getTime(scan_start_time, varName, "yyyy-MM-dd HH:mm:ss");
+                value = getTime(scanStartTime, varName, "yyyy-MM-dd HH:mm:ss");
             } else if (expr.indexOf("${SCAN_STOP_TIME") != -1) {
-                value = getTime(scan_stop_time, varName, "yyyy-MM-dd HH:mm:ss");
+                value = getTime(scanStopTime, varName, "yyyy-MM-dd HH:mm:ss");
             }
             if (value == null) {
                 log.warn(" expr [" + str + "] var["
@@ -64,8 +64,6 @@ final public class VarExprParser {
             }
             str = str.replace(expr, value);
         }
-        expr = value = null;
-        matcher = null;
         return str;
     }
 
@@ -111,11 +109,11 @@ final public class VarExprParser {
     /**
      * Support two variable substitutions
      * @param result
-     * @param scan_start_time
-     * @param scan_stop_time
+     * @param scanStartTime
+     * @param scanStopTime
      * @return
      */
-    public static String replaceTimeVar(String result,String scan_start_time, String scan_stop_time) {
+    public static String replaceTimeVar(String result,String scanStartTime, String scanStopTime) {
         boolean isReplace = false;
         if (result.indexOf("${SCAN_ST") != -1) {
             isReplace = true;
@@ -123,11 +121,11 @@ final public class VarExprParser {
         if (isReplace) {
             if (result.indexOf("${SCAN_START_TIME}") != -1) {
 
-                result = StringUtils.replace(result, "${SCAN_START_TIME}", scan_start_time);
+                result = StringUtils.replace(result, "${SCAN_START_TIME}", scanStartTime);
             }
             if (result.indexOf("${SCAN_STOP_TIME") != -1) {
 
-                result = StringUtils.replace(result, "${SCAN_STOP_TIME}", scan_stop_time);
+                result = StringUtils.replace(result, "${SCAN_STOP_TIME}", scanStopTime);
             }
         }
         return result;
index 19483ca..c08d8b0 100644 (file)
@@ -26,9 +26,8 @@ import java.io.InputStream;
 
 public class XmlUtil {
 
-    public static Document getDocument(InputStream is) throws XMLStreamException, JDOMException, IOException {
-        SAXBuilder builder = new SAXBuilder();
-        Document doc = builder.build(is);
-        return doc;
-    }
+       public static Document getDocument(InputStream is) throws JDOMException, IOException {
+               SAXBuilder builder = new SAXBuilder();
+               return builder.build(is);
+       }
 }
index d79e89d..b48287d 100644 (file)
@@ -68,7 +68,7 @@ public class Zip {
         if (directoryPath.isFile()) {
             compressFile(directoryPath.getAbsolutePath());
         } else {
-            File listFiles[] = directoryPath.listFiles();
+            File[] listFiles = directoryPath.listFiles();
             for (int i = 0; i < listFiles.length; i++)
                 if (listFiles[i].isFile()) {
                     compressFile(listFiles[i].getAbsolutePath());
@@ -80,21 +80,16 @@ public class Zip {
     }
 
     protected void compressFile(String absolutePath) throws IOException {
-           try{
-                   compressFileCount++;
-                   byte byteBuf[] = new byte[2048];
-                   zipOutput.putNextEntry(new ZipEntry(absolutePath.substring(relativeAddrIdx)));
-                   FileInputStream input = new FileInputStream(absolutePath);
-                   try{
-                           for (int count = 0; (count = input.read(byteBuf, 0, byteBuf.length)) != -1; )
-                                   zipOutput.write(byteBuf, 0, count);
-                   }finally{
-                           input.close();
-                           zipOutput.closeEntry();
-                   }
-           }catch(IOException e){
-                   throw e;    
-           }           
+           compressFileCount++;
+           byte[] byteBuf = new byte[2048];
+           zipOutput.putNextEntry(new ZipEntry(absolutePath.substring(relativeAddrIdx)));
+
+           try( FileInputStream input = new FileInputStream(absolutePath)){
+                   for (int count = 0; (count = input.read(byteBuf, 0, byteBuf.length)) != -1; )
+                           zipOutput.write(byteBuf, 0, count);
+           }finally{
+                   zipOutput.closeEntry();
+           }
     }
 
     public void setCompressLevel(int level) {