[Schmitzm-commits] r2369 - in trunk: schmitzm-core/src/main/java/de/schmitzm/io schmitzm-core/src/main/java/de/schmitzm/lang schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Wed Jul 24 13:18:16 CEST 2013


Author: mojays
Date: 2013-07-24 13:18:16 +0200 (Wed, 24 Jul 2013)
New Revision: 2369

Added:
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileComparator.java
Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
   trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagV1Panel.java
   trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagV2Panel.java
Log:
new FileComparator
IOUtil: findFiles(.) modified to work with individual Collection
ID3TagV1Panel/ID3TagV2Panel: handling of NULL year

Added: trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileComparator.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileComparator.java	                        (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileComparator.java	2013-07-24 11:18:16 UTC (rev 2369)
@@ -0,0 +1,89 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ * 
+ * This file is part of the SCHMITZM library - a collection of utility 
+ * classes based on Java 1.6, focusing (not only) on Java Swing 
+ * and the Geotools library.
+ * 
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ * 
+ * Contributors:
+ *     Martin O. J. Schmitz - initial API and implementation
+ *     Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.io;
+
+import java.io.File;
+import java.util.Comparator;
+import java.util.SortedSet;
+
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * Comparator for {@link File} objects, e.g. to order a {@link SortedSet}.
+ * @author Martin O.J. Schmitz
+ */
+public class FileComparator implements Comparator<File> {
+    protected boolean completePath = false;
+    protected boolean caseSensitive = false;
+  
+    /**
+     * Creates a new comparator.
+     * @param completePath indicates whether to compare the complete path or
+     *                     simply the file name (complete path only is used if
+     *                     name is equal)
+     * @param caseSensitive indicates whether compare is case-sensitive
+     */
+    public FileComparator(boolean completePath, boolean caseSensitive) {
+      this.completePath = completePath;
+      this.caseSensitive = caseSensitive;
+    }
+    
+  
+    /**
+     * Performs the string compare between 2 files.
+     */
+    @Override
+    public int compare(File o1, File o2) {
+      if ( o1 == null && o2 == null )
+        return 0;
+      
+      String fileName1 = o1 != null ? (completePath ? o1.getAbsolutePath() : o1.getName()) : null;
+      String fileName2 = o2 != null ? (completePath ? o2.getAbsolutePath() : o2.getName()) : null;
+      int ret = compareFilename(fileName1, fileName2);
+
+      if ( ret == 0 && !completePath ) {
+        fileName1 = o1.getAbsolutePath();
+        fileName2 = o2.getAbsolutePath();
+        ret = compareFilename(fileName1, fileName2);
+      }
+
+      return ret;
+    }
+    
+    /**
+     * Performs the string compare between 2 file names or file paths.
+     */
+    protected int compareFilename(String filename1, String filename2) {
+      filename1 = StringUtils.trimToEmpty(filename1);
+      filename2 = StringUtils.trimToEmpty(filename2);
+      return caseSensitive ? filename1.compareTo(filename2) : filename1.compareToIgnoreCase(filename2);
+    
+    }
+}

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2013-07-22 15:08:44 UTC (rev 2368)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/IOUtil.java	2013-07-24 11:18:16 UTC (rev 2369)
@@ -1245,6 +1245,20 @@
 		return new File(path3);
 	}
 
+    /**
+     * Searches for files in a directory (and its sub folders).
+     * 
+     * @param dir
+     *            directory to search in
+     * @param fileFilter
+     *            filter for the files to search for
+     * @param recursive
+     *            indicates whether sub directories are scanned
+     */
+    public static List<File> findFiles(File dir, FileFilter fileFilter, boolean recursive) {
+      return findFiles(dir, fileFilter, recursive, new ArrayList<File>());
+    }
+    
 	/**
 	 * Searches for files in a directory (and its sub folders).
 	 * 
@@ -1258,19 +1272,22 @@
 	 *            list to store the found files in (if {@code null} a new list
 	 *            is created and returned)
 	 */
-	public static Vector<File> findFiles(File dir, FileFilter fileFilter,
-			boolean recursive, Vector<File> foundFiles) {
+	public static <E extends Collection<File>> E findFiles(File dir, FileFilter fileFilter,
+			boolean recursive, E foundFiles) {
 		if (foundFiles == null)
-			foundFiles = new Vector<File>();
+//			foundFiles = new Vector<File>();
+		  return (E) findFiles(dir,fileFilter,recursive,new ArrayList<File>());
 		if (dir == null || !dir.isDirectory())
 			return foundFiles;
 
 		File[] files = dir.listFiles(fileFilter);
-		LangUtil.addObjectsToVector(files, foundFiles);
+//		LangUtil.addObjectsToVector(files, foundFiles);
+		foundFiles.addAll( Arrays.asList(files) );
 		if (recursive) {
 			File[] subDirs = dir.listFiles(FilterUtil.ALL_DIRS_FILTER);
-			for (File subDir : subDirs)
-				findFiles(subDir, fileFilter, recursive, foundFiles);
+			if ( subDirs != null )
+			  for (File subDir : subDirs)
+  				findFiles(subDir, fileFilter, recursive, foundFiles);
 		}
 		return foundFiles;
 	}

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2013-07-22 15:08:44 UTC (rev 2368)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2013-07-24 11:18:16 UTC (rev 2369)
@@ -768,7 +768,7 @@
 	public static Vector addObjectsToVector(Object[] obj, Vector vec) {
 		if (vec == null)
 			vec = new Vector();
-		for (int i = 0; i < obj.length; i++)
+		for (int i = 0; obj != null && i < obj.length; i++)
 			vec.add(obj[i]);
 		return vec;
 	}

Modified: trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagV1Panel.java
===================================================================
--- trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagV1Panel.java	2013-07-22 15:08:44 UTC (rev 2368)
+++ trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagV1Panel.java	2013-07-24 11:18:16 UTC (rev 2369)
@@ -112,7 +112,7 @@
     if ( albumCheckbox.getValue() )
       tag.setAlbum( StringUtils.trimToEmpty(album.getValue()) );
     if ( yearCheckbox.getValue() )
-      tag.setYear( year.getValue() == null ? null : year.getValue().toString() );
+      tag.setYear( year.getValue() == null ? "" : year.getValue().toString() );
     if ( genreCheckbox.getValue() )
       ID3TagUtil.setGenre(tag, (Genre)genre.getValue());
     if ( commentCheckbox.getValue() )

Modified: trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagV2Panel.java
===================================================================
--- trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagV2Panel.java	2013-07-22 15:08:44 UTC (rev 2368)
+++ trunk/schmitzm-mp3/src/main/java/de/schmitzm/mp3/id3/gui/ID3TagV2Panel.java	2013-07-24 11:18:16 UTC (rev 2369)
@@ -223,7 +223,7 @@
     if ( totalTracksCheckbox.getValue() )
       ID3TagUtil.setTotalTracks(tag2, ID3TagUtil.getTotalTracks(tag1));
     if ( yearCheckbox.getValue() )
-      tag2.setYear( tag1.getYear() );
+      ID3TagUtil.setYear(tag2, ID3TagUtil.getYear(tag1));
     if ( genreCheckbox.getValue() )
       tag2.setGenre( tag1.getGenre() );
     if ( commentCheckbox.getValue() )



More information about the Schmitzm-commits mailing list