source: josm/trunk/src/org/openstreetmap/josm/tools/Utils.java@ 3859

Last change on this file since 3859 was 3859, checked in by bastiK, 13 years ago

mapcss: dashes

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.awt.Color;
5import java.util.Collection;
6
7public 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 /**
44 * Get minimum of 3 values
45 */
46 public static int min(int a, int b, int c) {
47 if (b < c) {
48 if (a < b)
49 return a;
50 return b;
51 } else {
52 if (a < c) {
53 return a;
54 }
55 return c;
56 }
57 }
58
59 public static int max(int a, int b, int c, int d) {
60 return Math.max(Math.max(a, b), Math.max(c, d));
61 }
62
63 /**
64 * for convenience: test whether 2 objects are either both null or a.equals(b)
65 */
66 public static <T> boolean equal(T a, T b) {
67 if (a == b)
68 return true;
69 return (a != null && a.equals(b));
70 }
71
72 /**
73 * return the modulus in the range [0, n)
74 */
75 public static int mod(int a, int n) {
76 if (n <= 0)
77 throw new IllegalArgumentException();
78 int res = a % n;
79 if (res < 0) {
80 res += n;
81 }
82 return res;
83 }
84
85 /**
86 * Joins a list of strings (or objects that can be converted to string via
87 * Object.toString()) into a single string with fields separated by sep.
88 * @param sep the separator
89 * @param values collection of objects, null is converted to the
90 * empty string
91 * @return null if values is null. The joined string otherwise.
92 */
93 public static String join(String sep, Collection<?> values) {
94 if (sep == null)
95 throw new IllegalArgumentException();
96 if (values == null)
97 return null;
98 if (values.isEmpty())
99 return "";
100 StringBuilder s = null;
101 for (Object a : values) {
102 if (a == null) {
103 a = "";
104 }
105 if(s != null) {
106 s.append(sep).append(a.toString());
107 } else {
108 s = new StringBuilder(a.toString());
109 }
110 }
111 return s.toString();
112 }
113
114 /**
115 * convert Color to String
116 * (Color.toString() omits alpha value)
117 */
118 public static String toString(Color c) {
119 if (c == null)
120 return "null";
121 if (c.getAlpha() == 255)
122 return String.format("#%06x", c.getRGB() & 0x00ffffff);
123 else
124 return String.format("#%06x(alpha=%d)", c.getRGB() & 0x00ffffff, c.getAlpha());
125 }
126}
Note: See TracBrowser for help on using the repository browser.