Changeset 16331 in josm for trunk/src


Ignore:
Timestamp:
2020-04-18T10:41:20+02:00 (5 years ago)
Author:
simon04
Message:

HiDPISupport: use static initializer, drop Optional use

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/HiDPISupport.java

    r16056 r16331  
    1313import java.util.Collections;
    1414import java.util.List;
    15 import java.util.Optional;
    1615import java.util.function.Function;
    1716import java.util.function.UnaryOperator;
     
    3130public final class HiDPISupport {
    3231
    33     private static volatile Optional<Class<? extends Image>> baseMultiResolutionImageClass;
    34     private static volatile Optional<Constructor<? extends Image>> baseMultiResolutionImageConstructor;
    35     private static volatile Optional<Method> resolutionVariantsMethod;
     32    private static final Class<? extends Image> baseMultiResolutionImageClass;
     33    private static final Constructor<? extends Image> baseMultiResolutionImageConstructor;
     34    private static final Method resolutionVariantsMethod;
     35
     36    static {
     37        baseMultiResolutionImageClass = initBaseMultiResolutionImageClass();
     38        baseMultiResolutionImageConstructor = initBaseMultiResolutionImageConstructor();
     39        resolutionVariantsMethod = initResolutionVariantsMethod();
     40    }
    3641
    3742    private HiDPISupport() {
     
    5156    public static Image getMultiResolutionImage(Image base, ImageResource ir) {
    5257        double uiScale = getHiDPIScale();
    53         if (uiScale != 1.0 && getBaseMultiResolutionImageConstructor().isPresent()) {
     58        if (uiScale != 1.0 && baseMultiResolutionImageConstructor != null) {
    5459            ImageIcon zoomed = ir.getImageIcon(new Dimension(
    5560                    (int) Math.round(base.getWidth(null) * uiScale),
     
    7075    public static Image getMultiResolutionImage(List<Image> imgs) {
    7176        CheckParameterUtil.ensureThat(!imgs.isEmpty(), "imgs is empty");
    72         Optional<Constructor<? extends Image>> baseMrImageConstructor = getBaseMultiResolutionImageConstructor();
    73         if (baseMrImageConstructor.isPresent()) {
     77        if (baseMultiResolutionImageConstructor != null) {
    7478            try {
    75                 return baseMrImageConstructor.get().newInstance((Object) imgs.toArray(new Image[0]));
     79                return baseMultiResolutionImageConstructor.newInstance((Object) imgs.toArray(new Image[0]));
    7680            } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) {
    7781                Logging.error("Unexpected error while instantiating object of class BaseMultiResolutionImage: " + ex);
     
    9094     */
    9195    public static Image getBaseImage(Image img) {
    92         Optional<Class<? extends Image>> baseMrImageClass = getBaseMultiResolutionImageClass();
    93         Optional<Method> resVariantsMethod = getResolutionVariantsMethod();
    94         if (!baseMrImageClass.isPresent() || !resVariantsMethod.isPresent()) {
     96        if (baseMultiResolutionImageClass == null || resolutionVariantsMethod == null) {
    9597            return img;
    9698        }
    97         if (baseMrImageClass.get().isInstance(img)) {
     99        if (baseMultiResolutionImageClass.isInstance(img)) {
    98100            try {
    99101                @SuppressWarnings("unchecked")
    100                 List<Image> imgVars = (List<Image>) resVariantsMethod.get().invoke(img);
     102                List<Image> imgVars = (List<Image>) resolutionVariantsMethod.invoke(img);
    101103                if (!imgVars.isEmpty()) {
    102104                    return imgVars.get(0);
     
    119121     */
    120122    public static List<Image> getResolutionVariants(Image img) {
    121         Optional<Class<? extends Image>> baseMrImageClass = getBaseMultiResolutionImageClass();
    122         Optional<Method> resVariantsMethod = getResolutionVariantsMethod();
    123         if (!baseMrImageClass.isPresent() || !resVariantsMethod.isPresent()) {
     123        if (baseMultiResolutionImageClass == null || resolutionVariantsMethod == null) {
    124124            return Collections.singletonList(img);
    125125        }
    126         if (baseMrImageClass.get().isInstance(img)) {
     126        if (baseMultiResolutionImageClass.isInstance(img)) {
    127127            try {
    128128                @SuppressWarnings("unchecked")
    129                 List<Image> imgVars = (List<Image>) resVariantsMethod.get().invoke(img);
     129                List<Image> imgVars = (List<Image>) resolutionVariantsMethod.invoke(img);
    130130                if (!imgVars.isEmpty()) {
    131131                    return imgVars;
     
    189189    public static Image processMRImages(List<Image> imgs, Function<List<Image>, Image> processor) {
    190190        CheckParameterUtil.ensureThat(!imgs.isEmpty(), "at least one element expected");
    191         if (!getBaseMultiResolutionImageClass().isPresent()) {
     191        if (baseMultiResolutionImageClass != null) {
    192192            return processor.apply(imgs);
    193193        }
     
    205205    }
    206206
    207     private static Optional<Class<? extends Image>> getBaseMultiResolutionImageClass() {
    208         if (baseMultiResolutionImageClass == null) {
    209             synchronized (HiDPISupport.class) {
    210                 if (baseMultiResolutionImageClass == null) {
    211                     try {
    212                         @SuppressWarnings("unchecked")
    213                         Class<? extends Image> c = (Class<? extends Image>) Class.forName("java.awt.image.BaseMultiResolutionImage");
    214                         baseMultiResolutionImageClass = Optional.ofNullable(c);
    215                     } catch (ClassNotFoundException ex) {
    216                         // class is not present in Java 8
    217                         baseMultiResolutionImageClass = Optional.empty();
    218                         Logging.trace(ex);
    219                     }
    220                 }
    221             }
    222         }
    223         return baseMultiResolutionImageClass;
    224     }
    225 
    226     private static Optional<Constructor<? extends Image>> getBaseMultiResolutionImageConstructor() {
    227         if (baseMultiResolutionImageConstructor == null) {
    228             synchronized (HiDPISupport.class) {
    229                 if (baseMultiResolutionImageConstructor == null) {
    230                     getBaseMultiResolutionImageClass().ifPresent(klass -> {
    231                         try {
    232                             Constructor<? extends Image> constr = klass.getConstructor(Image[].class);
    233                             baseMultiResolutionImageConstructor = Optional.ofNullable(constr);
    234                         } catch (NoSuchMethodException ex) {
    235                             Logging.error("Cannot find expected constructor: " + ex);
    236                         }
    237                     });
    238                     if (baseMultiResolutionImageConstructor == null) {
    239                         baseMultiResolutionImageConstructor = Optional.empty();
    240                     }
    241                 }
    242             }
    243         }
    244         return baseMultiResolutionImageConstructor;
    245     }
    246 
    247     private static Optional<Method> getResolutionVariantsMethod() {
    248         if (resolutionVariantsMethod == null) {
    249             synchronized (HiDPISupport.class) {
    250                 if (resolutionVariantsMethod == null) {
    251                     getBaseMultiResolutionImageClass().ifPresent(klass -> {
    252                         try {
    253                             Method m = klass.getMethod("getResolutionVariants");
    254                             resolutionVariantsMethod = Optional.ofNullable(m);
    255                         } catch (NoSuchMethodException ex) {
    256                             Logging.error("Cannot find expected method: "+ex);
    257                         }
    258                     });
    259                     if (resolutionVariantsMethod == null) {
    260                         resolutionVariantsMethod = Optional.empty();
    261                     }
    262                 }
    263             }
    264         }
    265         return resolutionVariantsMethod;
     207    @SuppressWarnings("unchecked")
     208    private static Class<? extends Image> initBaseMultiResolutionImageClass() {
     209        try {
     210            return (Class<? extends Image>) Class.forName("java.awt.image.BaseMultiResolutionImage");
     211        } catch (ClassNotFoundException ex) {
     212            // class is not present in Java 8
     213            Logging.trace(ex);
     214            return null;
     215        }
     216    }
     217
     218    private static Constructor<? extends Image> initBaseMultiResolutionImageConstructor() {
     219        try {
     220            return baseMultiResolutionImageClass != null
     221                    ? baseMultiResolutionImageClass.getConstructor(Image[].class)
     222                    : null;
     223        } catch (NoSuchMethodException ex) {
     224            Logging.error("Cannot find expected constructor: " + ex);
     225            return null;
     226        }
     227    }
     228
     229    private static Method initResolutionVariantsMethod() {
     230        try {
     231            return baseMultiResolutionImageClass != null
     232                    ? baseMultiResolutionImageClass.getMethod("getResolutionVariants")
     233                    : null;
     234        } catch (NoSuchMethodException ex) {
     235            Logging.error("Cannot find expected method: " + ex);
     236            return null;
     237        }
    266238    }
    267239}
Note: See TracChangeset for help on using the changeset viewer.