Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.duplxey.nquiz.util;
- import java.util.EnumSet;
- public class EnumUtil {
- /**
- * Checks if the given value exists in the enumeration class.
- *
- * @param _enumClass Enum class
- * @param value Value
- * @param <E> /
- * @return True if exists
- */
- public static <E extends Enum<E>> boolean isValid(Class<E> _enumClass, String value) {
- try {
- return EnumSet.allOf(_enumClass).contains(Enum.valueOf(_enumClass, value));
- } catch (Exception e) {
- return false;
- }
- }
- }
- package com.duplxey.nquiz.util;
- import java.io.*;
- import java.util.stream.Collectors;
- public class FileUtil {
- /**
- * Gets the file's content as a string.
- *
- * @param path File's path
- * @return Content
- */
- public static String getFileContent(String path) {
- StringBuilder builder = new StringBuilder();
- try {
- BufferedReader br = new BufferedReader(new FileReader(path));
- String line;
- while ((line = br.readLine()) != null) {
- builder.append(line).append("\n");
- }
- br.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return builder.toString();
- }
- /**
- * Replaces the file's content with the given string.
- *
- * @param path File's path
- * @param content Content
- */
- public static void writeToFile(String path, String content) {
- try {
- BufferedWriter writer = new BufferedWriter(new FileWriter(path));
- writer.write(content);
- writer.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * Reads the given resource file as a string.
- *
- * @param fileName the path to the resource file
- * @return the file's contents or null if the file could not be opened
- */
- public static String getResourceContent(String fileName) {
- InputStream is = FileUtil.class.getClassLoader().getResourceAsStream(fileName);
- if (is != null) {
- BufferedReader reader = new BufferedReader(new InputStreamReader(is));
- return reader.lines().collect(Collectors.joining(System.lineSeparator()));
- }
- return null;
- }
- }
- package com.duplxey.nquiz.util;
- import com.google.gson.Gson;
- /**
- * Singleton for Gson
- * (I couldn't find anything in the documentation clarifying that you should share the instance of gson or create a new one every time)
- */
- public class GsonUtil {
- private Gson gson;
- private static GsonUtil instance;
- public static GsonUtil getInstance() {
- if (instance == null) {
- instance = new GsonUtil();
- }
- return instance;
- }
- private GsonUtil() {
- gson = new Gson();
- }
- public Gson getGson() {
- return gson;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement