/* * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package sun.java2d; import java.lang.ref.SoftReference; import java.lang.ref.WeakReference; /** * * @param */ public abstract class ReentrantContextProvider { // thread-local storage: inactive public static final byte USAGE_TL_INACTIVE = 0; // thread-local storage: in use public static final byte USAGE_TL_IN_USE = 1; // CLQ storage public static final byte USAGE_CLQ = 2; // hard reference public static final int REF_HARD = 0; // soft reference public static final int REF_SOFT = 1; // weak reference public static final int REF_WEAK = 2; /* members */ // internal reference type protected final ReferenceWrapper refWrapper; /** * Create a new ReentrantContext provider using the given reference type * among hard, soft or weak * @param refType reference type */ protected ReentrantContextProvider(final int refType) { switch (refType) { case REF_HARD: this.refWrapper = new ReferenceWrapper.HardReferenceWrapper(); break; case REF_SOFT: this.refWrapper = new ReferenceWrapper.SoftReferenceWrapper(); break; default: case REF_WEAK: this.refWrapper = new ReferenceWrapper.WeakReferenceWrapper(); break; } } /** * Return a new ReentrantContext instance * @return new ReentrantContext instance */ protected abstract K newContext(); /** * Give a ReentrantContext instance for the current thread * @return ReentrantContext instance */ public abstract K acquire(); /** * Restore the given ReentrantContext instance for reuse * @param ctx ReentrantContext instance */ public abstract void release(K ctx); static abstract class ReferenceWrapper { abstract K resolveReference(final Object ref); abstract Object getOrCreateReference(final K ctx); final static class HardReferenceWrapper extends ReferenceWrapper { @Override @SuppressWarnings("unchecked") K resolveReference(final Object ref) { return (K)ref; } @Override Object getOrCreateReference(final K ctx) { return ctx; } } final static class SoftReferenceWrapper extends ReferenceWrapper { @Override @SuppressWarnings("unchecked") K resolveReference(final Object ref) { return (ref != null) ? ((SoftReference) ref).get() : null; } @Override Object getOrCreateReference(final K ctx) { if (ctx.reference == null) { // Create the reference: ctx.reference = new SoftReference(ctx); } return ctx.reference; } } final static class WeakReferenceWrapper extends ReferenceWrapper { @Override @SuppressWarnings("unchecked") K resolveReference(final Object ref) { return (ref != null) ? ((WeakReference) ref).get() : null; } @Override Object getOrCreateReference(final K ctx) { if (ctx.reference == null) { // Create the reference: ctx.reference = new WeakReference(ctx); } return ctx.reference; } } } }