001package org.eclipse.aether;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 * 
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 * 
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.function.Supplier;
023
024/**
025 * A container for data that is specific to a repository system session. Both components within the repository system
026 * and clients of the system may use this storage to associate arbitrary data with a session.
027 * <p>
028 * Unlike a cache, this session data is not subject to purging. For this same reason, session data should also not be
029 * abused as a cache (i.e. for storing values that can be re-calculated) to avoid memory exhaustion.
030 * <p>
031 * <strong>Note:</strong> Actual implementations must be thread-safe.
032 * 
033 * @see RepositorySystemSession#getData()
034 * @noimplement This interface is not intended to be implemented by clients.
035 * @noextend This interface is not intended to be extended by clients.
036 */
037public interface SessionData
038{
039
040    /**
041     * Associates the specified session data with the given key.
042     * 
043     * @param key The key under which to store the session data, must not be {@code null}.
044     * @param value The data to associate with the key, may be {@code null} to remove the mapping.
045     */
046    void set( Object key, Object value );
047
048    /**
049     * Associates the specified session data with the given key if the key is currently mapped to the given value. This
050     * method provides an atomic compare-and-update of some key's value.
051     * 
052     * @param key The key under which to store the session data, must not be {@code null}.
053     * @param oldValue The expected data currently associated with the key, may be {@code null}.
054     * @param newValue The data to associate with the key, may be {@code null} to remove the mapping.
055     * @return {@code true} if the key mapping was successfully updated from the old value to the new value,
056     *         {@code false} if the current key mapping didn't match the expected value and was not updated.
057     */
058    boolean set( Object key, Object oldValue, Object newValue );
059
060    /**
061     * Gets the session data associated with the specified key.
062     * 
063     * @param key The key for which to retrieve the session data, must not be {@code null}.
064     * @return The session data associated with the key or {@code null} if none.
065     */
066    Object get( Object key );
067
068    /**
069     * Retrieve of compute the data associated with the specified key.
070     *
071     * @param key The key for which to retrieve the session data, must not be {@code null}.
072     * @param supplier The supplier will compute the new value.
073     * @return The session data associated with the key.
074     */
075    Object computeIfAbsent( Object key, Supplier<Object> supplier );
076
077}