
// PartialMultiKeyMapTester.java
// Andrew Davison, ad@coe.psu.ac.th, May 2023

/* Uses the version of the multikeymap developed by
   David Tesler (protobufel)
   at https://github.com/protobufel/multikeymapjava

   It behaves like a regular generic Map but with the ability
   to access its values by combining partial keys.

   See MultiKeyMapTester.java for the MultiKeyMap class
   offered by Apache Commons Collections,

  Usage:
    > compile PartialMultiKeyMapTester.java
    > run PartialMultiKeyMapTester
*/


import java.util.*;
import java.util.stream.*;

import com.github.protobufel.multikeymap.*;



public class PartialMultiKeyMapTester
{
  public static void main(String[] args)
  {
    MultiKeyMap<String, Iterable<String>, Integer> marks =
                                MultiKeyMaps.newMultiKeyMap();
        // first argument is type of all keys and their iterable

    // add records
    marks.put(Arrays.asList("Jim", "Midterm", "English"), 92);
    marks.put(Arrays.asList("Jim", "Midterm", "Math"), 100);
    marks.put(Arrays.asList("Jim", "Final", "English"), 90);
    marks.put(Arrays.asList("Jim", "Final", "Math"), 95);
    marks.put(Arrays.asList("Bill", "Midterm", "Math"), 45);
    marks.put(Arrays.asList("Bill", "Final", "Math"), 52);

    System.out.println("Map: " + marks);

    System.out.println("\nIterate over key:value pairs:");
    marks.forEach((key, value) -> System.out.println(" " + key + ":" + value));


    System.out.println("\nJim's score in Midterm English: " +
               marks.get(Arrays.asList("Jim", "Midterm", "English")));

    // get values using  partial keys
    System.out.println("\nJim's scores in English: ");
    Stream<Integer> sVals = marks.getValuesByPartialKey(
                               Arrays.asList("Jim", "English") );
    List<Integer> vals = sVals.collect(
                      java.util.stream.Collectors.toList());
    System.out.println(vals);


    System.out.println("\nAll the Final Math scores: ");
    sVals = marks.getValuesByPartialKey(
                               Arrays.asList("Final", "Math"),
                               Arrays.asList(1,2) );  // keys positions
    vals = sVals.collect(java.util.stream.Collectors.toList());
    System.out.println(vals);

    System.out.println("\nAll the Math scores: ");
    sVals = marks.getValuesByPartialKey(
                               Arrays.asList("Math"),
                               Arrays.asList(2) ); // key pos
    vals = sVals.collect(java.util.stream.Collectors.toList());
    System.out.println(vals);
  }  // end of main()

}  // end of PartialMultiKeyMapTester class