0

I want to create a series of custom POJO's in Java 8 to represent a data structure received and deserialised via JSON.

The data looks like (top level down)

RootNode
  <Set>LocalDate
    <Set>Location
       <Set>Shift
           Integer

So for example the data might look like:

2021-06-20 
    AMBULANCE\CA\Los Angeles
        Morning
            4
        Afternoon
            5
        Nights
            5
    AMBULANCE\CA\San Deigo
        Afternoon
            7
        Nights
            6
        Morning 
            4
2021-06-21
     AMBULANCE\CA\Los Angeles
         Night
            6
         Morning
            5
         Afternoon
            5
     ANBULANCE\CA\San Francisco
         Afternoon
            5
         Morning
            4
....

In the above structure each date has several child locations, each location has three child shifts, and each shift has a integer.

To represent this as Java Objects I have created:

public class RootNode {

    public RootNode(){}
    public LinkedHashMap<LocalDate, Location> locations;

}

public class Location {

   public Set<Shift> shifts;

}

public class Shift {

public enum ShiftTypes {
    MORNING, AFTERNOON, NIGHT
    }

    public Integer count;

}

There should be no duplicates of dates or locations or employees.

Do these classes correctly represent the data structure outlined?

All these classes are custom except for LocalDate which is native - I was uncertain whether to create a new custom date class which in turn has a Set attribute?

If this is the correct way to setup the structure what would the syntax be? Using extends?

UPDATE

I tried to extend LocalDate with:

public class ShiftLocalDate extends LocalDate {

    public Set<Shift> shifts;

}

But it appears this can't be done since LocalDate is final?

Ray Bond
  • 427
  • 3
  • 11
  • 1
    The context is not clear to me. What does a dataset represent? and why do you think a tree structure is the right thing for this data? – Eritrean Jun 24 '21 at 22:10
  • The question is if the structure created is the best way to store the data in java - at least while it is in memory, and do the nbested objects correctly represent the data as received (via JSON) – Ray Bond Jun 24 '21 at 22:24
  • If your goal is to represent the entire hierarchy in Java, then this Question might be a duplicate of: [*Represent tree hierarchy in java*](https://stackoverflow.com/q/11212656/642706) – Basil Bourque Jun 24 '21 at 23:28

1 Answers1

0

Flatten the hierarchy

Your inputs seem to represent a bunch of entries that were grouped into a hierarchy of date > location > shift.

Your class structure seems overwrought. Depending on the aims of your app, you may be better off by not slavishly copying that hierarchy. For manipulation in Java, it seems much simpler to “rehydrate” those inputs back into a series of entries, to flatten your hierarchy. These entries would be objects of a class that contain all the relevant data. If need be, you can later recreate the hierarchy from a collection of such objects.

For classes whose primary purpose is to communicate data transparently and immutably, in Java 16+ we can use use the records feature. In a record, you merely declare the type and name of each member field. The compiler implicitly creates the constructor, getters, equals & hashCode, and toString.

record WorkLogEntry ( LocalDate date , Location location , Shift shift , int countWorkers ) {} 

Likewise, define an appropriate record to represent Location. If locations are a limited set known entirely at compile-time, make an enum for these.

Create an enum for Shift as shown in your Question. But enum objects are constants, so should be named in all uppercase by convention.

enum Shift { MORNING , AFTERNOON , EVENING }

You can access an enum object via a string. For example:

Shift.valueOf( "Afternoon".toUppercase( Locale.UK ) )

Collect objects of type WorkLogEntry into a collection. Perhaps use a NavigableSet implementation such as TreeSet to keep the entries ordered by date.

Comparator< WorkLogEntry > comparator = Comparator.comparing( WorkLogEntry::date ).thenComparing( WorkLogEntry::location ).thenComparing( WorkLogEntry::shift ) ;
NavigableSet< WorkLogEntry > workLog = new TreeSet<>( comparator ) ;

If you want to recreate the hierarchy by date, use a NavigableMap.

NavigableMap< LocalDate , NavigableSet< WorkLogEntry > > map = new TreeMap<>() ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154