Browse Source

DateSortLinkedMultiValueMap

Andrew Grant 3 months ago
parent
commit
2f5612975c

+ 9 - 3
src/main/java/scot/carricksoftware/grantswriter/writer/latex/parts/people/subsections/PersonSubSectionTimeLineWriterImpl.java

@@ -13,6 +13,7 @@ import scot.carricksoftware.grantswriter.domains.census.CensusEntry;
 import scot.carricksoftware.grantswriter.domains.people.Person;
 import scot.carricksoftware.grantswriter.domains.people.Person;
 import scot.carricksoftware.grantswriter.services.censusentry.CensusEntryService;
 import scot.carricksoftware.grantswriter.services.censusentry.CensusEntryService;
 import scot.carricksoftware.grantswriter.writer.latex.LatexSubSectionHeader;
 import scot.carricksoftware.grantswriter.writer.latex.LatexSubSectionHeader;
+import scot.carricksoftware.grantswriter.writer.latex.parts.people.subsections.helpers.DateSortLinkedMultiValueMap;
 import scot.carricksoftware.grantswriter.writer.latex.parts.people.subsections.helpers.WriteTimeLine;
 import scot.carricksoftware.grantswriter.writer.latex.parts.people.subsections.helpers.WriteTimeLine;
 
 
 import java.util.List;
 import java.util.List;
@@ -26,15 +27,18 @@ public class PersonSubSectionTimeLineWriterImpl implements PersonSubSectionTimeL
     private final CensusEntryService censusEntryService;
     private final CensusEntryService censusEntryService;
     private final TimelineData timelineData;
     private final TimelineData timelineData;
     private final WriteTimeLine writeTimeLine;
     private final WriteTimeLine writeTimeLine;
+    private final DateSortLinkedMultiValueMap dateSortLinkedMultiValueMap;
 
 
     public PersonSubSectionTimeLineWriterImpl(LatexSubSectionHeader latexSubSectionHeader,
     public PersonSubSectionTimeLineWriterImpl(LatexSubSectionHeader latexSubSectionHeader,
                                               CensusEntryService censusEntryService,
                                               CensusEntryService censusEntryService,
                                               TimelineData timelineData,
                                               TimelineData timelineData,
-                                              WriteTimeLine writeTimeLine) {
+                                              WriteTimeLine writeTimeLine,
+                                              DateSortLinkedMultiValueMap dateSortLinkedMultiValueMapTwoMap) {
         this.latexSubSectionHeader = latexSubSectionHeader;
         this.latexSubSectionHeader = latexSubSectionHeader;
         this.censusEntryService = censusEntryService;
         this.censusEntryService = censusEntryService;
         this.timelineData = timelineData;
         this.timelineData = timelineData;
         this.writeTimeLine = writeTimeLine;
         this.writeTimeLine = writeTimeLine;
+        this.dateSortLinkedMultiValueMap = dateSortLinkedMultiValueMapTwoMap;
     }
     }
 
 
     @Override
     @Override
@@ -42,9 +46,11 @@ public class PersonSubSectionTimeLineWriterImpl implements PersonSubSectionTimeL
         logger.info("PersonSubSectionTimeLineWriterImp::write");
         logger.info("PersonSubSectionTimeLineWriterImp::write");
 
 
         latexSubSectionHeader.write("Timeline");
         latexSubSectionHeader.write("Timeline");
-        List<CensusEntry>  censusEntryList = censusEntryService.findAllByPerson(person);
+        List<CensusEntry> censusEntryList = censusEntryService.findAllByPerson(person);
         timelineData.clear();
         timelineData.clear();
-       timelineData.add(censusEntryList);
+        timelineData.add(censusEntryList);
+
+        dateSortLinkedMultiValueMap.sort(timelineData.getTimeline());
 
 
         writeTimeLine.write(timelineData.getTimeline());
         writeTimeLine.write(timelineData.getTimeline());
     }
     }

+ 0 - 7
src/main/java/scot/carricksoftware/grantswriter/writer/latex/parts/people/subsections/helpers/DateSortLinkedMultiValueMap.java

@@ -7,16 +7,9 @@ package scot.carricksoftware.grantswriter.writer.latex.parts.people.subsections.
 
 
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.LinkedMultiValueMap;
 
 
-import java.util.List;
-
 public interface DateSortLinkedMultiValueMap {
 public interface DateSortLinkedMultiValueMap {
 
 
     @SuppressWarnings("SameReturnValue")
     @SuppressWarnings("SameReturnValue")
     LinkedMultiValueMap<String, String> sort (@SuppressWarnings("unused") LinkedMultiValueMap<String, String> map);
     LinkedMultiValueMap<String, String> sort (@SuppressWarnings("unused") LinkedMultiValueMap<String, String> map);
 
 
-    @SuppressWarnings("unused")
-    void swapEntries(List<String> keyList, int i, int j);
-
-    @SuppressWarnings("unused")
-    int compareDates(String a, String b);
 }
 }

+ 27 - 48
src/main/java/scot/carricksoftware/grantswriter/writer/latex/parts/people/subsections/helpers/DateSortLinkedMultiValueMapImpl.java

@@ -10,71 +10,50 @@ import org.apache.logging.log4j.Logger;
 import org.springframework.stereotype.Component;
 import org.springframework.stereotype.Component;
 import org.springframework.util.LinkedMultiValueMap;
 import org.springframework.util.LinkedMultiValueMap;
 
 
-import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.List;
+import java.util.Objects;
 import java.util.Set;
 import java.util.Set;
 
 
+
 @Component
 @Component
 public class DateSortLinkedMultiValueMapImpl implements DateSortLinkedMultiValueMap {
 public class DateSortLinkedMultiValueMapImpl implements DateSortLinkedMultiValueMap {
 
 
-
     private static final Logger logger = LogManager.getLogger(DateSortLinkedMultiValueMapImpl.class);
     private static final Logger logger = LogManager.getLogger(DateSortLinkedMultiValueMapImpl.class);
 
 
     @Override
     @Override
     public LinkedMultiValueMap<String, String> sort(LinkedMultiValueMap<String, String> map) {
     public LinkedMultiValueMap<String, String> sort(LinkedMultiValueMap<String, String> map) {
+        logger.info("DateSortLinkedMultiValueMapImpl::sort");
+        LinkedHashMap<String, List<String>> tempMap = new LinkedHashMap<>();
+        mapConvert(map, tempMap);
+        dateSort(tempMap);
+        deConvert(tempMap, map);
+        return map;
+    }
 
 
-        logger.debug("LinkedMultiValueMapImpl::sort");
-        Set<String> keys = map.keySet();
-        List<String> keyList = new ArrayList<>(keys);
-        if (keyList.size() > 1) {
-            sortKeyList(keyList);
-        }
-        LinkedMultiValueMap<String, String> newMap = new LinkedMultiValueMap<>();
-        for (String key : keyList) {
-            List<String> values = map.get(key);
-            assert values != null;
-            newMap.put(key, values);
+    private void deConvert(HashMap<String, List<String>> tempMap, LinkedMultiValueMap<String, String> map) {
+        logger.info("DateSortLinkedMultiValueMapImpl::deConvert");
+        Set<String> keys = tempMap.keySet();
+        for (String key : keys) {
+            map.put(key, Objects.requireNonNull(map.get(key)));
         }
         }
-        return newMap;
     }
     }
 
 
-    @SuppressWarnings({"CommentedOutCode", "EmptyMethod", "unused"})
-    private void sortKeyList(List<String> keyList) {
-        //       int n = keyList.size();
-        //      boolean swapped;
-//        do {
-//            swapped = false;
-//            for (int i = 0; i < n - 1; i++) {
-//                if (compareTo(keyList.get(i), keyList.get(i + 1)) > 0) {
-//                    swapEntries(keyList, i + 1, i);
-//                    swapped = true;
-//                }
-//            }
-//        } while (swapped);
-
+    private void mapConvert(LinkedMultiValueMap<String, String> map, LinkedHashMap<String, List<String>> tempMap) {
+        Set<String> keys = map.keySet();
+        for (String key : keys) {
+            tempMap.put(key, map.get(key));
+        }
     }
     }
 
 
-    @Override
-    public void swapEntries(List<String> keyList, int i, int j) {
-        String temp = keyList.get(i);
-        keyList.set(i, keyList.get(j));
-        keyList.set(j, temp);
+    @SuppressWarnings({"EmptyMethod", "unused"})
+    private void dateSort(LinkedHashMap<String, List<String>> map) {
+        logger.info("DateSortLinkedMultiValueMapImpl::dateSort");
     }
     }
 
 
-    @Override
-    public int compareDates(String a, String b) {
-        String[] aParts = a.split("/");
-        String[] bParts = b.split("/");
-        if (Integer.parseInt(aParts[0]) > Integer.parseInt(bParts[0])) {
-            return 1;
-        }
-        if (Integer.parseInt(aParts[1]) > Integer.parseInt(bParts[1])) {
-            return 1;
-        }
-        if (Integer.parseInt(aParts[2]) > Integer.parseInt(bParts[2])) {
-            return 1;
-        }
-        return 0;
-    }
+
+
+
 }
 }
 
 

+ 9 - 2
src/test/java/scot/carricksoftware/grantswriter/writer/latex/parts/people/subsections/PersonSubSectionTimeLineWriterTest.java

@@ -13,6 +13,7 @@ import org.mockito.junit.jupiter.MockitoExtension;
 import scot.carricksoftware.grantswriter.data.TimelineData;
 import scot.carricksoftware.grantswriter.data.TimelineData;
 import scot.carricksoftware.grantswriter.services.censusentry.CensusEntryService;
 import scot.carricksoftware.grantswriter.services.censusentry.CensusEntryService;
 import scot.carricksoftware.grantswriter.writer.latex.LatexSubSectionHeader;
 import scot.carricksoftware.grantswriter.writer.latex.LatexSubSectionHeader;
+import scot.carricksoftware.grantswriter.writer.latex.parts.people.subsections.helpers.DateSortLinkedMultiValueMap;
 import scot.carricksoftware.grantswriter.writer.latex.parts.people.subsections.helpers.WriteTimeLine;
 import scot.carricksoftware.grantswriter.writer.latex.parts.people.subsections.helpers.WriteTimeLine;
 
 
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -34,14 +35,20 @@ class PersonSubSectionTimeLineWriterTest {
     @Mock
     @Mock
     private WriteTimeLine writeTimeLineMock;
     private WriteTimeLine writeTimeLineMock;
 
 
+    @Mock
+    private DateSortLinkedMultiValueMap dateSortLinkedMultiValueMapMock;
+
     @BeforeEach
     @BeforeEach
     void setUp() {
     void setUp() {
-        writer = new PersonSubSectionTimeLineWriterImpl(latexSubSectionHeaderMock,
+        writer = new PersonSubSectionTimeLineWriterImpl(
+                latexSubSectionHeaderMock,
                 censusEntryServiceMock,
                 censusEntryServiceMock,
                 timelineDataMock,
                 timelineDataMock,
-                writeTimeLineMock);
+                writeTimeLineMock,
+                dateSortLinkedMultiValueMapMock);
     }
     }
 
 
+
     @Test
     @Test
     void writeTest() {
     void writeTest() {
        assertNotNull(writer);
        assertNotNull(writer);

+ 0 - 27
src/test/java/scot/carricksoftware/grantswriter/writer/latex/parts/people/subsections/helpers/DateSortLinkedMultiValueMapTest.java

@@ -1,27 +0,0 @@
-/*
- * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
- *
- */
-
-package scot.carricksoftware.grantswriter.writer.latex.parts.people.subsections.helpers;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-
-import static org.junit.jupiter.api.Assertions.assertNotNull;
-
-class DateSortLinkedMultiValueMapTest {
-
-    @SuppressWarnings("unused")
-    private DateSortLinkedMultiValueMap underTest;
-
-    @BeforeEach
-    void setUp() {
-        underTest = new DateSortLinkedMultiValueMapImpl();
-    }
-
-    @Test
-    void constructorTest() {
-        assertNotNull(underTest);
-    }
-}

+ 0 - 45
src/test/java/scot/carricksoftware/grantswriter/writer/latex/parts/people/subsections/helpers/WriteTimeLineTest.java

@@ -1,45 +0,0 @@
-/*
- * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
- *
- */
-
-package scot.carricksoftware.grantswriter.writer.latex.parts.people.subsections.helpers;
-
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.extension.ExtendWith;
-import org.mockito.Mock;
-import org.mockito.junit.jupiter.MockitoExtension;
-import scot.carricksoftware.grantswriter.writer.FileWriter;
-import scot.carricksoftware.grantswriter.writer.latex.LatexLongTabLeEnd;
-import scot.carricksoftware.grantswriter.writer.latex.LatexLongTableStart;
-
-import static org.junit.jupiter.api.Assertions.*;
-
-@ExtendWith(MockitoExtension.class)
-class WriteTimeLineTest {
-
-    private WriteTimeLine writeTimeLine;
-
-    @Mock
-    private FileWriter fileWriterMock;
-
-    @Mock
-    private LatexLongTableStart latexLongTableStartMock;
-
-    @Mock
-    private LatexLongTabLeEnd latexLongTabLeEndMock;
-
-    @Mock
-    private DateSortLinkedMultiValueMap dateSortLinkedMultiValueMapMock;
-
-    @BeforeEach
-    void setUp() {
-        writeTimeLine = new WriteTimeLineImpl(fileWriterMock, latexLongTableStartMock, latexLongTabLeEndMock, dateSortLinkedMultiValueMapMock);
-    }
-
-    @Test
-    void constructorTest() {
-        assertNotNull(writeTimeLine);
-    }
-}