001package jmri.jmrit.swing.meter;
002
003import java.util.*;
004
005import javax.annotation.CheckReturnValue;
006import javax.annotation.Nonnull;
007
008import jmri.*;
009
010/**
011 * Default implementation of a MeterFrameManager.
012 * This class is only used by jmri.jmrit.swing.meter.MeterFrame and
013 * jmri.jmrit.swing.meter.configurexml.MeterFrameManagerXml so no need to store
014 * it in the InstanceManager.
015 *
016 * @author Daniel Bergqvist  Copyright (C) 2020
017 */
018public class MeterFrameManager {
019
020    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(MeterFrameManager.class);
021
022    private static final MeterFrameManager _instance = new MeterFrameManager();
023
024    private final Map<UUID, MeterFrame> _meterFrameList = new HashMap<>();
025
026    /**
027     * Get the instance of MeterFrameManager.
028     * @return the MeterFrameManager instance
029     */
030    @CheckReturnValue
031    public static MeterFrameManager getInstance() {
032        return _instance;
033    }
034
035    /**
036     * Create a new MeterFrameManager instance.
037     */
038    private MeterFrameManager() {
039        log.debug("registerSelf for config of type {}", getClass());
040        InstanceManager.getOptionalDefault(ConfigureManager.class).ifPresent(cm -> {
041            cm.registerConfig(this, getXMLOrder());
042            log.debug("registering for config of type {}", getClass());
043        });
044    }
045
046    /**
047     * Determine the order that types should be written when storing panel
048     * files. Uses one of the constants defined in this class.
049     * <p>
050     * Yes, that's an overly-centralized methodology, but it works for now.
051     *
052     * @return write order for this Manager; larger is later.
053     */
054    @CheckReturnValue
055    public int getXMLOrder() {
056        return Manager.METERFRAMES;
057    }
058
059    public void register(@Nonnull MeterFrame frame) {
060        _meterFrameList.put(frame.getUUID(), frame);
061    }
062
063    public void deregister(@Nonnull MeterFrame frame) {
064        _meterFrameList.remove(frame.getUUID());
065    }
066
067    public MeterFrame getByUUID(@Nonnull UUID uuid) {
068        return _meterFrameList.get(uuid);
069    }
070
071    public Collection<MeterFrame> getMeterFrames() {
072        return Collections.unmodifiableCollection(_meterFrameList.values());
073    }
074
075}