All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.autonomousapps.internal.utils.DependencyGraphAdapter.kt Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package com.autonomousapps.internal.utils

import com.autonomousapps.graph.*
import com.squareup.moshi.*

internal class DependencyGraphAdapter {

  @ToJson fun fromGraph(graph: DependencyGraph): List {
    return graph.edges()
  }

  @FromJson fun toGraph(edges: List): DependencyGraph {
    return DependencyGraph.newGraph(edges)
  }

  @ToJson fun fromNode(
    writer: JsonWriter,
    node: Node,
    consumerDelegate: JsonAdapter, producerDelegate: JsonAdapter
  ): Unit = when (node) {
    is ConsumerNode -> consumerDelegate.toJson(writer, node)
    is ProducerNode -> producerDelegate.toJson(writer, node)
  }

  @FromJson fun toNode(
    reader: JsonReader,
    consumerDelegate: JsonAdapter, producerDelegate: JsonAdapter
  ): Node? = try {
    // Most nodes will be ProducerNodes
    producerDelegate.fromJson(reader)
  } catch (_: Exception) {
    consumerDelegate.fromJson(reader)
  }
}

internal data class GraphJson(
  /**
   * A mapping of node-identifier to set-of-node-identifiers.
   */
  val map: Map>,
  /**
   * A mapping of node-identifier to node.
   */
  val nodes: Map
)

// I have a hard time letting go of this time-consuming-to-write code...
//  @ToJson fun fromGraph(graph: DependencyGraph): GraphJson {
//    val map = graph.map()
//    val stringMap = mutableMapOf>()
//    val nodes = mutableMapOf()
//    map.forEach { (from, tos) ->
//      stringMap[from.identifier] = tos.mapToSet { to -> to.identifier }
//      nodes.putIfAbsent(from.identifier, from)
//      tos.forEach { to ->
//        nodes.putIfAbsent(to.identifier, to)
//      }
//    }
//    return GraphJson(stringMap, nodes)
//  }
//
//  @FromJson fun toGraph(json: GraphJson): DependencyGraph {
//    val map = mutableMapOf>()
//    json.map.forEach { (from, tos) ->
//      val fromNode = json.nodes[from] ?: error("No node found for $from")
//      val toNodes = tos.mapToSet { to -> json.nodes[to] ?: error("No node found for $to") }
//      map[fromNode] = toNodes
//    }
//    return DependencyGraph.newGraph(map)
//  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy