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 java.util.concurrent.ConcurrentHashMap;
23  import java.util.concurrent.ConcurrentMap;
24  
25  /**
26   * A simple session data storage backed by a thread-safe map.
27   */
28  public final class DefaultSessionData
29      implements SessionData
30  {
31  
32      private final ConcurrentMap<Object, Object> data;
33  
34      public DefaultSessionData()
35      {
36          data = new ConcurrentHashMap<Object, Object>();
37      }
38  
39      public void set( Object key, Object value )
40      {
41          if ( key == null )
42          {
43              throw new IllegalArgumentException( "key must not be null" );
44          }
45  
46          if ( value != null )
47          {
48              data.put( key, value );
49          }
50          else
51          {
52              data.remove( key );
53          }
54      }
55  
56      public boolean set( Object key, Object oldValue, Object newValue )
57      {
58          if ( key == null )
59          {
60              throw new IllegalArgumentException( "key must not be null" );
61          }
62  
63          if ( newValue != null )
64          {
65              if ( oldValue == null )
66              {
67                  return data.putIfAbsent( key, newValue ) == null;
68              }
69              return data.replace( key, oldValue, newValue );
70          }
71          else
72          {
73              if ( oldValue == null )
74              {
75                  return !data.containsKey( key );
76              }
77              return data.remove( key, oldValue );
78          }
79      }
80  
81      public Object get( Object key )
82      {
83          if ( key == null )
84          {
85              throw new IllegalArgumentException( "key must not be null" );
86          }
87  
88          return data.get( key );
89      }
90  
91  }