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

Last change on this file since 4067 was 4065, checked in by jttt, 13 years ago

Improved wms cache

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