1 package org.eclipse.aether;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static java.util.Objects.requireNonNull;
23
24 import java.util.concurrent.ConcurrentHashMap;
25 import java.util.concurrent.ConcurrentMap;
26 import java.util.function.Supplier;
27
28
29
30
31 public final class DefaultSessionData
32 implements SessionData
33 {
34
35 private final ConcurrentMap<Object, Object> data;
36
37 public DefaultSessionData()
38 {
39 data = new ConcurrentHashMap<>();
40 }
41
42 public void set( Object key, Object value )
43 {
44 requireNonNull( key, "key cannot be null" );
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 requireNonNull( key, "key cannot be null" );
59
60 if ( newValue != null )
61 {
62 if ( oldValue == null )
63 {
64 return data.putIfAbsent( key, newValue ) == null;
65 }
66 return data.replace( key, oldValue, newValue );
67 }
68 else
69 {
70 if ( oldValue == null )
71 {
72 return !data.containsKey( key );
73 }
74 return data.remove( key, oldValue );
75 }
76 }
77
78 public Object get( Object key )
79 {
80 requireNonNull( key, "key cannot be null" );
81
82 return data.get( key );
83 }
84
85 public Object computeIfAbsent( Object key, Supplier<Object> supplier )
86 {
87 return data.computeIfAbsent( key, k -> supplier.get() );
88 }
89 }