eca9e6a06ba8ac33c787c70cf350b7c25201d715
[javainspect.git] / src / main / java / eu / svjatoslav / inspector / java / structure / Utils.java
1 /*
2  * JavaInspect - Utility to visualize java software
3  * Copyright (C) 2013-2019, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.inspector.java.structure;
11
12 import java.util.ArrayList;
13 import java.util.List;
14
15 public class Utils {
16
17     private static final List<String> systemDataTypes = new ArrayList<>();
18     private static final List<String> commonObjectMethods = new ArrayList<>();
19     private static final List<String> systemPackages = new ArrayList<>();
20     private static final List<String> darkColors = new ArrayList<>();
21     private static final List<String> lightColors = new ArrayList<>();
22     private static final List<String> enumMethods = new ArrayList<>();
23     private static int lastChosenDarkColor = -1;
24     private static int lastChosenLightColor = -1;
25
26     static {
27         initEnumMethods();
28         initSystemDataTypes();
29         initDarkColors();
30         initLightColors();
31         initCommonObjectMethods();
32         initSystemPackages();
33     }
34
35     /**
36      * retrieves colors from predefined palette
37      *
38      * @return next available dark color name
39      */
40     protected static String getNextDarkColor() {
41         lastChosenDarkColor++;
42         if (lastChosenDarkColor >= darkColors.size())
43             lastChosenDarkColor = 0;
44
45         return darkColors.get(lastChosenDarkColor);
46     }
47
48     /**
49      * retrieves colors from predefined palette
50      *
51      * @return next available light color name
52      */
53     protected static String getNextLightColor() {
54         lastChosenLightColor++;
55         if (lastChosenLightColor >= lightColors.size())
56             lastChosenLightColor = 0;
57
58         return lightColors.get(lastChosenLightColor);
59     }
60
61     private static void initCommonObjectMethods() {
62         commonObjectMethods.add("wait");
63         commonObjectMethods.add("equals");
64         commonObjectMethods.add("toString");
65         commonObjectMethods.add("hashCode");
66         commonObjectMethods.add("notify");
67         commonObjectMethods.add("notifyAll");
68         commonObjectMethods.add("getClass");
69     }
70
71     protected static void initDarkColors() {
72         darkColors.add("antiquewhite4");
73         darkColors.add("blueviolet");
74         darkColors.add("brown4");
75         darkColors.add("chartreuse4");
76         darkColors.add("cyan4");
77         darkColors.add("deeppink1");
78         darkColors.add("deepskyblue3");
79         darkColors.add("firebrick1");
80         darkColors.add("goldenrod3");
81         darkColors.add("gray0");
82     }
83
84     private static void initEnumMethods() {
85         enumMethods.add("values");
86         enumMethods.add("valueOf");
87         enumMethods.add("name");
88         enumMethods.add("compareTo");
89         enumMethods.add("valueOf");
90         enumMethods.add("getDeclaringClass");
91         enumMethods.add("ordinal");
92     }
93
94     private static void initLightColors() {
95         lightColors.add("olivedrab2");
96         lightColors.add("peachpuff2");
97         lightColors.add("seagreen1");
98         lightColors.add("violet");
99         lightColors.add("cyan");
100         lightColors.add("orange");
101     }
102
103     private static void initSystemDataTypes() {
104         systemDataTypes.add("void");
105         systemDataTypes.add("int");
106         systemDataTypes.add("long");
107         systemDataTypes.add("float");
108         systemDataTypes.add("double");
109         systemDataTypes.add("boolean");
110         systemDataTypes.add("char");
111         systemDataTypes.add("short");
112         systemDataTypes.add("byte");
113     }
114
115     private static void initSystemPackages() {
116         systemPackages.add("java.");
117         systemPackages.add("javax.");
118         systemPackages.add("sun.");
119     }
120
121     protected static boolean isCommonObjectMethod(final String name) {
122         return commonObjectMethods.contains(name);
123     }
124
125     protected static boolean isEnumMethod(final String name) {
126         return enumMethods.contains(name);
127     }
128
129     protected static boolean isSystemDataType(final String name) {
130         return systemDataTypes.contains(name);
131     }
132
133     protected static boolean isSystemPackage(final String name) {
134
135         for (final String packagePrefix : systemPackages)
136             if (name.startsWith(packagePrefix))
137                 return true;
138
139         return false;
140     }
141
142 }