• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ljacqu / wordeval / 14560783068

20 Apr 2025 03:14PM UTC coverage: 53.402% (+0.08%) from 53.324%
14560783068

push

github

ljacqu
Document all word forms with examples

268 of 576 branches covered (46.53%)

730 of 1367 relevant lines covered (53.4%)

3.09 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.85
/src/main/java/ch/jalu/wordeval/DataUtils.java
1
package ch.jalu.wordeval;
2

3
import com.google.gson.Gson;
4
import com.google.gson.GsonBuilder;
5
import lombok.Getter;
6
import lombok.NoArgsConstructor;
7

8
import java.io.File;
9
import java.io.IOException;
10
import java.io.UncheckedIOException;
11
import java.lang.reflect.Type;
12
import java.nio.file.Files;
13
import java.nio.file.Paths;
14
import java.util.List;
15

16
/**
17
 * Wrapper for interaction with the file system and JSON operations.
18
 */
19
@NoArgsConstructor
20
public class DataUtils {
21
  
22
  @Getter
3✔
23
  private String root = "";
24
  
25
  @Getter
3✔
26
  private boolean usePrettyPrint = false;
27
  
28
  @Getter(lazy = true)
5✔
29
  private final Gson gson = createGson();
30
  
31
  /**
32
   * Initializes an instance and sets whether to use JSON Pretty Print or not.
33
   *
34
   * @param usePrettyPrint JSON pretty print setting
35
   */
36
  public DataUtils(boolean usePrettyPrint) {
2✔
37
    this.usePrettyPrint = usePrettyPrint;
3✔
38
  }
1✔
39
  
40
  /**
41
   * Sets the root path (the path to append to file references).
42
   *
43
   * @param root the root to append to file paths
44
   */
45
  public void setRoot(String root) {
46
    if (!root.isEmpty() && !root.endsWith("/") && !root.endsWith(File.separator)) {
11!
47
      this.root = root + File.separator;
6✔
48
    } else {
49
      this.root = root; 
3✔
50
    }
51
  }
1✔
52
  
53
  /**
54
   * Writes the content to the given file.
55
   *
56
   * @param filename the name of the file to write to
57
   * @param content the content to write
58
   */
59
  public void writeToFile(String filename, String content) {
60
    try {
61
      Files.write(Paths.get(root + filename), content.getBytes());
×
62
    } catch (IOException e) {
×
63
      throw new UncheckedIOException("Could not write to file '" + filename + "'", e);
×
64
    }
×
65
  }
×
66
  
67
  /**
68
   * Reads a file's contents as UTF-8.
69
   *
70
   * @param filename the name of the file to read
71
   * @return the contents of the file
72
   */
73
  public static String readFile(String filename) {
74
    try {
75
      return Files.readString(Paths.get(filename));
×
76
    } catch (IOException e) {
×
77
      throw new UncheckedIOException("Could not read file '" + filename + "'", e);
×
78
    }
79
  }
80

81
  /**
82
   * Reads all lines of a file as UTF-8.
83
   *
84
   * @param filename the name of the file to read
85
   * @return the file's contents by line
86
   */
87
  public static List<String> readAllLines(String filename) {
88
    try {
89
      return Files.readAllLines(Paths.get(filename));
×
90
    } catch (IOException e) {
×
91
      throw new UncheckedIOException("Could not read from file '" + filename + "'", e);
×
92
    }
93
  }
94
  
95
  /**
96
   * Converts an object to its JSON representation.
97
   *
98
   * @param o the object to convert
99
   * @return the generated JSON
100
   */
101
  public String toJson(Object o) {
102
    return getGson().toJson(o);
5✔
103
  }
104

105
  /**
106
   * Deserializes JSON to the specified type.
107
   *
108
   * @param <T> the result type
109
   * @param json the JSON text to deserialize
110
   * @param typeOfT the type of the resulting object
111
   * @return resulting object
112
   */
113
  public <T> T fromJson(String json, Type typeOfT) {
114
    return getGson().fromJson(json, typeOfT);
×
115
  }
116
  
117
  private Gson createGson() {
118
    if (usePrettyPrint) {
3✔
119
      return new GsonBuilder().setPrettyPrinting().create();
6✔
120
    }
121
    return new Gson();
4✔
122
  }
123

124
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc