    001    // Copyright 2008, 2009, 2011 The Apache Software Foundation
    002    //
    003    // Licensed under the Apache License, Version 2.0 (the "License");
    004    // you may not use this file except in compliance with the License.
    005    // You may obtain a copy of the License at
    006    //
    007    //     http://www.apache.org/licenses/LICENSE-2.0
    008    //
    009    // Unless required by applicable law or agreed to in writing, software
    010    // distributed under the License is distributed on an "AS IS" BASIS,
    011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    012    // See the License for the specific language governing permissions and
    013    // limitations under the License.
    014
    015    package org.apache.tapestry5;
    016
    017    import java.util.concurrent.atomic.AtomicBoolean;
    018
    019    /**
    020     * Base implementation of
    021     * {@link org.apache.tapestry5.OptimizedSessionPersistedObject}. Subclasses
    022     * should invoke {@link #markDirty()} after the internal state of the object changes.
    023     * <p>
    024     * Due to the concurrent nature of session attributes it's important that markDirty occurs <strong>after</strong>
    025     * the object has been changed. If the change occurs before the object has been mutated it's possible that another
    026     * thread may re-store the object before the changes are actually made!
    027     * <p>
    028     * @since 5.1.1.0
    029     */
    030    public abstract class BaseOptimizedSessionPersistedObject implements OptimizedSessionPersistedObject
    031    {
    032        private transient AtomicBoolean dirty = new AtomicBoolean(false);
    033
    034        public final boolean checkAndResetDirtyMarker()
    035        {
    036            return dirty.getAndSet(false);
    037        }
    038
    039        /**
    040         * Invoked by the subclass after internal state of the object changes.
    041         */
    042        protected final void markDirty()
    043        {
    044            dirty.set(true);
    045        }
    046    }