package ru.dbotthepony.mc.otm.core.collect

import it.unimi.dsi.fastutil.Hash
import java.lang.ref.Reference

object ReferenceHashStrategy : Hash.Strategy<Any?> {
	override fun equals(a: Any?, b: Any?): Boolean {
		if (a === b) return true
		if (a == null || b == null) return false

		if (a is Reference<*>) {
			val ref = a.get()
			if (b is Reference<*>) return ref != null && ref == b.get()
			return ref == b
		} else if (b is Reference<*>) {
			return b.get() == a
		} else {
			return a == b
		}
	}

	override fun hashCode(o: Any?): Int {
		return o.hashCode()
	}
}
