Advertisement
Guest User

Untitled

a guest
May 25th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. package com.duplxey.nquiz.util;
  2.  
  3. import java.util.EnumSet;
  4.  
  5. public class EnumUtil {
  6.  
  7.     /**
  8.      * Checks if the given value exists in the enumeration class.
  9.      *
  10.      * @param _enumClass  Enum class
  11.      * @param value  Value
  12.      * @param <E>  /
  13.      * @return  True if exists
  14.      */
  15.     public static <E extends Enum<E>> boolean isValid(Class<E> _enumClass, String value) {
  16.         try {
  17.             return EnumSet.allOf(_enumClass).contains(Enum.valueOf(_enumClass, value));
  18.         } catch (Exception e) {
  19.             return false;
  20.         }
  21.     }
  22. }
  23.  
  24. package com.duplxey.nquiz.util;
  25.  
  26. import java.io.*;
  27. import java.util.stream.Collectors;
  28.  
  29. public class FileUtil {
  30.  
  31.     /**
  32.      * Gets the file's content as a string.
  33.      *
  34.      * @param path  File's path
  35.      * @return  Content
  36.      */
  37.     public static String getFileContent(String path) {
  38.         StringBuilder builder = new StringBuilder();
  39.         try {
  40.             BufferedReader br = new BufferedReader(new FileReader(path));
  41.             String line;
  42.             while ((line = br.readLine()) != null) {
  43.                 builder.append(line).append("\n");
  44.             }
  45.             br.close();
  46.         } catch (IOException e) {
  47.             e.printStackTrace();
  48.         }
  49.         return builder.toString();
  50.     }
  51.  
  52.     /**
  53.      * Replaces the file's content with the given string.
  54.      *
  55.      * @param path  File's path
  56.      * @param content  Content
  57.      */
  58.     public static void writeToFile(String path, String content) {
  59.         try {
  60.             BufferedWriter writer = new BufferedWriter(new FileWriter(path));
  61.             writer.write(content);
  62.             writer.close();
  63.         } catch (IOException e) {
  64.             e.printStackTrace();
  65.         }
  66.     }
  67.  
  68.     /**
  69.      * Reads the given resource file as a string.
  70.      *
  71.      * @param fileName the path to the resource file
  72.      * @return the file's contents or null if the file could not be opened
  73.      */
  74.     public static String getResourceContent(String fileName) {
  75.         InputStream is = FileUtil.class.getClassLoader().getResourceAsStream(fileName);
  76.         if (is != null) {
  77.             BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  78.             return reader.lines().collect(Collectors.joining(System.lineSeparator()));
  79.         }
  80.         return null;
  81.     }
  82. }
  83.  
  84. package com.duplxey.nquiz.util;
  85.  
  86. import com.google.gson.Gson;
  87.  
  88. /**
  89.  * Singleton for Gson
  90.  * (I couldn't find anything in the documentation clarifying that you should share the instance of gson or create a new one every time)
  91.  */
  92. public class GsonUtil {
  93.  
  94.     private Gson gson;
  95.     private static GsonUtil instance;
  96.  
  97.     public static GsonUtil getInstance() {
  98.         if (instance == null) {
  99.             instance = new GsonUtil();
  100.         }
  101.         return instance;
  102.     }
  103.  
  104.     private GsonUtil() {
  105.         gson = new Gson();
  106.     }
  107.  
  108.     public Gson getGson() {
  109.         return gson;
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement