diff options
author | waqas.ikram <waqas.ikram@ericsson.com> | 2018-05-30 09:48:40 +0100 |
---|---|---|
committer | waqas.ikram <waqas.ikram@ericsson.com> | 2018-05-30 16:29:30 +0100 |
commit | 8bad2ad828fad2f5f99a9a291ca0f5c51b62e4ca (patch) | |
tree | 7b02ec8dd5dcda881b9133169a753fc6671e44bf /model/utilities/src | |
parent | 55d93a12cc5575c872724f48585304b5eec77fea (diff) |
Fixing Sonar bugs and code smells
Change-Id: I661cd409d01320fe11a29b7ea72b9e8f70e72a6c
Issue-ID: POLICY-856
Signed-off-by: waqas.ikram <waqas.ikram@ericsson.com>
Diffstat (limited to 'model/utilities/src')
4 files changed, 35 insertions, 42 deletions
diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/CollectionUtils.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/CollectionUtils.java index fb7207e0b..7ca50a613 100644 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/CollectionUtils.java +++ b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/CollectionUtils.java @@ -1,4 +1,4 @@ -/* +/*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * ================================================================================ diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TextFileUtils.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TextFileUtils.java index d05245f23..d119a3a35 100644 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TextFileUtils.java +++ b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TextFileUtils.java @@ -1,4 +1,4 @@ -/* +/*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * ================================================================================ @@ -22,15 +22,15 @@ package org.onap.policy.apex.model.utilities; import java.io.BufferedReader; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.file.Files; +import java.nio.file.Paths; /** - * The Class TextFileUtils is class that provides useful functions for handling text files. Functions to read and wrtie text files to strings and strings are - * provided. + * The Class TextFileUtils is class that provides useful functions for handling text files. + * Functions to read and wrtie text files to strings and strings are provided. * * @author Liam Fallon (liam.fallon@ericsson.com) */ @@ -38,9 +38,9 @@ public abstract class TextFileUtils { private static final int READER_CHAR_BUFFER_SIZE_4096 = 4096; private TextFileUtils() { - // This class cannot be initialized + // This class cannot be initialized } - + /** * Method to return the contents of a text file as a string. * @@ -49,12 +49,7 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static String getTextFileAsString(final String textFilePath) throws IOException { - final File textFile = new File(textFilePath); - final FileInputStream textFileInputStream = new FileInputStream(textFile); - final byte[] textData = new byte[(int) textFile.length()]; - textFileInputStream.read(textData); - textFileInputStream.close(); - return new String(textData); + return new String(Files.readAllBytes(Paths.get(textFilePath))); } /** @@ -77,9 +72,7 @@ public abstract class TextFileUtils { * @throws IOException on errors reading text from the file */ public static void putStringAsFile(final String outString, final File textFile) throws IOException { - final FileOutputStream textFileOutputStream = new FileOutputStream(textFile); - textFileOutputStream.write(outString.getBytes()); - textFileOutputStream.close(); + Files.write(textFile.toPath(), outString.getBytes()); } /** @@ -110,8 +103,7 @@ public abstract class TextFileUtils { if (charsRead > 0) { builder.append(chars, 0, charsRead); } - } - while (charsRead > 0); + } while (charsRead > 0); return builder.toString(); } } diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TreeMapUtils.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TreeMapUtils.java index 02ab0dd24..cb0f799a0 100644 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TreeMapUtils.java +++ b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TreeMapUtils.java @@ -1,4 +1,4 @@ -/* +/*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * ================================================================================ @@ -24,10 +24,11 @@ import java.util.AbstractMap.SimpleEntry; import java.util.ArrayList; import java.util.List; import java.util.Map.Entry; -import java.util.TreeMap; +import java.util.NavigableMap; /** - * This class provides utility functions for tree maps. A function to find the nearest match in the tree map to an input string is provided. + * This class provides utility functions for tree maps. A function to find the nearest match in the + * tree map to an input string is provided. * * @author Liam Fallon (liam.fallon@ericsson.com) */ @@ -36,18 +37,19 @@ public abstract class TreeMapUtils { /** * This class is a utility class that can't be instantiated. */ - private TreeMapUtils() { - } + private TreeMapUtils() {} /** - * Find the list of entries that matches a given word, for example "p" will match "put", "policy", and "push". + * Find the list of entries that matches a given word, for example "p" will match "put", + * "policy", and "push". * * @param <T> the generic type for the value of the tree map * @param searchMap the map that the method operates on * @param word the word to search for * @return the list of entries in the {@code searchMap} that match the {@code word} */ - public static <T> List<Entry<String, T>> findMatchingEntries(final TreeMap<String, T> searchMap, final String word) { + public static <T> List<Entry<String, T>> findMatchingEntries(final NavigableMap<String, T> searchMap, + final String word) { final List<Entry<String, T>> foundNodes = new ArrayList<>(); // A straight match check @@ -60,8 +62,7 @@ public abstract class TreeMapUtils { String foundKeyword = searchMap.floorKey(word); if (foundKeyword == null) { foundKeyword = searchMap.firstKey(); - } - else { + } else { foundKeyword = searchMap.higherKey(foundKeyword); } @@ -70,8 +71,7 @@ public abstract class TreeMapUtils { if (foundKeyword.startsWith(word)) { foundNodes.add(new SimpleEntry<>(foundKeyword, searchMap.get(foundKeyword))); foundKeyword = searchMap.higherKey(foundKeyword); - } - else { + } else { break; } } diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TextFileUtilsTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TextFileUtilsTest.java index 4e5cba374..c84ee8618 100644 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TextFileUtilsTest.java +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TextFileUtilsTest.java @@ -1,4 +1,4 @@ -/* +/*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. * ================================================================================ @@ -27,26 +27,27 @@ import java.io.FileInputStream; import java.io.IOException; import org.junit.Test; -import org.onap.policy.apex.model.utilities.TextFileUtils; /** * @author Liam Fallon (liam.fallon@ericsson.com) */ public class TextFileUtilsTest { + private static final String FILE_CONTENT = "This is the contents of a text file"; + @Test public void test() throws IOException { - File tempTextFile = File.createTempFile("Test", "txt"); - - TextFileUtils.putStringAsTextFile("This is the contents of a text file", tempTextFile.getAbsolutePath()); - - String textFileString0 = TextFileUtils.getTextFileAsString(tempTextFile.getAbsolutePath()); - assertEquals("This is the contents of a text file", textFileString0); - - FileInputStream fis = new FileInputStream(tempTextFile); - String textFileString1 = TextFileUtils.getStreamAsString(fis); + final File tempTextFile = File.createTempFile("Test", "txt"); + + TextFileUtils.putStringAsTextFile(FILE_CONTENT, tempTextFile.getAbsolutePath()); + + final String textFileString0 = TextFileUtils.getTextFileAsString(tempTextFile.getAbsolutePath()); + assertEquals(FILE_CONTENT, textFileString0); + + final FileInputStream fis = new FileInputStream(tempTextFile); + final String textFileString1 = TextFileUtils.getStreamAsString(fis); assertEquals(textFileString0, textFileString1); - + } } |