View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.util;
17  
18  /**
19   * An attribute which allows to store a value reference. It may be updated atomically and so is thread-safe.
20   *
21   * @param <T>   the type of the value it holds.
22   */
23  public interface Attribute<T> {
24  
25      /**
26       * Returns the key of this attribute.
27       */
28      AttributeKey<T> key();
29  
30      /**
31       * Returns the current value, which may be {@code null}
32       */
33      T get();
34  
35      /**
36       * Sets the value
37       */
38      void set(T value);
39  
40      /**
41       *  Atomically sets to the given value and returns the old value which may be {@code null} if non was set before.
42       */
43      T getAndSet(T value);
44  
45      /**
46       *  Atomically sets to the given value if this {@link Attribute} does not contain a value at the moment.
47       *  If it was not possible to set the value as it contains a value it will just return the current value.
48       */
49      T setIfAbsent(T value);
50  
51      /**
52       * Removes this attribute from the {@link AttributeMap} and returns the old value..  Subsequent {@link #get()}
53       * calls will return @{code null}.
54       */
55      T getAndRemove();
56  
57      /**
58       * Atomically sets the value to the given updated value if the current value == the expected value.
59       * If it the set was successful it returns {@code true} otherwise {@code false}.
60       */
61      boolean compareAndSet(T oldValue, T newValue);
62  
63      /**
64       * Removes this attribute from the {@link AttributeMap}.  Subsequent {@link #get()} calls will return @{code null}.
65       */
66      void remove();
67  }