litesoft
@ 939
litesoft / trunk / Java / core / Server / src / org / litesoft / textfiledirectory / TextFileDirectoryFileSystem.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
package org.litesoft.textfiledirectory; import java.io.*; import org.litesoft.commonfoundation.annotations.*; import org.litesoft.commonfoundation.typeutils.*; import org.litesoft.core.simpletypes.*; import org.litesoft.util.*; public class TextFileDirectoryFileSystem extends AbstractTextFileDirectory { private final File mDirectory; public TextFileDirectoryFileSystem( File pDirectory ) { mDirectory = Objects.assertNotNull( "Directory", pDirectory ); if ( mDirectory.isFile() ) { throw new IllegalArgumentException( "Directory '" + pDirectory + "' is not a directory, but a file!" ); } } @Override public String getDirectoryPath() { return mDirectory.getAbsolutePath(); } @Override public boolean exists() { return mDirectory.isDirectory(); } @Override public String[] getFiles( String... pExtensions ) { final String[] zExtensions = preprocessExtensions( pExtensions ); if ( !mDirectory.isDirectory() ) { return Strings.EMPTY_ARRAY; } if ( zExtensions.length == 0 ) { return mDirectory.list(); } return mDirectory.list( new FilenameFilter() { @Override public boolean accept( File dir, String name ) { for ( String zExtension : zExtensions ) { if ( name.endsWith( zExtension ) ) { return true; } } return false; } } ); } @Override public TextFile load( @NotNull String pFileName ) throws FileDoesNotExists { String[] zLines = FileUtils.loadTextFile( getFile( pFileName = Strings.assertNotNullNotEmpty( "FileName", pFileName ) ) ); return new TextFile( pFileName, zLines ); } @Override public void create( @NotNull TextFile pFile ) throws FileAlreadyExists { write( assertNotExist( getFile( pFile ) ), pFile ); } @Override public void update( @NotNull TextFile pFile ) throws FileDoesNotExists { write( assertIsFile( getFile( pFile ) ), pFile ); } @Override public void delete( @NotNull TextFile pFile ) throws FileDoesNotExists { delete( getFile( pFile ) ); } @Override public void delete( @NotNull String pFileName ) throws FileDoesNotExists { delete( getFile( Strings.assertNotNullNotEmpty( "FileName", pFileName ) ) ); } private void write( File pFile, TextFile pTextFile ) { Directories.insureParent( pFile ); FileUtils.storeTextFile( pFile, pTextFile.getLines() ); } private void delete( File pFile ) { FileUtils.deleteIfExists( assertIsFile( pFile ) ); } private File assertIsFile( File pFile ) { assertExists( pFile.isFile(), pFile.getName() ); return pFile; } private File assertNotExist( File pFile ) { assertNotExists( pFile.exists(), pFile.getName() ); return pFile; } private File getFile( TextFile pFile ) { return getFile( Objects.assertNotNull( "File", pFile ).getFileName() ); } private File getFile( String pFileName ) { return new File( mDirectory, pFileName ); } } |
Commits for litesoft/trunk/Java/core/Server/src/org/litesoft/textfiledirectory/TextFileDirectoryFileSystem.java
Revision | Author | Commited | Message |
---|---|---|---|
939 Diff | GeorgeS | Mon 02 Jun, 2014 21:30:31 +0000 | Extracting commonfoundation |
836 Diff | GeorgeS | Wed 05 Sep, 2012 15:01:18 +0000 | |
823 Diff | GeorgeS | Sun 19 Aug, 2012 16:10:13 +0000 | |
821 Diff | GeorgeS | Sun 19 Aug, 2012 00:08:41 +0000 | |
804 Diff | GeorgeS | Wed 15 Aug, 2012 12:48:51 +0000 | |
800 Diff | GeorgeS | Wed 15 Aug, 2012 03:33:38 +0000 | |
791 Diff | GeorgeS | Sun 12 Aug, 2012 15:22:58 +0000 | ! |
789 | GeorgeS | Sat 11 Aug, 2012 17:47:31 +0000 |