View Javadoc
1   package org.eclipse.aether;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static java.util.Objects.requireNonNull;
23  
24  import java.util.concurrent.ConcurrentHashMap;
25  import java.util.concurrent.ConcurrentMap;
26  
27  /**
28   * A simple session data storage backed by a thread-safe map.
29   */
30  public final class DefaultSessionData
31      implements SessionData
32  {
33  
34      private final ConcurrentMap<Object, Object> data;
35  
36      public DefaultSessionData()
37      {
38          data = new ConcurrentHashMap<>();
39      }
40  
41      public void set( Object key, Object value )
42      {
43          requireNonNull( key, "key cannot be null" );
44  
45          if ( value != null )
46          {
47              data.put( key, value );
48          }
49          else
50          {
51              data.remove( key );
52          }
53      }
54  
55      public boolean set( Object key, Object oldValue, Object newValue )
56      {
57          requireNonNull( key, "key cannot be null" );
58  
59          if ( newValue != null )
60          {
61              if ( oldValue == null )
62              {
63                  return data.putIfAbsent( key, newValue ) == null;
64              }
65              return data.replace( key, oldValue, newValue );
66          }
67          else
68          {
69              if ( oldValue == null )
70              {
71                  return !data.containsKey( key );
72              }
73              return data.remove( key, oldValue );
74          }
75      }
76  
77      public Object get( Object key )
78      {
79          requireNonNull( key, "key cannot be null" );
80  
81          return data.get( key );
82      }
83  
84  }