This is the old documentation of lombok-pg. The new version can be found in the wiki of the github repository. Take me to the new Version then!

@ReadLock/@WriteLock

Overview

Detailed Description

With Lombok

01 import lombok.ReadLock;
02 import lombok.Validate.NotEmpty;
03 import lombok.Validate.NotNull;
04 import lombok.WriteLock;
05 
06 import java.util.HashMap;
07 import java.util.Map;
08 
09 class LockExample {
10   private Map<String, String> dictionary = new HashMap<String, String>();
11   
12   @WriteLock("dictionaryLock")
13   public void put(@NotEmpty String key, @NotNull String value) {
14     dictionary.put(key, value);
15   }
16   
17   @ReadLock("dictionaryLock")
18   public String get(@NotEmpty String key) {
19     return dictionary.get(key);
20   }
21 }

Vanilla Java

01 import java.util.Map;
02 import java.util.HashMap;
03 
04 class LockExample {
05   private Map<String, String> dictionary = new HashMap<String, String>();
06   private final java.util.concurrent.locks.ReadWriteLock dictionaryLock = new java.util.concurrent.locks.ReentrantReadWriteLock();
07 
08   public void put(String key, String value) {
09     if (key == null || key.isEmpty()) {
10       throw new java.lang.IllegalArgumentException("The validated object is empty");
11     }
12     if (value == null) {
13       throw new java.lang.IllegalArgumentException("The validated object is null");
14     }
15     this.dictionaryLock.writeLock().lock();
16     try {
17       dictionary.put(key, value);
18     finally {
19       this.dictionaryLock.writeLock().unlock();
20     }
21   }
22 
23   public String get(String key) {
24     if (key == null || key.isEmpty()) {
25       throw new java.lang.IllegalArgumentException("The validated object is empty");
26     }
27     this.dictionaryLock.readLock().lock();
28     try {
29       return dictionary.get(key);
30     finally {
31       this.dictionaryLock.readLock().unlock();
32     }
33   }
34 }

Small print

Smallprint