Skip to content

Commit

Permalink
Add ci for coding style (#2)
Browse files Browse the repository at this point in the history
* Create style.yml

* Google Java Format

* changed commit message

* removed pr from style action

* use AOSP

* Fixed styles from CI

* Apply google-java-formatter-1.10.0

* Add 'format.sh' script to call google-java-formatter

In order to work one must crete a file `./.env` that defines
`GOOGLE_JAVA_FORMATTER_JAR` as environment variable pointing to the
location of the google-java-formatter jar file.

Example `./.env`
```
GOOGLE_JAVA_FORMATTER_JAR="/usr/local/Cellar/google-java-format/1.10.0/libexec/google-java-format.jar"
```

* fixed ci

* formatting

* added distribution

* fix

* added code style violation

* removed return value

* Apply production build from CI

Co-authored-by: github-actions[bot] <action@github.com>

Co-authored-by: github-actions <>
Co-authored-by: Oliver Herrmann <herrmann@id-on.de>
Co-authored-by: github-actions[bot] <action@github.com>
  • Loading branch information
3 people authored Jun 13, 2021
1 parent ab2e87b commit d2de907
Show file tree
Hide file tree
Showing 28 changed files with 154 additions and 123 deletions.
5 changes: 0 additions & 5 deletions .classpath
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-16">
<attributes>
<attribute name="maven.pomderived" value="true"/>
Expand Down
41 changes: 41 additions & 0 deletions .github/workflows/style.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Example workflow
name: Format

on:
push:

jobs:
formatting:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2 # v2 minimum required

- name: Setup Java
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: '16'

- name: Setup google-java-format
run: |
wget https://github.com/google/google-java-format/releases/download/v1.10.0/google-java-format-1.10.0-all-deps.jar
echo -e "GOOGLE_JAVA_FORMATTER_JAR=google-java-format-1.10.0-all-deps.jar" >> .env
- name: Format code
run: /bin/sh bin/format.sh

- name: Commit formatted code
uses: EndBug/add-and-commit@v4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
add: 'src/'

author_name: ${{ github.event.pusher.name }}
author_email: ${{ github.event.pusher.email }}

message: 'Apply production build from CI
Co-authored-by: github-actions[bot] <action@github.com>'
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
target
.env
google-java-format-1.10.0-all-deps.jar
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ Outputs to `target/algorithm-analyzer-x.x.x.jar`
## test

`mvn test`

## formatting code
`/bin/sh format.sh`

Calls google-code-formatter locally.
17 changes: 17 additions & 0 deletions bin/format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
# set -e

# read 'GOOGLE_JAVA_FORMATTER_JAR' from env
. ./.env

java --add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
--add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
-jar $GOOGLE_JAVA_FORMATTER_JAR --set-exit-if-changed -a -r $(find . -type f -name "*.java")

retVal=$?
if [ $retVal -ne 0 ]; then
echo "[ERROR]: Some files needed formatting"
fi
9 changes: 2 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
Expand All @@ -26,6 +27,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<archive>
<manifest>
Expand Down Expand Up @@ -83,13 +85,6 @@
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<!-- <executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions> -->
<configuration>
<mainClass>${mainclass}</mainClass>
</configuration>
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/github/cbl/algorithm_analyzer/Main.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package com.github.cbl.algorithm_analyzer;

import com.github.cbl.algorithm_analyzer.contracts.Algorithm;
import com.github.cbl.algorithm_analyzer.contracts.Event;
import com.github.cbl.algorithm_analyzer.contracts.EventConsumer;
import com.github.cbl.algorithm_analyzer.contracts.Algorithm;
import com.github.cbl.algorithm_analyzer.sorts.bubblesort.BubbleSort;
import com.github.cbl.algorithm_analyzer.util.GeneralEventConsumer;
import com.github.cbl.algorithm_analyzer.util.LogEventVisitor;

public class Main {
public static void main(String[] args) throws Exception {
final Integer[] array = { 6, 5, 4, 3, 2, 1 };
final Integer[] array = {6, 5, 4, 3, 2, 1};

final Algorithm<Event,BubbleSort.Data<Integer>> a = new BubbleSort<Integer>();
final Algorithm<Event, BubbleSort.Data<Integer>> a = new BubbleSort<Integer>();
final EventConsumer<Event> ec = new GeneralEventConsumer();

a.run(ec, new BubbleSort.Data<>(array));

ec.visitEvents(new LogEventVisitor());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
public interface Algorithm<E extends Event, D> {
/**
* Execute an arbritary algorithm
*
* @param events a consumer of possible events
* @param data the run time data for this execution
*/
public void run(EventConsumer<E> events, D data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ public interface Event {
default void accept(EventVisitor visitor) {
visitor.visit(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

/**
* Event visitor interface
*
* Used to act in some way on a collection of events with the ability
* to handle each event differently based on its specific implementation.
*
*
* <p>Used to act in some way on a collection of events with the ability to handle each event
* differently based on its specific implementation.
*
* @see https://en.wikipedia.org/wiki/Visitor_pattern
*/
public interface EventVisitor {

default void visit(Event e) {}

default <T> void visit(BubbleSort.PartialStateEvent<T> e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @param <V> type of vertices
* @param <E> type of edge metadata (e.g. weight)
*/
public interface Graph<V,E> {
public interface Graph<V, E> {

abstract boolean hasEdge(V from, V to);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@

/**
* Adjacent matrix based graph for int-vertices and no edge weight
* TODO: should take type parameters
*
* <p>TODO: should take type parameters
*/
public class AdjacentMatrixGraph implements Graph<Integer,Void> {
public class AdjacentMatrixGraph implements Graph<Integer, Void> {

private boolean[][] adj;
private int verticeCount;

public AdjacentMatrixGraph(int verticeCount) {
this.verticeCount = verticeCount;
adj = new boolean[verticeCount][];
for (int i = 0; i < verticeCount; i++)
adj[i] = new boolean[verticeCount];
for (int i = 0; i < verticeCount; i++) adj[i] = new boolean[verticeCount];
}

public void setEdge(Integer from, Integer to) {
if (from >= 0 && to >= 0 && from < verticeCount && to < verticeCount)
adj[from][to] = true;
if (from >= 0 && to >= 0 && from < verticeCount && to < verticeCount) adj[from][to] = true;
}

public void deleteEdge(Integer from, Integer to) {
if (from >= 0 && to >= 0 && from < verticeCount && to < verticeCount)
adj[from][to] = false;
if (from >= 0 && to >= 0 && from < verticeCount && to < verticeCount) adj[from][to] = false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

public class Tiefensuche implements Algorithm<Event, Tiefensuche.Data> {

public static record Data (Graph<Integer,Void> graph, Integer startNode) {};
public static record Data(Graph<Integer, Void> graph, Integer startNode) {}
;

public void run(EventConsumer<Event> logger, Data data) {
// TODO: implement
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package com.github.cbl.algorithm_analyzer.sorts.InsertionSort.Events;

public class Step {

}
public class Step {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package com.github.cbl.algorithm_analyzer.sorts.InsertionSort;

public class InsertionSort {

}
public class InsertionSort {}
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
package com.github.cbl.algorithm_analyzer.sorts.bubblesort;

import java.util.StringJoiner;

import com.github.cbl.algorithm_analyzer.contracts.Algorithm;
import com.github.cbl.algorithm_analyzer.contracts.Event;
import com.github.cbl.algorithm_analyzer.contracts.EventConsumer;
import com.github.cbl.algorithm_analyzer.util.ArrayPrinter;
import com.github.cbl.algorithm_analyzer.util.ArrayWriter;
import com.github.cbl.algorithm_analyzer.util.Comparator;

public class BubbleSort<T extends Comparable<T>> implements Algorithm<Event,BubbleSort.Data<T>> {
import java.util.StringJoiner;

public static record Data<T extends Comparable<T>> (T[] array) {}
public class BubbleSort<T extends Comparable<T>> implements Algorithm<Event, BubbleSort.Data<T>> {

public static record PartialStateEvent<T>(T[] array, long comparisons, long writes) implements Event {
public static record Data<T extends Comparable<T>>(T[] array) {}

public static record PartialStateEvent<T>(T[] array, long comparisons, long writes)
implements Event {
@Override
public String toString() {
final StringJoiner sj = new StringJoiner("\n");
sj.add(ArrayPrinter.toString(array));
sj.add("Comparisons: " + comparisons);
sj.add("Writes: " + writes);

return sj.toString();
}
};
}
;

@Override
public void run(EventConsumer<Event> events, Data<T> data) {
Expand All @@ -40,10 +42,12 @@ public void run(EventConsumer<Event> events, Data<T> data) {
w.change(arr, i, i + 1);
}
}
events.accept(new PartialStateEvent<T>(arr.clone(), c.getComparisonsSnapshot(), w.getWritesSnapshot()));
events.accept(
new PartialStateEvent<T>(
arr.clone(), c.getComparisonsSnapshot(), w.getWritesSnapshot()));
} while (!wasSorted);

events.accept(new PartialStateEvent<T>(arr, c.getComparisons(), w.getWrites()));
};

}
;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
package com.github.cbl.algorithm_analyzer.trees.AvlTree;

public class AvlTree {

}
public class AvlTree {}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.github.cbl.algorithm_analyzer.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringJoiner;

/**
* Utility class for pretty-printing arrays
*/
/** Utility class for pretty-printing arrays */
public class ArrayPrinter {

/**
* Pretty-formats an array to a 'list of fields' w/ ASCII symbols
*
* @param arr the array
* @return a string representing the array
*/
Expand Down Expand Up @@ -55,5 +53,4 @@ private static String lowerBorder(List<Integer> widths) {
}
return sj.toString();
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.github.cbl.algorithm_analyzer.util;

/**
* Utility class for changing array field and storing the number of writes
*/
/** Utility class for changing array field and storing the number of writes */
public class ArrayWriter {

private long writes = 0;
private long snapshot = 0;

public <T> void change(T[] arr, int i, int j) {
assert(arr != null);
assert(i >= 0 && i < arr.length);
assert(j >= 0 && j < arr.length);
assert (arr != null);
assert (i >= 0 && i < arr.length);
assert (j >= 0 && j < arr.length);

if (i == j) {
// TODO: ist das so richtig? Wie ist das genau definiert mit den 'Schreibzugriffen'?
Expand All @@ -33,5 +31,4 @@ public long getWritesSnapshot() {
snapshot = writes;
return writes - oldSnapshot;
}

}
Loading

0 comments on commit d2de907

Please sign in to comment.