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  import org.apache.maven.api.annotations.Experimental;
23  import org.apache.maven.api.annotations.Nonnull;
24  import org.apache.maven.api.annotations.Nullable;
25  import org.apache.maven.api.annotations.Provider;
26  import org.apache.maven.api.annotations.ThreadSafe;
27  
28  /**
29   * A container for data that is specific to a session.
30   * All components may use this storage to associate arbitrary data with a session.
31   * <p>
32   * Unlike a cache, this session data is not subject to purging. For this same reason, session data should also not be
33   * abused as a cache (i.e. for storing values that can be re-calculated) to avoid memory exhaustion.
34   * <p>
35   * <strong>Note:</strong> Actual implementations must be thread-safe.
36   *
37   * @see Session#getData()
38   * @since 4.0
39   */
40  @Experimental
41  @ThreadSafe
42  @Provider
43  public interface SessionData {
44  
45      /**
46       * Associates the specified session data with the given key.
47       *
48       * @param key the key under which to store the session data, must not be {@code null}
49       * @param value the data to associate with the key, may be {@code null} to remove the mapping
50       */
51      void set(@Nonnull Object key, @Nullable Object value);
52  
53      /**
54       * Associates the specified session data with the given key if the key is currently mapped to the given value. This
55       * method provides an atomic compare-and-update of some key's value.
56       *
57       * @param key the key under which to store the session data, must not be {@code null}
58       * @param oldValue the expected data currently associated with the key, may be {@code null}
59       * @param newValue the data to associate with the key, may be {@code null} to remove the mapping
60       * @return {@code true} if the key mapping was successfully updated from the old value to the new value,
61       *         {@code false} if the current key mapping didn't match the expected value and was not updated.
62       */
63      boolean set(@Nonnull Object key, @Nullable Object oldValue, @Nullable Object newValue);
64  
65      /**
66       * Gets the session data associated with the specified key.
67       *
68       * @param key the key for which to retrieve the session data, must not be {@code null}
69       * @return the session data associated with the key or {@code null} if none
70       */
71      @Nullable
72      Object get(@Nonnull Object key);
73  
74      /**
75       * Retrieve of compute the data associated with the specified key.
76       *
77       * @param key the key for which to retrieve the session data, must not be {@code null}
78       * @param supplier the supplier will compute the new value
79       * @return the session data associated with the key
80       */
81      @Nullable
82      Object computeIfAbsent(@Nonnull Object key, @Nonnull Supplier<Object> supplier);
83  }