Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/date context alignment fallback #2060

Merged
merged 10 commits into from
Aug 31, 2021

Conversation

thoniTUB
Copy link
Collaborator

before the fallback for a date context alignment was determined by the order of the hash map. Now an ordered data structure is used there

if(resolution.supportsAlignment(alignmentHint) ) {
return alignmentHint;
}
return resolution.getDefaultAlignment();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warum nimmst du hier den Default?

Copy link
Collaborator Author

@thoniTUB thoniTUB Aug 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Njimefo
Im ExportFormular kann der Nutzer verschiedene zeitliche Stratifizierungen auswählen, also ob Events über Tage, Quartale oder Jahre zusammen gefasst werden sollen (Monate, Wochen wären theoretisch auch möglich). Das ist die Resolution, also die Auflösung der Stratifizierung.

Dann gibt es noch das kalendarische Alignment. Das gibt an, woran sich die, durch die Resolution festgelegten, Datumsbereiche ausrichten. So kannst du zum Beispiel eine Auflösung von Jahren am heutigen
Tag (31.08.2021 - 31.08.2022),
Quartal (01.07.2021 - 30.06.2022) oder
Jahr (01.01.2021 - 31.12.2021) ausrichten.

Jetzt zum Default: Die API lässt es zu, dass du ein alignmentHint gibts. Es passt aber ein gröberes Aligment nicht zu einer feineren Auflösung: Es ist nicht möglich Quartale am Kalenderjahr auszurichten. An dieser Stelle gibt es einen Fallback zum Default.

@@ -132,7 +132,10 @@ public String getLocalizedTypeLabel() {
}

private static DateContext.Alignment getFittingAlignment(DateContext.Alignment alignmentHint, DateContext.Resolution resolution) {
return resolution.getSupportedAlignments().contains(alignmentHint)? alignmentHint : resolution.getSupportedAlignments().iterator().next();
if(resolution.supportsAlignment(alignmentHint) ) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=> isAlignmentSupported

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jep, das ist besser

// We need to define this in a static block so the runtime can solve this cyclic
// dependency between Resolution and alignment. Otherwise a NPE is thrown by the
// Collection
COMPLETE.compatibleAlignments = List.of(Alignment.NO_ALIGN);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Werde aus dem Kommentar leider nicht schlau, aber warum kannst du das nicht im Konstruktor machen?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Beide kompilieren, aber der Erste läuft auf Grund der NPE nicht:

import lombok.RequiredArgsConstructor;
import java.util.List;

public class Test {

	@org.junit.jupiter.api.Test
	public void test() {
		E1 e = E1.A;
	}

	@RequiredArgsConstructor
	public static enum E1{
		A(List.of(E2.a));

		private final List<E2> e;
	}

	@RequiredArgsConstructor
	public static enum E2 {
		a(List.of(E1.A));

		private final List<E1> e;
	}
}

Ohne Collection dazwischen geht es:

import lombok.RequiredArgsConstructor;
import java.util.List;

public class Test {

	@org.junit.jupiter.api.Test
	public void test() {
		E1 e = E1.A;
	}

	@RequiredArgsConstructor
	public static enum E1{
		A(E2.a);

		private final E2 e;
	}

	@RequiredArgsConstructor
	public static enum E2 {
		a(E1.A);

		private final E1 e;
	}
}

// dependency between Resolution and alignment. Otherwise a NPE is thrown by the
// Collection
COMPLETE.compatibleAlignments = List.of(Alignment.NO_ALIGN);
// Beware that the first alignment is considered the default.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kannst du das nicht einfach direkt kodieren für ein extra feld?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ich habe es jetzt als Methode definiert, dann gibt es bei der Konstruktion auch keine cyc Dependenies

}

/**
* Returns the amount of calendar alignment sub date ranges that would fit in to this resolution.
*/
@JsonIgnore
public OptionalInt getAmountForAlignment(Alignment alignment){
if (!this.compatibleAlignmentsAndAmount.containsKey(alignment)) {
if (!this.compatibleAlignments.contains(alignment)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

supportsAlignment benutzen.

@@ -270,8 +287,24 @@ public OptionalInt getAmountForAlignment(Alignment alignment){
QUARTER(CDateRange::getCoveredQuarters),
YEAR(CDateRange::getCoveredYears);

static {
NO_ALIGN.amountPerResolution = Map.of(Resolution.COMPLETE, 1);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auch hier mir leider nicht klar warum du das nicht im ctor machen kannst.

Frage mich gerade auch, ob das nicht sogar was wäre das schon in einer DatteTime Bibliothek kodiert ist.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ich glaube nicht, weil es unterschiedliche Definitionen für manches gibt. Im Finanzsektor ist ein Jahr manchmal nur 360 Tage lang.


List<DateContext> contexts = DateContext.generateRelativeContexts(event, indexPlacement, featureTime, outcomeTime, timeUnit, ExportForm.getResolutionAlignmentMap(List.of(YEARS, QUARTERS), YEAR));

assertThat(contexts).containsExactly (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uff :D

@thoniTUB thoniTUB requested a review from awildturtok August 31, 2021 14:19
thoniTUB and others added 2 commits August 31, 2021 16:21
…ryError.java

Co-authored-by: awildturtok <1553491+awildturtok@users.noreply.github.com>
*/
@JsonIgnore
public OptionalInt getAmountForResolution(Resolution resolution) {
Integer amount = getAmountPerResolution().get(resolution);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Würde ich über contains machen aber unwichtig

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Habs angepasst

import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum CalendarUnit {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doku

import lombok.RequiredArgsConstructor;
import java.util.List;

public class Test {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Die sollte jetzt raus sein

@thoniTUB thoniTUB requested a review from awildturtok August 31, 2021 14:59
@thoniTUB thoniTUB merged commit 2e51801 into release Aug 31, 2021
@delete-merged-branch delete-merged-branch bot deleted the fix/date-context-alignment-fallback branch August 31, 2021 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants