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.Objects;
22  import java.util.function.Supplier;
23  
24  import org.apache.maven.api.annotations.Experimental;
25  import org.apache.maven.api.annotations.Nonnull;
26  import org.apache.maven.api.annotations.Nullable;
27  import org.apache.maven.api.annotations.Provider;
28  import org.apache.maven.api.annotations.ThreadSafe;
29  
30  /**
31   * A container for data that is specific to a session.
32   * All components may use this storage to associate arbitrary data with a session.
33   * <p>
34   * Unlike a cache, this session data is not subject to purging. For this same reason, session data should also not be
35   * abused as a cache (i.e. for storing values that can be re-calculated) to avoid memory exhaustion.
36   * <p>
37   * <strong>Note:</strong> Actual implementations must be thread-safe.
38   *
39   * @see Session#getData()
40   * @since 4.0.0
41   */
42  @Experimental
43  @ThreadSafe
44  @Provider
45  public interface SessionData {
46  
47      /**
48       * Associates the specified session data with the given key.
49       *
50       * @param key the key under which to store the session data, must not be {@code null}
51       * @param value the data to associate with the key, may be {@code null} to remove the mapping
52       */
53      <T> void set(@Nonnull Key<T> key, @Nullable T value);
54  
55      /**
56       * Associates the specified session data with the given key if the key is currently mapped to the given value. This
57       * method provides an atomic compare-and-update of some key's value.
58       *
59       * @param key the key under which to store the session data, must not be {@code null}
60       * @param oldValue the expected data currently associated with the key, may be {@code null}
61       * @param newValue the data to associate with the key, may be {@code null} to remove the mapping
62       * @return {@code true} if the key mapping was successfully updated from the old value to the new value,
63       *         {@code false} if the current key mapping didn't match the expected value and was not updated.
64       */
65      <T> boolean replace(@Nonnull Key<T> key, @Nullable T oldValue, @Nullable T newValue);
66  
67      /**
68       * Gets the session data associated with the specified key.
69       *
70       * @param key the key for which to retrieve the session data, must not be {@code null}
71       * @return the session data associated with the key or {@code null} if none
72       */
73      @Nullable
74      <T> T get(@Nonnull Key<T> key);
75  
76      /**
77       * Retrieve of compute the data associated with the specified key.
78       *
79       * @param key the key for which to retrieve the session data, must not be {@code null}
80       * @param supplier the supplier will compute the new value
81       * @return the session data associated with the key
82       */
83      @Nullable
84      <T> T computeIfAbsent(@Nonnull Key<T> key, @Nonnull Supplier<T> supplier);
85  
86      /**
87       * Create a key using the given class as an identifier and as the type of the object.
88       */
89      static <T> Key<T> key(Class<T> clazz) {
90          return new Key<>(clazz, clazz);
91      }
92  
93      /**
94       * Create a key using the given class and id.
95       */
96      static <T> Key<T> key(Class<T> clazz, Object id) {
97          return new Key<>(clazz, id);
98      }
99  
100     /**
101      * Key used to query the session data
102      * @param <T> the type of the object associated to this key
103      */
104     final class Key<T> {
105 
106         private final Class<T> type;
107         private final Object id;
108 
109         private Key(Class<T> type, Object id) {
110             this.type = type;
111             this.id = id;
112         }
113 
114         public Class<T> type() {
115             return type;
116         }
117 
118         @Override
119         public boolean equals(Object o) {
120             if (this == o) {
121                 return true;
122             }
123             if (o == null || getClass() != o.getClass()) {
124                 return false;
125             }
126             Key<?> key = (Key<?>) o;
127             return Objects.equals(id, key.id) && Objects.equals(type, key.type);
128         }
129 
130         @Override
131         public int hashCode() {
132             return Objects.hash(id, type);
133         }
134     }
135 }