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