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

org.jboss.util.WeakObject Maven / Gradle / Ivy

The newest version!
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2015, Red Hat, Inc., and individual contributors as indicated
 * by the @authors tag.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.util;

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

/**
 * Convenience class to wrap an Object into a WeakReference.
 *
 * 

Modified from java.util.WeakHashMap.WeakKey. * * @version $Revision$ * @author Jason Dillon */ @SuppressWarnings("unchecked") public final class WeakObject extends WeakReference { /** The hash code of the nested object */ protected final int hashCode; /** * Construct a WeakObject. * * @param obj Object to reference. */ public WeakObject(final Object obj) { super(obj); hashCode = obj.hashCode(); } /** * Construct a WeakObject. * * @param obj Object to reference. * @param queue Reference queue. */ public WeakObject(final Object obj, final ReferenceQueue queue) { super(obj, queue); hashCode = obj.hashCode(); } /** * Check the equality of an object with this. * * @param obj Object to test equality with. * @return True if object is equal. */ public boolean equals(final Object obj) { if (obj == this) return true; if (obj != null && obj.getClass() == getClass()) { WeakObject soft = (WeakObject)obj; Object a = this.get(); Object b = soft.get(); if (a == null || b == null) return false; if (a == b) return true; return a.equals(b); } return false; } /** * Return the hash code of the nested object. * * @return The hash code of the nested object. */ public int hashCode() { return hashCode; } ///////////////////////////////////////////////////////////////////////// // Factory Methods // ///////////////////////////////////////////////////////////////////////// /** * Create a WeakObject for the given object. * * @param obj Object to reference. * @return WeakObject or null if object is null. */ public static WeakObject create(final Object obj) { if (obj == null) return null; else return new WeakObject(obj); } /** * Create a WeakObject for the given object. * * @param obj Object to reference. * @param queue Reference queue. * @return WeakObject or null if object is null. */ public static WeakObject create(final Object obj, final ReferenceQueue queue) { if (obj == null) return null; else return new WeakObject(obj, queue); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy