MCPcopy Create free account
hub / github.com/Berkeley-CS61B/skeleton-sp23 / NodeChecker

Class NodeChecker

proj1a/tests/NodeChecker.java:11–229  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

9import static org.junit.jupiter.api.Assertions.fail;
10
11public class NodeChecker {
12
13 /**
14 * This method checks whether a given class is a linked node or not.
15 *
16 * @param clazz the class you want to check
17 * @param doublyLinked whether or not the list <em>can</em> be doubly linked
18 */
19 public static boolean isNode(Class<?> clazz, boolean doublyLinked) {
20 // Get fields
21 SortedSet<String> fields = Stream
22 .of(clazz.getDeclaredFields())
23 .filter(f -> !f.isSynthetic())
24 .map(Field::getName)
25 .collect(Collectors.toCollection(TreeSet::new));
26
27 boolean hasData = false;
28 int nodeFields = 0;
29
30 // Check fields
31 for (String field : fields) {
32 Field f = null;
33 try {
34 f = clazz.getDeclaredField(field);
35 f.setAccessible(true);
36 } catch (NoSuchFieldException ex) {
37 ex.printStackTrace();
38 fail();
39 }
40
41 if (f.getType().equals(clazz)) {
42 // Linked to another node
43 nodeFields++;
44 if (nodeFields == 2 && !doublyLinked) {
45 // Returns false if the list is doubly linked
46 return false;
47 } else if (nodeFields == 3) {
48 // Don't allow triply linked and up
49 return false;
50 }
51 } else if (f.getType().equals(Object.class)) {
52 // Has a generic type to store data
53 if (hasData) {
54 // Checks for multiple data fields
55 return false;
56 }
57 hasData = true;
58 } else {
59 return false;
60 }
61 }
62
63 // Get constructors
64 Constructor<?>[] constructors = clazz.getDeclaredConstructors();
65
66 // Checks arguments to the constructors
67 for (Constructor<?> c : constructors) {
68 boolean hasGenericArgument = false;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected