Skip to content

Commit

Permalink
Use new Java language features
Browse files Browse the repository at this point in the history
Migrate code to use:

- record classes
- enhanced switch statements
- more
  • Loading branch information
niknetniko committed Nov 30, 2023
1 parent 88efd4b commit 496ce95
Show file tree
Hide file tree
Showing 321 changed files with 2,052 additions and 4,637 deletions.
8 changes: 6 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ android {
debug {
// Disable crashlytics in debug builds if necessary.
ext.enableCrashlytics = Boolean.parseBoolean(props.getProperty("hydra.debug.reporting"))
testCoverageEnabled true
// testCoverageEnabled true
}
}

Expand Down Expand Up @@ -199,6 +199,9 @@ dependencies {
implementation 'dev.chrisbanes.insetter:insetter:0.6.1'
implementation 'com.github.niqdev:ipcam-view:2.4.0'

annotationProcessor 'io.soabase.record-builder:record-builder-processor:37'
compileOnly 'io.soabase.record-builder:record-builder-core:37'

// Dependencies for the Play Store version.
storeImplementation 'com.google.android.gms:play-services-maps:18.2.0'
storeImplementation 'com.google.firebase:firebase-analytics:21.4.0'
Expand Down Expand Up @@ -230,9 +233,10 @@ dependencies {
testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.15.2'
testImplementation 'com.shazam:shazamcrest:0.11'
testImplementation 'org.skyscreamer:jsonassert:1.5.1'
testImplementation 'org.jeasy:easy-random-core:5.0.0'
testImplementation 'org.jeasy:easy-random-core:6.0.0-SNAPSHOT'
testImplementation 'org.apache.commons:commons-lang3:3.13.0'
testImplementation 'commons-validator:commons-validator:1.7'
testImplementation 'com.google.guava:guava:32.1.3-jre'
}


Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@

<!-- Details about association events. -->
<activity
android:name=".association.event.EventDetailsActivity"
android:name=".association.EventDetailsActivity"
android:label="@string/event_detail_activity_title"
android:launchMode="singleTop"
android:parentActivityName=".MainActivity" />
Expand Down
18 changes: 4 additions & 14 deletions app/src/main/java/be/ugent/zeus/hydra/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -683,24 +683,14 @@ public interface OnBackPressed {
private static final class TutorialEndEvent implements Event {
@Nullable
@Override
public String getEventName() {
public String eventName() {
return Reporting.getEvents().tutorialComplete();
}
}

/**
* Groups an update for the navigation drawer.
*/
private static class DrawerUpdate {
@NavigationSource
final int navigationSource;
final Fragment fragment;
final MenuItem menuItem;

private DrawerUpdate(int navigationSource, Fragment fragment, MenuItem menuItem) {
this.navigationSource = navigationSource;
this.fragment = fragment;
this.menuItem = menuItem;
* Groups an update for the navigation drawer.
*/
private record DrawerUpdate(@NavigationSource int navigationSource, Fragment fragment, MenuItem menuItem) {
}
}
}
101 changes: 30 additions & 71 deletions app/src/main/java/be/ugent/zeus/hydra/association/Association.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,49 +26,47 @@
import android.os.Parcelable;
import androidx.annotation.Nullable;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* Represents an association registered with the DSA.
*
* @author feliciaan
* @author Niko Strijbol
*/
public final class Association implements Parcelable {

private String abbreviation;
private String name;
private List<String> path;
@Nullable
private String description;
private String email;
@Nullable
private String logo;
@Nullable
private String website;

public Association() {
// Moshi uses this!
}

/** @noinspection ProtectedMemberInFinalClass*/
protected Association(Parcel in) {
abbreviation = in.readString();
name = in.readString();
path = in.createStringArrayList();
description = in.readString();
email = in.readString();
logo = in.readString();
website = in.readString();
public record Association(
String abbreviation,
String name,
List<String> path,
@Nullable String description,
@Nullable String email,
@Nullable String logo,
@Nullable String website
) implements Parcelable {

private Association(Parcel in) {
this(
in.readString(),
in.readString(),
in.createStringArrayList(),
in.readString(),
in.readString(),
in.readString(),
in.readString()
);
}

public static Association unknown(String name) {
Association association = new Association();
association.abbreviation = "unknown";
association.name = name;
association.description = "Onbekende vereniging";
return association;
return new Association(
"unknown",
name,
Collections.emptyList(),
"Onbekende vereniging",
null,
null,
null
);
}

@Override
Expand Down Expand Up @@ -98,43 +96,4 @@ public Association[] newArray(int size) {
return new Association[size];
}
};

@Nullable
public String getDescription() {
return description;
}

@Nullable
public String getWebsite() {
return website;
}

/**
* @return A name for this association. If a full name is available, that is returned. If not, the display name is.
*/
public String getName() {
return name;
}

public String getAbbreviation() {
return abbreviation;
}

@Nullable
public String getImageLink() {
return logo;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Association that = (Association) o;
return Objects.equals(abbreviation, that.abbreviation);
}

@Override
public int hashCode() {
return Objects.hash(abbreviation);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 The Hydra authors
* Copyright (c) 2023 Niko Strijbol
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,33 +20,28 @@
* SOFTWARE.
*/

package be.ugent.zeus.hydra.association.event;
package be.ugent.zeus.hydra.association;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
* @author Niko Strijbol
* Top-level response object for the DSA API.
* <p>
* This is basically an object with an association list inside it.
* While the list won't be null most of the time, we do not control this API,
* so we assume is can be null to have a more robust app.
*/
public final class EventPage {

/** @noinspection unused*/
private List<Event> entries;

public List<Event> getEntries() {
return entries;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EventPage eventPage = (EventPage) o;
return Objects.equals(entries, eventPage.entries);
}

public record AssociationList(
@Nullable List<Association> associations
) {
@NonNull
@Override
public int hashCode() {
return Objects.hash(entries);
public List<Association> associations() {
return Objects.requireNonNullElse(associations, Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021 The Hydra authors
* Copyright (c) 2023 Niko Strijbol
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -20,24 +20,41 @@
* SOFTWARE.
*/

package be.ugent.zeus.hydra.association.common;
package be.ugent.zeus.hydra.association;

import android.util.Pair;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import be.ugent.zeus.hydra.association.Association;

/**
* Represents a mapping of association abbreviation to their object.
* A class representing a map of associations.
*
* @author Niko Strijbol
* <p>
* The AssociationMap class provides methods to access associations based on their abbreviation,
* check if an association was requested to be shown, and combine the requested associations with
* the association list.
* </p>
*/
public interface AssociationMap {
public class AssociationMap {

private final Map<String, Association> associationMap;
private final Set<String> associationRequested;

public AssociationMap() {
this(Collections.emptyList(), Collections.emptySet());
}

public AssociationMap(@NonNull List<Association> list, @NonNull Set<String> requestedAssociations) {
this.associationMap = new HashMap<>();
for (var association : list) {
associationMap.put(association.abbreviation(), association);
}
this.associationRequested = requestedAssociations;
}

/**
* Get the association linked to the specified mapping.
Expand All @@ -49,29 +66,35 @@ public interface AssociationMap {
* @return The association.
*/
@NonNull
Association get(@Nullable String abbreviation);
public Association get(@Nullable String abbreviation) {
return associationMap.computeIfAbsent(abbreviation, Association::unknown);
}

/**
* @return A stream of all known associations.
*/
Stream<Association> associations();
@NonNull
public Stream<Association> associations() {
return associationMap.values().stream();
}

/**
* Check if the associations was requested to be shown by the request that
* Check if the association was requested to be shown by the request that
* produced this association map.
*
*
* @param abbreviation The abbreviation.
*
* @return True if requested to be shown, false otherwise.
*/
boolean isRequested(@NonNull String abbreviation);
public boolean isRequested(@NonNull String abbreviation) {
return associationRequested.contains(abbreviation);
}

/**
* Combine the {@link #isRequested(String)} data with the association list.
*/
default List<Pair<Association, Boolean>> getSelectedAssociations() {
public List<Pair<Association, Boolean>> requestedAssociations() {
return this.associations()
.map(a -> Pair.create(a, this.isRequested(a.getAbbreviation())))
.map(a -> Pair.create(a, this.isRequested(a.abbreviation())))
.collect(Collectors.toList());
}
}
Loading

0 comments on commit 496ce95

Please sign in to comment.