View Javadoc

1   // ========================================================================
2   // Copyright (c) 2012 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses.
12  // ========================================================================
13  
14  package org.eclipse.jetty.util;
15  
16  import java.util.concurrent.atomic.AtomicInteger;
17  import java.util.concurrent.atomic.AtomicLong;
18  
19  public class Atomics
20  {
21      private Atomics()
22      {
23      }
24  
25      public static void updateMin(AtomicLong currentMin, long newValue)
26      {
27          long oldValue = currentMin.get();
28          while (newValue < oldValue)
29          {
30              if (currentMin.compareAndSet(oldValue, newValue))
31                  break;
32              oldValue = currentMin.get();
33          }
34      }
35  
36      public static void updateMax(AtomicLong currentMax, long newValue)
37      {
38          long oldValue = currentMax.get();
39          while (newValue > oldValue)
40          {
41              if (currentMax.compareAndSet(oldValue, newValue))
42                  break;
43              oldValue = currentMax.get();
44          }
45      }
46  
47      public static void updateMin(AtomicInteger currentMin, int newValue)
48      {
49          int oldValue = currentMin.get();
50          while (newValue < oldValue)
51          {
52              if (currentMin.compareAndSet(oldValue, newValue))
53                  break;
54              oldValue = currentMin.get();
55          }
56      }
57  
58      public static void updateMax(AtomicInteger currentMax, int newValue)
59      {
60          int oldValue = currentMax.get();
61          while (newValue > oldValue)
62          {
63              if (currentMax.compareAndSet(oldValue, newValue))
64                  break;
65              oldValue = currentMax.get();
66          }
67      }
68  }