2 * Copyright 2005 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.photos.concurrent;
18 import java.io.IOException;
19 import java.io.InputStream;
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;
27 * Decorator for an album providing defined behavior when used in a concurrent setting.
29 public class ConcurrentAlbum extends ConcurrentPhotoEntry implements Album {
32 * Constructs concurrent album as a decorator for an album implementation.
33 * @param aAlbum Album to decorate.
35 public ConcurrentAlbum(Album aAlbum) {
39 private Album decorated() {
40 return (Album)getEntry();
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.
48 private <T extends PhotoEntry> T decorate(T aEntry) {
49 if ( aEntry == null ) {
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);
57 throw new IllegalArgumentException("Entry is neither a photo nor an album: " + aEntry);
62 * @see org.wamblee.photos.model.Album#getEntry(java.lang.String)
64 public PhotoEntry getEntry(String aPath) {
67 return decorate(decorated().getEntry(aPath));
73 public PhotoEntry getEntry(Path aPath) {
76 return decorate(decorated().getEntry(aPath));
83 * @see org.wamblee.photos.model.Album#getEntry(int)
85 public PhotoEntry getEntry(int aIndex) {
88 return decorate(decorated().getEntry(aIndex));
95 * @see org.wamblee.photos.model.Album#size()
100 return decorated().size();
102 lock().releaseRead();
107 * @see org.wamblee.photos.model.Album#addImage(java.lang.String, java.io.InputStream)
109 public void addImage(String aId, InputStream aImage) throws IOException {
110 lock().acquireWrite();
112 decorated().addImage(aId, aImage);
114 lock().releaseWrite();
119 * @see org.wamblee.photos.model.Album#addAlbum(java.lang.String)
121 public void addAlbum(String aId) throws IOException {
122 lock().acquireWrite();
124 decorated().addAlbum(aId);
126 lock().releaseWrite();
131 * @see org.wamblee.photos.model.Album#removeEntry(java.lang.String)
133 public void removeEntry(String aId) throws IOException {
134 lock().acquireWrite();
136 decorated().removeEntry(aId);
138 lock().releaseWrite();
142 public Photo findPhotoBefore(String aId) {
143 lock().acquireWrite();
145 return decorate(decorated().findPhotoBefore(aId));
147 lock().releaseWrite();
151 public Photo findPhotoAfter(String aId) {
152 lock().acquireWrite();
154 return decorate(decorated().findPhotoAfter(aId));
156 lock().releaseWrite();