-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSFamily.java
101 lines (79 loc) · 2.26 KB
/
OSFamily.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
/*
* This file is part of Jeso.
* Copyright (c) 2019-2021 Jonathan Bradley Whited
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*/
package com.esotericpig.jeso;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Random;
/**
* @author Jonathan Bradley Whited
* @see <a href="https://en.wikipedia.org/wiki/Category:Operating_system_families" target="_blank">OS Families [Wikipedia]</a>
*/
public enum OSFamily {
LINUX("linux"),MACOS("darwin mac osx"),WINDOWS("win","darwin"),UNKNOWN;
public static final List<OSFamily> VALUES = Collections.unmodifiableList(Arrays.asList(values()));
public static OSFamily getRandValue(Random rand) {
return VALUES.get(rand.nextInt(VALUES.size()));
}
public static OSFamily guessFromName(String osName) {
osName = osName.replaceAll("\\s+","").toLowerCase(Locale.ENGLISH);
OSFamily osFamily = UNKNOWN;
for(OSFamily osf: VALUES) {
if(osf.badWords == null && osf.goodWords == null) {
continue;
}
if(osf.goodWords != null) {
boolean isNext = true;
for(String word: osf.goodWords) {
if(osName.contains(word)) {
isNext = false;
break;
}
}
if(isNext) {
continue;
}
}
if(osf.badWords != null) {
boolean isNext = false;
for(String word: osf.badWords) {
if(osName.contains(word)) {
isNext = true;
break;
}
}
if(isNext) {
continue;
}
}
osFamily = osf;
break;
}
return osFamily;
}
protected final List<String> badWords;
protected final List<String> goodWords;
private OSFamily() {
this(null);
}
private OSFamily(String goodWords) {
this(goodWords,null);
}
private OSFamily(String goodWords,String badWords) {
this.badWords = (badWords != null)
? Collections.unmodifiableList(Arrays.asList(badWords.split("\\s+"))) : null;
this.goodWords = (goodWords != null)
? Collections.unmodifiableList(Arrays.asList(goodWords.split("\\s+"))) : null;
}
public List<String> getBadWords() {
return badWords;
}
public List<String> getGoodWords() {
return goodWords;
}
}