001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.plugins.streetside; 003 004import java.util.Collection; 005import java.util.List; 006import java.util.concurrent.CopyOnWriteArrayList; 007 008import org.openstreetmap.josm.plugins.streetside.model.UserProfile; 009 010/** 011 * Class that stores a sequence of {@link StreetsideAbstractImage} objects. 012 * 013 * @author nokutu 014 * @see StreetsideAbstractImage 015 */ 016 017public class StreetsideSequence { 018 019 020 /** 021 * Unique identifier. Used only for {@link StreetsideImage} sequences. 022 */ 023 private String id; 024 private UserProfile user; 025 026 private double la; 027 private double lo; 028 029 /** 030 * Epoch time when the sequence was created 031 */ 032 private long cd; 033 034 /** 035 * The images in the sequence. 036 */ 037 private List<StreetsideAbstractImage> images; 038 039 /** 040 * Unique identifier. Used only for {@link StreetsideImage} sequences. 041 */ 042 043 /** 044 * @param string 045 * @param string2 046 * @param capturedAt2 047 */ 048 public StreetsideSequence(String id, Long ca) { 049 this.id = id; 050 cd = ca; 051 images = new CopyOnWriteArrayList<>(); 052 } 053 054 /** 055 * @param string 056 * @param la 057 * @param lo 058 */ 059 public StreetsideSequence(String id, double la, double lo) { 060 this.id = id; 061 this.la = la; 062 this.lo = lo; 063 images = new CopyOnWriteArrayList<>(); 064 } 065 066 /** 067 * No argument constructor for StreetsideSequence - necessary for JSON serialization 068 */ 069 public StreetsideSequence() { 070 images = new CopyOnWriteArrayList<>(); 071 } 072 073 public StreetsideSequence(String id, double la, double lo, long ca) { 074 this.id = id; 075 this.la = la; 076 this.lo = lo; 077 cd = ca; 078 images = new CopyOnWriteArrayList<>(); 079} 080 081// TODO: Are all my sequences only set with id values? (no LatLon/Cas?) @rrh 082public StreetsideSequence(String id) { 083 this.id = id; 084 images = new CopyOnWriteArrayList<>(); 085} 086 087/** 088 * Adds a new {@link StreetsideAbstractImage} object to the database. 089 * 090 * @param image The {@link StreetsideAbstractImage} object to be added 091 */ 092 public synchronized void add(StreetsideAbstractImage image) { 093 images.add(image); 094 image.setSequence(this); 095 } 096 097 /** 098 * Adds a set of {@link StreetsideAbstractImage} objects to the database. 099 * 100 * @param images The set of {@link StreetsideAbstractImage} objects to be added. 101 */ 102 public synchronized void add(Collection<? extends StreetsideAbstractImage> images) { 103 this.images.addAll(images); 104 images.forEach(img -> img.setSequence(this)); 105 } 106 107 /** 108 * Returns the next {@link StreetsideAbstractImage} in the sequence of a given 109 * {@link StreetsideAbstractImage} object. 110 * 111 * @param image The {@link StreetsideAbstractImage} object whose next image is 112 * going to be returned. 113 * 114 * @return The next {@link StreetsideAbstractImage} object in the sequence. 115 * 116 * @throws IllegalArgumentException if the given {@link StreetsideAbstractImage} object doesn't belong 117 * the this sequence. 118 */ 119 public StreetsideAbstractImage next(StreetsideAbstractImage image) { 120 int i = images.indexOf(image); 121 if (i == -1) { 122 throw new IllegalArgumentException(); 123 } 124 if (i == images.size() - 1) { 125 return null; 126 } 127 return images.get(i + 1); 128 } 129 130 /** 131 * Returns the previous {@link StreetsideAbstractImage} in the sequence of a 132 * given {@link StreetsideAbstractImage} object. 133 * 134 * @param image The {@link StreetsideAbstractImage} object whose previous image is 135 * going to be returned. 136 * 137 * @return The previous {@link StreetsideAbstractImage} object in the sequence. 138 * 139 * @throws IllegalArgumentException if the given {@link StreetsideAbstractImage} object doesn't belong 140 * the this sequence. 141 */ 142 public StreetsideAbstractImage previous(StreetsideAbstractImage image) { 143 int i = images.indexOf(image); 144 if (i < 0) { 145 throw new IllegalArgumentException(); 146 } 147 if (i == 0) { 148 return null; 149 } 150 return images.get(i - 1); 151 } 152 153 /** 154 * Removes a {@link StreetsideAbstractImage} object from the database. 155 * 156 * @param image The {@link StreetsideAbstractImage} object to be removed. 157 */ 158 public void remove(StreetsideAbstractImage image) { 159 images.remove(image); 160 } 161 162 /** 163 * @param id the id to set 164 */ 165 public void setId(String id) { 166 this.id = id; 167 } 168 169 /** 170 * @return the la 171 */ 172 public double getLa() { 173 return la; 174 } 175 176 /** 177 * @param la the la to set 178 */ 179 public void setLa(double la) { 180 this.la = la; 181 } 182 183 /** 184 * @return the lo 185 */ 186 public double getLo() { 187 return lo; 188 } 189 190 /** 191 * @param lo the lo to set 192 */ 193 public void setLo(double lo) { 194 this.lo = lo; 195 } 196 197 /** 198 * Returns the Epoch time when the sequence was captured. 199 * 200 * Negative values mean, no value is set. 201 * 202 * @return A long containing the Epoch time when the sequence was captured. 203 */ 204 public long getCd() { 205 return cd; 206 } 207 208 /** 209 * Returns all {@link StreetsideAbstractImage} objects contained by this 210 * object. 211 * 212 * @return A {@link List} object containing all the 213 * {@link StreetsideAbstractImage} objects that are part of the 214 * sequence. 215 */ 216 public List<StreetsideAbstractImage> getImages() { 217 return images; 218 } 219 220 /** 221 * Returns the unique identifier of the sequence. 222 * 223 * @return A {@code String} containing the unique identifier of the sequence. 224 * null means that the sequence has been created locally for imported 225 * images. 226 */ 227 public String getId() { 228 return id; 229 } 230 231 public UserProfile getUser() { 232 return user; 233 } 234}