Skip to content

Commit

Permalink
[basics] add records and sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
GradedJestRisk committed Jan 9, 2024
1 parent 27f31b2 commit d741095
Show file tree
Hide file tree
Showing 20 changed files with 365 additions and 0 deletions.
9 changes: 9 additions & 0 deletions basics/records/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

5 changes: 5 additions & 0 deletions basics/records/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
45 changes: 45 additions & 0 deletions basics/records/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.3/userguide/building_java_projects.html in the Gradle documentation.
* This project uses @Incubating APIs which are subject to change.
*/

plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

dependencies {
// This dependency is used by the application.
implementation 'com.google.guava:guava:32.1.1-jre'
testImplementation "org.assertj:assertj-core:3.25.0"
}

testing {
suites {
// Configure the built-in test suite
test {
// Use JUnit Jupiter test framework
useJUnitJupiter('5.9.3')
}
}
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

application {
// Define the main class for the application.
mainClass = 'records.App'
}
Empty file.
14 changes: 14 additions & 0 deletions basics/records/app/src/main/java/records/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package records;

public class App {
public String getGreeting() {
return "Hello World!";
}

public static void main(String[] args) {
System.out.println(new App().getGreeting());
}
}
14 changes: 14 additions & 0 deletions basics/records/app/src/test/java/records/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package records;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class AppTest {
@Test void appHasAGreeting() {
App classUnderTest = new App();
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
}
}
94 changes: 94 additions & 0 deletions basics/records/app/src/test/java/records/recordTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package records;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

// https://www.baeldung.com/java-record-keyword
@DisplayName("#record")
class recordTest {

@DisplayName("it should be instantiated")
@Test
void test1() {

// given
record Person(String name) {}
;

// when
Person john = new Person("john");

// then
assertEquals(john.name(), "john");
}

@DisplayName("it cannot be mutated")
@Test
void test2() {

// given
record Person(String name) {}
;
Person john = new Person("john");

// when
// Cannot assign a value to final variable 'name'
// john.name = "Mary";

// then
assertEquals(john.name(), "john");
}

@DisplayName("it can be compared")
@Nested
class comparison {
@DisplayName("using equals")
@Test
void test3() {

// given
record Person(String name) {}
;
Person johnDoe = new Person("john");
Person johnWayne = new Person("john");

// when
// then
assertEquals(johnDoe, johnWayne);
}

@DisplayName("using hasCode")
@Test
void test3b() {

// given
record Person(String name) {}
;
Person johnDoe = new Person("john");
Person johnWayne = new Person("john");

// when
// then
assertEquals(johnDoe.hashCode(), johnWayne.hashCode());
}
}

@DisplayName("it can be serialized")
@Test
void test4() {

// given
record Person(String name) {}
;
Person john = new Person("john");

// when
String data = john.toString();

// then
assertEquals(data, "Person[name=john]");
}
}
6 changes: 6 additions & 0 deletions basics/records/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties

org.gradle.parallel=true
org.gradle.caching=true

Binary file added basics/records/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions basics/records/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
15 changes: 15 additions & 0 deletions basics/records/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* This file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
* For more detailed information on multi-project builds, please refer to https://docs.gradle.org/8.3/userguide/building_swift_projects.html in the Gradle documentation.
* This project uses @Incubating APIs which are subject to change.
*/

plugins {
// Apply the foojay-resolver plugin to allow automatic download of JDKs
id 'org.gradle.toolchains.foojay-resolver-convention' version '0.4.0'
}

rootProject.name = 'record'
include('app')
9 changes: 9 additions & 0 deletions basics/sorting/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# https://help.github.com/articles/dealing-with-line-endings/
#
# Linux start script should use lf
/gradlew text eol=lf

# These are Windows script files and should use crlf
*.bat text eol=crlf

5 changes: 5 additions & 0 deletions basics/sorting/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Ignore Gradle project-specific cache directory
.gradle

# Ignore Gradle build output directory
build
44 changes: 44 additions & 0 deletions basics/sorting/app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.3/userguide/building_java_projects.html in the Gradle documentation.
* This project uses @Incubating APIs which are subject to change.
*/

plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
}

repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}

dependencies {
// This dependency is used by the application.
implementation("com.google.guava:guava:32.1.1-jre")
}

testing {
suites {
// Configure the built-in test suite
val test by getting(JvmTestSuite::class) {
// Use JUnit Jupiter test framework
useJUnitJupiter("5.9.3")
}
}
}

// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(17))
}
}

application {
// Define the main class for the application.
mainClass.set("sorting.App")
}
14 changes: 14 additions & 0 deletions basics/sorting/app/src/main/java/sorting/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package sorting;

public class App {
public String getGreeting() {
return "Hello World!";
}

public static void main(String[] args) {
System.out.println(new App().getGreeting());
}
}
56 changes: 56 additions & 0 deletions basics/sorting/app/src/test/java/sorting/AppTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package sorting;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class AppTest {

public class Player implements Comparable<Player> {
private int ranking;
private String name;
private int age;
public Player(int ranking, String name, int age){
this.ranking = ranking;
this.name = name;
this.age = age;
}
private int getRanking(){
return this.ranking;
}

@Override
public int compareTo(Player otherPlayer) {
return Integer.compare(getRanking(), otherPlayer.getRanking());
}

}

@DisplayName("Using Comparable interface")
@Test void sortByProperty() {
// given
List<Player> footballTeam = new ArrayList<>();
Player john = new Player(2, "John", 20);
Player roger = new Player(1, "Roger", 22);
Player steven = new Player(3, "Steven", 24);
footballTeam.add(john);
footballTeam.add(roger);
footballTeam.add(steven);

// when
Collections.sort(footballTeam);

// then
List<Player> expected = new ArrayList<>();
expected.add(roger);
expected.add(john);
expected.add(steven);

assertEquals(footballTeam, expected);
}
}
6 changes: 6 additions & 0 deletions basics/sorting/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This file was generated by the Gradle 'init' task.
# https://docs.gradle.org/current/userguide/build_environment.html#sec:gradle_configuration_properties

org.gradle.parallel=true
org.gradle.caching=true

Binary file added basics/sorting/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions basics/sorting/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
15 changes: 15 additions & 0 deletions basics/sorting/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* This file was generated by the Gradle 'init' task.
*
* The settings file is used to specify which projects to include in your build.
* For more detailed information on multi-project builds, please refer to https://docs.gradle.org/8.3/userguide/building_swift_projects.html in the Gradle documentation.
* This project uses @Incubating APIs which are subject to change.
*/

plugins {
// Apply the foojay-resolver plugin to allow automatic download of JDKs
id("org.gradle.toolchains.foojay-resolver-convention") version "0.4.0"
}

rootProject.name = "sorting"
include("app")

0 comments on commit d741095

Please sign in to comment.