import org.libelektra.KDB;
import org.libelektra.KDBException;
import org.libelektra.Key;
import org.libelektra.KeySet;
import org.libelektra.plugin.Echo;

/** Simple hello world to see how Elektra can be used in Java. */
public class HelloElektra {

  public static void main(String[] args) {
    final Key key = Key.create("user:/hello_world", "Hello World");
    System.out.println(key); // to get name
    System.out.println(key.getString());

    final KeySet ks = KeySet.create(10, Key.create("user:/hello_world2", "Hello World2"), key);

    for (Key k : ks) {
      System.out.println("iter: " + k.getName() + " " + k.getString());
    }
    System.out.println(ks);

    final KeySet ks2 = ks.dup();
    ks2.copy(ks);
    System.out.println(ks2.size());
    ks2.append(ks);
    ks2.append(key);

    try (final KDB kdb = KDB.open(key)) {
      kdb.get(ks, key);
      Key k = ks.lookup(key).orElseThrow(AssertionError::new);
      System.out.println(k.getString());
    } catch (KDBException e) {
      System.out.println(e);
    }

    final Echo dp = new Echo();
    dp.open(ks, key);
    dp.get(ks, key);
    dp.close(key);

    final Key b = Key.create("user:/boolean", "true");
    System.out.println(b.getBoolean());
    b.setBoolean(false);
    System.out.println(b.getBoolean());

    Key n = Key.create("user:/weird\\/name///\\\\/is/\no/_\\\\problem");
    n.keyNameIterator().forEachRemaining(s -> System.out.println("itername: " + s));

    Key cutpoint = Key.create("user:/cutpoint"),
        ka = Key.create("user:/cutpoint/hello", "hiback"),
        kb = Key.create("user:/cutpoint/hello2", "hellotoo"),
        kc = Key.create("user:/different/hello", "hellothere");
    KeySet whole = KeySet.create(ka, kb, kc), cut = whole.cut(cutpoint);

    ka.setString("hibackagain");
    kb.setBoolean(false);
  }
}

@@ -283,24 +283,105 @@
 g%5B%5D args) %7B%0A
+    // Example 1: create a Key and print it%0A    System.out.println(%22Example 1%22);%0A
     final Ke
@@ -517,24 +517,134 @@
 tString());%0A
+    System.out.println();%0A%0A    // Example 2: create a KeySet and print it%0A    System.out.println(%22Example 2%22);
 %0A    final K
@@ -724,25 +724,24 @@
 d2%22), key);%0A
-%0A
     for (Key
@@ -857,16 +857,118 @@
 ln(ks);%0A
+    System.out.println();%0A%0A    // Example 3: duplicating a KeySet%0A    System.out.println(%22Example 3%22);
 %0A    fin
@@ -1084,24 +1084,126 @@
 ppend(key);%0A
+    System.out.println();%0A%0A    // Example 4: reading from the KDB%0A    System.out.println(%22Example 4%22);
 %0A    try (fi
@@ -1431,103 +1431,342 @@
   %7D%0A
-%0A    final Echo dp = new Echo();%0A    dp.open(ks, key);%0A    dp.get(ks, key);%0A    dp.close(key);%0A
+    System.out.println();%0A%0A    // Example 5: manually calling a Plugin (normally not needed)%0A    System.out.println(%22Example 5%22);%0A    final Echo dp = new Echo();%0A    dp.open(ks, key);%0A    dp.get(ks, key);%0A    dp.close(key);%0A    System.out.println();%0A%0A    // Example 6: Keys support different types%0A    System.out.println(%22Example 6%22);
 %0A   
@@ -1918,24 +1918,143 @@
 Boolean());%0A
+    System.out.println();%0A%0A    // Example 7: iterating over keyname parts of a Key%0A    System.out.println(%22Example 7%22);
 %0A    Key n =
@@ -2200,16 +2200,122 @@
  + s));%0A
+    System.out.println();%0A%0A    // Example 8: cutting part of a KeySet%0A    System.out.println(%22Example 8%22);
 %0A    Key
@@ -2508,23 +2508,30 @@
 (%22user:/
-differe
+outside/cutpoi
 nt/hello
@@ -2592,17 +2592,94 @@
  kb, kc)
-,
+;%0A    System.out.println(%22Whole:%22);%0A    System.out.println(whole);%0A%0A    KeySet
  cut = w
@@ -2702,65 +2702,156 @@
 t);%0A
-%0A
     
-ka.setString(%22hibackagain%22);%0A    kb.setBoolean(false
+System.out.println(%22Cut:%22);%0A    System.out.println(cut);%0A    System.out.println(%22Rest:%22);%0A    System.out.println(whole);%0A    System.out.println(
 );%0A 

clarify HelloElektra code