just before adding authorization service.
[photos] / src / main / java / org / wamblee / photos / concurrent / ConcurrentAlbum.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */ 
16 package org.wamblee.photos.concurrent;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20
21 import org.wamblee.photos.model.Album;
22 import org.wamblee.photos.model.Path;
23 import org.wamblee.photos.model.Photo;
24 import org.wamblee.photos.model.PhotoEntry;
25
26 /**
27  * Decorator for an album providing defined behavior when used in a concurrent setting. 
28  */
29 public class ConcurrentAlbum extends ConcurrentPhotoEntry implements Album {
30     
31     /**
32      * Constructs concurrent album as a decorator for an album implementation. 
33      * @param aAlbum Album to decorate. 
34      */
35     public ConcurrentAlbum(Album aAlbum) {
36         super(aAlbum); 
37     }
38     
39     private Album decorated() {
40         return (Album)getEntry(); 
41     }
42     
43     /**
44      * Creates a decorate for the photo entry to make it safe for concurrent access.  
45      * @param aEntry Entry to decorate
46      * @return Decorated photo. 
47      */
48     private <T extends PhotoEntry> T decorate(T aEntry) {
49         if ( aEntry == null ) {
50             return null;    
51         }
52         else if ( aEntry instanceof Photo) {
53             return (T) new ConcurrentPhoto((Photo)aEntry); 
54         } else if ( aEntry instanceof Album) {
55             return (T) new ConcurrentAlbum((Album)aEntry); 
56         } else {
57             throw new IllegalArgumentException("Entry is neither a photo nor an album: " + aEntry); 
58         }
59     }
60
61     /* (non-Javadoc)
62      * @see org.wamblee.photos.model.Album#getEntry(java.lang.String)
63      */
64     public PhotoEntry getEntry(String aPath) {
65         lock().acquireRead(); 
66         try {
67             return decorate(decorated().getEntry(aPath));    
68         } finally {
69             lock().releaseRead(); 
70         }
71     }
72     
73     public PhotoEntry getEntry(Path aPath) {
74          lock().acquireRead(); 
75          try {
76              return decorate(decorated().getEntry(aPath));    
77          } finally {
78              lock().releaseRead(); 
79          }
80     }
81
82     /* (non-Javadoc)
83      * @see org.wamblee.photos.model.Album#getEntry(int)
84      */
85     public PhotoEntry getEntry(int aIndex) {
86         lock().acquireRead(); 
87         try {
88             return decorate(decorated().getEntry(aIndex)); 
89         } finally {
90             lock().releaseRead(); 
91         }
92     }
93
94     /* (non-Javadoc)
95      * @see org.wamblee.photos.model.Album#size()
96      */
97     public int size() {
98         lock().acquireRead(); 
99         try {
100             return decorated().size(); 
101         } finally {
102             lock().releaseRead(); 
103         }
104     }
105
106     /* (non-Javadoc)
107      * @see org.wamblee.photos.model.Album#addImage(java.lang.String, java.io.InputStream)
108      */
109     public void addImage(String aId, InputStream aImage) throws IOException {
110         lock().acquireWrite();
111         try {
112             decorated().addImage(aId, aImage); 
113         } finally {
114             lock().releaseWrite(); 
115         }
116     }
117
118     /* (non-Javadoc)
119      * @see org.wamblee.photos.model.Album#addAlbum(java.lang.String)
120      */
121     public void addAlbum(String aId) throws IOException {
122         lock().acquireWrite();
123         try {
124             decorated().addAlbum(aId);  
125         } finally {
126             lock().releaseWrite(); 
127         }
128     }
129
130     /* (non-Javadoc)
131      * @see org.wamblee.photos.model.Album#removeEntry(java.lang.String)
132      */
133     public void removeEntry(String aId) throws IOException {
134         lock().acquireWrite();
135         try {
136             decorated().removeEntry(aId);  
137         } finally {
138             lock().releaseWrite(); 
139         }
140     }
141     
142     public Photo findPhotoBefore(String aId) {
143          lock().acquireWrite();
144          try {
145              return decorate(decorated().findPhotoBefore(aId));  
146          } finally {
147              lock().releaseWrite(); 
148          }
149     }
150     
151     public Photo findPhotoAfter(String aId) {
152          lock().acquireWrite();
153          try {
154              return decorate(decorated().findPhotoAfter(aId));  
155          } finally {
156              lock().releaseWrite(); 
157          }
158     }
159 }