23
23
import java .io .InputStream ;
24
24
import java .net .URL ;
25
25
import java .util .Objects ;
26
+ import java .util .function .BiFunction ;
27
+ import java .util .function .Function ;
26
28
import java .util .regex .Matcher ;
27
29
import java .util .regex .Pattern ;
28
30
44
46
*/
45
47
class FileImageDescriptor extends ImageDescriptor implements IAdaptable {
46
48
47
- private class ImageProvider implements ImageFileNameProvider {
48
-
49
- @ Override
50
- public String getImagePath (int zoom ) {
51
- final boolean logIOException = zoom == 100 ;
52
- if (zoom == 100 ) {
53
- return getFilePath (name , logIOException );
54
- }
55
- String xName = getxName (name , zoom );
56
- if (xName != null ) {
57
- String xResult = getFilePath (xName , logIOException );
58
- if (xResult != null ) {
59
- return xResult ;
60
- }
61
- }
62
- String xPath = getxPath (name , zoom );
63
- if (xPath != null ) {
64
- String xResult = getFilePath (xPath , logIOException );
65
- if (xResult != null ) {
66
- return xResult ;
67
- }
68
- }
69
- return null ;
70
- }
71
-
49
+ private ImageFileNameProvider createImageFileNameProvider () {
50
+ return zoom -> {
51
+ boolean logException = zoom == 100 ;
52
+ return getImageSource (name , n -> n , FileImageDescriptor ::getxName , n -> getFilePath (n , logException ), zoom );
53
+ };
72
54
}
73
55
74
56
private static final Pattern XPATH_PATTERN = Pattern .compile ("(\\ d+)x(\\ d+)" ); //$NON-NLS-1$
@@ -106,10 +88,9 @@ public String getImagePath(int zoom) {
106
88
107
89
@ Override
108
90
public boolean equals (Object o ) {
109
- if (!(o instanceof FileImageDescriptor )) {
91
+ if (!(o instanceof FileImageDescriptor other )) {
110
92
return false ;
111
93
}
112
- FileImageDescriptor other = (FileImageDescriptor ) o ;
113
94
return Objects .equals (location , other .location ) && Objects .equals (name , other .name );
114
95
}
115
96
@@ -122,9 +103,9 @@ public boolean equals(Object o) {
122
103
*/
123
104
@ Override
124
105
public ImageData getImageData (int zoom ) {
125
- InputStream in = getStream ( zoom );
126
- if (in != null ) {
127
- try (BufferedInputStream stream = new BufferedInputStream (in )) {
106
+ InputStream inputStream = getImageSource ( name , n -> n , FileImageDescriptor :: getxName , this :: getStream , zoom );
107
+ if (inputStream != null ) {
108
+ try (InputStream stream = new BufferedInputStream (inputStream )) {
128
109
return new ImageData (stream );
129
110
} catch (SWTException e ) {
130
111
if (e .code != SWT .ERROR_INVALID_IMAGE ) {
@@ -139,28 +120,35 @@ public ImageData getImageData(int zoom) {
139
120
}
140
121
141
122
/**
142
- * Returns a stream on the image contents. Returns null if a stream could
143
- * not be opened.
123
+ * Returns a the image contents in the form returned by the given image-source
124
+ * factory. Returns null if the content could not be found or accessed
144
125
*
145
126
* @param zoom the zoom factor
146
- * @return the buffered stream on the file or <code>null</code> if the
147
- * file cannot be found
127
+ * @return the the file content or {@code null} if the file cannot be found
148
128
*/
149
- private InputStream getStream (int zoom ) {
150
- if (zoom == 100 ) {
151
- return getStream (name );
152
- }
153
-
154
- InputStream xstream = getStream (getxName (name , zoom ));
155
- if (xstream != null ) {
156
- return xstream ;
157
- }
158
-
159
- InputStream xpath = getStream (getxPath (name , zoom ));
160
- if (xpath != null ) {
161
- return xpath ;
129
+ static <E , R > R getImageSource (String root , Function <String , E > elementParser ,
130
+ BiFunction <E , Integer , E > getXElement , Function <E , R > getImageSource , int zoom ) {
131
+ E element = elementParser .apply (root );
132
+ if (element != null ) {
133
+ if (zoom == 100 ) {
134
+ return getImageSource .apply (element );
135
+ }
136
+ @ SuppressWarnings ("boxing" )
137
+ E xName = getXElement .apply (element , zoom );
138
+ if (xName != null ) {
139
+ R xResult = getImageSource .apply (xName );
140
+ if (xResult != null ) {
141
+ return xResult ;
142
+ }
143
+ }
144
+ String xPath = getxPath (root , zoom );
145
+ if (xPath != null ) {
146
+ E xPathElement = elementParser .apply (xPath );
147
+ if (xPathElement != null ) {
148
+ return getImageSource .apply (xPathElement );
149
+ }
150
+ }
162
151
}
163
-
164
152
return null ;
165
153
}
166
154
@@ -173,20 +161,17 @@ private InputStream getStream(int zoom) {
173
161
* does not denotes an existing resource
174
162
*/
175
163
private InputStream getStream (String fileName ) {
176
- if (fileName != null ) {
177
- if (location != null ) {
178
- return location .getResourceAsStream (fileName );
179
- }
180
- try {
181
- return new FileInputStream (fileName );
182
- } catch (FileNotFoundException e ) {
183
- return null ;
184
- }
164
+ if (location != null ) {
165
+ return location .getResourceAsStream (fileName );
166
+ }
167
+ try {
168
+ return new FileInputStream (fileName );
169
+ } catch (FileNotFoundException e ) {
170
+ return null ;
185
171
}
186
- return null ;
187
172
}
188
173
189
- static String getxPath (String name , int zoom ) {
174
+ private static String getxPath (String name , int zoom ) {
190
175
Matcher matcher = XPATH_PATTERN .matcher (name );
191
176
if (matcher .find ()) {
192
177
try {
@@ -244,7 +229,7 @@ public Image createImage(boolean returnMissingImageOnError, Device device) {
244
229
// We really want a fresh ImageFileNameProvider instance to make
245
230
// sure the code that uses created images can use equals(),
246
231
// see Image#equals
247
- return new Image (device , new ImageProvider ());
232
+ return new Image (device , createImageFileNameProvider ());
248
233
} catch (SWTException | IllegalArgumentException exception ) {
249
234
// If we fail, fall back to the old 1x implementation.
250
235
}
@@ -283,7 +268,7 @@ private Image createDefaultImage(boolean returnMissingImageOnError,
283
268
* @param name the file name
284
269
* @return {@link String} or <code>null</code> if the file cannot be found
285
270
*/
286
- String getFilePath (String name , boolean logIOException ) {
271
+ private String getFilePath (String name , boolean logIOException ) {
287
272
if (location == null )
288
273
return IPath .fromOSString (name ).toOSString ();
289
274
@@ -317,7 +302,7 @@ public <T> T getAdapter(Class<T> adapter) {
317
302
}
318
303
}
319
304
if (adapter == ImageFileNameProvider .class ) {
320
- return adapter .cast (new ImageProvider ());
305
+ return adapter .cast (createImageFileNameProvider ());
321
306
}
322
307
return null ;
323
308
}
0 commit comments