diff options
author | Jonas Smedegaard <dr@jones.dk> | 2025-04-29 08:09:28 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2025-04-29 08:09:28 +0200 |
commit | 3738c1fc8b2fa92b819b1ff948b3b39de60757b7 (patch) | |
tree | c5db7a878770da6c8180cc468184712d585b0d88 | |
parent | 794be4694f1a1fa36d87543aab840331c6d39745 (diff) |
always use brace in for- and if-construct
11 files changed, 120 insertions, 55 deletions
diff --git a/src/com.example.portfolio2/com/example/portfolio2/Database.java b/src/com.example.portfolio2/com/example/portfolio2/Database.java index a4a9271..a6a144a 100644 --- a/src/com.example.portfolio2/com/example/portfolio2/Database.java +++ b/src/com.example.portfolio2/com/example/portfolio2/Database.java @@ -29,8 +29,9 @@ class Database { /// @param name activity name /// @return index of activity as integer int getActivityIndeks(final String name) { - if (name == null) + if (name == null) { return -1; + } ArrayList<String> result = db.query("select indeks from activity a where name is '" + name + "';", "indeks"); return Integer.parseInt(result.getFirst()); @@ -69,11 +70,13 @@ class Database { /// @param program programme name /// @return ECTS points as String String getSumEcts(final String program) { - if (program == null) + if (program == null) { return "0"; + } ArrayList<String> result = db.query("select sum(activity.ects) as total_ects,student.name from student left outer join participation on student.studid = participation.studid inner join activity on participation.indeks = activity.indeks where program is '" + program + "' group by student.studid ;", "total_ects"); - if (result.isEmpty()) + if (result.isEmpty()) { return "0"; + } return result.getFirst(); } diff --git a/src/com.example.portfolio2/com/example/portfolio2/MyDB.java b/src/com.example.portfolio2/com/example/portfolio2/MyDB.java index 9b02d39..ea0f6d6 100644 --- a/src/com.example.portfolio2/com/example/portfolio2/MyDB.java +++ b/src/com.example.portfolio2/com/example/portfolio2/MyDB.java @@ -15,7 +15,9 @@ public class MyDB { /// foo MyDB() { - if (conn == null) open(); + if (conn == null) { + open(); + } } /// foo @@ -25,7 +27,9 @@ public class MyDB { conn = DriverManager.getConnection(url); } catch (SQLException e) { System.out.println("cannot open"); - if (conn != null) close(); + if (conn != null) { + close(); + } throw new RuntimeException(e); } ; @@ -34,7 +38,9 @@ public class MyDB { /// foo public void close() { try { - if (conn != null) conn.close(); + if (conn != null) { + conn.close(); + } } catch (SQLException e) { throw new RuntimeException(e); } @@ -44,7 +50,9 @@ public class MyDB { /// foo /// @param sql foo public void cmd(final String sql) { - if (conn == null) open(); + if (conn == null) { + open(); + } if (conn == null) { System.out.println("No connection"); return; @@ -73,7 +81,9 @@ public class MyDB { /// @return foo public ArrayList<String> query(final String query, final String fld) { ArrayList<String> res = new ArrayList<>(); - if (conn == null) open(); + if (conn == null) { + open(); + } if (conn == null) { System.out.println("No connection"); throw new RuntimeException("No connection"); diff --git a/src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java b/src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java index 793260a..dbd4aa8 100644 --- a/src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java +++ b/src/com.example.portfolio3/com/example/portfolio3/AbstractGraph.java @@ -22,8 +22,9 @@ public abstract class AbstractGraph implements Graph { /// @param s foo /// @return Vertex public Vertex vertex(final String s) { - if (vertexMap.containsKey(s)) + if (vertexMap.containsKey(s)) { return vertexMap.get(s); + } Vertex v = new Vertex(s); vertexMap.put(s, v); vertexSet.add(v); diff --git a/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java b/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java index 88e54d6..f90172e 100644 --- a/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java +++ b/src/com.example.portfolio3/com/example/portfolio3/AdjListGraph.java @@ -20,23 +20,27 @@ public class AdjListGraph extends AbstractGraph { /// foo public void insertEdge(final Vertex v1, final Vertex v2, final int w) { Edge e = new Edge(v1, v2, w); - if (!outEdge.containsKey(e.from())) + if (!outEdge.containsKey(e.from())) { outEdge.put(e.from(), new HashSet<Edge>()); + } outEdge.get(e.from()).add(e); } /// foo public Collection<Edge> edges() { Set<Edge> edges = new HashSet<>(); - for (Vertex v: outEdge.keySet())edges.addAll(outEdge.get(v)); + for (Vertex v: outEdge.keySet()) { + edges.addAll(outEdge.get(v)); + } return edges; } /// foo public Collection<Edge> outEdge(final Vertex v) { - if (!outEdge.containsKey(v)) + if (!outEdge.containsKey(v)) { return new HashSet<Edge>(); + } return outEdge.get(v); } @@ -45,11 +49,13 @@ public class AdjListGraph extends AbstractGraph { public Integer getWeight(final Vertex v1, final Vertex v2) { // linear in number of outedges from vertices - if (!outEdge.containsKey(v1)) + if (!outEdge.containsKey(v1)) { return null; + } for (Edge e: outEdge.get(v1)) { - if (e.to() == v2) + if (e.to() == v2) { return e.weight(); + } } return null; diff --git a/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java b/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java index e85db6c..b538f24 100644 --- a/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java +++ b/src/com.example.portfolio3/com/example/portfolio3/AdjMapGraph.java @@ -20,17 +20,20 @@ class AdjMapGraph extends AbstractGraph { /// foo public void insertEdge(final Vertex v1, final Vertex v2, final int w) { Edge e = new Edge(v1,v2, w); - if (!outEdge.containsKey(e.from())) + if (!outEdge.containsKey(e.from())) { outEdge.put(e.from(), new HashMap<Vertex, Edge>()); + } outEdge.get(e.from()).put(e.to(), e); } /// foo public Collection<Edge> edges() { Set<Edge> edges = new HashSet<>(); - for (Vertex v : outEdge.keySet()) - for (Vertex w : outEdge.get(v).keySet()) + for (Vertex v : outEdge.keySet()) { + for (Vertex w : outEdge.get(v).keySet()) { edges.add(outEdge.get(v).get(w)); + } + } return edges; } @@ -43,10 +46,12 @@ class AdjMapGraph extends AbstractGraph { /// foo public Integer getWeight(final Vertex v1, final Vertex v2) { // constant time operation - if (!outEdge.containsKey(v1)) + if (!outEdge.containsKey(v1)) { return null; - if (!outEdge.get(v1).containsKey(v2)) + } + if (!outEdge.get(v1).containsKey(v2)) { return null; + } return outEdge.get(v1).get(v2).weight(); } diff --git a/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java b/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java index 8bd9259..2f3b036 100644 --- a/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java +++ b/src/com.example.portfolio3/com/example/portfolio3/EdgeGraph.java @@ -29,9 +29,11 @@ class EdgeGraph extends AbstractGraph { /// foo public Collection<Edge> outEdge(final Vertex v) { ArrayList<Edge> outEdge = new ArrayList<>(); - for (Edge e: edges) - if (e.from() == v) + for (Edge e: edges) { + if (e.from() == v) { outEdge.add(e); + } + } return outEdge; } @@ -40,9 +42,10 @@ class EdgeGraph extends AbstractGraph { public Integer getWeight(final Vertex v1, final Vertex v2) { // linear in number of edges in the graph - for (Edge e:edges) { - if (e.from() == v1 && e.to() == v2) + for (Edge e: edges) { + if (e.from() == v1 && e.to() == v2) { return e.weight(); + } } return null; diff --git a/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java b/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java index 6b47bd2..5c0dd80 100644 --- a/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java +++ b/src/com.example.portfolio3/com/example/portfolio3/GraphAlgorithms.java @@ -40,8 +40,9 @@ public class GraphAlgorithms { /// @return boolean public static boolean isPath(final List<Edge> edges) { for (int i = 1; i < edges.size(); i++) { - if (edges.get(i - 1).to() != edges.get(i).from()) + if (edges.get(i - 1).to() != edges.get(i).from()) { return false; + } } return true; @@ -58,8 +59,9 @@ public class GraphAlgorithms { int length = 0; for (int i = 1; i < path.size(); i++) { Integer w = g.getWeight(path.get(i - 1),path.get(i)); - if (w == null) + if (w == null) { return null; + } length += w; } @@ -79,10 +81,12 @@ public class GraphAlgorithms { /// @return int static int cmpEdgeWeight(final Edge e1, final Edge e2) { int w1 = e1.weight(), w2 = e2.weight(); - if (w1 != w2) + if (w1 != w2) { return w1 - w2; - if (e1.from() != e2.from()) + } + if (e1.from() != e2.from()) { return e1.from().name().compareTo(e2.from().name()); + } return e1.to().name().compareTo(e2.to().name()); } @@ -95,11 +99,13 @@ public class GraphAlgorithms { /// @param e2 foo /// @return int static int cmpEdgeFrom(final Edge e1, final Edge e2) { - if (e1.from() != e2.from()) + if (e1.from() != e2.from()) { return e1.from().name().compareTo(e2.from().name()); + } int w1 = e1.weight(), w2 = e2.weight(); - if (w1 != w2) + if (w1 != w2) { return w1 - w2; + } return e1.to().name().compareTo(e2.to().name()); } @@ -112,10 +118,12 @@ public class GraphAlgorithms { /// @param e2 foo /// @return int static int cmpEdgeTo(final Edge e1, final Edge e2) { - if (e1.to() != e2.to()) + if (e1.to() != e2.to()) { return e1.to().name().compareTo(e2.to().name()); - if (e1.from() != e2.from()) + } + if (e1.from() != e2.from()) { return e1.from().name().compareTo(e2.from().name()); + } int w1 = e1.weight(), w2 = e2.weight(); return w1 - w2; @@ -186,13 +194,16 @@ public class GraphAlgorithms { //System.out.println("visited " + w); visited.add(w); Collection<Edge> outedge = g.outEdge(w); - if (outedge == null) + if (outedge == null) { continue; + } for (Edge e: outedge) { - if (visited.contains(e.to())) + if (visited.contains(e.to())) { continue; - if (thisLevel.contains(e.to())) + } + if (thisLevel.contains(e.to())) { continue; + } nextLevel.add(e.to()); } } @@ -222,12 +233,14 @@ public class GraphAlgorithms { /// @param v foo /// @param visited foo private static void visitDepthFirst(final Graph g, final Vertex v, final Set<Vertex> visited) { - if (visited.contains(v)) + if (visited.contains(v)) { return; + } //System.out.println("visited "+v); visited.add(v); - for (Edge e: g.outEdge(v)) + for (Edge e: g.outEdge(v)) { visitDepthFirst(g, e.to(), visited); + } } /// an implementation of Prim's algorithm @@ -247,15 +260,19 @@ public class GraphAlgorithms { while (true) { Edge nearest = null; for (Edge e: edges) { - if (!frontier.contains(e.from())) + if (!frontier.contains(e.from())) { continue; - if (frontier.contains(e.to())) + } + if (frontier.contains(e.to())) { continue; - if (nearest == null || nearest.weight() > e.weight()) + } + if (nearest == null || nearest.weight() > e.weight()) { nearest = e; + } } - if (nearest == null) + if (nearest == null) { break; + } mst.add(nearest); frontier.add(nearest.to()); } @@ -278,8 +295,9 @@ public class GraphAlgorithms { HashSet<Vertex> done = new HashSet<>(); HashMap<Vertex,Edge> prev = new HashMap<>(); HashMap<Vertex,Integer> weight = new HashMap<>(); - for (Vertex w: g.vertices()) + for (Vertex w: g.vertices()) { weight.put(w, maxint); + } // start node is done, distance 0 from start weight.put(start, 0); @@ -294,7 +312,9 @@ public class GraphAlgorithms { for (Vertex w1:done) { for (Edge e :g.outEdge(w1)) { Vertex w2 = e.to(); - if (done.contains(w2)) continue; + if (done.contains(w2)) { + continue; + } if ((weight.get(w1) + e.weight()) < neardist) { nearest = e.to(); neardist = weight.get(w1) + e.weight(); @@ -305,7 +325,9 @@ public class GraphAlgorithms { // System.out.println("find nearest "+done2near); // if no more, then we are done - if (nearest == null) break; + if (nearest == null) { + break; + } // update distance from this node to other nodes for (Edge e1 : g.outEdge(nearest)) { @@ -338,11 +360,13 @@ public class GraphAlgorithms { try { BufferedReader in = new BufferedReader(new FileReader(file)); for (String line = in.readLine(); line != null; line = in.readLine()) { - if (line.length() == 0) + if (line.length() == 0) { continue; + } String[] arr = line.split(","); - if (arr.length != 3) + if (arr.length != 3) { throw new RuntimeException("CSV file format error: " + line); + } g.insertEdge(arr[0].trim(), arr[1].trim(), Integer.parseInt(arr[2].trim())); g.insertEdge(arr[1].trim(), arr[0].trim(), Integer.parseInt(arr[2].trim())); } @@ -358,8 +382,9 @@ public class GraphAlgorithms { public static void printGraph(final Graph g) { for (Vertex v: sortVertex(g.vertices())) { System.out.println(v.toString()); - for (Edge e: sortEdgesTo(g.outEdge(v))) + for (Edge e: sortEdgesTo(g.outEdge(v))) { System.out.println(" " + e.toString()); + } } } @@ -389,8 +414,9 @@ public class GraphAlgorithms { BufferedReader in = new BufferedReader(new FileReader(f)); while (true) { String s = in.readLine(); - if (s == null) + if (s == null) { break; + } list.add(s); } in.close(); diff --git a/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java b/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java index 5a1f327..67c095e 100644 --- a/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java +++ b/src/com.example.portfolio3/com/example/portfolio3/MatrixGraph.java @@ -37,11 +37,13 @@ public class MatrixGraph extends AbstractGraph { /// @param v vertex /// @return int private int getIndex(final Vertex v) { - if (vertex2index.containsKey(v)) + if (vertex2index.containsKey(v)) { return vertex2index.get(v); + } int index = vertex2index.size(); - if (index >= index2vertex.length) + if (index >= index2vertex.length) { throw new RuntimeException("Too many vertices in graph"); + } vertex2index.put(v, index); index2vertex[index] = v; @@ -59,7 +61,9 @@ public class MatrixGraph extends AbstractGraph { for (int i = 0; i < numVertex; i++) { for (int j = 0; j < numVertex; j++) { Integer weight = matrix[i][j]; // may be null - if (weight == null) continue; + if (weight == null) { + continue; + } edges.add(new Edge(index2vertex[i],index2vertex[j],weight)); } } @@ -73,7 +77,9 @@ public class MatrixGraph extends AbstractGraph { int i = vertex2index.get(v1); for (int j = 0; j < numVertex; j++) { Integer weight = matrix[i][j]; // may be null - if (weight == null) continue; + if (weight == null) { + continue; + } edges.add(new Edge(v1,index2vertex[j],weight)); } diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Control.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Control.java index d004c6a..a90aca1 100644 --- a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Control.java +++ b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Control.java @@ -72,8 +72,9 @@ public class Control { /// Display list of activity entries public void showActivities() { String toarea = ""; - for (String t: model.getActivities()) + for (String t: model.getActivities()) { toarea += t + "\n"; + } view.setArea(toarea); } diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/GUI.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/GUI.java index 50c0a61..cd52b3c 100644 --- a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/GUI.java +++ b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/GUI.java @@ -48,8 +48,9 @@ public class GUI { /// Delete last activity from list public void delOneActivity() { - if (list.size() > 0) + if (list.size() > 0) { list.remove(list.size() - 1); + } } /// Delete all activities from list diff --git a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java index 29b4759..a67456c 100644 --- a/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java +++ b/src/dk.biks.bachelorizer/dk/biks/bachelorizer/Graph.java @@ -184,8 +184,9 @@ public final class Graph { Set<Vertex> isolated = all_set.stream() .filter(x -> { - if (x == current) + if (x == current) { return false; + } Integer weight = g.getWeight(current, x); return weight == null && !done.contains(x); @@ -209,7 +210,9 @@ public final class Graph { AbstractGraph h = new AdjListGraph(); for (Set<Vertex> s: sets) { for (Set<Vertex> t: sets) { - if (t == s) continue; + if (t == s) { + continue; + } int sum = 0; for (Vertex v: s) { // get num of students with this combination of modules. |