1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.util.Collection;
|
---|
6 |
|
---|
7 | public class Utils {
|
---|
8 |
|
---|
9 | public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) {
|
---|
10 | for (T item : collection) {
|
---|
11 | if (predicate.evaluate(item))
|
---|
12 | return true;
|
---|
13 | }
|
---|
14 | return false;
|
---|
15 | }
|
---|
16 |
|
---|
17 | public static <T> boolean exists(Iterable collection, Class<? extends T> klass) {
|
---|
18 | for (Object item : collection) {
|
---|
19 | if (klass.isInstance(item))
|
---|
20 | return true;
|
---|
21 | }
|
---|
22 | return false;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public static <T> T find(Iterable<? extends T> collection, Predicate<? super T> predicate) {
|
---|
26 | for (T item : collection) {
|
---|
27 | if (predicate.evaluate(item))
|
---|
28 | return item;
|
---|
29 | }
|
---|
30 | return null;
|
---|
31 | }
|
---|
32 |
|
---|
33 | public static <T> T find(Iterable collection, Class<? extends T> klass) {
|
---|
34 | for (Object item : collection) {
|
---|
35 | if (klass.isInstance(item)) {
|
---|
36 | @SuppressWarnings("unchecked") T res = (T) item;
|
---|
37 | return res;
|
---|
38 | }
|
---|
39 | }
|
---|
40 | return null;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public static <T> int indexOf(Iterable<? extends T> collection, Predicate<? super T> predicate) {
|
---|
44 | int i = 0;
|
---|
45 | for (T item : collection) {
|
---|
46 | if (predicate.evaluate(item))
|
---|
47 | return i;
|
---|
48 | i++;
|
---|
49 | }
|
---|
50 | return -1;
|
---|
51 | }
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Get minimum of 3 values
|
---|
55 | */
|
---|
56 | public static int min(int a, int b, int c) {
|
---|
57 | if (b < c) {
|
---|
58 | if (a < b)
|
---|
59 | return a;
|
---|
60 | return b;
|
---|
61 | } else {
|
---|
62 | if (a < c) {
|
---|
63 | return a;
|
---|
64 | }
|
---|
65 | return c;
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public static int max(int a, int b, int c, int d) {
|
---|
70 | return Math.max(Math.max(a, b), Math.max(c, d));
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * for convenience: test whether 2 objects are either both null or a.equals(b)
|
---|
75 | */
|
---|
76 | public static <T> boolean equal(T a, T b) {
|
---|
77 | if (a == b)
|
---|
78 | return true;
|
---|
79 | return (a != null && a.equals(b));
|
---|
80 | }
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * return the modulus in the range [0, n)
|
---|
84 | */
|
---|
85 | public static int mod(int a, int n) {
|
---|
86 | if (n <= 0)
|
---|
87 | throw new IllegalArgumentException();
|
---|
88 | int res = a % n;
|
---|
89 | if (res < 0) {
|
---|
90 | res += n;
|
---|
91 | }
|
---|
92 | return res;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Joins a list of strings (or objects that can be converted to string via
|
---|
97 | * Object.toString()) into a single string with fields separated by sep.
|
---|
98 | * @param sep the separator
|
---|
99 | * @param values collection of objects, null is converted to the
|
---|
100 | * empty string
|
---|
101 | * @return null if values is null. The joined string otherwise.
|
---|
102 | */
|
---|
103 | public static String join(String sep, Collection<?> values) {
|
---|
104 | if (sep == null)
|
---|
105 | throw new IllegalArgumentException();
|
---|
106 | if (values == null)
|
---|
107 | return null;
|
---|
108 | if (values.isEmpty())
|
---|
109 | return "";
|
---|
110 | StringBuilder s = null;
|
---|
111 | for (Object a : values) {
|
---|
112 | if (a == null) {
|
---|
113 | a = "";
|
---|
114 | }
|
---|
115 | if(s != null) {
|
---|
116 | s.append(sep).append(a.toString());
|
---|
117 | } else {
|
---|
118 | s = new StringBuilder(a.toString());
|
---|
119 | }
|
---|
120 | }
|
---|
121 | return s.toString();
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * convert Color to String
|
---|
126 | * (Color.toString() omits alpha value)
|
---|
127 | */
|
---|
128 | public static String toString(Color c) {
|
---|
129 | if (c == null)
|
---|
130 | return "null";
|
---|
131 | if (c.getAlpha() == 255)
|
---|
132 | return String.format("#%06x", c.getRGB() & 0x00ffffff);
|
---|
133 | else
|
---|
134 | return String.format("#%06x(alpha=%d)", c.getRGB() & 0x00ffffff, c.getAlpha());
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * convert float range 0 <= x <= 1 to integer range 0..255
|
---|
139 | * when dealing with colors and color alpha value
|
---|
140 | * @return null if val is null, the corresponding int if val is in the
|
---|
141 | * range 0...1. If val is outside that range, return 255
|
---|
142 | */
|
---|
143 | public static Integer color_float2int(Float val) {
|
---|
144 | if (val == null)
|
---|
145 | return null;
|
---|
146 | if (val < 0 || val > 1)
|
---|
147 | return 255;
|
---|
148 | return (int) (255f * val + 0.5f);
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * convert back
|
---|
153 | */
|
---|
154 | public static Float color_int2float(Integer val) {
|
---|
155 | if (val == null)
|
---|
156 | return null;
|
---|
157 | if (val < 0 || val > 255)
|
---|
158 | return 1f;
|
---|
159 | return ((float) val) / 255f;
|
---|
160 | }
|
---|
161 |
|
---|
162 | }
|
---|