Path: blob/master/src/java.desktop/share/classes/sun/java2d/StateTrackableDelegate.java
41152 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d;2627import sun.java2d.StateTrackable.State;28import static sun.java2d.StateTrackable.State.*;2930/**31* This class provides a basic pre-packaged implementation of the32* complete {@link StateTrackable} interface with implementations33* of the required methods in the interface and methods to manage34* transitions in the state of the object.35* Classes which wish to implement StateTrackable could create an36* instance of this class and delegate all of their implementations37* for {@code StateTrackable} methods to the corresponding methods38* of this class.39*/40public final class StateTrackableDelegate implements StateTrackable {41/**42* The {@code UNTRACKABLE_DELEGATE} provides an implementation43* of the StateTrackable interface that is permanently in the44* {@link State#UNTRACKABLE UNTRACKABLE} state.45*/46public static final StateTrackableDelegate UNTRACKABLE_DELEGATE =47new StateTrackableDelegate(UNTRACKABLE);4849/**50* The {@code IMMUTABLE_DELEGATE} provides an implementation51* of the StateTrackable interface that is permanently in the52* {@link State#IMMUTABLE IMMUTABLE} state.53*/54public static final StateTrackableDelegate IMMUTABLE_DELEGATE =55new StateTrackableDelegate(IMMUTABLE);5657/**58* Returns a {@code StateTrackableDelegate} instance with the59* specified initial {@link State State}.60* If the specified {@code State} is61* {@link State#UNTRACKABLE UNTRACKABLE} or62* {@link State#IMMUTABLE IMMUTABLE}63* then the approprirate static instance64* {@link #UNTRACKABLE_DELEGATE} or {@link #IMMUTABLE_DELEGATE}65* is returned.66*/67public static StateTrackableDelegate createInstance(State state) {68switch (state) {69case UNTRACKABLE:70return UNTRACKABLE_DELEGATE;71case STABLE:72return new StateTrackableDelegate(STABLE);73case DYNAMIC:74return new StateTrackableDelegate(DYNAMIC);75case IMMUTABLE:76return IMMUTABLE_DELEGATE;77default:78throw new InternalError("unknown state");79}80}8182private State theState;83StateTracker theTracker; // package private for easy access from tracker84private int numDynamicAgents;8586/**87* Constructs a StateTrackableDelegate object with the specified88* initial State.89*/90private StateTrackableDelegate(State state) {91this.theState = state;92}9394/**95* @inheritDoc96* @since 1.797*/98public State getState() {99return theState;100}101102/**103* @inheritDoc104* @since 1.7105*/106public synchronized StateTracker getStateTracker() {107StateTracker st = theTracker;108if (st == null) {109switch (theState) {110case IMMUTABLE:111st = StateTracker.ALWAYS_CURRENT;112break;113case STABLE:114st = new StateTracker() {115public boolean isCurrent() {116return (theTracker == this);117}118};119break;120case DYNAMIC:121// We return the NEVER_CURRENT tracker, but that is122// just temporary while we are in the DYNAMIC state.123// NO BREAK124case UNTRACKABLE:125st = StateTracker.NEVER_CURRENT;126break;127}128theTracker = st;129}130return st;131}132133/**134* This method provides an easy way for delegating classes to135* change the overall {@link State State} of the delegate to136* {@link State#IMMUTABLE IMMUTABLE}.137* @throws IllegalStateException if the current state is138* {@link State#UNTRACKABLE UNTRACKABLE}139* @see #setUntrackable140* @since 1.7141*/142public synchronized void setImmutable() {143if (theState == UNTRACKABLE || theState == DYNAMIC) {144throw new IllegalStateException("UNTRACKABLE or DYNAMIC "+145"objects cannot become IMMUTABLE");146}147theState = IMMUTABLE;148theTracker = null;149}150151/**152* This method provides an easy way for delegating classes to153* change the overall {@link State State} of the delegate to154* {@link State#UNTRACKABLE UNTRACKABLE}.155* This method is typically called when references to the156* internal data buffers have been made public.157* @throws IllegalStateException if the current state is158* {@link State#IMMUTABLE IMMUTABLE}159* @see #setImmutable160* @since 1.7161*/162public synchronized void setUntrackable() {163if (theState == IMMUTABLE) {164throw new IllegalStateException("IMMUTABLE objects cannot "+165"become UNTRACKABLE");166}167theState = UNTRACKABLE;168theTracker = null;169}170171/**172* This method provides an easy way for delegating classes to173* manage temporarily setting the overall {@link State State}174* of the delegate to {@link State#DYNAMIC DYNAMIC}175* during well-defined time frames of dynamic pixel updating.176* This method should be called once before each flow of control177* that might dynamically update the pixels in an uncontrolled178* or unpredictable fashion.179* <p>180* The companion method {@link #removeDynamicAgent} method should181* also be called once after each such flow of control has ended.182* Failing to call the remove method will result in this object183* permanently becoming {@link State#DYNAMIC DYNAMIC}184* and therefore effectively untrackable.185* <p>186* This method will only change the {@link State State} of the187* delegate if it is currently {@link State#STABLE STABLE}.188*189* @throws IllegalStateException if the current state is190* {@link State#IMMUTABLE IMMUTABLE}191* @since 1.7192*/193public synchronized void addDynamicAgent() {194if (theState == IMMUTABLE) {195throw new IllegalStateException("Cannot change state from "+196"IMMUTABLE");197}198++numDynamicAgents;199if (theState == STABLE) {200theState = DYNAMIC;201theTracker = null;202}203}204205/**206* This method provides an easy way for delegating classes to207* manage restoring the overall {@link State State} of the208* delegate back to {@link State#STABLE STABLE}209* after a well-defined time frame of dynamic pixel updating.210* This method should be called once after each flow of control211* that might dynamically update the pixels in an uncontrolled212* or unpredictable fashion has ended.213* <p>214* The companion method {@link #addDynamicAgent} method should215* have been called at some point before each such flow of216* control began.217* If this method is called without having previously called218* the add method, the {@link State State} of this object219* will become unreliable.220* <p>221* This method will only change the {@link State State} of the222* delegate if the number of outstanding dynamic agents has223* gone to 0 and it is currently224* {@link State#DYNAMIC DYNAMIC}.225*226* @since 1.7227*/228protected synchronized void removeDynamicAgent() {229if (--numDynamicAgents == 0 && theState == DYNAMIC) {230theState = STABLE;231theTracker = null;232}233}234235/**236* This method provides an easy way for delegating classes to237* indicate that the contents have changed.238* This method will invalidate outstanding StateTracker objects239* so that any other agents which maintain cached information240* about the pixels will know to refresh their cached copies.241* This method should be called after every modification to242* the data, such as any calls to any of the setElem methods.243* <p>244* Note that, for efficiency, this method does not check the245* {@link State State} of the object to see if it is compatible246* with being marked dirty247* (i.e. not {@link State#IMMUTABLE IMMUTABLE}).248* It is up to the callers to enforce the fact that an249* {@code IMMUTABLE} delegate is never modified.250* @since 1.7251*/252public void markDirty() {253theTracker = null;254}255}256257258