/*
 * Bach - Java Shell Builder
 * Copyright (C) 2020 Christian Stein
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.sormuras.bach.project;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;

/** An external modules manager backed by the {@link Base#libraries() lib} directory. */
public final class Library {

  public static Library of() {
    return new Library(Set.of(), Map.of());
  }

  public Library withRequires(String... moreRequires) {
    var requires = new TreeSet<>(requires());
    Collections.addAll(requires, moreRequires);
    return new Library(requires, links);
  }

  public Library with(Link... moreLinks) {
    var links = new TreeMap<>(links());
    for (var link : moreLinks) links.put(link.module(), link);
    return new Library(requires, links);
  }

  private final Set<String> requires;
  private final Map<String, Link> links;

  public Library(Set<String> requires, Map<String, Link> links) {
    this.requires = requires;
    this.links = links;
  }

  public Set<String> requires() {
    return requires;
  }

  public Map<String, Link> links() {
    return links;
  }

  public Optional<Link> findLink(String module) {
    return Optional.ofNullable(links.get(module));
  }
}

@@ -662,16 +662,105 @@
 oject;%0A%0A
+import de.sormuras.bach.internal.Factory;%0Aimport de.sormuras.bach.internal.Factory.Kind;%0A
 import j
@@ -1031,136 +1031,146 @@
 %0A  p
-ublic static Library of() %7B%0A    return new Library(Set.of(), Map.of());%0A  %7D%0A%0A  public Library withRequires(String... moreRequire
+rivate final Set%3CString%3E requires;%0A  private final Map%3CString, Link%3E links;%0A%0A  public Library(Set%3CString%3E requires, Map%3CString, Link%3E link
 s) %7B
@@ -1174,20 +1174,21 @@
 ) %7B%0A    
-var 
+this.
 requires
@@ -1194,30 +1194,16 @@
 s = 
-new TreeSet%3C%3E(
 requires
 ());
@@ -1202,60 +1202,72 @@
 ires
-())
 ;%0A    
-Collections.addAll(requires, moreR
+this.links = links;%0A  %7D%0A%0A  public Set%3CString%3E r
 equires
-);
+() %7B
 %0A   
@@ -1278,90 +1278,64 @@
 urn 
-new Library(requires, links);%0A  %7D%0A%0A  public Library with(Link... moreL
+requires;%0A  %7D%0A%0A  public Map%3CString, Link%3E l
 inks
+(
 ) %7B%0A    
 var 
@@ -1334,391 +1334,561 @@
     
-var links = new TreeMap%3C%3E(links());%0A    for (var link : moreLinks) links.put(link.module(), link);%0A    return new Library(requires, links);%0A  %7D%0A%0A  private final Set%3CString%3E requires;%0A  private final Map%3CString, Link%3E
+return links;%0A  %7D%0A%0A  //%0A  // Configuration API%0A  //%0A%0A  @Factory%0A  public static Library of() %7B%0A    return new Library(Set.of(), Map.of());%0A  %7D%0A%0A  @Factory(Kind.SETTER)%0A  public Library requires(Set%3CString%3E requires) %7B%0A    return new Library(requires,
  links
+)
 ;%0A
-%0A  public Library(Set%3CString%3E requires, Map%3CString, Link%3E links) %7B%0A    this.requires = requires;%0A    this.links = links;%0A  %7D%0A%0A  public Set%3CString%3E requires() %7B
+  %7D%0A%0A  @Factory(Kind.SETTER)%0A  public Library links(Map%3CString, Link%3E links) %7B%0A    return new Library(requires, links);%0A  %7D%0A%0A  @Factory(Kind.OPERATOR)%0A  public Library withRequires(String... moreRequires) %7B%0A    var merged = new TreeSet%3C%3E(requires);%0A    Collections.addAll(merged, moreRequires);
 %0A   
@@ -1895,32 +1895,40 @@
  return requires
+(merged)
 ;%0A  %7D%0A%0A  public 
@@ -1924,64 +1924,234 @@
 %0A%0A  
-public Map%3CString, Link%3E links() %7B%0A    return links;%0A  %7D
+@Factory(Kind.OPERATOR)%0A  public Library with(Link... moreLinks) %7B%0A    var merged = new TreeMap%3C%3E(links);%0A    for (var link : moreLinks) merged.put(link.module(), link);%0A    return links(merged);%0A  %7D%0A%0A  //%0A  // Normal API%0A  //
 %0A%0A  

Clean up Library's API and member order