View Javadoc
1   /*******************************************************************************
2    * Copyright (c) 2010, 2014 Sonatype, Inc.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *    Sonatype, Inc. - initial API and implementation
10   *******************************************************************************/
11  package org.eclipse.aether;
12  
13  import java.util.concurrent.ConcurrentHashMap;
14  import java.util.concurrent.ConcurrentMap;
15  
16  /**
17   * A simple session data storage backed by a thread-safe map.
18   */
19  public final class DefaultSessionData
20      implements SessionData
21  {
22  
23      private final ConcurrentMap<Object, Object> data;
24  
25      public DefaultSessionData()
26      {
27          data = new ConcurrentHashMap<Object, Object>();
28      }
29  
30      public void set( Object key, Object value )
31      {
32          if ( key == null )
33          {
34              throw new IllegalArgumentException( "key must not be null" );
35          }
36  
37          if ( value != null )
38          {
39              data.put( key, value );
40          }
41          else
42          {
43              data.remove( key );
44          }
45      }
46  
47      public boolean set( Object key, Object oldValue, Object newValue )
48      {
49          if ( key == null )
50          {
51              throw new IllegalArgumentException( "key must not be null" );
52          }
53  
54          if ( newValue != null )
55          {
56              if ( oldValue == null )
57              {
58                  return data.putIfAbsent( key, newValue ) == null;
59              }
60              return data.replace( key, oldValue, newValue );
61          }
62          else
63          {
64              if ( oldValue == null )
65              {
66                  return !data.containsKey( key );
67              }
68              return data.remove( key, oldValue );
69          }
70      }
71  
72      public Object get( Object key )
73      {
74          if ( key == null )
75          {
76              throw new IllegalArgumentException( "key must not be null" );
77          }
78  
79          return data.get( key );
80      }
81  
82  }