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

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

mapcss: add role based selection (collaborative work by Gubaer and me)

  • Property svn:eol-style set to native
File size: 5.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.awt.Color;
5import java.io.File;
6import java.io.IOException;
7import java.io.InputStream;
8import java.io.OutputStream;
9import java.text.MessageFormat;
10import java.util.Collection;
11
12/**
13 * Basic utils, that can be useful in different parts of the program.
14 */
15public class Utils {
16
17 public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) {
18 for (T item : collection) {
19 if (predicate.evaluate(item))
20 return true;
21 }
22 return false;
23 }
24
25 public static <T> boolean exists(Iterable<T> collection, Class<? extends T> klass) {
26 for (Object item : collection) {
27 if (klass.isInstance(item))
28 return true;
29 }
30 return false;
31 }
32
33 public static <T> T find(Iterable<? extends T> collection, Predicate<? super T> predicate) {
34 for (T item : collection) {
35 if (predicate.evaluate(item))
36 return item;
37 }
38 return null;
39 }
40
41 @SuppressWarnings("unchecked")
42 public static <T> T find(Iterable<? super T> collection, Class<? extends T> klass) {
43 for (Object item : collection) {
44 if (klass.isInstance(item))
45 return (T) item;
46 }
47 return null;
48 }
49
50 public static <T> int indexOf(Iterable<? extends T> collection, Predicate<? super T> predicate) {
51 int i = 0;
52 for (T item : collection) {
53 if (predicate.evaluate(item))
54 return i;
55 i++;
56 }
57 return -1;
58 }
59
60 /**
61 * Get minimum of 3 values
62 */
63 public static int min(int a, int b, int c) {
64 if (b < c) {
65 if (a < b)
66 return a;
67 return b;
68 } else {
69 if (a < c)
70 return a;
71 return c;
72 }
73 }
74
75 public static int max(int a, int b, int c, int d) {
76 return Math.max(Math.max(a, b), Math.max(c, d));
77 }
78
79 /**
80 * for convenience: test whether 2 objects are either both null or a.equals(b)
81 */
82 public static <T> boolean equal(T a, T b) {
83 if (a == b)
84 return true;
85 return (a != null && a.equals(b));
86 }
87
88 public static void ensure(boolean condition, String message, Object...data) {
89 if (!condition)
90 throw new AssertionError(
91 MessageFormat.format(message,data)
92 );
93 }
94
95 /**
96 * return the modulus in the range [0, n)
97 */
98 public static int mod(int a, int n) {
99 if (n <= 0)
100 throw new IllegalArgumentException();
101 int res = a % n;
102 if (res < 0) {
103 res += n;
104 }
105 return res;
106 }
107
108 /**
109 * Joins a list of strings (or objects that can be converted to string via
110 * Object.toString()) into a single string with fields separated by sep.
111 * @param sep the separator
112 * @param values collection of objects, null is converted to the
113 * empty string
114 * @return null if values is null. The joined string otherwise.
115 */
116 public static String join(String sep, Collection<?> values) {
117 if (sep == null)
118 throw new IllegalArgumentException();
119 if (values == null)
120 return null;
121 if (values.isEmpty())
122 return "";
123 StringBuilder s = null;
124 for (Object a : values) {
125 if (a == null) {
126 a = "";
127 }
128 if(s != null) {
129 s.append(sep).append(a.toString());
130 } else {
131 s = new StringBuilder(a.toString());
132 }
133 }
134 return s.toString();
135 }
136
137 /**
138 * convert Color to String
139 * (Color.toString() omits alpha value)
140 */
141 public static String toString(Color c) {
142 if (c == null)
143 return "null";
144 if (c.getAlpha() == 255)
145 return String.format("#%06x", c.getRGB() & 0x00ffffff);
146 else
147 return String.format("#%06x(alpha=%d)", c.getRGB() & 0x00ffffff, c.getAlpha());
148 }
149
150 /**
151 * convert float range 0 <= x <= 1 to integer range 0..255
152 * when dealing with colors and color alpha value
153 * @return null if val is null, the corresponding int if val is in the
154 * range 0...1. If val is outside that range, return 255
155 */
156 public static Integer color_float2int(Float val) {
157 if (val == null)
158 return null;
159 if (val < 0 || val > 1)
160 return 255;
161 return (int) (255f * val + 0.5f);
162 }
163
164 /**
165 * convert back
166 */
167 public static Float color_int2float(Integer val) {
168 if (val == null)
169 return null;
170 if (val < 0 || val > 255)
171 return 1f;
172 return ((float) val) / 255f;
173 }
174
175 public static Color complement(Color clr) {
176 return new Color(255 - clr.getRed(), 255 - clr.getGreen(), 255 - clr.getBlue(), clr.getAlpha());
177 }
178
179 public static int copyStream(InputStream source, OutputStream destination) throws IOException {
180 int count = 0;
181 byte[] b = new byte[512];
182 int read;
183 while ((read = source.read(b)) != -1) {
184 count += read;
185 destination.write(b, 0, read);
186 }
187 return count;
188 }
189
190 public static boolean deleteDirectory(File path) {
191 if( path.exists() ) {
192 File[] files = path.listFiles();
193 for(int i=0; i<files.length; i++) {
194 if(files[i].isDirectory()) {
195 deleteDirectory(files[i]);
196 }
197 else {
198 files[i].delete();
199 }
200 }
201 }
202 return( path.delete() );
203 }
204}
Note: See TracBrowser for help on using the repository browser.