Subversion Repository Public Repository

litesoft

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.esotericsoftware.utils;

import java.io.*;
import java.nio.channels.*;

import org.litesoft.logger.*;

@SuppressWarnings({"UnusedDeclaration"})
public class Util
{
    public static LineSink PROGRESS_LINE_SINK = LineSink.SYSTEM_OUT;

    public static void progress( String pMessage )
    {
        PROGRESS_LINE_SINK.addLine( pMessage );
    }

    public static final File CANONICAL_USER_DIR;

    static
    {
        try
        {
            CANONICAL_USER_DIR = new File( System.getProperty( "user.dir" ) ).getCanonicalFile();
        }
        catch ( IOException e )
        {
            throw new Error( e );
        }
    }

    /**
     * True if running on a Mac OS.
     */
    public static final boolean isMac = System.getProperty( "os.name" ).toLowerCase().contains( "mac os x" );

    /**
     * True if running on a Windows OS.
     */
    public static final boolean isWindows = System.getProperty( "os.name" ).toLowerCase().contains( "windows" );

    public static final String[] EMPTY_STRING_ARRAY = new String[0];

    public static final Logger LOGGER = LoggerFactory.getLogger( Util.class );

    private static final IFileSystem FILE_SYSTEM = new IFileSystem()
    {
        @Override
        public boolean isWindows()
        {
            return isWindows;
        }

        @Override
        public char separatorChar()
        {
            return File.separatorChar;
        }

        @Override
        public String canonicalCurrentPath()
        {
            return CANONICAL_USER_DIR.getPath();
        }

        @Override
        public boolean exists( String path )
        {
            return new File( path ).exists();
        }

        @Override
        public String canonicalizeNormalizedExisting( String path )
                throws IOException
        {
            File zFile = new File( path );
            if ( zFile.exists() )
            {
                return zFile.getCanonicalPath();
            }
            throw new FileNotFoundException( path );
        }
    };

    public static String noEmpty( String pToTest )
    {
        if ( pToTest != null )
        {
            if ( (pToTest = pToTest.trim()).length() != 0 )
            {
                return pToTest;
            }
        }
        return null;
    }

    public static <T> T deNull( T pToTest, T pDefaultValue )
    {
        return (pToTest != null) ? pToTest : pDefaultValue;
    }

    public static <T> T assertNotNull( String pWhat, T pToTest )
    {
        if ( pToTest == null )
        {
            throw new IllegalArgumentException( pWhat + " cannot be null." );
        }
        return pToTest;
    }

    public static String assertNotEmpty( String pWhat, String pToTest )
    {
        if ( (pToTest = assertNotNull( pWhat, pToTest ).trim()).length() == 0 )
        {
            throw new IllegalArgumentException( pWhat + " cannot be empty/blank." );
        }
        return pToTest;
    }

    public static void assertNotEmpty( String pWhat, String[] pToTest )
    {
        assertNotNull( pWhat, pToTest );
        if ( pToTest.length == 0 )
        {
            throw new IllegalArgumentException( pWhat + " cannot be empty." );
        }
    }

    public static int assertNotNegative( String pWhat, int pInt )
    {
        if ( pInt < 0 )
        {
            throw new IllegalArgumentException( pWhat + " was " + pInt + ", cannot be negative." );
        }
        return pInt;
    }

    public static void assertPairedEntries( String pWhat, Object[] pArray )
    {
        if ( (pArray != null) && ((pArray.length & 1) == 1) ) // Odd Length == Not Paired!
        {
            throw new IllegalArgumentException( pWhat + " had '" + pArray.length + "' entries, should have been either '" + (pArray.length - 1) + "' or '" + (pArray.length + 1) + "'" );
        }
    }

    public static File assertExists( String pWhat, File pToTest )
    {
        assertNotNull( pWhat, pToTest );
        if ( !pToTest.exists() )
        {
            throw new IllegalArgumentException( pWhat + " not found: " + pToTest.getAbsolutePath() );
        }
        return pToTest;
    }

    public static String replace( String pSource, String pOfInterest, String pReplaceWith )
    {
        for ( int at; -1 != (at = pSource.indexOf( pOfInterest )); )
        {
            pSource = pSource.substring( 0, at ) + pReplaceWith + pSource.substring( at + pOfInterest.length() );
        }
        return pSource;
    }

    /**
     * Copies one file to another.
     */
    @SuppressWarnings({"ResultOfMethodCallIgnored"})
    public static void copyFile( File in, File out )
            throws IOException
    {
        out.getParentFile().mkdirs();
        FileChannel sourceChannel = new FileInputStream( in ).getChannel();
        FileChannel destinationChannel = new FileOutputStream( out ).getChannel();
        sourceChannel.transferTo( 0, sourceChannel.size(), destinationChannel );
        sourceChannel.close();
        destinationChannel.close();
    }

    /**
     * Creates the directories in the specified path.
     */
    public static String mkdir( String path )
    {
        if ( new File( path = assertNotEmpty( "path", path ) ).mkdirs() )
        {
            LOGGER.trace.log( "Created directory: ", path );
        }
        return path;
    }

    /**
     * Deletes a directory and all files and directories it contains.
     */
    public static boolean delete( File pFile )
    {
        assertNotNull( "File", pFile );
        if ( pFile.isDirectory() )
        {
            File[] zFiles = pFile.listFiles();
            for ( File zFile : zFiles )
            {
                if ( !delete( zFile ) )
                {
                    return false;
                }
            }
        }
        LOGGER.trace.log( "Deleting file: ", pFile );
        return pFile.delete();
    }

    /**
     * Deletes a file or directory and all files and subdirecties under it.
     */
    public static boolean delete( String fileName )
    {
        return delete( new File( assertNotEmpty( "fileName", fileName ) ) );
    }

    public static Closeable dispose( Closeable pCloseable )
    {
        if ( pCloseable != null )
        {
            try
            {
                pCloseable.close();
            }
            catch ( IOException ignore )
            {
                // Whatever!
            }
            pCloseable = null;
        }
        return pCloseable;
    }

    public static boolean isAbsolutePath( String path )
    {
        assertNotNull( "path", path );
        return FileSupport.isAbsoluteNormalizedPath( FILE_SYSTEM, CANONICAL_USER_DIR.getPath(), FileSupport.normalizePath( FILE_SYSTEM, path ) );
    }

    public static String normalizePath( String path )
    {
        assertNotNull( "path", path );
        return FileSupport.normalizePath( FILE_SYSTEM, path );
    }

    public static File canonicalizePath( String path )
            throws IOException
    {
        return canonicalizePath( CANONICAL_USER_DIR, path );
    }

    public static File canonicalizePath( File pCanonicalParentDirIfPathRelative, String path )
            throws IOException
    {
        assertNotNull( "path", path );
        return new File( FileSupport.canonicalizeNormalizedPath( FILE_SYSTEM, pCanonicalParentDirIfPathRelative.getPath(), FileSupport.normalizePath( FILE_SYSTEM, path ) ) );
    }
}

Commits for litesoft/trunk/Java/ScarPlus/src/com/esotericsoftware/utils/Util.java

Diff revisions: vs.
Revision Author Commited Message
314 Diff Diff GeorgeS picture GeorgeS Fri 15 Jul, 2011 01:01:49 +0000
308 Diff Diff GeorgeS picture GeorgeS Sun 10 Jul, 2011 23:55:06 +0000
307 Diff Diff GeorgeS picture GeorgeS Sun 10 Jul, 2011 01:43:51 +0000
306 Diff Diff GeorgeS picture GeorgeS Sat 09 Jul, 2011 00:00:17 +0000
303 Diff Diff GeorgeS picture GeorgeS Fri 08 Jul, 2011 15:17:00 +0000
302 Diff Diff GeorgeS picture GeorgeS Fri 08 Jul, 2011 14:08:10 +0000
301 Diff Diff GeorgeS picture GeorgeS Fri 08 Jul, 2011 00:02:58 +0000
300 Diff Diff GeorgeS picture GeorgeS Thu 07 Jul, 2011 00:30:55 +0000
299 Diff Diff GeorgeS picture GeorgeS Tue 05 Jul, 2011 04:45:50 +0000
287 GeorgeS picture GeorgeS Mon 20 Jun, 2011 06:24:24 +0000