From 05859a2e581424238d07d2450e54bfa453750fba Mon Sep 17 00:00:00 2001 From: Erik Forkalsrud Date: Sat, 1 Feb 2025 18:16:13 -0800 Subject: [PATCH] Clean up various IntelliJ warnings --- Readme.md | 4 +- .../forkalsrud/album/exif/DirectoryEntry.java | 74 ++++++++----------- .../album/video/EncodingProcessListener.java | 4 +- .../forkalsrud/album/web/AlbumServlet.java | 57 ++++++-------- 4 files changed, 58 insertions(+), 81 deletions(-) diff --git a/Readme.md b/Readme.md index 452eb60..90ccebf 100644 --- a/Readme.md +++ b/Readme.md @@ -16,8 +16,8 @@ Create a file: `~/forkalsrud.org/photo.properties` and for each named "root" (`photo` in the example below), point the `base` property and `dbdir` properties -to the direcory holding the photos, and the diractory to be populated with -BerkelyDB files to your photo folder: +to the directory holding the photos, and the directory to be populated with +BerkeleyDB files to your photo folder: root.photo.base=/home/photo root.photo.dbdir=/home/resin/web/db diff --git a/src/main/java/org/forkalsrud/album/exif/DirectoryEntry.java b/src/main/java/org/forkalsrud/album/exif/DirectoryEntry.java index 4d4ddb8..c86f161 100644 --- a/src/main/java/org/forkalsrud/album/exif/DirectoryEntry.java +++ b/src/main/java/org/forkalsrud/album/exif/DirectoryEntry.java @@ -8,11 +8,8 @@ import java.io.FileInputStream; import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Collections; import java.util.Comparator; import java.util.Date; -import java.util.HashMap; -import java.util.Iterator; import java.util.List; import org.forkalsrud.album.db.DirectoryDatabase; @@ -27,14 +24,14 @@ public class DirectoryEntry extends EntryWithChildren { public interface ServiceApi { - public DirectoryDatabase getDirectoryDatabase(); - public DirectoryMetadataGenerator getMetadataGenerator(); + DirectoryDatabase getDirectoryDatabase(); + DirectoryMetadataGenerator getMetadataGenerator(); } ServiceApi services; DirectoryProps cache; boolean childrenLoaded = false; - Comparator sort = null; + Comparator ordering = null; Date earliest = null; boolean groupByYear; @@ -129,7 +126,7 @@ public class DirectoryEntry extends EntryWithChildren { private void sort() { - Collections.sort(children, sort); + children.sort(ordering); } @@ -139,45 +136,35 @@ public class DirectoryEntry extends EntryWithChildren { String coverFileName = props.getProperty("cover"); String caption = props.getProperty("caption"); setCaption(caption); - sort = ComparatorFactory.getSort(props.getProperty("sort")); + ordering = ComparatorFactory.getSort(props.getProperty("sort")); - HashMap entryMap = new HashMap(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss"); - Date oldest = new Date(file.lastModified()); - - Iterator i = props.keySet().iterator(); - while (i.hasNext()) { - String key = (String)i.next(); + for (String key : props.stringPropertyNames()) { if (key.startsWith("file.") && key.endsWith(".dimensions")) { String name = key.substring("file.".length(), key.length() - ".dimensions".length()); - if (!entryMap.containsKey(name)) { - File f = new File(file, name); - String type = props.getProperty("file." + name + ".type", "image"); - FileEntry entry = new FileEntry(this, f, type); - Thumbnail thumbnail = new Thumbnail(f, type); - entry.setCaption(props.getProperty("file." + name + ".caption")); - Date fileDate = sdf.parse(props.getProperty("file." + name + ".captureDate")); - if (fileDate.before(oldest)) { - oldest = fileDate; - } - entry.setDate(fileDate); - thumbnail.setSize(new Dimension(props.getProperty("file." + name + ".dimensions"))); - thumbnail.setOrientation(Integer.parseInt(props.getProperty("file." + name + ".orientation"))); - thumbnail.setEtag(props.getProperty("file." + name + ".etag")); - entry.setThumbnail(thumbnail); - boolean hidden = Boolean.parseBoolean(props.getProperty("file." + name + ".hidden")); - if (!hidden) { - children.add(entry); - if (name.equals(coverFileName)) { - setThumbnail(thumbnail); - } - } - String duration = props.getProperty("file." + name + ".length"); - if (duration != null) { - thumbnail.setDuration(duration); + File f = new File(file, name); + String type = props.getProperty("file." + name + ".type", "image"); + FileEntry entry = new FileEntry(this, f, type); + Thumbnail thumbnail = new Thumbnail(f, type); + entry.setCaption(props.getProperty("file." + name + ".caption")); + Date fileDate = sdf.parse(props.getProperty("file." + name + ".captureDate")); + entry.setDate(fileDate); + thumbnail.setSize(new Dimension(props.getProperty("file." + name + ".dimensions"))); + thumbnail.setOrientation(Integer.parseInt(props.getProperty("file." + name + ".orientation"))); + thumbnail.setEtag(props.getProperty("file." + name + ".etag")); + entry.setThumbnail(thumbnail); + boolean hidden = Boolean.parseBoolean(props.getProperty("file." + name + ".hidden")); + if (!hidden) { + children.add(entry); + if (name.equals(coverFileName)) { + setThumbnail(thumbnail); } } + String duration = props.getProperty("file." + name + ".length"); + if (duration != null) { + thumbnail.setDuration(duration); + } } else if (key.startsWith("dir.") && !key.endsWith(".hidden") && !key.endsWith(".caption")) { String name = key.substring("dir.".length()); boolean hidden = Boolean.parseBoolean(props.getProperty("dir." + name + ".hidden")); @@ -187,9 +174,7 @@ public class DirectoryEntry extends EntryWithChildren { setThumbnail(dir.getThumbnail()); } Date fileDate = dir.getEarliest(); - if (fileDate != null && fileDate.before(oldest)) { - oldest = fileDate; - } + dir.setDate(fileDate); dir.setCaption(props.getProperty("dir." + name + ".caption")); if (!dir.getContents().isEmpty()) { children.add(dir); @@ -197,7 +182,10 @@ public class DirectoryEntry extends EntryWithChildren { } } } - this.earliest = oldest; + this.earliest = children.stream() + .map(Entry::getDate) + .min(Date::compareTo) + .orElse(new Date(file.lastModified())); this.groupByYear = "year".equalsIgnoreCase(props.getProperty("group")); if (thumbnail == null && !children.isEmpty()) { setThumbnail(children.get(0).getThumbnail()); diff --git a/src/main/java/org/forkalsrud/album/video/EncodingProcessListener.java b/src/main/java/org/forkalsrud/album/video/EncodingProcessListener.java index 9b07c64..efac8ea 100644 --- a/src/main/java/org/forkalsrud/album/video/EncodingProcessListener.java +++ b/src/main/java/org/forkalsrud/album/video/EncodingProcessListener.java @@ -2,8 +2,8 @@ package org.forkalsrud.album.video; public interface EncodingProcessListener { - public abstract void chunkAvailable(int chunkNo); + void chunkAvailable(int chunkNo); - public abstract void codingFinished(int lastCunkNo); + void codingFinished(int lastChunkNo); } \ No newline at end of file diff --git a/src/main/java/org/forkalsrud/album/web/AlbumServlet.java b/src/main/java/org/forkalsrud/album/web/AlbumServlet.java index 183f93e..369161c 100644 --- a/src/main/java/org/forkalsrud/album/web/AlbumServlet.java +++ b/src/main/java/org/forkalsrud/album/web/AlbumServlet.java @@ -36,7 +36,7 @@ import com.sleepycat.je.EnvironmentConfig; public class AlbumServlet extends HttpServlet { - private static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AlbumServlet.class); + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AlbumServlet.class); static void addDummyLoggerFor(String... names) { for (String name : names) @@ -72,7 +72,6 @@ public class AlbumServlet } }); } - hh = ll.getHandlers(); } catch (Exception e) { e.printStackTrace(); } @@ -95,9 +94,9 @@ public class AlbumServlet * maps files to URIs relative to servlet, e.g. /home/joe/photos/holiday/arrival.jpg -> /photos/holiday/arrival.jpg * assuming base is /home/joe/photos */ - public class Mapper { + public static class Mapper { - private File base; + private final File base; public Mapper(File base) { this.base = base; @@ -108,7 +107,7 @@ public class AlbumServlet return appendFile(buf, file).toString(); } - StringBuilder appendFile(StringBuilder buf, File file) { + private StringBuilder appendFile(StringBuilder buf, File file) { if (file == null) { return buf; } @@ -118,16 +117,6 @@ public class AlbumServlet return appendFile(buf, file.getParentFile()).append('/').append(file.getName()); } } - - Calendar cal = Calendar.getInstance(); - - public String year(Date d) { - if (d == null) { - return ""; - } - cal.setTime(d); - return String.valueOf(cal.get(Calendar.YEAR)); - } } @@ -138,7 +127,7 @@ public class AlbumServlet String basePrefix; DirectoryEntryFactory dirEntryFactory; - private Environment environment; + private final Environment environment; ThumbnailDatabase thumbDb; DirectoryDatabase dirDb; MovieDatabase movieDb; @@ -219,8 +208,8 @@ public class AlbumServlet res.setContentType("text/html"); req.setAttribute("search", query); req.setAttribute("entry", results); - req.setAttribute("thmb", new Integer(250)); - req.setAttribute("full", new Integer(800)); + req.setAttribute("thmb", 250); + req.setAttribute("full", 800); RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/photo.vm"); rd.forward(req, res); } @@ -229,7 +218,7 @@ public class AlbumServlet void handlePhoto(HttpServletRequest req, HttpServletResponse res, FileEntry entry) throws Exception { res.setContentType("text/html"); req.setAttribute("entry", entry); - req.setAttribute("thmb", new Integer(800)); + req.setAttribute("thmb", 800); RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/photo.vm"); rd.forward(req, res); } @@ -237,20 +226,20 @@ public class AlbumServlet void handleAlbum(HttpServletRequest req, HttpServletResponse res, DirectoryEntry entry) throws Exception { res.setContentType("text/html"); req.setAttribute("entry", entry); - req.setAttribute("thmb", new Integer(250)); - req.setAttribute("full", new Integer(800)); + req.setAttribute("thmb", 250); + req.setAttribute("full", 800); req.setAttribute("D", "$"); RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/ng.html"); rd.forward(req, res); } - void handleMovieFrame(HttpServletRequest req, HttpServletResponse res, FileEntry entry) throws Exception { + void handleMovieFrame(HttpServletRequest req, HttpServletResponse res, FileEntry entry) { File file = entry.getPath(); if (notModified(req, file)) { res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); res.setDateHeader("Expires", System.currentTimeMillis() + (30 * 24 * 3600 * 1000L)); // 30 days - log.info(file.getName() + " not modified (based on date)"); + log.info("{} not modified (based on date)", file.getName()); return; } int secondNo = 3; @@ -265,7 +254,7 @@ public class AlbumServlet if (cimg.lastModified >= fileTs) { // log.info("cache hit on " + key); } else { - log.info(" " + key + " has changed so cache entry wil be refreshed"); + log.info(" {} has changed so cache entry wil be refreshed", key); cimg = null; } } @@ -273,7 +262,7 @@ public class AlbumServlet try { cimg = movieCoder.extractFrame(file, secondNo, entry.getThumbnail(), size); thumbDb.store(key, cimg); - log.info(" " + key + " added to the cache with size " + cimg.bits.length + " -- now " + thumbDb.size() + " entries"); + log.info(" {} added to the cache with size {} -- now {} entries", key, cimg.bits.length, thumbDb.size()); } catch (Exception e) { //e.fillInStackTrace(); throw new RuntimeException("sadness", e); @@ -297,7 +286,7 @@ public class AlbumServlet if (notModified(req, file)) { res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); res.setDateHeader("Expires", System.currentTimeMillis() + (30 * 24 * 3600 * 1000L)); // 30 days - log.info(file.getName() + " not modified (based on date)"); + log.info("{} not modified (based on date)", file.getName()); return; } try { @@ -429,7 +418,7 @@ public class AlbumServlet } res.setContentType("text/html"); req.setAttribute("entry", entry); - req.setAttribute("thmb", new Integer(640)); + req.setAttribute("thmb", 640); RequestDispatcher rd = req.getRequestDispatcher("/WEB-INF/velocity/edit.vm"); rd.forward(req, res); } @@ -443,14 +432,14 @@ public class AlbumServlet if (notModified(req, file)) { res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); res.setDateHeader("Expires", System.currentTimeMillis() + (30 * 24 * 3600 * 1000L)); // 30 days - log.info(file.getName() + " not modified (based on date)"); + log.info("{} not modified (based on date)", file.getName()); return; } String fileEtag = thumbnail.getEtag() + "-" + size; if (etagMatches(req, fileEtag)) { res.setStatus(HttpServletResponse.SC_NOT_MODIFIED); res.setDateHeader("Expires", System.currentTimeMillis() + (30 * 24 * 3600 * 1000L)); // 30 days - log.info(file.getName() + " not modified (based on etag)"); + log.info("{} not modified (based on etag)", file.getName()); return; } @@ -461,7 +450,7 @@ public class AlbumServlet if (cimg.lastModified == file.lastModified()) { // log.info("cache hit on " + key); } else { - log.info(" " + key + " has changed so cache entry wil be refreshed"); + log.info(" {} has changed so cache entry wil be refreshed", key); cimg = null; } } @@ -469,7 +458,7 @@ public class AlbumServlet cimg = pictureScaler.scalePicture(file, thumbnail, size); thumbDb.store(key, cimg); int byteSize = cimg.bits != null ? cimg.bits.length : 0; - log.info(" " + key + " added to the cache with size " + byteSize + " -- now " + thumbDb.size() + " entries"); + log.info(" {} added to the cache with size {} -- now {} entries", key, byteSize, thumbDb.size()); } res.setStatus(HttpServletResponse.SC_OK); res.setDateHeader("Last-Modified", file.lastModified()); @@ -483,7 +472,7 @@ public class AlbumServlet - Map roots = new HashMap(); + Map roots = new HashMap<>(); PictureScaler pictureScaler; @@ -570,7 +559,7 @@ public class AlbumServlet @Override public void destroy() { log.info("Shutting down Album"); - roots.values().forEach(r -> r.close()); + roots.values().forEach(Root::close); } @@ -627,7 +616,7 @@ public class AlbumServlet ObjectMapper json = new ObjectMapper(); ArrayNode arr = json.createArrayNode(); - roots.values().stream().map(r -> r.getName()).forEach(arr::add); + roots.values().stream().map(Root::getName).forEach(arr::add); json.writeValue(res.getOutputStream(), arr); return; }