summaryrefslogtreecommitdiffstats
path: root/bundle-plugin/src/main/scala/com/yahoo/container/plugin/classanalysis/PackageTally.scala
blob: 2d2460cc9fd22352090539789b9c7713eb6b9699 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.plugin.classanalysis

import com.yahoo.container.plugin.util.Maps

/**
 *
 * @author  tonytv
 */
final class PackageTally (private val definedPackagesMap : Map[String, Option[ExportPackageAnnotation]],
                          referencedPackagesUnfiltered : Set[String]) {

  val referencedPackages = referencedPackagesUnfiltered diff definedPackages

  def definedPackages = definedPackagesMap.keySet

  def exportedPackages = definedPackagesMap collect { case (name, Some(export)) => (name, export) }

  /**
   * Represents the classes for two package tallies that are deployed as a single unit.
   *
   * ExportPackageAnnotations from this has precedence over the other.
   */
  def combine(other: PackageTally): PackageTally = {
    new PackageTally(
      Maps.combine(definedPackagesMap, other.definedPackagesMap)(_ orElse _),
      referencedPackages ++ other.referencedPackages)
  }
}


object PackageTally {
  def fromAnalyzedClassFiles(analyzedClassFiles : Seq[ClassFileMetaData]) : PackageTally = {
    combine(
      for (metaData <- analyzedClassFiles)
      yield {
        new PackageTally(
          Map(Packages.packageName(metaData.name) -> metaData.exportPackage),
          metaData.referencedClasses.map(Packages.packageName))
      })
  }

  def combine(packageTallies : Iterable[PackageTally]) : PackageTally = (empty /: packageTallies)(_.combine(_))

  val empty : PackageTally = new PackageTally(Map(), Set())
}