001// Licensed to the Apache Software Foundation (ASF) under one
002// or more contributor license agreements.  See the NOTICE file
003// distributed with this work for additional information
004// regarding copyright ownership.  The ASF licenses this file
005// to you under the Apache License, Version 2.0 (the
006// "License"); you may not use this file except in compliance
007// with the License.  You may obtain a copy of the License at
008//
009// http://www.apache.org/licenses/LICENSE-2.0
010//
011// Unless required by applicable law or agreed to in writing,
012// software distributed under the License is distributed on an
013// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
014// KIND, either express or implied.  See the License for the
015// specific language governing permissions and limitations
016// under the License.
017
018package org.apache.tapestry5.services;
019
020/**
021 * Used by {@link ApplicationStateManager} to manage a specific kind of Session State Object (SSO)
022 * persistence. The
023 * strategy is responsible for managing SSO instances within its domain.
024 * <p>
025 * <em>NOTE: The term "Application" here is a hold-over from Tapestry 5.0, which used the @ApplicationState
026 * (deprecated and deleted) annotation, and called them "ASOs" (Application State Objects). This service
027 * would be better named "SessionStatePersistenceStrategy" (but renaming it would cause backwards
028 * compatibility issues).</em>
029 *
030 * @see org.apache.tapestry5.services.ApplicationStatePersistenceStrategySource
031 */
032public interface ApplicationStatePersistenceStrategy
033{
034    /**
035     * Gets the SSO from the domain. If the SSO does not already exist, it is created and stored, then returned.
036     */
037    <T> T get(Class<T> ssoClass, ApplicationStateCreator<T> creator);
038
039    /**
040     * Stores a new SSO, possibly replacing the existing one.
041     *
042     * @param <T>
043     * @param ssoClass
044     * @param sso      instance to store, or null to delete existing
045     */
046    <T> void set(Class<T> ssoClass, T sso);
047
048    /**
049     * Returns true if the SSO already exists, false if null.
050     */
051    <T> boolean exists(Class<T> ssoClass);
052
053    /**
054     * Returns the SSO if it exists or null.
055     */
056    default <T> T getIfExists(Class<T> ssoClass) {
057        return exists(ssoClass) ? get(ssoClass, () -> null) : null;
058    }
059}