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

com.softicar.platform.common.container.map.weak.identity.WeakIdentityReference Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.container.map.weak.identity;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

public class WeakIdentityReference extends WeakReference {

	private final int hashCode;

	public WeakIdentityReference(T object) {

		super(object);
		this.hashCode = System.identityHashCode(object);
	}

	public WeakIdentityReference(T object, ReferenceQueue referenceQueue) {

		super(object, referenceQueue);
		this.hashCode = System.identityHashCode(object);
	}

	@Override
	public int hashCode() {

		return hashCode;
	}

	@Override
	public boolean equals(Object other) {

		// references are always equal to themselves, which is a necessary property
		// to allow the removal of references with collected objects from a container
		if (this == other) {
			return true;
		}

		// if the referenced object has been collected, it can't be identical to any other object
		T object = get();
		if (object == null) {
			return false;
		}

		// now, compare referenced objects for identity
		if (other instanceof WeakIdentityReference) {
			WeakIdentityReference otherReference = (WeakIdentityReference) other;
			return object == otherReference.get();
		}

		return false;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy