View Javadoc

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