Skip to content

Commit

Permalink
Standardize Java formatting (#29)
Browse files Browse the repository at this point in the history
* Use Google Java Formatter and add standard header during build

* Apply formatting changes
  • Loading branch information
edreed authored Feb 28, 2024
1 parent 1aad77f commit fe4b4b7
Show file tree
Hide file tree
Showing 14 changed files with 441 additions and 509 deletions.
23 changes: 23 additions & 0 deletions nrgcommon/.styleguide-license
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
MIT License

Copyright (c) $YEAR Newport Robotics Group

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
13 changes: 12 additions & 1 deletion nrgcommon/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ plugins {
id 'java-library'

id 'maven-publish'
id "com.diffplug.spotless" version "6.25.0"
}

spotless {
java {
googleJavaFormat()
licenseHeaderFile "./.styleguide-license"
}
}

group = 'com.nrg948'
Expand Down Expand Up @@ -92,4 +100,7 @@ publishing {
from(components.java)
}
}
}
}

// Reformat Java files before compiling the library.
compileJava.dependsOn 'spotlessApply'
15 changes: 6 additions & 9 deletions nrgcommon/src/main/java/com/nrg948/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ of this software and associated documentation files (the "Software"), to deal
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package com.nrg948;

import com.nrg948.annotations.Annotations;
Expand All @@ -30,17 +29,15 @@ of this software and associated documentation files (the "Software"), to deal
/** A class to initialize the NRG Common Library. */
public final class Common {
/* Disallow instantiation */
private Common() {
}
private Common() {}

/**
* Initializes the NRG Common library.
*
* This method must be called in the <code>Robot.initRobot()</code> method
* before the <code>RobotContainer</code> is created.
*
* @param pkgs The packages to scan for annotations implemented by the NRG
* Common Library.
*
* <p>This method must be called in the <code>Robot.initRobot()</code> method before the <code>
* RobotContainer</code> is created.
*
* @param pkgs The packages to scan for annotations implemented by the NRG Common Library.
*/
public static void init(String... pkgs) {
Annotations.init(pkgs);
Expand Down
44 changes: 16 additions & 28 deletions nrgcommon/src/main/java/com/nrg948/annotations/Annotations.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ of this software and associated documentation files (the "Software"), to deal
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package com.nrg948.annotations;

import static org.reflections.scanners.Scanners.FieldsAnnotated;
Expand All @@ -37,44 +36,37 @@ of this software and associated documentation files (the "Software"), to deal
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.reflections.Reflections;
import org.reflections.Store;
import org.reflections.serializers.Serializer;
import org.reflections.serializers.XmlSerializer;
import org.reflections.util.ConfigurationBuilder;
import org.reflections.util.QueryFunction;

/**
* A class providing access to types annotated by the NRG Common Library
* annotations.
*/
/** A class providing access to types annotated by the NRG Common Library annotations. */
public final class Annotations {
private static Reflections reflections;

/* Disallow instantiation */
private Annotations() {
}
private Annotations() {}

/**
* Initializes the annotation metadata for the NRG Common Library.
*
* @param pkgs The packages to scan for annotations implemented by the NRG
* Common Library.
*
* @param pkgs The packages to scan for annotations implemented by the NRG Common Library.
*/
public static void init(String... pkgs) {
reflections = loadFromMetadata().orElseGet(() -> scanPackages(pkgs));
}

/**
* Creates and initializes a {@link Reflections} instance from metadata, if
* present.
*
* Reflections metadata is stored in the META-INF/reflections directory in the
* program's JAR file.
*
* @return An optional {@link Reflections} instance initialized from metadata.
* If no metadata is present, {@link Optional#empty()} is returned.
* Creates and initializes a {@link Reflections} instance from metadata, if present.
*
* <p>Reflections metadata is stored in the META-INF/reflections directory in the program's JAR
* file.
*
* @return An optional {@link Reflections} instance initialized from metadata. If no metadata is
* present, {@link Optional#empty()} is returned.
*/
private static Optional<Reflections> loadFromMetadata() {
Optional<Reflections> reflections = Optional.empty();
Expand Down Expand Up @@ -112,12 +104,9 @@ private static Optional<Reflections> loadFromMetadata() {
}

/**
* Scans the specified packages for annotations implemented by the NRG Common
* Library.
*
* @param pkgs The packages to scan for annotations implemented by the NRG
* Common Library.
*
* Scans the specified packages for annotations implemented by the NRG Common Library.
*
* @param pkgs The packages to scan for annotations implemented by the NRG Common Library.
* @return The annotation metadata for the specified packages.
*/
private static Reflections scanPackages(String... pkgs) {
Expand All @@ -133,10 +122,9 @@ private static Reflections scanPackages(String... pkgs) {

/**
* Returns the set of elements annotated with the specified annotation.
*
* @param <T> The type of element.
*
* @param <T> The type of element.
* @param query The query function.
*
* @return The set of elements annotated with the specified annotation.
*/
public static <T> Set<T> get(QueryFunction<Store, T> query) {
Expand Down
Loading

0 comments on commit fe4b4b7

Please sign in to comment.