/* * Copyright 2005 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.photos.concurrent; import java.io.IOException; import java.io.InputStream; import org.wamblee.photos.model.Album; import org.wamblee.photos.model.Path; import org.wamblee.photos.model.Photo; import org.wamblee.photos.model.PhotoEntry; /** * Decorator for an album providing defined behavior when used in a concurrent setting. */ public class ConcurrentAlbum extends ConcurrentPhotoEntry implements Album { /** * Constructs concurrent album as a decorator for an album implementation. * @param aAlbum Album to decorate. */ public ConcurrentAlbum(Album aAlbum) { super(aAlbum); } private Album decorated() { return (Album)getEntry(); } /** * Creates a decorate for the photo entry to make it safe for concurrent access. * @param aEntry Entry to decorate * @return Decorated photo. */ private T decorate(T aEntry) { if ( aEntry == null ) { return null; } else if ( aEntry instanceof Photo) { return (T) new ConcurrentPhoto((Photo)aEntry); } else if ( aEntry instanceof Album) { return (T) new ConcurrentAlbum((Album)aEntry); } else { throw new IllegalArgumentException("Entry is neither a photo nor an album: " + aEntry); } } /* (non-Javadoc) * @see org.wamblee.photos.model.Album#getEntry(java.lang.String) */ public PhotoEntry getEntry(String aPath) { lock().acquireRead(); try { return decorate(decorated().getEntry(aPath)); } finally { lock().releaseRead(); } } public PhotoEntry getEntry(Path aPath) { lock().acquireRead(); try { return decorate(decorated().getEntry(aPath)); } finally { lock().releaseRead(); } } /* (non-Javadoc) * @see org.wamblee.photos.model.Album#getEntry(int) */ public PhotoEntry getEntry(int aIndex) { lock().acquireRead(); try { return decorate(decorated().getEntry(aIndex)); } finally { lock().releaseRead(); } } /* (non-Javadoc) * @see org.wamblee.photos.model.Album#size() */ public int size() { lock().acquireRead(); try { return decorated().size(); } finally { lock().releaseRead(); } } /* (non-Javadoc) * @see org.wamblee.photos.model.Album#addImage(java.lang.String, java.io.InputStream) */ public void addImage(String aId, InputStream aImage) throws IOException { lock().acquireWrite(); try { decorated().addImage(aId, aImage); } finally { lock().releaseWrite(); } } /* (non-Javadoc) * @see org.wamblee.photos.model.Album#addAlbum(java.lang.String) */ public void addAlbum(String aId) throws IOException { lock().acquireWrite(); try { decorated().addAlbum(aId); } finally { lock().releaseWrite(); } } /* (non-Javadoc) * @see org.wamblee.photos.model.Album#removeEntry(java.lang.String) */ public void removeEntry(String aId) throws IOException { lock().acquireWrite(); try { decorated().removeEntry(aId); } finally { lock().releaseWrite(); } } public Photo findPhotoBefore(String aId) { lock().acquireWrite(); try { return decorate(decorated().findPhotoBefore(aId)); } finally { lock().releaseWrite(); } } public Photo findPhotoAfter(String aId) { lock().acquireWrite(); try { return decorate(decorated().findPhotoAfter(aId)); } finally { lock().releaseWrite(); } } }