KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > holders > ChangeNotifyingSynchronizedIntHolder


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.holders;
25
26 import java.io.*;
27 import com.mchange.v2.ser.UnsupportedVersionException;
28
29 public final class ChangeNotifyingSynchronizedIntHolder implements ThreadSafeIntHolder, Serializable
30 {
31     transient int value;
32     transient boolean notify_all;
33
34     public ChangeNotifyingSynchronizedIntHolder( int value, boolean notify_all )
35     {
36     this.value = value;
37     this.notify_all = notify_all;
38     }
39
40     public ChangeNotifyingSynchronizedIntHolder()
41     { this(0, true); }
42
43     public synchronized int getValue()
44     { return value; }
45
46     public synchronized void setValue(int value)
47     {
48     if (value != this.value)
49         {
50         this.value = value;
51         doNotify();
52         }
53     }
54
55     public synchronized void increment()
56     {
57     ++value;
58     doNotify();
59     }
60
61     public synchronized void decrement()
62     {
63     --value;
64     doNotify();
65     }
66
67     //must be called from a sync'ed block...
68
private void doNotify()
69     {
70     if (notify_all) this.notifyAll();
71     else this.notify();
72     }
73
74     //Serialization
75
static final long serialVersionUID = 1; //override to take control of versioning
76
private final static short VERSION = 0x0001;
77     
78     private void writeObject(ObjectOutputStream out) throws IOException
79     {
80     out.writeShort(VERSION);
81     out.writeInt(value);
82     out.writeBoolean(notify_all);
83     }
84     
85     private void readObject(ObjectInputStream in) throws IOException
86     {
87     short version = in.readShort();
88     switch (version)
89         {
90         case 0x0001:
91         this.value = in.readInt();
92         this.notify_all = in.readBoolean();
93         break;
94         default:
95         throw new UnsupportedVersionException(this, version);
96         }
97     }
98 }
99
Popular Tags