0N/A/*
2362N/A * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.java2d;
0N/A
0N/Aimport sun.java2d.StateTrackable.State;
0N/Aimport static sun.java2d.StateTrackable.State.*;
0N/A
0N/A/**
0N/A * This class provides a basic pre-packaged implementation of the
0N/A * complete {@link StateTrackable} interface with implementations
0N/A * of the required methods in the interface and methods to manage
0N/A * transitions in the state of the object.
0N/A * Classes which wish to implement StateTrackable could create an
0N/A * instance of this class and delegate all of their implementations
0N/A * for {@code StateTrackable} methods to the corresponding methods
0N/A * of this class.
0N/A */
0N/Apublic final class StateTrackableDelegate implements StateTrackable {
0N/A /**
0N/A * The {@code UNTRACKABLE_DELEGATE} provides an implementation
0N/A * of the StateTrackable interface that is permanently in the
0N/A * {@link State#UNTRACKABLE UNTRACKABLE} state.
0N/A */
0N/A public final static StateTrackableDelegate UNTRACKABLE_DELEGATE =
0N/A new StateTrackableDelegate(UNTRACKABLE);
0N/A
0N/A /**
0N/A * The {@code IMMUTABLE_DELEGATE} provides an implementation
0N/A * of the StateTrackable interface that is permanently in the
0N/A * {@link State#IMMUTABLE IMMUTABLE} state.
0N/A */
0N/A public final static StateTrackableDelegate IMMUTABLE_DELEGATE =
0N/A new StateTrackableDelegate(IMMUTABLE);
0N/A
0N/A /**
0N/A * Returns a {@code StateTrackableDelegate} instance with the
0N/A * specified initial {@link State State}.
0N/A * If the specified {@code State} is
0N/A * {@link State#UNTRACKABLE UNTRACKABLE} or
0N/A * {@link State#IMMUTABLE IMMUTABLE}
0N/A * then the approprirate static instance
0N/A * {@link #UNTRACKABLE_DELEGATE} or {@link #IMMUTABLE_DELEGATE}
0N/A * is returned.
0N/A */
0N/A public static StateTrackableDelegate createInstance(State state) {
0N/A switch (state) {
0N/A case UNTRACKABLE:
0N/A return UNTRACKABLE_DELEGATE;
0N/A case STABLE:
0N/A return new StateTrackableDelegate(STABLE);
0N/A case DYNAMIC:
0N/A return new StateTrackableDelegate(DYNAMIC);
0N/A case IMMUTABLE:
0N/A return IMMUTABLE_DELEGATE;
0N/A default:
0N/A throw new InternalError("unknown state");
0N/A }
0N/A }
0N/A
0N/A private State theState;
0N/A StateTracker theTracker; // package private for easy access from tracker
0N/A private int numDynamicAgents;
0N/A
0N/A /**
0N/A * Constructs a StateTrackableDelegate object with the specified
0N/A * initial State.
0N/A */
0N/A private StateTrackableDelegate(State state) {
0N/A this.theState = state;
0N/A }
0N/A
0N/A /**
0N/A * @inheritDoc
0N/A * @since 1.7
0N/A */
0N/A public State getState() {
0N/A return theState;
0N/A }
0N/A
0N/A /**
0N/A * @inheritDoc
0N/A * @since 1.7
0N/A */
0N/A public synchronized StateTracker getStateTracker() {
0N/A StateTracker st = theTracker;
0N/A if (st == null) {
0N/A switch (theState) {
0N/A case IMMUTABLE:
0N/A st = StateTracker.ALWAYS_CURRENT;
0N/A break;
0N/A case STABLE:
0N/A st = new StateTracker() {
0N/A public boolean isCurrent() {
0N/A return (theTracker == this);
0N/A }
0N/A };
0N/A break;
0N/A case DYNAMIC:
0N/A // We return the NEVER_CURRENT tracker, but that is
0N/A // just temporary while we are in the DYNAMIC state.
0N/A // NO BREAK
0N/A case UNTRACKABLE:
0N/A st = StateTracker.NEVER_CURRENT;
0N/A break;
0N/A }
0N/A theTracker = st;
0N/A }
0N/A return st;
0N/A }
0N/A
0N/A /**
0N/A * This method provides an easy way for delegating classes to
0N/A * change the overall {@link State State} of the delegate to
0N/A * {@link State#IMMUTABLE IMMUTABLE}.
0N/A * @throws IllegalStateException if the current state is
0N/A * {@link State#UNTRACKABLE UNTRACKABLE}
0N/A * @see #setUntrackable
0N/A * @since 1.7
0N/A */
0N/A public synchronized void setImmutable() {
0N/A if (theState == UNTRACKABLE || theState == DYNAMIC) {
0N/A throw new IllegalStateException("UNTRACKABLE or DYNAMIC "+
0N/A "objects cannot become IMMUTABLE");
0N/A }
0N/A theState = IMMUTABLE;
0N/A theTracker = null;
0N/A }
0N/A
0N/A /**
0N/A * This method provides an easy way for delegating classes to
0N/A * change the overall {@link State State} of the delegate to
0N/A * {@link State#UNTRACKABLE UNTRACKABLE}.
0N/A * This method is typically called when references to the
0N/A * internal data buffers have been made public.
0N/A * @throws IllegalStateException if the current state is
0N/A * {@link State#IMMUTABLE IMMUTABLE}
0N/A * @see #setImmutable
0N/A * @since 1.7
0N/A */
0N/A public synchronized void setUntrackable() {
0N/A if (theState == IMMUTABLE) {
0N/A throw new IllegalStateException("IMMUTABLE objects cannot "+
0N/A "become UNTRACKABLE");
0N/A }
0N/A theState = UNTRACKABLE;
0N/A theTracker = null;
0N/A }
0N/A
0N/A /**
0N/A * This method provides an easy way for delegating classes to
0N/A * manage temporarily setting the overall {@link State State}
0N/A * of the delegate to {@link State#DYNAMIC DYNAMIC}
0N/A * during well-defined time frames of dynamic pixel updating.
0N/A * This method should be called once before each flow of control
0N/A * that might dynamically update the pixels in an uncontrolled
0N/A * or unpredictable fashion.
0N/A * <p>
0N/A * The companion method {@link #removeDynamicAgent} method should
0N/A * also be called once after each such flow of control has ended.
0N/A * Failing to call the remove method will result in this object
0N/A * permanently becoming {@link State#DYNAMIC DYNAMIC}
0N/A * and therefore effectively untrackable.
0N/A * <p>
0N/A * This method will only change the {@link State State} of the
0N/A * delegate if it is currently {@link State#STABLE STABLE}.
0N/A *
0N/A * @throws IllegalStateException if the current state is
0N/A * {@link State#IMMUTABLE IMMUTABLE}
0N/A * @since 1.7
0N/A */
0N/A public synchronized void addDynamicAgent() {
0N/A if (theState == IMMUTABLE) {
0N/A throw new IllegalStateException("Cannot change state from "+
0N/A "IMMUTABLE");
0N/A }
0N/A ++numDynamicAgents;
0N/A if (theState == STABLE) {
0N/A theState = DYNAMIC;
0N/A theTracker = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This method provides an easy way for delegating classes to
0N/A * manage restoring the overall {@link State State} of the
0N/A * delegate back to {@link State#STABLE STABLE}
0N/A * after a well-defined time frame of dynamic pixel updating.
0N/A * This method should be called once after each flow of control
0N/A * that might dynamically update the pixels in an uncontrolled
0N/A * or unpredictable fashion has ended.
0N/A * <p>
0N/A * The companion method {@link #addDynamicAgent} method should
0N/A * have been called at some point before each such flow of
0N/A * control began.
0N/A * If this method is called without having previously called
0N/A * the add method, the {@link State State} of this object
0N/A * will become unreliable.
0N/A * <p>
0N/A * This method will only change the {@link State State} of the
0N/A * delegate if the number of outstanding dynamic agents has
0N/A * gone to 0 and it is currently
0N/A * {@link State#DYNAMIC DYNAMIC}.
0N/A *
0N/A * @since 1.7
0N/A */
0N/A protected synchronized void removeDynamicAgent() {
0N/A if (--numDynamicAgents == 0 && theState == DYNAMIC) {
0N/A theState = STABLE;
0N/A theTracker = null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * This method provides an easy way for delegating classes to
0N/A * indicate that the contents have changed.
0N/A * This method will invalidate outstanding StateTracker objects
0N/A * so that any other agents which maintain cached information
0N/A * about the pixels will know to refresh their cached copies.
0N/A * This method should be called after every modification to
0N/A * the data, such as any calls to any of the setElem methods.
0N/A * <p>
0N/A * Note that, for efficiency, this method does not check the
0N/A * {@link State State} of the object to see if it is compatible
0N/A * with being marked dirty
0N/A * (i.e. not {@link State#IMMUTABLE IMMUTABLE}).
0N/A * It is up to the callers to enforce the fact that an
0N/A * {@code IMMUTABLE} delegate is never modified.
0N/A * @since 1.7
0N/A */
0N/A public final void markDirty() {
0N/A theTracker = null;
0N/A }
0N/A}