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

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

use IPrimitive to make upload code work for both OsmPrimitive and PrimitiveData

  • Property svn:eol-style set to native
File size: 7.1 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.io.Reader;
10import java.text.MessageFormat;
11import java.util.Collection;
12
13/**
14 * Basic utils, that can be useful in different parts of the program.
15 */
16public class Utils {
17
18 public static <T> boolean exists(Iterable<? extends T> collection, Predicate<? super T> predicate) {
19 for (T item : collection) {
20 if (predicate.evaluate(item))
21 return true;
22 }
23 return false;
24 }
25
26 public static <T> boolean exists(Iterable<T> collection, Class<? extends T> klass) {
27 for (Object item : collection) {
28 if (klass.isInstance(item))
29 return true;
30 }
31 return false;
32 }
33
34 public static <T> T find(Iterable<? extends T> collection, Predicate<? super T> predicate) {
35 for (T item : collection) {
36 if (predicate.evaluate(item))
37 return item;
38 }
39 return null;
40 }
41
42 @SuppressWarnings("unchecked")
43 public static <T> T find(Iterable<? super T> collection, Class<? extends T> klass) {
44 for (Object item : collection) {
45 if (klass.isInstance(item))
46 return (T) item;
47 }
48 return null;
49 }
50
51 /**
52 * Filter a collection by (sub)class.
53 * This is an efficient read-only implementation.
54 */
55 public static <S, T extends S> SubclassFilteredCollection<S, T> filteredCollection(Collection<S> collection, final Class<T> klass) {
56 return new SubclassFilteredCollection<S, T>(collection, new Predicate<S>() {
57 @Override
58 public boolean evaluate(S o) {
59 return klass.isInstance(o);
60 }
61 });
62 }
63
64 public static <T> int indexOf(Iterable<? extends T> collection, Predicate<? super T> predicate) {
65 int i = 0;
66 for (T item : collection) {
67 if (predicate.evaluate(item))
68 return i;
69 i++;
70 }
71 return -1;
72 }
73
74 /**
75 * Get minimum of 3 values
76 */
77 public static int min(int a, int b, int c) {
78 if (b < c) {
79 if (a < b)
80 return a;
81 return b;
82 } else {
83 if (a < c)
84 return a;
85 return c;
86 }
87 }
88
89 public static int max(int a, int b, int c, int d) {
90 return Math.max(Math.max(a, b), Math.max(c, d));
91 }
92
93 /**
94 * for convenience: test whether 2 objects are either both null or a.equals(b)
95 */
96 public static <T> boolean equal(T a, T b) {
97 if (a == b)
98 return true;
99 return (a != null && a.equals(b));
100 }
101
102 public static void ensure(boolean condition, String message, Object...data) {
103 if (!condition)
104 throw new AssertionError(
105 MessageFormat.format(message,data)
106 );
107 }
108
109 /**
110 * return the modulus in the range [0, n)
111 */
112 public static int mod(int a, int n) {
113 if (n <= 0)
114 throw new IllegalArgumentException();
115 int res = a % n;
116 if (res < 0) {
117 res += n;
118 }
119 return res;
120 }
121
122 /**
123 * Joins a list of strings (or objects that can be converted to string via
124 * Object.toString()) into a single string with fields separated by sep.
125 * @param sep the separator
126 * @param values collection of objects, null is converted to the
127 * empty string
128 * @return null if values is null. The joined string otherwise.
129 */
130 public static String join(String sep, Collection<?> values) {
131 if (sep == null)
132 throw new IllegalArgumentException();
133 if (values == null)
134 return null;
135 if (values.isEmpty())
136 return "";
137 StringBuilder s = null;
138 for (Object a : values) {
139 if (a == null) {
140 a = "";
141 }
142 if(s != null) {
143 s.append(sep).append(a.toString());
144 } else {
145 s = new StringBuilder(a.toString());
146 }
147 }
148 return s.toString();
149 }
150
151 /**
152 * convert Color to String
153 * (Color.toString() omits alpha value)
154 */
155 public static String toString(Color c) {
156 if (c == null)
157 return "null";
158 if (c.getAlpha() == 255)
159 return String.format("#%06x", c.getRGB() & 0x00ffffff);
160 else
161 return String.format("#%06x(alpha=%d)", c.getRGB() & 0x00ffffff, c.getAlpha());
162 }
163
164 /**
165 * convert float range 0 <= x <= 1 to integer range 0..255
166 * when dealing with colors and color alpha value
167 * @return null if val is null, the corresponding int if val is in the
168 * range 0...1. If val is outside that range, return 255
169 */
170 public static Integer color_float2int(Float val) {
171 if (val == null)
172 return null;
173 if (val < 0 || val > 1)
174 return 255;
175 return (int) (255f * val + 0.5f);
176 }
177
178 /**
179 * convert back
180 */
181 public static Float color_int2float(Integer val) {
182 if (val == null)
183 return null;
184 if (val < 0 || val > 255)
185 return 1f;
186 return ((float) val) / 255f;
187 }
188
189 public static Color complement(Color clr) {
190 return new Color(255 - clr.getRed(), 255 - clr.getGreen(), 255 - clr.getBlue(), clr.getAlpha());
191 }
192
193 public static int copyStream(InputStream source, OutputStream destination) throws IOException {
194 int count = 0;
195 byte[] b = new byte[512];
196 int read;
197 while ((read = source.read(b)) != -1) {
198 count += read;
199 destination.write(b, 0, read);
200 }
201 return count;
202 }
203
204 public static boolean deleteDirectory(File path) {
205 if( path.exists() ) {
206 File[] files = path.listFiles();
207 for(int i=0; i<files.length; i++) {
208 if(files[i].isDirectory()) {
209 deleteDirectory(files[i]);
210 }
211 else {
212 files[i].delete();
213 }
214 }
215 }
216 return( path.delete() );
217 }
218
219 /**
220 * <p>Utility method for closing an input stream.</p>
221 *
222 * @param is the input stream. May be null.
223 */
224 public static void close(InputStream is){
225 if (is == null) return;
226 try {
227 is.close();
228 } catch(IOException e){
229 // ignore
230 }
231 }
232
233 /**
234 * <p>Utility method for closing an output stream.</p>
235 *
236 * @param os the output stream. May be null.
237 */
238 public static void close(OutputStream os){
239 if (os == null) return;
240 try {
241 os.close();
242 } catch(IOException e){
243 // ignore
244 }
245 }
246
247 /**
248 * <p>Utility method for closing a reader.</p>
249 *
250 * @param reader the reader. May be null.
251 */
252 public static void close(Reader reader){
253 if (reader == null) return;
254 try {
255 reader.close();
256 } catch(IOException e){
257 // ignore
258 }
259 }
260}
Note: See TracBrowser for help on using the repository browser.