Synthia
Generic and flexible data structure generator
Utilities.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2021 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package examples.util;
20 
21 import java.io.PrintStream;
22 import java.lang.reflect.Array;
23 import java.util.Collection;
24 
25 /**
26  * Object providing a few utility methods to simplify the examples in this
27  * project.
28  * @ingroup Examples
29  */
30 public class Utilities
31 {
32  /**
33  * Prints an object to a print stream.
34  * @param ps The print stream to print to
35  * @param o The object
36  */
37  public static void print(PrintStream ps, Object o)
38  {
39  if (o == null)
40  {
41  ps.print("null");
42  }
43  else if (o.getClass().isArray())
44  {
45  ps.print("[");
46  if (o.getClass().getComponentType().isPrimitive())
47  {
48  int length = Array.getLength(o);
49  for (int i = 0; i < length; i++)
50  {
51  Object obj = Array.get(o, i);
52  if (i > 0)
53  {
54  ps.print(",");
55  }
56  print(ps, obj);
57  }
58  }
59  else
60  {
61  Object[] objects = (Object[]) o;
62  for (int i = 0; i < objects.length; i++)
63  {
64  if (i > 0)
65  {
66  ps.print(",");
67  }
68  print(ps, objects[i]);
69  }
70  }
71  ps.print("]");
72  }
73  else if (o instanceof Collection)
74  {
75  boolean first = true;
76  Collection<?> array = (Collection<?>) o;
77  ps.print("[");
78  for (Object e : array)
79  {
80  if (first)
81  {
82  first = false;
83  }
84  else
85  {
86  ps.print(",");
87  }
88  print(ps, e);
89  }
90  ps.print("]");
91  }
92  else if (o instanceof Number)
93  {
94  Number n = (Number) o;
95  if (n.intValue() == n.floatValue())
96  {
97  ps.print(n.intValue());
98  }
99  else
100  {
101  ps.print(n.floatValue());
102  }
103  }
104  }
105 
106  /**
107  * Calls {@link #print(PrintStream, Object)} and appends a new line.
108  * @param ps The print stream to print to
109  * @param o The object
110  */
111  public static void println(PrintStream ps, Object o)
112  {
113  print(ps, o);
114  ps.println();
115  }
116 
117  /**
118  * Creates a simple 5-color gradient.
119  * @param x A fraction between 0 and 1
120  * @return A color associated to the fraction, represented as an
121  * RGB color in hexadecimal.
122  */
123  public static String colorGradient(float fraction)
124  {
125  if (fraction < 0.2)
126  {
127  return "#0000FF";
128  }
129  if (fraction < 0.4)
130  {
131  return "#00FFFF";
132  }
133  if (fraction < 0.6)
134  {
135  return "#00FF00";
136  }
137  if (fraction < 0.8)
138  {
139  return "#FFFF00";
140  }
141  return "#FF0000";
142  }
143 }
examples.util.Utilities.colorGradient
static String colorGradient(float fraction)
Creates a simple 5-color gradient.
Definition: Utilities.java:123
examples.util.Utilities.println
static void println(PrintStream ps, Object o)
Calls print(PrintStream, Object) and appends a new line.
Definition: Utilities.java:111
examples.util.Utilities
Object providing a few utility methods to simplify the examples in this project.
Definition: Utilities.java:30
examples.util.Utilities.print
static void print(PrintStream ps, Object o)
Prints an object to a print stream.
Definition: Utilities.java:37