GriffonApplicationUtils.java
001 /*
002  * Copyright 2008-2015 the original author or authors.
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *     http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package griffon.util;
017 
018 import javax.annotation.Nonnull;
019 import javax.annotation.Nullable;
020 import java.util.Locale;
021 
022 import static griffon.util.GriffonNameUtils.isBlank;
023 
024 /**
025  * Assorted utility methods and constants.
026  *
027  @author Andres Almiray
028  */
029 public final class GriffonApplicationUtils {
030     private GriffonApplicationUtils() {
031     }
032 
033     private static final boolean isWindows;
034     private static final boolean isWindows95;
035     private static final boolean isWindows98;
036     private static final boolean isWindowsNT;
037     private static final boolean isWindows2000;
038     private static final boolean isWindows2003;
039     private static final boolean isWindowsXP;
040     private static final boolean isWindowsVista;
041     private static final boolean isWindows7;
042     private static final boolean isWindows8;
043     private static final boolean isWindows10;
044 
045     /**
046      * True if running Linux, Solaris or MacOSX
047      */
048     private static final boolean isUnix;
049 
050     private static final boolean isLinux;
051     private static final boolean isSolaris;
052     private static final boolean isMacOSX;
053 
054     private static final String osArch;
055     private static final String osName;
056     private static final String osVersion;
057     private static final String javaVersion;
058     private static final boolean is64Bit;
059 
060     private static final boolean isJdk4;
061     private static final boolean isJdk5;
062     private static final boolean isJdk6;
063     private static final boolean isJdk7;
064     private static final boolean isJdk8;
065     private static final boolean isJdk9;
066 
067     public static final String platform;
068     public static final String basePlatform;
069 
070     static {
071         osArch = System.getProperty("os.arch");
072         osName = System.getProperty("os.name");
073         is64Bit = osArch.contains("64");
074 
075         if (osName.contains("Windows")) {
076             basePlatform = "windows";
077             isWindows = true;
078             isLinux = false;
079             isUnix = false;
080             isMacOSX = false;
081             isSolaris = false;
082             if (osName.contains("95")) {
083                 isWindows95 = true;
084                 isWindows98 = false;
085                 isWindowsNT = false;
086                 isWindows2000 = false;
087                 isWindows2003 = false;
088                 isWindowsXP = false;
089                 isWindowsVista = false;
090                 isWindows7 = false;
091                 isWindows8 = false;
092                 isWindows10 = false;
093             else if (osName.contains("98")) {
094                 isWindows95 = false;
095                 isWindows98 = true;
096                 isWindowsNT = false;
097                 isWindows2000 = false;
098                 isWindows2003 = false;
099                 isWindowsXP = false;
100                 isWindowsVista = false;
101                 isWindows7 = false;
102                 isWindows8 = false;
103                 isWindows10 = false;
104             else if (osName.contains("NT")) {
105                 isWindows95 = false;
106                 isWindows98 = false;
107                 isWindowsNT = false;
108                 isWindows2000 = true;
109                 isWindows2003 = false;
110                 isWindowsXP = false;
111                 isWindowsVista = false;
112                 isWindows7 = false;
113                 isWindows8 = false;
114                 isWindows10 = false;
115             else if (osName.contains("2003")) {
116                 isWindows95 = false;
117                 isWindows98 = false;
118                 isWindowsNT = false;
119                 isWindows2000 = false;
120                 isWindows2003 = true;
121                 isWindowsXP = true;
122                 isWindowsVista = false;
123                 isWindows7 = false;
124                 isWindows8 = false;
125                 isWindows10 = false;
126             else if (osName.contains("XP")) {
127                 isWindows95 = false;
128                 isWindows98 = false;
129                 isWindowsNT = true;
130                 isWindows2000 = true;
131                 isWindows2003 = true;
132                 isWindowsXP = false;
133                 isWindowsVista = false;
134                 isWindows7 = false;
135                 isWindows8 = false;
136                 isWindows10 = false;
137             else if (osName.contains("Vista")) {
138                 isWindows95 = false;
139                 isWindows98 = false;
140                 isWindowsNT = false;
141                 isWindows2000 = false;
142                 isWindows2003 = false;
143                 isWindowsXP = false;
144                 isWindowsVista = true;
145                 isWindows7 = false;
146                 isWindows8 = false;
147                 isWindows10 = false;
148             else if (osName.contains("Windows 7")) {
149                 isWindows95 = false;
150                 isWindows98 = false;
151                 isWindowsNT = false;
152                 isWindows2000 = false;
153                 isWindows2003 = false;
154                 isWindowsXP = false;
155                 isWindowsVista = false;
156                 isWindows7 = true;
157                 isWindows8 = false;
158                 isWindows10 = false;
159             else if (osName.equals("Windows 8")) {
160                 isWindows95 = false;
161                 isWindows98 = false;
162                 isWindowsNT = false;
163                 isWindows2000 = false;
164                 isWindows2003 = false;
165                 isWindowsXP = false;
166                 isWindowsVista = false;
167                 isWindows7 = false;
168                 isWindows8 = true;
169                 isWindows10 = false;
170             else if (osName.equals("Windows 8.1"|| osName.equals("Windows 10")) {
171                 isWindows95 = false;
172                 isWindows98 = false;
173                 isWindowsNT = false;
174                 isWindows2000 = false;
175                 isWindows2003 = false;
176                 isWindowsXP = false;
177                 isWindowsVista = false;
178                 isWindows7 = false;
179                 isWindows8 = false;
180                 isWindows10 = true;
181             else {
182                 isWindows95 = false;
183                 isWindows98 = false;
184                 isWindowsNT = false;
185                 isWindows2000 = false;
186                 isWindows2003 = false;
187                 isWindowsXP = false;
188                 isWindowsVista = false;
189                 isWindows7 = false;
190                 isWindows8 = false;
191                 isWindows10 = false;
192             }
193         else if (osName.contains("Linux")) {
194             basePlatform = "linux";
195             isWindows = false;
196             isLinux = true;
197             isUnix = true;
198             isMacOSX = false;
199             isSolaris = false;
200             isWindows95 = false;
201             isWindows98 = false;
202             isWindowsNT = false;
203             isWindows2000 = false;
204             isWindows2003 = false;
205             isWindowsXP = false;
206             isWindowsVista = false;
207             isWindows7 = false;
208             isWindows8 = false;
209             isWindows10 = false;
210         else if (osName.contains("Solaris"|| osName.contains("SunOS")) {
211             basePlatform = "solaris";
212             isWindows = false;
213             isLinux = false;
214             isUnix = true;
215             isMacOSX = false;
216             isSolaris = true;
217             isWindows95 = false;
218             isWindows98 = false;
219             isWindowsNT = false;
220             isWindows2000 = false;
221             isWindows2003 = false;
222             isWindowsXP = false;
223             isWindowsVista = false;
224             isWindows7 = false;
225             isWindows8 = false;
226             isWindows10 = false;
227         else if (osName.contains("Mac OS")) {
228             basePlatform = "macosx";
229             isWindows = false;
230             isLinux = false;
231             isUnix = true;
232             isMacOSX = true;
233             isSolaris = false;
234             isWindows95 = false;
235             isWindows98 = false;
236             isWindowsNT = false;
237             isWindows2000 = false;
238             isWindows2003 = false;
239             isWindowsXP = false;
240             isWindowsVista = false;
241             isWindows7 = false;
242             isWindows8 = false;
243             isWindows10 = false;
244         else {
245             basePlatform = "unknown";
246             isWindows = false;
247             isLinux = false;
248             isUnix = false;
249             isMacOSX = false;
250             isSolaris = false;
251             isWindows95 = false;
252             isWindows98 = false;
253             isWindowsNT = false;
254             isWindows2000 = false;
255             isWindows2003 = false;
256             isWindowsXP = false;
257             isWindowsVista = false;
258             isWindows7 = false;
259             isWindows8 = false;
260             isWindows10 = false;
261         }
262 
263         osVersion = System.getProperty("os.version");
264         javaVersion = System.getProperty("java.version");
265         String version = javaVersion.substring(03);
266         isJdk4 = true;
267         switch (version) {
268             case "1.9":
269                 isJdk9 = true;
270                 isJdk8 = true;
271                 isJdk7 = true;
272                 isJdk6 = true;
273                 isJdk5 = true;
274                 break;
275             case "1.8":
276                 isJdk9 = false;
277                 isJdk8 = true;
278                 isJdk7 = true;
279                 isJdk6 = true;
280                 isJdk5 = true;
281                 break;
282             case "1.7":
283                 isJdk9 = false;
284                 isJdk8 = false;
285                 isJdk7 = true;
286                 isJdk6 = true;
287                 isJdk5 = true;
288                 break;
289             case "1.6":
290                 isJdk9 = false;
291                 isJdk8 = false;
292                 isJdk7 = false;
293                 isJdk6 = true;
294                 isJdk5 = true;
295                 break;
296             case "1.5":
297                 isJdk9 = false;
298                 isJdk8 = false;
299                 isJdk7 = false;
300                 isJdk6 = false;
301                 isJdk5 = true;
302                 break;
303             default:
304                 isJdk9 = false;
305                 isJdk8 = false;
306                 isJdk7 = false;
307                 isJdk6 = false;
308                 isJdk5 = false;
309                 break;
310         }
311 
312         platform = basePlatform + (is64Bit && !isSolaris ? "64" "");
313     }
314 
315     public static boolean isWindows() {
316         return isWindows;
317     }
318 
319     public static boolean isWindows95() {
320         return isWindows95;
321     }
322 
323     public static boolean isWindows98() {
324         return isWindows98;
325     }
326 
327     public static boolean isWindowsNT() {
328         return isWindowsNT;
329     }
330 
331     public static boolean isWindows2000() {
332         return isWindows2000;
333     }
334 
335     public static boolean isWindows2003() {
336         return isWindows2003;
337     }
338 
339     public static boolean isWindowsXP() {
340         return isWindowsXP;
341     }
342 
343     public static boolean isWindowsVista() {
344         return isWindowsVista;
345     }
346 
347     public static boolean isWindows7() {
348         return isWindows7;
349     }
350 
351     public static boolean isWindows8() {
352         return isWindows8;
353     }
354 
355     public static boolean isWindows10() {
356         return isWindows10;
357     }
358 
359     public static boolean isUnix() {
360         return isUnix;
361     }
362 
363     public static boolean isLinux() {
364         return isLinux;
365     }
366 
367     public static boolean isSolaris() {
368         return isSolaris;
369     }
370 
371     public static boolean isMacOSX() {
372         return isMacOSX;
373     }
374 
375     public static String getOsArch() {
376         return osArch;
377     }
378 
379     public static String getOsName() {
380         return osName;
381     }
382 
383     public static String getOsVersion() {
384         return osVersion;
385     }
386 
387     public static String getJavaVersion() {
388         return javaVersion;
389     }
390 
391     public static boolean is64Bit() {
392         return is64Bit;
393     }
394 
395     @Deprecated
396     public static boolean isJdk14() {
397         return isJdk4();
398     }
399 
400     @Deprecated
401     public static boolean isJdk15() {
402         return isJdk5();
403     }
404 
405     @Deprecated
406     public static boolean isJdk16() {
407         return isJdk6();
408     }
409 
410     @Deprecated
411     public static boolean isJdk17() {
412         return isJdk7();
413     }
414 
415     @Deprecated
416     public static boolean isJdk18() {
417         return isJdk8();
418     }
419 
420     public static boolean isJdk4() {
421         return isJdk4;
422     }
423 
424     public static boolean isJdk5() {
425         return isJdk5;
426     }
427 
428     public static boolean isJdk6() {
429         return isJdk6;
430     }
431 
432     public static boolean isJdk7() {
433         return isJdk7;
434     }
435 
436     public static boolean isJdk8() {
437         return isJdk8;
438     }
439 
440     public static boolean isJdk9() {
441         return isJdk9;
442     }
443 
444     public static String getPlatform() {
445         return platform;
446     }
447 
448     public static String getBasePlatform() {
449         return basePlatform;
450     }
451 
452     public static boolean getIsWindows() {
453         return isWindows;
454     }
455 
456     public static boolean getIsWindows95() {
457         return isWindows95;
458     }
459 
460     public static boolean getIsWindows98() {
461         return isWindows98;
462     }
463 
464     public static boolean getIsWindowsNT() {
465         return isWindowsNT;
466     }
467 
468     public static boolean getIsWindows2000() {
469         return isWindows2000;
470     }
471 
472     public static boolean getIsWindows2003() {
473         return isWindows2003;
474     }
475 
476     public static boolean getIsWindowsXP() {
477         return isWindowsXP;
478     }
479 
480     public static boolean getIsWindowsVista() {
481         return isWindowsVista;
482     }
483 
484     public static boolean getIsWindows7() {
485         return isWindows7;
486     }
487 
488     public static boolean getIsWindows8() {
489         return isWindows8;
490     }
491 
492     public static boolean getIsWindows10() {
493         return isWindows10;
494     }
495 
496     public static boolean getIsUnix() {
497         return isUnix;
498     }
499 
500     public static boolean getIsLinux() {
501         return isLinux;
502     }
503 
504     public static boolean getIsSolaris() {
505         return isSolaris;
506     }
507 
508     public static boolean getIsMacOSX() {
509         return isMacOSX;
510     }
511 
512     public static boolean getIs64Bit() {
513         return is64Bit;
514     }
515 
516     @Deprecated
517     public static boolean getIsJdk14() {
518         return isJdk4;
519     }
520 
521     @Deprecated
522     public static boolean getIsJdk15() {
523         return isJdk5;
524     }
525 
526     @Deprecated
527     public static boolean getIsJdk16() {
528         return isJdk6;
529     }
530 
531     @Deprecated
532     public static boolean getIsJdk17() {
533         return isJdk7;
534     }
535 
536     @Deprecated
537     public static boolean getIsJdk18() {
538         return isJdk8;
539     }
540 
541     public static boolean getIsJdk4() {
542         return isJdk4;
543     }
544 
545     public static boolean getIsJdk5() {
546         return isJdk5;
547     }
548 
549     public static boolean getIsJdk6() {
550         return isJdk6;
551     }
552 
553     public static boolean getIsJdk7() {
554         return isJdk7;
555     }
556 
557     public static boolean getIsJdk8() {
558         return isJdk8;
559     }
560 
561     public static boolean getIsJdk9() {
562         return isJdk9;
563     }
564 
565     @Nonnull
566     @SuppressWarnings("ConstantConditions")
567     public static Locale parseLocale(@Nullable String locale) {
568         if (isBlank(locale)) return Locale.getDefault();
569         String[] parts = locale.split("_");
570         switch (parts.length) {
571             case 1:
572                 return new Locale(parts[0]);
573             case 2:
574                 return new Locale(parts[0], parts[1]);
575             case 3:
576                 return new Locale(parts[0], parts[1], parts[2]);
577             default:
578                 return Locale.getDefault();
579         }
580     }
581 }