just before adding authorization service.
[photos] / src / main / java / org / wamblee / photos / model / filesystem / FileSystemPhoto.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.model.filesystem;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileNotFoundException;
21 import java.io.IOException;
22 import java.io.InputStream;
23
24 import org.wamblee.photos.model.Photo;
25 import org.wamblee.photos.model.PhotoEntry;
26
27 /**
28  * Represents a photo stored on the file system.
29  */
30 public class FileSystemPhoto implements Photo {
31
32         private File _thumbnail;
33         private File _photo;
34         private String _path; 
35
36         public FileSystemPhoto(File aThumbnail, File aPhoto, String aPath) {
37                 _thumbnail = aThumbnail;
38                 _photo = aPhoto;
39                 _path = aPath;
40         }
41
42         /*
43          * (non-Javadoc)
44          * 
45          * @see org.wamblee.photos.database.PhotoEntry#getPath()
46          */
47         public String getPath() {
48                 return _path;
49         }
50
51         /*
52          * (non-Javadoc)
53          * 
54          * @see org.wamblee.photos.database.Photo#getThumbNail()
55          */
56         public InputStream getThumbNail() throws IOException {
57                 return openFile(_thumbnail);
58         }
59
60         /*
61          * (non-Javadoc)
62          * 
63          * @see org.wamblee.photos.database.Photo#getFile()
64          */
65         public InputStream getPhoto() throws IOException {
66                 return openFile(_photo);
67         }
68
69         private InputStream openFile(File aFile) throws FileNotFoundException {
70                 return new FileInputStream(aFile);
71         }
72
73         /*
74          * (non-Javadoc)
75          * 
76          * @see org.wamblee.photos.database.PhotoEntry#getId()
77          */
78         public String getId() {
79                 return getId(_path);
80         }
81     
82     public static String getId(String aPath) {
83         int index = aPath.lastIndexOf("/");
84         if (index < 0) {
85             return aPath;
86         }
87         return aPath.substring(index + 1);
88     }
89
90         public String toString() {
91                 return print("");
92         }
93
94         String print(String aIndent) {
95                 return aIndent + "IMG: " + _photo.getName();
96         }
97     
98     /* (non-Javadoc)
99      * @see java.lang.Object#equals(java.lang.Object)
100      */
101     @Override
102     public boolean equals(Object obj) {
103         if ( !(obj instanceof Photo) ) { 
104             return false; 
105         }
106         return getPath().equals( ((Photo)obj).getPath()); 
107     }
108     
109     /* (non-Javadoc)
110      * @see java.lang.Object#hashCode()
111      */
112     @Override
113     public int hashCode() {
114         return getPath().hashCode();
115     }
116     
117     /* (non-Javadoc)
118      * @see java.lang.Comparable#compareTo(java.lang.Object)
119      */
120     public int compareTo(PhotoEntry aEntry) {
121         return getId().compareTo(((PhotoEntry)aEntry).getId()); 
122     }
123 }