Bladeren bron

@PMD.StaticHelper

FileHelper allows inheritance

PathHelper - better handling of mixed windows/linux paths in portable way
PathHelper#asFile
PathHelper#compare
Ranides Atterwim 12 jaren geleden
bovenliggende
commit
44cfda2907

+ 27 - 16
src/main/java/net/ranides/assira/annotations/PMD.java

@@ -14,20 +14,20 @@ import static java.lang.annotation.RetentionPolicy.*;
 /**
  * Adnotacje dla PMD. Pozwalają wskazać przeznaczenie / charakterystykę danego
  * fragmentu kodu, a PMD na tej podstawie włączy lub wyłączy niektóre reguły.
- * 
+ *
  * <p>reguły zwracające uwagę na adnotacje jeszcze nie zaimplementowane</p>
  * @author ranides
  */
 
 public final class PMD {
-    
+
     private PMD() {
         // utility class
     }
-   
+
     /**
      * Klasa testowa, może mieć dużo danych pomieszanych z kodem
-     * <p><b>Suppress:</b></p> 
+     * <p><b>Suppress:</b></p>
      * <ul>
      *      <li>PMD.AvoidDuplicateLiterals</li>
      * </ul>
@@ -36,11 +36,11 @@ public final class PMD {
     @Target({TYPE,METHOD})
     public @interface UnitTest {
     }
-    
-    
+
+
     /**
      * Klasa intensywnie latająca po tablicach, których właścicielem jest ktoś inny
-     * <p><b>Suppress:</b></p> 
+     * <p><b>Suppress:</b></p>
      * <ul>
      *      <li>PMD.ArrayIsStoredDirectly</li>
      *      <li>PMD.MethodReturnsInternalArray</li>
@@ -52,10 +52,10 @@ public final class PMD {
     @Target({TYPE,METHOD})
     public @interface ArrayWrapper {
     }
-    
+
     /**
      * Klasa definijąca umyślnie metody, których nazwy są w stylu innym niż java
-     * <p><b>Suppress:</b></p> 
+     * <p><b>Suppress:</b></p>
      * <ul>
      *      <li>PMD.MethodNameRules</li>
      * </ul>
@@ -63,7 +63,7 @@ public final class PMD {
     @Retention(SOURCE)
     @Target({TYPE,METHOD})
     public @interface API {
-        
+
         /**
          * Konwencja nazw użyta zamiast reguł zalecanych przez Sun'a
          * @return default "C"
@@ -71,11 +71,11 @@ public final class PMD {
         String style() default "C";
 
     }
-    
-    
+
+
     /**
      * Formatka SWINGa, kod generowany dla swinga łamie serię reguł PMD z definicji
-     * <p><b>Suppress:</b></p> 
+     * <p><b>Suppress:</b></p>
      * <ul>
      *      <li>PMD.TooManyFields</li>
      *      <li>PMD.TooManyDifferentMethods</li>
@@ -85,7 +85,18 @@ public final class PMD {
     @Target({TYPE,METHOD})
     public @interface SwingForm {
     }
-    
-    
-    
+
+
+    /**
+     * Utility class, posiada tylko metody statyczne, nie można tworzyć jej instancji
+     * <p><b>Suppress:</b></p>
+     * <ul>
+     *      <li>PMD.UseSingleton</li>
+     * </ul>
+     */
+    @Retention(SOURCE)
+    @Target({TYPE})
+    public @interface StaticHelper {
+    }
+
 }

+ 5 - 3
src/main/java/net/ranides/assira/io/FileHelper.java

@@ -11,6 +11,7 @@ import edu.umd.cs.findbugs.annotations.SuppressWarnings;
 import java.io.*;
 import java.util.Arrays;
 import java.util.MissingResourceException;
+import net.ranides.assira.annotations.PMD;
 import net.ranides.assira.generic.PrivilegedActions;
 import net.ranides.assira.generic.ValueUtils;
 import net.ranides.assira.trace.StackInspector;
@@ -20,9 +21,10 @@ import net.ranides.assira.trace.StackInspector;
  * czynności na plikach.
  * @author ranides
  */
-public final class FileHelper {
+@PMD.StaticHelper
+public class FileHelper {
 
-    private FileHelper() { }
+    protected FileHelper() { }
 
     /**
      * Usuwa wskazany plik. Nie zwraca uwagi na to, czy plik jest katalogiem,
@@ -302,5 +304,5 @@ public final class FileHelper {
     private static String getClassDir(Class<?> clazz) {
         return clazz.getPackage().getName().replace(".", File.separator);
     }
-    
+
 }

+ 34 - 26
src/main/java/net/ranides/assira/io/PathHelper.java

@@ -23,16 +23,16 @@ import net.ranides.assira.text.Strings;
 public final class PathHelper {
 
     private PathHelper() { }
-    
+
     /**
-     * Lista reprezentująca ścieżkę do pliku rozbitą na fragmenty (gdzie każdy 
+     * Lista reprezentująca ścieżkę do pliku rozbitą na fragmenty (gdzie każdy
      * fragment to jeden poziom w systemie plików). Kolejność elementów w liście
-     * odpowiada kolejności elementów w "standardowej" ścieżce - pierwszy element 
+     * odpowiada kolejności elementów w "standardowej" ścieżce - pierwszy element
      * to root, następny to podkatalog... i tak aż nazwy wskazywanego pliku,
      * która znajduje się na ostatniej pozycji.
-     * 
+     *
      * <p>
-     * Przykładowo ścieżka {@code "/a/b/c/d.txt"} jest reprezentowana jako 
+     * Przykładowo ścieżka {@code "/a/b/c/d.txt"} jest reprezentowana jako
      * {@code [ "a", "b", "c", "d.txt" ]}
      * </p>
      */
@@ -42,17 +42,17 @@ public final class PathHelper {
         FilePath(List<String> components) {
             this.components = components.toArray(new String[components.size()]);
         }
-        
+
         @Override
         public String get(int index) {
             return components[index];
         }
-        
+
         @Override
         public int size() {
             return components.length;
         }
-        
+
         /**
          * Konwertuje ścieżkę na standardowy obiekt {@link File}
          * @return
@@ -60,14 +60,14 @@ public final class PathHelper {
         public File toFile() {
             return new File(StringUtils.join(components, File.separator));
         }
-        
+
     }
 
     /**
-     * Wyciąga z podanej ścieżki nazwę dysku (litera+dwukropek). Metoda tylko dla systemów z 
+     * Wyciąga z podanej ścieżki nazwę dysku (litera+dwukropek). Metoda tylko dla systemów z
      * rodziny Windows. Na systemach unix-like zwraca pusty String.
-     * @param file 
-     * @return 
+     * @param file
+     * @return
      */
     public static String getDrive(File file) {
         String path = file.getPath();
@@ -111,14 +111,14 @@ public final class PathHelper {
      * @throws PathConvertException jeśli nie udało się pobrać kanonicznej ścieżki do pliku
      */
     public static List<String> explode(String path) throws PathConvertException {
-        return explode(new File(path));
+        return explode(asFile(path));
     }
 
     /**
      * Zwraca względną ścieżkę do podanego pliku względem katalogu domowego.
      * @param home ścieżka do katalogu domowego
      * @param file ścieżka do pliku
-     * @return 
+     * @return
      * @throws PathConvertException jeśli pliki znajdują się na różnych dyskach
      */
     public static String asRelative(FilePath home, FilePath file) throws PathConvertException {
@@ -156,7 +156,7 @@ public final class PathHelper {
      * Zwraca względną ścieżkę do podanego pliku względem katalogu domowego.
      * @param home ścieżka do katalogu domowego
      * @param file ścieżka do pliku
-     * @return 
+     * @return
      * @throws PathConvertException jeśli pliki znajdują się na różnych dyskach, lub ścieżka
      * {@code home} nie wskazuje na katalog
      */
@@ -171,28 +171,28 @@ public final class PathHelper {
      * Zwraca względną ścieżkę do podanego pliku względem katalogu domowego.
      * @param home ścieżka do katalogu domowego
      * @param file ścieżka do pliku
-     * @return 
+     * @return
      * @throws PathConvertException jeśli pliki znajdują się na różnych dyskach, lub ścieżka
      * {@code home} nie wskazuje na katalog
      */
     public static String asRelative(String home, String file) throws PathConvertException {
-        return asRelative(new File(home), new File(file));
+        return asRelative(asFile(home), asFile(file));
     }
 
     /**
      * Zwraca względną ścieżkę do podanego pliku względem katalogu bieżącego aplikacji.
      * @param file
-     * @return 
+     * @return
      * @throws PathConvertException jeśli plik znajduje się na innym dysku, niż katalog bieżący
      */
     public static String asRelative(String file) throws PathConvertException {
-        return asRelative(new File("."), new File(file));
+        return asRelative(new File("."), asFile(file));
     }
 
     /**
      * Zwraca względną ścieżkę do podanego pliku względem katalogu bieżącego aplikacji.
      * @param file
-     * @return 
+     * @return
      * @throws PathConvertException jeśli plik znajduje się na innym dysku, niż katalog bieżący
      */
     public static String asRelative(File file) throws PathConvertException {
@@ -207,12 +207,12 @@ public final class PathHelper {
      * @return
      */
     public static String asAbsolute(String file) {
-        return new File(file).getAbsolutePath();
+        return asFile(file).getAbsolutePath();
     }
 
     /**
      * Zamienia podaną względną ścieżkę na absolutną. Strategia konwersji jest
-     * zależna od systemu operacyjnego oraz wersji JDK. Aby poznać szczegóły 
+     * zależna od systemu operacyjnego oraz wersji JDK. Aby poznać szczegóły
      * zobacz opis funkcji {@link File#getAbsolutePath}.
      * @param file
      * @return
@@ -245,7 +245,7 @@ public final class PathHelper {
      * @return
      */
     public static String asNormalized(String path) {
-        return asNormalized(new File(path)).getAbsolutePath();
+        return asNormalized(asFile(path)).getAbsolutePath();
     }
 
     /**
@@ -257,7 +257,7 @@ public final class PathHelper {
         int index = path.lastIndexOf(".");
         return (-1==index) ? "" : path.substring(index);
     }
-    
+
     /**
      * Sprawdza, czy ścieżka wskazuje na plik z podanym rozszerzeniem.
      * @param path
@@ -270,7 +270,7 @@ public final class PathHelper {
 
     /**
      * Zwraca nową ścieżkę zmieniając rozszerzenie pliku, na który wskazywała
-     * ścieżka podana jako argument. Funkcja <b>nie</b> wykonuje operacji na 
+     * ścieżka podana jako argument. Funkcja <b>nie</b> wykonuje operacji na
      * systemie plików - jak wszystkie funkcje z klasy {@code PathHelper}, metoda
      * pracuje tylko i wyłącznie na nazwach.
      * @param path
@@ -296,7 +296,7 @@ public final class PathHelper {
         int index = name.lastIndexOf(".");
         return index<0 ? name : name.substring(0, index);
     }
-    
+
     /**
      * Zwraca ścieżkę wskazującą na katalog zawierający podany plik/katalog.
      * W razie potrzeby funkcja konwertuje ścieżkę do lokalizacji absolutnej
@@ -319,4 +319,12 @@ public final class PathHelper {
         }
         return new File(parent);
     }
+
+    public static File asFile(String path) {
+        return new File(path.replace('/', File.separatorChar).replace('\\', File.separatorChar));
+    }
+
+    public static int compare(String path1, String path2) {
+        return asFile(path1).compareTo(asFile(path2));
+    }
 }

+ 34 - 32
src/test/java/net/ranides/assira/io/PathHelperTest.java

@@ -6,7 +6,6 @@
  */
 package net.ranides.assira.io;
 
-import net.ranides.assira.trace.LoggerUtils;
 import org.junit.Test;
 import static org.junit.Assert.*;
 
@@ -16,10 +15,10 @@ import static org.junit.Assert.*;
  */
 @SuppressWarnings("PMD.AvoidDuplicateLiterals")
 public class PathHelperTest {
-    
+
     public PathHelperTest() {
     }
-    
+
     @Test
     public void testExtension() throws PathConvertException {
         assertEquals(".txt", PathHelper.getExtension("file.txt"));
@@ -27,38 +26,41 @@ public class PathHelperTest {
         assertEquals(".txt", PathHelper.getExtension("/base/file.txt"));
         assertEquals(".txt", PathHelper.getExtension("/base/file.log.txt"));
     }
-    
-    
+
+
     @Test
     public void testRelative() throws PathConvertException {
-        
-        assertEquals("..\\..\\temp", PathHelper.asRelative("base/home/system/drivers", "base/home/temp"));
+
+        assertPathEquals("..\\..\\temp", PathHelper.asRelative("base/home/system/drivers", "base/home/temp"));
 
         
-        assertEquals("..\\..\\temp", PathHelper.asRelative("base\\home\\system\\drivers", "base\\home\\temp"));
-        
-        assertEquals("..\\..\\temp\\data\\int\\value", PathHelper.asRelative("base\\home\\system\\drivers", "base\\home\\temp\\data\\int\\value"));
-        
-        assertEquals("..\\..\\temp\\data\\int", PathHelper.asRelative("base\\home\\system\\drivers", "base\\home\\temp\\data\\int"));
-        
-        assertEquals("..\\..\\temp\\data", PathHelper.asRelative("base\\home\\system\\drivers", "base\\home\\temp\\data"));
-        
-        assertEquals("..\\..\\temp", PathHelper.asRelative("home\\system\\drivers", "home\\temp"));
-        
-        assertEquals("..\\..\\temp", PathHelper.asRelative("\\system\\drivers", "\\temp"));
-        
-        assertEquals("..\\temp", PathHelper.asRelative("base\\home\\system", "base\\home\\temp"));
-        
-        assertEquals("..\\temp", PathHelper.asRelative("base\\home\\system\\", "base\\home\\temp"));
-        
-        assertEquals("..\\temp", PathHelper.asRelative("base\\home\\system\\", "base\\home\\temp\\"));
-        
-        assertEquals("..\\temp", PathHelper.asRelative("base\\home\\system", "base\\home\\temp\\"));
-        
-        assertEquals("..", PathHelper.asRelative("base\\home\\system", "base\\home\\"));
-        
-        assertEquals("..", PathHelper.asRelative("base\\home\\system", "base\\home"));
-        
-        assertTrue(true);
+        assertPathEquals("..\\..\\temp", PathHelper.asRelative("base\\home\\system\\drivers", "base\\home\\temp"));
+
+        assertPathEquals("..\\..\\temp\\data\\int\\value", PathHelper.asRelative("base\\home\\system\\drivers", "base\\home\\temp\\data\\int\\value"));
+
+        assertPathEquals("..\\..\\temp\\data\\int", PathHelper.asRelative("base\\home\\system\\drivers", "base\\home\\temp\\data\\int"));
+
+        assertPathEquals("..\\..\\temp\\data", PathHelper.asRelative("base\\home\\system\\drivers", "base\\home\\temp\\data"));
+
+        assertPathEquals("..\\..\\temp", PathHelper.asRelative("home\\system\\drivers", "home\\temp"));
+
+        assertPathEquals("..\\..\\temp", PathHelper.asRelative("\\system\\drivers", "\\temp"));
+
+        assertPathEquals("..\\temp", PathHelper.asRelative("base\\home\\system", "base\\home\\temp"));
+
+        assertPathEquals("..\\temp", PathHelper.asRelative("base\\home\\system\\", "base\\home\\temp"));
+
+        assertPathEquals("..\\temp", PathHelper.asRelative("base\\home\\system\\", "base\\home\\temp\\"));
+
+        assertPathEquals("..\\temp", PathHelper.asRelative("base\\home\\system", "base\\home\\temp\\"));
+
+        assertPathEquals("..", PathHelper.asRelative("base\\home\\system", "base\\home\\"));
+
+        assertPathEquals("..", PathHelper.asRelative("base\\home\\system", "base\\home"));
+
+    }
+
+    private static void assertPathEquals(String expected, String value) {
+        assertEquals(PathHelper.asFile(expected), PathHelper.asFile(value));
     }
 }