View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.api;
20  
21  import java.util.function.Supplier;
22  
23  import org.apache.maven.api.annotations.Experimental;
24  import org.apache.maven.api.annotations.Nonnull;
25  import org.apache.maven.api.annotations.Nullable;
26  import org.apache.maven.api.annotations.Provider;
27  import org.apache.maven.api.annotations.ThreadSafe;
28  
29  /**
30   * A container for data that is specific to a session.
31   * All components may use this storage to associate arbitrary data with a session.
32   * <p>
33   * Unlike a cache, this session data is not subject to purging. For this same reason, session data should also not be
34   * abused as a cache (i.e. for storing values that can be re-calculated) to avoid memory exhaustion.
35   * <p>
36   * <strong>Note:</strong> Actual implementations must be thread-safe.
37   *
38   * @see Session#getData()
39   * @since 4.0.0
40   */
41  @Experimental
42  @ThreadSafe
43  @Provider
44  public interface SessionData {
45  
46      /**
47       * Associates the specified session data with the given key.
48       *
49       * @param key the key under which to store the session data, must not be {@code null}
50       * @param value the data to associate with the key, may be {@code null} to remove the mapping
51       */
52      void set(@Nonnull Object key, @Nullable Object value);
53  
54      /**
55       * Associates the specified session data with the given key if the key is currently mapped to the given value. This
56       * method provides an atomic compare-and-update of some key's value.
57       *
58       * @param key the key under which to store the session data, must not be {@code null}
59       * @param oldValue the expected data currently associated with the key, may be {@code null}
60       * @param newValue the data to associate with the key, may be {@code null} to remove the mapping
61       * @return {@code true} if the key mapping was successfully updated from the old value to the new value,
62       *         {@code false} if the current key mapping didn't match the expected value and was not updated.
63       */
64      boolean set(@Nonnull Object key, @Nullable Object oldValue, @Nullable Object newValue);
65  
66      /**
67       * Gets the session data associated with the specified key.
68       *
69       * @param key the key for which to retrieve the session data, must not be {@code null}
70       * @return the session data associated with the key or {@code null} if none
71       */
72      @Nullable
73      Object get(@Nonnull Object key);
74  
75      /**
76       * Retrieve of compute the data associated with the specified key.
77       *
78       * @param key the key for which to retrieve the session data, must not be {@code null}
79       * @param supplier the supplier will compute the new value
80       * @return the session data associated with the key
81       */
82      @Nullable
83      Object computeIfAbsent(@Nonnull Object key, @Nonnull Supplier<Object> supplier);
84  }