Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 17 | package org.apache.felix.shell.remote; |
| 18 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 19 | /** |
| 20 | * Provides an atomic integer. |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 21 | */ |
| 22 | class AtomicInteger |
| 23 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 24 | private int m_value; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 25 | |
| 26 | public AtomicInteger() |
| 27 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 28 | m_value = 0; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 29 | }//constructor |
| 30 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 31 | /** |
| 32 | * Constructs a new <tt>AtomicInteger</tt> |
| 33 | * with a given initial value. |
| 34 | * |
| 35 | * @param value the initial value. |
| 36 | */ |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 37 | public AtomicInteger(int value) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 38 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 39 | m_value = value; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 40 | }//constructor |
| 41 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 42 | /** |
| 43 | * Increments this <tt>AtomicInteger</tt> by one. |
| 44 | * |
| 45 | * @return the resulting value. |
| 46 | */ |
| 47 | public synchronized int increment() |
| 48 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 49 | return ++m_value; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 50 | }//increment |
| 51 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 52 | /** |
| 53 | * Decrements this <tt>AtomicInteger</tt> by one. |
| 54 | * |
| 55 | * @return the resulting value. |
| 56 | */ |
| 57 | public synchronized int decrement() |
| 58 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 59 | return --m_value; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 60 | }//decrement |
| 61 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 62 | /** |
| 63 | * Sets the value of this <tt>AtomicInteger</tt>. |
| 64 | * |
| 65 | * @param i the new value. |
| 66 | */ |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 67 | public synchronized void set(int i) |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 68 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 69 | m_value = i; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 70 | }//set |
| 71 | |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 72 | /** |
| 73 | * Returns the value of this <tt>AtomicInteger</tt>. |
| 74 | * |
| 75 | * @return the actual value. |
| 76 | */ |
| 77 | public synchronized int get() |
| 78 | { |
Richard S. Hall | 59aef19 | 2009-04-25 14:50:37 +0000 | [diff] [blame] | 79 | return m_value; |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 80 | }//get |
Felix Meschberger | efb2d08 | 2008-08-19 13:18:47 +0000 | [diff] [blame] | 81 | }//class AtomicInteger |