1 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 private void doNotify() 69 { 70 if (notify_all) this.notifyAll(); 71 else this.notify(); 72 } 73 74 static final long serialVersionUID = 1; 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 |