
package com.google.idea.blaze.golang.sync;

import com.google.idea.blaze.base.sync.libraries.LibrarySource;
import com.intellij.openapi.roots.libraries.Library;
import java.util.function.Predicate;

/** Prevents garbage collection of Go libraries */
class BlazeGoLibrarySource extends LibrarySource.Adapter {

  static final BlazeGoLibrarySource INSTANCE = new BlazeGoLibrarySource();

  private BlazeGoLibrarySource() {}

  @Override
  public Predicate<Library> getGcRetentionFilter() {
    return BlazeGoLibrarySource::isGoLibrary;
  }

  static boolean isGoLibrary(Library library) {
    String name = library.getName();
    if (name == null) {
      return false;
    }
    for (String prefix : BlazeGoSyncPlugin.GO_LIBRARY_PREFIXES) {
      if (name.startsWith(prefix)) {
        return true;
      }
    }
    return false;
  }
}
